オブジェクト・カウントUltralytics YOLOv8
オブジェクト・カウントとは何か?
による物体計数では、ビデオやカメラストリーム内の特定の物体を正確に識別し、計数します。 Ultralytics YOLOv8YOLOv8 、リアルタイムアプリケーションに優れており、最先端のアルゴリズムとディープラーニング機能により、群衆分析や監視などのさまざまなシナリオで効率的かつ正確なオブジェクト計数を提供します。
見るんだ: オブジェクト・カウントUltralytics YOLOv8 |
見るんだ: クラス別オブジェクト・カウントUltralytics YOLOv8 |
オブジェクト・カウントの利点
- リソースの最適化:オブジェクトカウンティングは、正確なカウントを提供し、在庫管理などのアプリケーションでリソース割り当てを最適化することで、効率的なリソース管理を容易にします。
- セキュリティの強化:物体計数は、実体を正確に追跡して計数することにより、セキュリティと監視を強化し、事前の脅威検知を支援します。
- 情報に基づいた意思決定オブジェクトカウンティングは、小売、交通管理、その他様々な領域において、意思決定やプロセスの最適化のための貴重な洞察を提供します。
実世界での応用
物流 | 水産養殖 |
---|---|
ベルトコンベアによるパケットカウントUltralytics YOLOv8 | 海での魚の数え方Ultralytics YOLOv8 |
YOLOv8 例を用いたオブジェクトのカウント
import cv2
from ultralytics import YOLO, solutions
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 = solutions.ObjectCounter(
view_img=True,
reg_pts=region_points,
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()
import cv2
from ultralytics import YOLO, solutions
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 = solutions.ObjectCounter(
view_img=True,
reg_pts=region_points,
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()
import cv2
from ultralytics import YOLO, solutions
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 = solutions.ObjectCounter(
view_img=True,
reg_pts=line_points,
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()
import cv2
from ultralytics import YOLO, solutions
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 = solutions.ObjectCounter(
view_img=True,
reg_pts=line_points,
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()
リージョンは移動可能
領域の端をクリックして、フレーム内の任意の場所に移動できます。
議論 ObjectCounter
以下はその表である。 ObjectCounter
という議論がある:
名称 | タイプ | デフォルト | 説明 |
---|---|---|---|
names |
dict |
None |
クラス名の辞書。 |
reg_pts |
list |
[(20, 400), (1260, 400)] |
カウント領域を定義する点のリスト。 |
count_reg_color |
tuple |
(255, 0, 255) |
カウント領域のRGBカラー。 |
count_txt_color |
tuple |
(0, 0, 0) |
カウントテキストのRGBカラー。 |
count_bg_color |
tuple |
(255, 255, 255) |
カウントテキストの背景のRGB色。 |
line_thickness |
int |
2 |
バウンディングボックスの線の太さ。 |
track_thickness |
int |
2 |
トラックラインの太さ。 |
view_img |
bool |
False |
ビデオストリームを表示するかどうかを制御するフラグ。 |
view_in_counts |
bool |
True |
ビデオストリームにインカウントを表示するかどうかを制御するフラグ。 |
view_out_counts |
bool |
True |
ビデオストリームにアウトカウントを表示するかどうかを制御するフラグ。 |
draw_tracks |
bool |
False |
オブジェクトトラックを描画するかどうかを制御するフラグ。 |
track_color |
tuple |
None |
トラックのRGBカラー。 |
region_thickness |
int |
5 |
オブジェクトのカウント領域の厚さ。 |
line_dist_thresh |
int |
15 |
ラインカウンタのユークリッド距離のしきい値。 |
cls_txtdisplay_gap |
int |
50 |
各クラスカウント間のギャップを表示する。 |
論争 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 |
物体追跡結果を表示する |
よくあるご質問
Ultralytics YOLOv8 を使ってビデオ内のオブジェクトを数えるには?
Ultralytics YOLOv8 を使用してビデオ内のオブジェクトをカウントするには、次の手順に従います:
- 必要なライブラリをインポートする
cv2
,ultralytics
). - 事前に訓練されたYOLOv8 モデルをロードする。
- 計数領域を定義する(多角形、線など)。
- ビデオキャプチャを設定し、オブジェクトカウンターを初期化する。
- 各フレームを処理してオブジェクトを追跡し、定義された領域内でカウントする。
以下は、地域でカウントする簡単な例である:
import cv2
from ultralytics import YOLO, solutions
def count_objects_in_region(video_path, output_video_path, model_path):
"""Count objects in a specific region within a video."""
model = YOLO(model_path)
cap = cv2.VideoCapture(video_path)
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))
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
counter = solutions.ObjectCounter(
view_img=True, reg_pts=region_points, 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()
count_objects_in_region("path/to/video.mp4", "output_video.avi", "yolov8n.pt")
その他の設定やオプションについては、オブジェクト・カウントのセクションを参照してください。
Ultralytics YOLOv8 、物体カウントに使用する利点は?
Ultralytics YOLOv8 を物体計数に使用すると、いくつかの利点がある:
- リソースの最適化正確な計数を提供することで効率的なリソース管理を促進し、在庫管理などの産業におけるリソース配分の最適化を支援する。
- セキュリティの強化:エンティティの正確な追跡とカウントにより、セキュリティと監視を強化し、脅威の事前検知を支援します。
- 情報に基づいた意思決定意思決定のための貴重な洞察を提供し、小売、交通管理などの領域におけるプロセスを最適化します。
実際のアプリケーションとコード例については、オブジェクト・カウントの利点のセクションをご覧ください。
Ultralytics YOLOv8 を使って特定のクラスのオブジェクトを数えるには?
Ultralytics YOLOv8 を使ってオブジェクトの特定のクラスをカウントするには、トラッキングの段階で、関心のあるクラスを指定する必要がある。以下はPython の例である:
import cv2
from ultralytics import YOLO, solutions
def count_specific_classes(video_path, output_video_path, model_path, classes_to_count):
"""Count specific classes of objects in a video."""
model = YOLO(model_path)
cap = cv2.VideoCapture(video_path)
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)]
video_writer = cv2.VideoWriter(output_video_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
counter = solutions.ObjectCounter(
view_img=True, reg_pts=line_points, 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()
count_specific_classes("path/to/video.mp4", "output_specific_classes.avi", "yolov8n.pt", [0, 2])
この例では classes_to_count=[0, 2]
というクラスのオブジェクトを数える。 0
そして 2
(例:人と車)。
なぜ、リアルタイム・アプリケーションに他のオブジェクト検出モデルではなく、YOLOv8 を使う必要があるのか?
Ultralytics YOLOv8 は、Faster R-CNN、SSD、および以前のバージョン(YOLO )のような他の物体検出モデルと比較して、いくつかの利点を提供します:
- スピードと効率性: YOLOv8 はリアルタイム処理機能を備えており、監視や自律走行など高速推論を必要とするアプリケーションに最適です。
- 精度:物体の検出と追跡タスクに最先端の精度を提供し、誤検出の数を減らし、システム全体の信頼性を向上させます。
- 統合の容易さ: YOLOv8 は、モバイルやエッジデバイスを含む様々なプラットフォームやデバイスとのシームレスな統合を提供し、これは最新のAIアプリケーションにとって極めて重要である。
- 柔軟性:特定のユースケースの要件を満たすために設定可能なモデルで、オブジェクト検出、セグメンテーション、トラッキングなどのさまざまなタスクをサポートします。
Ultralytics YOLOv8 ドキュメントで、その機能と性能比較についてさらに深く掘り下げてご覧ください。
YOLOv8 、群衆分析や交通管理のような高度なアプリケーションに使用できますか?
そう、Ultralytics YOLOv8 、そのリアルタイム検出能力、拡張性、統合の柔軟性により、群衆分析や交通管理などの高度なアプリケーションに完璧に適している。その高度な機能により、動的な環境でも高精度の物体追跡、カウント、分類が可能です。使用例
- 群衆分析:大規模な集まりを監視・管理し、安全を確保し、群衆の流れを最適化する。
- 交通管理:車両を追跡・カウントし、交通パターンを分析し、渋滞をリアルタイムで管理します。
より詳細な情報と実装の詳細については、YOLOv8 を使ったオブジェクトカウントの実世界での応用に関するガイドを参照してください。