|
|
|
@ -90,6 +90,46 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly |
|
|
|
|
cv2.destroyAllWindows() |
|
|
|
|
``` |
|
|
|
|
|
|
|
|
|
=== "OBB Object Counting" |
|
|
|
|
|
|
|
|
|
```python |
|
|
|
|
import cv2 |
|
|
|
|
|
|
|
|
|
from ultralytics import YOLO, solutions |
|
|
|
|
|
|
|
|
|
model = YOLO("yolov8n-obb.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, |
|
|
|
|
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 in Polygon" |
|
|
|
|
|
|
|
|
|
```python |
|
|
|
@ -123,7 +163,6 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
@ -165,7 +204,6 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
@ -207,7 +245,6 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly |
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|