Add OBB Counting example in Docs (#16485)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
f9c01500eb
commit
4e825567a9
1 changed files with 40 additions and 3 deletions
|
|
@ -90,6 +90,46 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
cv2.destroyAllWindows()
|
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"
|
=== "Count in Polygon"
|
||||||
|
|
||||||
```python
|
```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.")
|
print("Video frame is empty or video processing has been successfully completed.")
|
||||||
break
|
break
|
||||||
tracks = model.track(im0, persist=True, show=False)
|
tracks = model.track(im0, persist=True, show=False)
|
||||||
|
|
||||||
im0 = counter.start_counting(im0, tracks)
|
im0 = counter.start_counting(im0, tracks)
|
||||||
video_writer.write(im0)
|
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.")
|
print("Video frame is empty or video processing has been successfully completed.")
|
||||||
break
|
break
|
||||||
tracks = model.track(im0, persist=True, show=False)
|
tracks = model.track(im0, persist=True, show=False)
|
||||||
|
|
||||||
im0 = counter.start_counting(im0, tracks)
|
im0 = counter.start_counting(im0, tracks)
|
||||||
video_writer.write(im0)
|
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.")
|
print("Video frame is empty or video processing has been successfully completed.")
|
||||||
break
|
break
|
||||||
tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
|
tracks = model.track(im0, persist=True, show=False, classes=classes_to_count)
|
||||||
|
|
||||||
im0 = counter.start_counting(im0, tracks)
|
im0 = counter.start_counting(im0, tracks)
|
||||||
video_writer.write(im0)
|
video_writer.write(im0)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue