Fix region-counting indents (#17835)
Signed-off-by: UltralyticsAssistant <web@ultralytics.com> Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
55b2137f18
commit
386a3b7625
4 changed files with 39 additions and 45 deletions
|
|
@ -4,7 +4,7 @@ description: Learn how to use Ultralytics YOLOv8 for precise object counting in
|
|||
keywords: object counting, regions, YOLOv8, computer vision, Ultralytics, efficiency, accuracy, automation, real-time, applications, surveillance, monitoring
|
||||
---
|
||||
|
||||
# Object Counting in Different Regions using Ultralytics YOLOv8 🚀
|
||||
# Object Counting in Different Regions using Ultralytics YOLO 🚀
|
||||
|
||||
## What is Object Counting in Regions?
|
||||
|
||||
|
|
@ -39,44 +39,45 @@ keywords: object counting, regions, YOLOv8, computer vision, Ultralytics, effici
|
|||
=== "Python"
|
||||
|
||||
```python
|
||||
import cv2
|
||||
from ultralytics import solutions
|
||||
import cv2
|
||||
|
||||
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))
|
||||
from ultralytics import solutions
|
||||
|
||||
# Define region points
|
||||
# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)] # Pass region as list
|
||||
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))
|
||||
|
||||
# pass region as dictionary
|
||||
region_points = {
|
||||
"region-01": [(50, 50), (250, 50), (250, 250), (50, 250)],
|
||||
"region-02": [(640, 640), (780, 640), (780, 720), (640, 720)]
|
||||
}
|
||||
# Define region points
|
||||
# region_points = [(20, 400), (1080, 400), (1080, 360), (20, 360)] # Pass region as list
|
||||
|
||||
# Video writer
|
||||
video_writer = cv2.VideoWriter("region_counting.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
|
||||
# pass region as dictionary
|
||||
region_points = {
|
||||
"region-01": [(50, 50), (250, 50), (250, 250), (50, 250)],
|
||||
"region-02": [(640, 640), (780, 640), (780, 720), (640, 720)],
|
||||
}
|
||||
|
||||
# Init Object Counter
|
||||
region = solutions.RegionCounter(
|
||||
show=True,
|
||||
region=region_points,
|
||||
model="yolo11n.pt",
|
||||
)
|
||||
# Video writer
|
||||
video_writer = cv2.VideoWriter("region_counting.avi", cv2.VideoWriter_fourcc(*"mp4v"), fps, (w, h))
|
||||
|
||||
# Process video
|
||||
while cap.isOpened():
|
||||
success, im0 = cap.read()
|
||||
if not success:
|
||||
print("Video frame is empty or video processing has been successfully completed.")
|
||||
break
|
||||
im0 = region.count(im0)
|
||||
video_writer.write(im0)
|
||||
# Init Object Counter
|
||||
region = solutions.RegionCounter(
|
||||
show=True,
|
||||
region=region_points,
|
||||
model="yolo11n.pt",
|
||||
)
|
||||
|
||||
cap.release()
|
||||
video_writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
# Process video
|
||||
while cap.isOpened():
|
||||
success, im0 = cap.read()
|
||||
if not success:
|
||||
print("Video frame is empty or video processing has been successfully completed.")
|
||||
break
|
||||
im0 = region.count(im0)
|
||||
video_writer.write(im0)
|
||||
|
||||
cap.release()
|
||||
video_writer.release()
|
||||
cv2.destroyAllWindows()
|
||||
```
|
||||
|
||||
!!! tip "Ultralytics Example Code"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue