Ultralytics YOLOv8 🚀を使ったオブジェクト・カウント
オブジェクト・カウントとは何か?
による物体計数では、ビデオやカメラストリーム内の特定の物体を正確に識別し、計数します。 Ultralytics YOLOv8YOLOv8 、リアルタイムアプリケーションに優れており、最先端のアルゴリズムとディープラーニング機能により、群衆分析や監視などのさまざまなシナリオで効率的かつ正確なオブジェクト計数を提供します。
見るんだ: オブジェクト・カウントUltralytics YOLOv8
オブジェクト・カウントの利点
- リソースの最適化:オブジェクトカウンティングは、正確なカウントを提供し、在庫管理などのアプリケーションでリソース割り当てを最適化することで、効率的なリソース管理を容易にします。
- セキュリティの強化:物体計数は、実体を正確に追跡して計数することにより、セキュリティと監視を強化し、事前の脅威検知を支援します。
- 情報に基づいた意思決定オブジェクトカウンティングは、小売、交通管理、その他様々な領域において、意思決定やプロセスの最適化のための貴重な洞察を提供します。
実世界での応用
物流 | 水産養殖 |
---|---|
ベルトコンベアによるパケットカウントUltralytics YOLOv8 | 海での魚の数え方Ultralytics YOLOv8 |
YOLOv8 例を用いたオブジェクトのカウント
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Define region points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Init Object Counter
counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=region_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Define region points as a polygon with 5 points
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360), (20, 400)]
# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Init Object Counter
counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=region_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
# Define line points
line_points = [(20, 400), (1080, 400)]
# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Init Object Counter
counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False)
im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
from ultralytics import YOLO
from ultralytics.solutions import object_counter
import cv2
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
assert cap.isOpened(), "Error reading video file"
w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
line_points = [(20, 400), (1080, 400)] # line or region points
classes_to_count = [0, 2] # person and car classes for count
# Video writer
video_writer = cv2.VideoWriter("object_counting_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
fps,
(w, h))
# Init Object Counter
counter = object_counter.ObjectCounter()
counter.set_args(view_img=True,
reg_pts=line_points,
classes_names=model.names,
draw_tracks=True,
line_thickness=2)
while cap.isOpened():
success, im0 = cap.read()
if not success:
print("Video frame is empty or video processing has been successfully completed.")
break
tracks = model.track(im0, persist=True, show=False,
classes=classes_to_count)
im0 = counter.start_counting(im0, tracks)
video_writer.write(im0)
cap.release()
video_writer.release()
cv2.destroyAllWindows()
リージョンは移動可能
領域の端をクリックして、フレーム内の任意の場所に移動できます。
オプションの引数 set_args
名称 | タイプ | デフォルト | 説明 |
---|---|---|---|
view_img |
bool |
False |
カウント付きフレームを表示 |
view_in_counts |
bool |
True |
ビデオフレームにのみインカウントを表示 |
view_out_counts |
bool |
True |
ビデオフレームにのみアウトカウントを表示 |
line_thickness |
int |
2 |
バウンディングボックスを増やし、テキストの太さを数える |
reg_pts |
list |
[(20, 400), (1260, 400)] |
リージョン・エリアを定義するポイント |
classes_names |
dict |
model.model.names |
クラス名辞書 |
count_reg_color |
RGB Color |
(255, 0, 255) |
リージョンまたはラインをカウントするオブジェクトの色 |
track_thickness |
int |
2 |
トラッキング・ラインの太さ |
draw_tracks |
bool |
False |
トラックラインの描画を有効にする |
track_color |
RGB Color |
(0, 255, 0) |
各線路の色 |
line_dist_thresh |
int |
15 |
ラインカウンターのユークリッド距離のしきい値 |
count_txt_color |
RGB Color |
(255, 255, 255) |
オブジェクト・カウント・テキストの前景色 |
region_thickness |
int |
5 |
オブジェクトのカウンター領域またはラインの太さ |
count_bg_color |
RGB Color |
(255, 255, 255) |
蛍光ペンの色を数える |
論争 model.track
名称 | タイプ | デフォルト | 説明 |
---|---|---|---|
source |
im0 |
None |
画像またはビデオのソース・ディレクトリ |
persist |
bool |
False |
フレーム間のトラックの持続 |
tracker |
str |
botsort.yaml |
トラッキングメソッド「bytetrack」または「botsort |
conf |
float |
0.3 |
信頼閾値 |
iou |
float |
0.5 |
借用書のしきい値 |
classes |
list |
None |
つまり、class=0、またはclass=[0,2,3]。 |
verbose |
bool |
True |
物体追跡結果を表示する |