ultralytics 8.0.224 Counting and Heatmaps updates (#6855)

Signed-off-by: Glenn Jocher <glenn.jocher@ultralytics.com>
Co-authored-by: Muhammad Rizwan Munawar <chr043416@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Glenn Jocher 2023-12-08 02:34:32 +01:00 committed by GitHub
parent 0f5406ec21
commit 2b49d71772
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 225 additions and 145 deletions

View file

@ -35,12 +35,11 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8s.pt")
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
# Heatmap Init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_CIVIDIS,
imw=cap.get(4), # should same as im0 width
@ -52,7 +51,7 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
if not success:
exit(0)
results = model.track(im0, persist=True)
frame = heatmap_obj.generate_heatmap(im0, tracks=results)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
```
@ -62,14 +61,13 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8s.pt")
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
classes_for_heatmap = [0, 2]
# Heatmap init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_CIVIDIS,
imw=cap.get(4), # should same as im0 width
@ -80,29 +78,28 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
success, im0 = cap.read()
if not success:
exit(0)
results = model.track(im0, persist=True,
classes=classes_for_heatmap)
frame = heatmap_obj.generate_heatmap(im0, tracks=results)
results = model.track(im0, persist=True, classes=classes_for_heatmap)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
```
=== "Heatmap with Save Output"
```python
from ultralytics import YOLO
import heatmap
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8n.pt")
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
# Video writer
video_writer = cv2.VideoWriter("heatmap_output.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
int(cap.get(5)),
(int(cap.get(3)), int(cap.get(4))))
# Heatmap init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_CIVIDIS,
imw=cap.get(4), # should same as im0 width
@ -113,22 +110,55 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
success, im0 = cap.read()
if not success:
exit(0)
results = model.track(im0, persist=True)
frame = heatmap_obj.generate_heatmap(im0, tracks=results)
results = model.track(im0, persist=True, classes=classes_for_heatmap)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
video_writer.write(im0)
video_writer.release()
```
=== "Heatmap with Object Counting"
```python
from ultralytics import YOLO
from ultralytics.solutions import heatmap
import cv2
model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model
cap = cv2.VideoCapture("path/to/video/file.mp4") # Video file Path, webcam 0
assert cap.isOpened(), "Error reading video file"
# Region for object counting
count_reg_pts = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
# Heatmap Init
heatmap_obj = heatmap.Heatmap()
heatmap_obj.set_args(colormap=cv2.COLORMAP_JET,
imw=cap.get(4), # should same as im0 width
imh=cap.get(3), # should same as im0 height
view_img=True,
count_reg_pts=count_reg_pts)
while cap.isOpened():
success, im0 = cap.read()
if not success:
exit(0)
results = model.track(im0, persist=True)
im0 = heatmap_obj.generate_heatmap(im0, tracks=results)
```
### Arguments `set_args`
| Name | Type | Default | Description |
|---------------|----------------|---------|--------------------------------|
| view_img | `bool` | `False` | Display the frame with heatmap |
| colormap | `cv2.COLORMAP` | `None` | cv2.COLORMAP for heatmap |
| imw | `int` | `None` | Width of Heatmap |
| imh | `int` | `None` | Height of Heatmap |
| heatmap_alpha | `float` | `0.5` | Heatmap alpha value |
| Name | Type | Default | Description |
|---------------------|----------------|-----------------|---------------------------------|
| view_img | `bool` | `False` | Display the frame with heatmap |
| colormap | `cv2.COLORMAP` | `None` | cv2.COLORMAP for heatmap |
| imw | `int` | `None` | Width of Heatmap |
| imh | `int` | `None` | Height of Heatmap |
| heatmap_alpha | `float` | `0.5` | Heatmap alpha value |
| count_reg_pts | `list` | `None` | Object counting region points |
| count_txt_thickness | `int` | `2` | Count values text size |
| count_reg_color | `tuple` | `(255, 0, 255)` | Counting region color |
| region_thickness | `int` | `5` | Counting region thickness value |
### Arguments `model.track`
@ -140,3 +170,32 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
| `conf` | `float` | `0.3` | Confidence Threshold |
| `iou` | `float` | `0.5` | IOU Threshold |
| `classes` | `list` | `None` | filter results by class, i.e. classes=0, or classes=[0,2,3] |
### Heatmap COLORMAPs
| Colormap Name | Description |
|---------------------------------|----------------------------------------|
| `cv::COLORMAP_AUTUMN` | Autumn color map |
| `cv::COLORMAP_BONE` | Bone color map |
| `cv::COLORMAP_JET` | Jet color map |
| `cv::COLORMAP_WINTER` | Winter color map |
| `cv::COLORMAP_RAINBOW` | Rainbow color map |
| `cv::COLORMAP_OCEAN` | Ocean color map |
| `cv::COLORMAP_SUMMER` | Summer color map |
| `cv::COLORMAP_SPRING` | Spring color map |
| `cv::COLORMAP_COOL` | Cool color map |
| `cv::COLORMAP_HSV` | HSV (Hue, Saturation, Value) color map |
| `cv::COLORMAP_PINK` | Pink color map |
| `cv::COLORMAP_HOT` | Hot color map |
| `cv::COLORMAP_PARULA` | Parula color map |
| `cv::COLORMAP_MAGMA` | Magma color map |
| `cv::COLORMAP_INFERNO` | Inferno color map |
| `cv::COLORMAP_PLASMA` | Plasma color map |
| `cv::COLORMAP_VIRIDIS` | Viridis color map |
| `cv::COLORMAP_CIVIDIS` | Cividis color map |
| `cv::COLORMAP_TWILIGHT` | Twilight color map |
| `cv::COLORMAP_TWILIGHT_SHIFTED` | Shifted Twilight color map |
| `cv::COLORMAP_TURBO` | Turbo color map |
| `cv::COLORMAP_DEEPGREEN` | Deep Green color map |
These colormaps are commonly used for visualizing data with different color representations.

View file

@ -23,7 +23,6 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
| ![Conveyor Belt Packets Counting Using Ultralytics YOLOv8](https://github.com/RizwanMunawar/ultralytics/assets/62513924/70e2d106-510c-4c6c-a57a-d34a765aa757) | ![Fish Counting in Sea using Ultralytics YOLOv8](https://github.com/RizwanMunawar/ultralytics/assets/62513924/c60d047b-3837-435f-8d29-bb9fc95d2191) |
| Conveyor Belt Packets Counting Using Ultralytics YOLOv8 | Fish Counting in Sea using Ultralytics YOLOv8 |
!!! Example "Object Counting Example"
=== "Object Counting"
@ -34,9 +33,7 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
counter = object_counter.ObjectCounter() # Init Object Counter
region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)]
@ -61,9 +58,7 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
classes_to_count = [0, 2]
counter = object_counter.ObjectCounter() # Init Object Counter
@ -91,9 +86,7 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
model = YOLO("yolov8n.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
video_writer = cv2.VideoWriter("object_counting.avi",
cv2.VideoWriter_fourcc(*'mp4v'),
@ -134,7 +127,6 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
| track_thickness | `int` | `2` | Tracking line thickness |
| draw_tracks | `bool` | `False` | Draw Tracks lines |
### Arguments `model.track`
| Name | Type | Default | Description |

View file

@ -23,7 +23,6 @@ Monitoring workouts through pose estimation with [Ultralytics YOLOv8](https://gi
| ![PushUps Counting](https://github.com/RizwanMunawar/ultralytics/assets/62513924/cf016a41-589f-420f-8a8c-2cc8174a16de) | ![PullUps Counting](https://github.com/RizwanMunawar/ultralytics/assets/62513924/cb20f316-fac2-4330-8445-dcf5ffebe329) |
| PushUps Counting | PullUps Counting |
!!! Example "Workouts Monitoring Example"
=== "Workouts Monitoring"
@ -34,9 +33,7 @@ Monitoring workouts through pose estimation with [Ultralytics YOLOv8](https://gi
model = YOLO("yolov8n-pose.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
gym_object = ai_gym.AIGym() # init AI GYM module
gym_object.set_args(line_thickness=2,
@ -62,9 +59,7 @@ Monitoring workouts through pose estimation with [Ultralytics YOLOv8](https://gi
model = YOLO("yolov8n-pose.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")
if not cap.isOpened():
print("Error reading video file")
exit(0)
assert cap.isOpened(), "Error reading video file"
video_writer = cv2.VideoWriter("workouts.avi",
cv2.VideoWriter_fourcc(*'mp4v'),