ãã«ããªããžã§ã¯ãã»ãã©ããã³ã°Ultralytics YOLO
ãããªè§£æã®é åã«ããããªããžã§ã¯ããã©ããã³ã°ã¯ããã¬ãŒã å ã®ãªããžã§ã¯ãã®äœçœ®ãšã¯ã©ã¹ãèå¥ããã ãã§ãªãããããªã®é²è¡ã«å¿ããŠæ€åºãããåãªããžã§ã¯ãã®äžæã®IDãç¶æããéèŠãªã¿ã¹ã¯ã§ããç£èŠãã»ãã¥ãªãã£ãããªã¢ã«ã¿ã€ã ã®ã¹ããŒãåæãŸã§ãã¢ããªã±ãŒã·ã§ã³ã¯ç¡éã§ãã
ç©äœè¿œè·¡ã«Ultralytics YOLO ãéžã¶çç±ã¯ïŒ
Ultralytics ãã©ãã«ãŒããã®åºåã¯ãæšæºçãªç©äœæ€åºãšäžèŽããŠããããç©äœIDãšããä»å 䟡å€ããããããã«ããããããªã¹ããªãŒã å ã®ãªããžã§ã¯ããç°¡åã«è¿œè·¡ãããã®åŸã®åæãå®è¡ããããšãã§ããŸãããªããžã§ã¯ã远跡ã®ããŒãºã«Ultralytics YOLO ã®äœ¿çšãæ€èšãã¹ãçç±ã¯ä»¥äžã®éãã§ãïŒ
- å¹çæ§ïŒ 粟床ãæãªãããšãªãããããªã¹ããªãŒã ããªã¢ã«ã¿ã€ã ã§åŠçã
- æè»æ§ïŒè€æ°ã®ãã©ããã³ã°ã¢ã«ãŽãªãºã ãšèšå®ããµããŒãã
- 䜿ããããïŒã·ã³ãã«ãªPython API ãšCLI ãªãã·ã§ã³ã«ãããè¿ éãªçµ±åãšå°å ¥ãå¯èœã
- ã«ã¹ã¿ãã€ãºæ§ïŒã«ã¹ã¿ã ãã¬ãŒãã³ã°ãããYOLO ã¢ãã«ã§ç°¡åã«äœ¿çšã§ãããã¡ã€ã³åºæã®ã¢ããªã±ãŒã·ã§ã³ã«çµ±åã§ããã
èŠããã ïŒ Ultralytics YOLO ã«ããç©äœæ€åºãšè¿œè·¡ .
å®äžçã§ã®å¿çš
亀é | å°å£² | æ°Žç£é€æ® |
---|---|---|
è»äž¡è¿œè·¡ | ããŒãã«ã»ãã©ããã³ã° | éã®è¿œè·¡ |
ç¹åŸŽäžèŠ§
Ultralytics YOLO ãªããžã§ã¯ãæ€åºæ©èœãæ¡åŒµããããã¹ãã§å€ç®çãªãªããžã§ã¯ããã©ããã³ã°ãæäŸããŸãïŒ
- ãªã¢ã«ã¿ã€ã ãã©ããã³ã°ïŒãã€ãã¬ãŒã ã¬ãŒããããªã®ãªããžã§ã¯ããã·ãŒã ã¬ã¹ã«ãã©ããã³ã°ã
- è€æ°ã®ãã©ãã«ãŒããµããŒãïŒç¢ºç«ãããæ§ã ãªãã©ããã³ã°ã¢ã«ãŽãªãºã ããéžæã§ããŸãã
- ã«ã¹ã¿ãã€ãºå¯èœãªãã©ãã«ãŒèšå®ïŒæ§ã ãªãã©ã¡ãŒã¿ã調æŽããããšã§ããã©ããã³ã°ã¢ã«ãŽãªãºã ãç¹å®ã®èŠä»¶ã«åãããããšãã§ããŸãã
å©çšå¯èœãªãã©ãã«ãŒ
Ultralytics YOLO ã¯æ¬¡ã®ãã©ããã³ã°ã¢ã«ãŽãªãºã ããµããŒãããŸãããããã¯æ¬¡ã®ãããªé¢é£ããYAMLèšå®ãã¡ã€ã«ãæž¡ãããšã§æå¹ã«ã§ããŸã tracker=tracker_type.yaml
:
- ãããã»ãœãŒã - çšé
botsort.yaml
ãã¯ãªãã¯ããŠããã®ãã©ãã«ãŒãæå¹ã«ããŠãã ããã - ãã€ããã©ã㯠- çšé
bytetrack.yaml
ãã¯ãªãã¯ããŠããã®ãã©ãã«ãŒãæå¹ã«ããŠãã ããã
ããã©ã«ãã®ãã©ãã«ãŒã¯BoT-SORTã§ããã
ãã©ããã³ã°
ãã©ãã«ãŒãããå€æ å ±
ãªããžã§ã¯ãã®ä¿¡é ŒåºŠã¹ã³ã¢ãäœãå Žåãã€ãŸãã以äžã®å Žåã§ããã track_high_thresh
ãã®å Žåãæ£åžžã«è¿ããæŽæ°ããããã©ãã¯ã¯ãªãã
ãããªã¹ããªãŒã ã§ãã©ãã«ãŒãå®è¡ããã«ã¯ãYOLO11nãYOLO11n-segãYOLO11n-poseãªã©ã®åŠç¿æžã¿ã®æ€åºãã»ã°ã¡ã³ããããŒãºã¢ãã«ã䜿çšããã
äŸ
from ultralytics import YOLO
# Load an official or custom model
model = YOLO("yolo11n.pt") # Load an official Detect model
model = YOLO("yolo11n-seg.pt") # Load an official Segment model
model = YOLO("yolo11n-pose.pt") # Load an official Pose model
model = YOLO("path/to/best.pt") # Load a custom trained model
# Perform tracking with the model
results = model.track("https://youtu.be/LNwODJXcvt4", show=True) # Tracking with default tracker
results = model.track("https://youtu.be/LNwODJXcvt4", show=True, tracker="bytetrack.yaml") # with ByteTrack
# Perform tracking with various models using the command line interface
yolo track model=yolo11n.pt source="https://youtu.be/LNwODJXcvt4" # Official Detect model
yolo track model=yolo11n-seg.pt source="https://youtu.be/LNwODJXcvt4" # Official Segment model
yolo track model=yolo11n-pose.pt source="https://youtu.be/LNwODJXcvt4" # Official Pose model
yolo track model=path/to/best.pt source="https://youtu.be/LNwODJXcvt4" # Custom trained model
# Track using ByteTrack tracker
yolo track model=path/to/best.pt tracker="bytetrack.yaml"
äžèšã®äœ¿çšæ¹æ³ãããããããã«ããã©ããã³ã°ã¯ãããªãã¹ããªãŒãã³ã°ãœãŒã¹äžã§å®è¡ãããå šãŠã®DetectãSegmentãPoseã¢ãã«ã§å©çšå¯èœã§ãã
æ§æ
ãã©ãã«ãŒãããå€æ å ±
ãªããžã§ã¯ãã®ä¿¡é ŒåºŠã¹ã³ã¢ãäœãå Žåãã€ãŸãã以äžã®å Žåã§ããã track_high_thresh
ãã®å Žåãæ£åžžã«è¿ããæŽæ°ããããã©ãã¯ã¯ãªãã
åŒæ°ã®è¿œè·¡
ãã©ããã³ã°èšå®ã¯ã次ã®ãããªããããã£ãäºæž¬ã¢ãŒããšå
±æããŠããã conf
, iou
ãã㊠show
.詳现ãªèšå®ã«ã€ããŠã¯ äºæž¬ãã ã®ã¢ãã«ããŒãžã
äŸ
ãã©ãã«ãŒã®éžæ
Ultralytics ãŸããä¿®æ£ãããã©ãã«ãŒèšå®ãã¡ã€ã«ã䜿ãããšãã§ããŸãããããè¡ãã«ã¯ãåçŽã«ãã©ãã«ãŒèšå®ãã¡ã€ã«ã®ã³ããŒïŒäŸãã°ã custom_tracker.yaml
ããïŒã ultralytics/ãã©ãã«ãŒãº ãé€ãïŒãå€æŽããã tracker_type
)ãããŒãºã«åãããŠéžæããããšãã§ããã
äŸ
ãã©ããã³ã°åŒæ°ã®å æ¬çãªãªã¹ãã«ã€ããŠã¯ãultralytics/cfg/trackersããŒãžãåç §ããŠäžããã
Python äŸ
ããŒã·ã¹ãã³ã°ã»ãã©ãã¯ã»ã«ãŒã
以äžã¯ãPython ã䜿ã£ãã¹ã¯ãªããã§ããã ãªãŒãã³CV (cv2
)ãšYOLO11 ããããªãã¬ãŒã ã«å¯ŸããŠãªããžã§ã¯ããã©ããã³ã°ãå®è¡ããŸãããã®ã¹ã¯ãªããã¯ãå¿
èŠãªããã±ãŒãž(opencv-python
ãã㊠ultralytics
).ãã® persist=True
åŒæ°ã¯ãçŸåšã®ç»åãŸãã¯ãã¬ãŒã ãã·ãŒã±ã³ã¹ã®æ¬¡ã§ãããçŸåšã®ç»åã«åã®ç»åããã®ãã©ãã¯ãæåŸ
ããããšããã©ãã«ãŒã«äŒããŸãã
ãã©ããã³ã°ä»ãã¹ããªãŒãã³ã°ã»ãã©ãŒã«ãŒã
import cv2
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("yolo11n.pt")
# Open the video file
video_path = "path/to/video.mp4"
cap = cv2.VideoCapture(video_path)
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLO11 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("YOLO11 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
ããã®å€æŽã«ã泚æãã ããã model(frame)
ãžã® model.track(frame)
ãã®ã¹ã¯ãªããã¯ãåçŽãªæ€åºã®ä»£ããã«ãªããžã§ã¯ãã»ãã©ããã³ã°ãå¯èœã«ããããã®ä¿®æ£ã¹ã¯ãªããã¯ããããªã®åãã¬ãŒã ã«å¯ŸããŠãã©ãã«ãŒãå®è¡ããçµæãèŠèŠåããŠãŠã£ã³ããŠã«è¡šç€ºãããã«ãŒãã¯'q'ãæŒãããšã§çµäºã§ããã
çµæçãªãã©ãã¯ã®ãããã
é£ç¶ãããã¬ãŒã äžã®ãªããžã§ã¯ããã©ãã¯ãèŠèŠåããããšã§ããããªå ã®æ€åºããããªããžã§ã¯ãã®ç§»åãã¿ãŒã³ãåäœã«é¢ãã貎éãªæŽå¯ãåŸãããšãã§ããŸããUltralytics YOLO11 ã䜿ãã°ããããã®è»è·¡ãã·ãŒã ã¬ã¹ãã€å¹ççã«ããããããããšãã§ããŸãã
以äžã®äŸã§ã¯ãYOLO11 ã®ãã©ããã³ã°æ©èœãå©çšããŠãæ€åºããããªããžã§ã¯ãã®åããè€æ°ã®ãããªãã¬ãŒã ã«ããã£ãŠããããããæ¹æ³ã瀺ãããã®ã¹ã¯ãªããã§ã¯ããããªãã¡ã€ã«ãéãããã¬ãŒã ããšã«èªã¿èŸŒã¿ãYOLO ã®ã¢ãã«ãå©çšããŠæ§ã ãªãªããžã§ã¯ããèå¥ãã远跡ããŸããæ€åºãããããŠã³ãã£ã³ã°ããã¯ã¹ã®äžå¿ç¹ãä¿æããããããæ¥ç¶ããããšã§ã远跡ããããªããžã§ã¯ãããã©ã£ãçµè·¯ãè¡šãç·ãæãããšãã§ããã
è€æ°ã®ãããªãã¬ãŒã ã«ãã©ãã¯ããããããã
from collections import defaultdict
import cv2
import numpy as np
from ultralytics import YOLO
# Load the YOLO11 model
model = YOLO("yolo11n.pt")
# Open the video file
video_path = "path/to/video.mp4"
cap = cv2.VideoCapture(video_path)
# Store the track history
track_history = defaultdict(lambda: [])
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLO11 tracking on the frame, persisting tracks between frames
results = model.track(frame, persist=True)
# Get the boxes and track IDs
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Plot the tracks
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
track = track_history[track_id]
track.append((float(x), float(y))) # x, y center point
if len(track) > 30: # retain 90 tracks for 90 frames
track.pop(0)
# Draw the tracking lines
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)
# Display the annotated frame
cv2.imshow("YOLO11 Tracking", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
ãã«ãã¹ã¬ãã远跡
ãã«ãã¹ã¬ãããã©ããã³ã°ã¯ãè€æ°ã®ãããªã¹ããªãŒã ã«å¯ŸããŠåæã«ãªããžã§ã¯ããã©ããã³ã°ãå®è¡ããæ©èœãæäŸããŸããããã¯ãè€æ°ã®ç£èŠã«ã¡ã©ãªã©ããã®è€æ°ã®ãããªå ¥åãæ±ãå Žåã«ç¹ã«æå¹ã§ãåæåŠçã«ããå¹çãšããã©ãŒãã³ã¹ãå€§å¹ ã«åäžããŸãã
æäŸãããPython ã¹ã¯ãªããã§ã¯ãPython ã® threading
ã¢ãžã¥ãŒã«ã䜿ã£ãŠããã©ãã«ãŒã®è€æ°ã®ã€ã³ã¹ã¿ã³ã¹ãåæã«å®è¡ããããšãã§ãããåã¹ã¬ããã¯1ã€ã®ãããªãã¡ã€ã«ã«å¯Ÿãããã©ãã«ãŒã®å®è¡ãæ
åœãããã¹ãŠã®ã¹ã¬ãããããã¯ã°ã©ãŠã³ãã§åæã«å®è¡ãããã
åã¹ã¬ãããæ£ãããã©ã¡ãŒã¿ïŒãããªãã¡ã€ã«ã䜿çšããã¢ãã«ããã¡ã€ã«ã€ã³ããã¯ã¹ïŒãåãåãããã«ãé¢æ° run_tracker_in_thread
ãã®é¢æ°ã¯ãããã®ãã©ã¡ãŒã¿ãåãåããã¡ã€ã³ãã©ããã³ã°ã«ãŒããå«ã¿ãŸãããã®é¢æ°ã¯ãããªããã¬ãŒã ããšã«èªã¿èŸŒã¿ããã©ãã«ãŒãå®è¡ããçµæã衚瀺ããŸãã
ãã®äŸã§ã¯2ã€ã®ç°ãªãã¢ãã«ã䜿çšããŠããïŒ yolo11n.pt
ãã㊠yolo11n-seg.pt
ããããç°ãªããããªãã¡ã€ã«ã®ãªããžã§ã¯ãã远跡ããããããªãã¡ã€ã«ã¯ video_file1
ãã㊠video_file2
.
ã«ã€ã㊠daemon=True
ãã©ã¡ãŒã¿ã® threading.Thread
ã€ãŸããã¡ã€ã³ã»ããã°ã©ã ãçµäºãããšåæã«ããããã®ã¹ã¬ããã¯ã¯ããŒãºãããã次ã«ãã¹ã¬ããã start()
ãã㊠join()
ã䜿ãããšã§ãäž¡æ¹ã®ãã©ãã«ãŒã¹ã¬ãããçµäºãããŸã§ã¡ã€ã³ã¹ã¬ãããåŸ
ãããããšãã§ããã
æåŸã«ããã¹ãŠã®ã¹ã¬ãããã¿ã¹ã¯ãå®äºããããçµæã衚瀺ãããŠã£ã³ããŠã¯ cv2.destroyAllWindows()
.
ãã©ããã³ã°ä»ãã¹ããªãŒãã³ã°ã»ãã©ãŒã«ãŒã
import threading
import cv2
from ultralytics import YOLO
# Define model names and video sources
MODEL_NAMES = ["yolo11n.pt", "yolo11n-seg.pt"]
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam
def run_tracker_in_thread(model_name, filename):
"""
Run YOLO tracker in its own thread for concurrent processing.
Args:
model_name (str): The YOLO11 model object.
filename (str): The path to the video file or the identifier for the webcam/external camera source.
"""
model = YOLO(model_name)
results = model.track(filename, save=True, stream=True)
for r in results:
pass
# Create and start tracker threads using a for loop
tracker_threads = []
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
tracker_threads.append(thread)
thread.start()
# Wait for all tracker threads to finish
for thread in tracker_threads:
thread.join()
# Clean up and close windows
cv2.destroyAllWindows()
ãã®äŸã¯ãããå€ãã®ã¹ã¬ãããäœæããåãæ¹æ³ãé©çšããããšã«ãã£ãŠãããå€ãã®ãããªãã¡ã€ã«ãã¢ãã«ãæ±ãããã«ç°¡åã«æ¡åŒµããããšãã§ããŸãã
æ°ãããã©ãã«ãŒãæçš¿ãã
ããªãã¯å€ãªããžã§ã¯ããã©ããã³ã°ã«ç²ŸéããUltralytics YOLO ããã©ããã³ã°ã¢ã«ãŽãªãºã ã®å®è£ ãé©å¿ã«æåããããšããããŸããïŒultralytics/cfg/trackers ã® Trackers ã»ã¯ã·ã§ã³ãžã®æçš¿ããåŸ ã¡ããŠããŸãïŒããªãã®å®éã®ã¢ããªã±ãŒã·ã§ã³ããœãªã¥ãŒã·ã§ã³ã¯ããã©ããã³ã°ã¿ã¹ã¯ã«åãçµããŠãŒã¶ãŒã«ãšã£ãŠè²Žéãªãã®ãšãªãã§ãããã
ãã®ã»ã¯ã·ã§ã³ã«è²¢ç®ããããšã§ãUltralytics YOLO ãã¬ãŒã ã¯ãŒã¯ã§å©çšå¯èœãªãã©ããã³ã°ã»ãœãªã¥ãŒã·ã§ã³ã®ç¯å²ãæ¡å€§ããã³ãã¥ããã£ã®ããã«æ©èœãšãŠãŒãã£ãªãã£ã®æ°ããªã¬ã€ã€ãŒãè¿œå ããã®ã«åœ¹ç«ã¡ãŸãã
ããªãã®è²¢ç®ãéå§ããã«ã¯ããã«ãªã¯ãšã¹ã (PR) ð ïž ãæåºããããã®å æ¬çãªæé ã«ã€ããŠã¯ãè²¢ç®ã¬ã€ããåç §ããŠãã ãããç§ãã¡ã¯ãããªããäœããããããŠããããã楜ãã¿ã«ããŠããŸãïŒ
äžç·ã«ãUltralytics YOLO ãšã³ã·ã¹ãã ðã®ãã©ããã³ã°æ©èœã匷åããŸãããïŒ
ããããã質å
ãã«ããªããžã§ã¯ãã»ãã©ããã³ã°ãšã¯äœã§ããïŒãŸããUltralytics YOLO ã¯ã©ã®ããã«ãµããŒãããŠããŸããïŒ
Ultralytics YOLO ã¯ããªããžã§ã¯ã ID ãšãšãã«ãªã¢ã«ã¿ã€ã ã®ãã©ããã³ã°ãæäŸããããšã§ããããµããŒãããã»ãã¥ãªãã£ç£èŠãã¹ããŒãåæãªã©ã®ã¿ã¹ã¯ã容æã«ããŸããã·ã¹ãã ã¯BoT-SORTãByteTrackã®ãããªãã©ãã«ãŒã䜿çšããYAMLãã¡ã€ã«ãä»ããŠèšå®ããããšãã§ããã
Ultralytics YOLO çšã®ã«ã¹ã¿ã ã»ãã©ãã«ãŒãèšå®ããã«ã¯ïŒ
æ¢åã®ãã©ãã«ãŒèšå®ãã¡ã€ã«(äŸã custom_tracker.yaml
)ããã§ããã Ultralytics trackerèšå®ãã£ã¬ã¯ã㪠ãé€ããå¿
èŠã«å¿ããŠãã©ã¡ãŒã¿ãå€æŽããã tracker_type
.ãã®ãã¡ã€ã«ããã©ããã³ã°ã¢ãã«ã§æ¬¡ã®ããã«äœ¿çšããïŒ
äŸ
è€æ°ã®ãããªã¹ããªãŒã ã§åæã«ãªããžã§ã¯ããã©ããã³ã°ãå®è¡ããã«ã¯ïŒ
è€æ°ã®ãããªã¹ããªãŒã ã«å¯ŸããŠåæã«ãªããžã§ã¯ããã©ããã³ã°ãå®è¡ããã«ã¯ãPython ã® threading
ã¢ãžã¥ãŒã«ã䜿çšãããåã¹ã¬ããã¯å¥ã
ã®ãããªã¹ããªãŒã ãåŠçããããããã©ã®ããã«èšå®ãããã®äŸã以äžã«ç€ºãïŒ
ãã«ãã¹ã¬ãã远跡
import threading
import cv2
from ultralytics import YOLO
# Define model names and video sources
MODEL_NAMES = ["yolo11n.pt", "yolo11n-seg.pt"]
SOURCES = ["path/to/video.mp4", "0"] # local video, 0 for webcam
def run_tracker_in_thread(model_name, filename):
"""
Run YOLO tracker in its own thread for concurrent processing.
Args:
model_name (str): The YOLO11 model object.
filename (str): The path to the video file or the identifier for the webcam/external camera source.
"""
model = YOLO(model_name)
results = model.track(filename, save=True, stream=True)
for r in results:
pass
# Create and start tracker threads using a for loop
tracker_threads = []
for video_file, model_name in zip(SOURCES, MODEL_NAMES):
thread = threading.Thread(target=run_tracker_in_thread, args=(model_name, video_file), daemon=True)
tracker_threads.append(thread)
thread.start()
# Wait for all tracker threads to finish
for thread in tracker_threads:
thread.join()
# Clean up and close windows
cv2.destroyAllWindows()
Ultralytics YOLO ãçšããå€ãªããžã§ã¯ã远跡ã®å®äžçã§ã®å¿çšãšã¯ïŒ
Ultralytics YOLO ã«ãããã«ããªããžã§ã¯ãã»ãã©ããã³ã°ã¯ã以äžã®ãããªæ°å€ãã®çšéãããïŒ
- 茞éïŒäº€é管çãšèªåŸèµ°è¡ã®ããã®è»äž¡è¿œè·¡ã
- å°å£²æ¥åºèåæããã³ã»ãã¥ãªãã£ã®ããã®ããŒãã«ãã©ããã³ã°ã
- æ°Žç£é€æ®ïŒæ°Žçç°å¢ã¢ãã¿ãªã³ã°ã®ããã®éé¡è¿œè·¡ã
ãããã®ã¢ããªã±ãŒã·ã§ã³ã¯ãUltralytics YOLO ã®ãã€ãã¬ãŒã ã¬ãŒããããªããªã¢ã«ã¿ã€ã ã§åŠçããèœåã®æ©æµãåããã
Ultralytics YOLO ã䜿ã£ãŠãè€æ°ã®ãããªãã¬ãŒã ã«ãŸããããªããžã§ã¯ãã®è»è·¡ãèŠèŠåããã«ã¯ïŒ
è€æ°ã®ãããªãã¬ãŒã ã«ããããªããžã§ã¯ãã®è»è·¡ãèŠèŠåããããã«ãYOLO ã¢ãã«ã®ãã©ããã³ã°æ©èœãš OpenCV ã䜿ã£ãŠãæ€åºããããªããžã§ã¯ãã®è»è·¡ãæãããšãã§ããŸãããããå®æŒããã¹ã¯ãªããã®äŸã§ãïŒ
è€æ°ã®ãããªãã¬ãŒã ã«ãã©ãã¯ããããããã
from collections import defaultdict
import cv2
import numpy as np
from ultralytics import YOLO
model = YOLO("yolo11n.pt")
video_path = "path/to/video.mp4"
cap = cv2.VideoCapture(video_path)
track_history = defaultdict(lambda: [])
while cap.isOpened():
success, frame = cap.read()
if success:
results = model.track(frame, persist=True)
boxes = results[0].boxes.xywh.cpu()
track_ids = results[0].boxes.id.int().cpu().tolist()
annotated_frame = results[0].plot()
for box, track_id in zip(boxes, track_ids):
x, y, w, h = box
track = track_history[track_id]
track.append((float(x), float(y)))
if len(track) > 30:
track.pop(0)
points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2))
cv2.polylines(annotated_frame, [points], isClosed=False, color=(230, 230, 230), thickness=10)
cv2.imshow("YOLO11 Tracking", annotated_frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
break
cap.release()
cv2.destroyAllWindows()
ãã®ã¹ã¯ãªããã¯ããã©ããã³ã°ããããªããžã§ã¯ãã®ç§»åçµè·¯ã瀺ããã©ããã³ã°ã©ã€ã³ãæéçµéãšãšãã«ããããããŸãã