Add polygon regions drawing support in object-counting.md and minor docs update (#8885)

This commit is contained in:
Muhammad Rizwan Munawar 2024-03-12 16:18:13 +05:00 committed by GitHub
parent 3623d62ea8
commit 014f0b4b8d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 2 deletions

View file

@ -36,7 +36,7 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
!!! Example "Object Counting using YOLOv8 Example"
=== "Region"
=== "Count in Region"
```python
from ultralytics import YOLO
@ -78,8 +78,51 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
video_writer.release()
cv2.destroyAllWindows()
```
=== "Count in Polygon"
=== "Line"
```python
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)
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 Line"
```python
from ultralytics import YOLO