From 4e825567a9757389db107dd4bb37be050ca7f590 Mon Sep 17 00:00:00 2001 From: Muhammad Rizwan Munawar Date: Wed, 25 Sep 2024 18:12:08 +0500 Subject: [PATCH] Add OBB Counting example in Docs (#16485) Co-authored-by: UltralyticsAssistant --- docs/en/guides/object-counting.md | 43 ++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/en/guides/object-counting.md b/docs/en/guides/object-counting.md index bdec66e42d..7c1367b29e 100644 --- a/docs/en/guides/object-counting.md +++ b/docs/en/guides/object-counting.md @@ -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)