From 38eaf5e29f2a269ee50de659470c91ae44bb23f8 Mon Sep 17 00:00:00 2001 From: Muhammad Rizwan Munawar Date: Fri, 22 Dec 2023 05:56:44 +0500 Subject: [PATCH] Add line counting and circular heatmaps in Ultralytics Solutions (#7113) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- docs/en/guides/heatmaps.md | 255 ++++++++++++++---------- docs/en/guides/object-counting.md | 196 ++++++++++-------- ultralytics/solutions/heatmap.py | 133 +++++++++--- ultralytics/solutions/object_counter.py | 107 +++++++--- ultralytics/utils/plotting.py | 82 ++++++-- 5 files changed, 526 insertions(+), 247 deletions(-) diff --git a/docs/en/guides/heatmaps.md b/docs/en/guides/heatmaps.md index 7db8c3da..2ebc4526 100644 --- a/docs/en/guides/heatmaps.md +++ b/docs/en/guides/heatmaps.md @@ -20,14 +20,19 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult | Transportation | Retail | |:-----------------------------------------------------------------------------------------------------------------------------------------------:|:---------------------------------------------------------------------------------------------------------------------------------------:| -| ![Ultralytics YOLOv8 Transportation Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/50d197b8-c7f6-4ecf-a664-3d4363b073de) | ![Ultralytics YOLOv8 Retail Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/ffd0649f-5ff5-48d2-876d-6bdffeff5c54) | +| ![Ultralytics YOLOv8 Transportation Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/288d7053-622b-4452-b4e4-1f41aeb764aa) | ![Ultralytics YOLOv8 Retail Heatmap](https://github.com/RizwanMunawar/ultralytics/assets/62513924/a9139af0-2cb7-41fe-a0d5-29a300dee768) | | Ultralytics YOLOv8 Transportation Heatmap | Ultralytics YOLOv8 Retail Heatmap | ???+ tip "heatmap_alpha" heatmap_alpha value should be in range (0.0 - 1.0) -!!! Example "Heatmap Example" +???+ tip "decay_factor" + + Used for removal of heatmap after object removed from frame, value should be in range (0.0 - 1.0) + + +!!! Example "Heatmaps using Ultralytics YOLOv8 Example" === "Heatmap" ```python @@ -35,31 +40,126 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult from ultralytics.solutions import heatmap import cv2 - model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model + model = YOLO("yolov8n.pt") cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" - # Heatmap Init + # 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)))) + + # Init heatmap heatmap_obj = heatmap.Heatmap() - heatmap_obj.set_args(colormap=cv2.COLORMAP_CIVIDIS, - imw=cap.get(4), # should same as cap width - imh=cap.get(3), # should same as cap height + heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA , + imw=cap.get(4), # should same as cap height + imh=cap.get(3), # should same as cap width view_img=True, - decay_factor=0.99) + shape="circle") while cap.isOpened(): success, im0 = cap.read() if not success: - print("Video frame is empty or video processing has been successfully completed.") - break + print("Video frame is empty or video processing has been successfully completed.") + break + tracks = model.track(im0, persist=True, show=False) - results = model.track(im0, persist=True) - im0 = heatmap_obj.generate_heatmap(im0, tracks=results) + im0 = heatmap_obj.generate_heatmap(im0, tracks) + video_writer.write(im0) + cap.release() + video_writer.release() + cv2.destroyAllWindows() + + ``` + + === "Line Counting" + ```python + from ultralytics import YOLO + from ultralytics.solutions import heatmap + import cv2 + + model = YOLO("yolov8n.pt") + cap = cv2.VideoCapture("path/to/video/file.mp4") + 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)))) + + line_points = [(256, 409), (694, 532)] # line for object counting + + # Init heatmap + heatmap_obj = heatmap.Heatmap() + heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA , + imw=cap.get(4), # should same as cap height + imh=cap.get(3), # should same as cap width + view_img=True, + shape="circle", + count_reg_pts=line_points) + + 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 = heatmap_obj.generate_heatmap(im0, tracks) + video_writer.write(im0) + + cap.release() + video_writer.release() cv2.destroyAllWindows() ``` - === "Heatmap with im0" + === "Region Counting" + ```python + from ultralytics import YOLO + from ultralytics.solutions import heatmap + import cv2 + + model = YOLO("yolov8n.pt") + cap = cv2.VideoCapture("path/to/video/file.mp4") + 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)))) + + # Define region points + region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)] + + # Init heatmap + heatmap_obj = heatmap.Heatmap() + heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA , + imw=cap.get(4), # should same as cap height + imh=cap.get(3), # should same as cap width + view_img=True, + shape="circle", + count_reg_pts=region_points) + + 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 = heatmap_obj.generate_heatmap(im0, tracks) + video_writer.write(im0) + + cap.release() + video_writer.release() + cv2.destroyAllWindows() + ``` + + === "Im0" ```python from ultralytics import YOLO from ultralytics.solutions import heatmap @@ -71,10 +171,11 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult # Heatmap Init heatmap_obj = heatmap.Heatmap() - heatmap_obj.set_args(colormap=cv2.COLORMAP_JET, - imw=im0.shape[0], # should same as im0 width - imh=im0.shape[1], # should same as im0 height - view_img=True) + heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA , + imw=cap.get(4), # should same as cap height + imh=cap.get(3), # should same as cap width + view_img=True, + shape="circle") results = model.track(im0, persist=True) @@ -82,43 +183,13 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult cv2.imwrite("ultralytics_output.png", im0) ``` - === "Heatmap with Specific Classes" + === "Specific Classes" ```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") - 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 cap width - imh=cap.get(3), # should same as cap height - view_img=True) - - while cap.isOpened(): - success, im0 = cap.read() - if not success: - print("Video frame is empty or video processing has been successfully completed.") - break - results = model.track(im0, persist=True, classes=classes_for_heatmap) - im0 = heatmap_obj.generate_heatmap(im0, tracks=results) - - cv2.destroyAllWindows() - ``` - - === "Heatmap with Save Output" - ```python - from ultralytics import YOLO - from ultralytics.solutions import heatmap - import cv2 - - model = YOLO("yolov8s.pt") # YOLOv8 custom/pretrained model + model = YOLO("yolov8n.pt") cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" @@ -128,74 +199,50 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult int(cap.get(5)), (int(cap.get(3)), int(cap.get(4)))) - # Heatmap init + classes_for_heatmap = [0, 2] # classes for heatmap + + # Init heatmap heatmap_obj = heatmap.Heatmap() - heatmap_obj.set_args(colormap=cv2.COLORMAP_CIVIDIS, - imw=cap.get(4), # should same as cap width - imh=cap.get(3), # should same as cap height - view_img=True) + heatmap_obj.set_args(colormap=cv2.COLORMAP_PARULA , + imw=cap.get(4), # should same as cap height + imh=cap.get(3), # should same as cap width + view_img=True, + shape="circle") while cap.isOpened(): success, im0 = cap.read() if not success: - print("Video frame is empty or video processing has been successfully completed.") - break - results = model.track(im0, persist=True) - im0 = heatmap_obj.generate_heatmap(im0, tracks=results) + print("Video frame is empty or video processing has been successfully completed.") + break + tracks = model.track(im0, persist=True, show=False, + classes=classes_for_heatmap) + + im0 = heatmap_obj.generate_heatmap(im0, tracks) video_writer.write(im0) + cap.release() video_writer.release() cv2.destroyAllWindows() ``` - === "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 cap width - imh=cap.get(3), # should same as cap height - view_img=True, - count_reg_pts=count_reg_pts) - - while cap.isOpened(): - success, im0 = cap.read() - if not success: - print("Video frame is empty or video processing has been successfully completed.") - break - results = model.track(im0, persist=True) - im0 = heatmap_obj.generate_heatmap(im0, tracks=results) - - cv2.destroyAllWindows() - - ``` - ### 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 | -| 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 | -| decay_factor | `float` | `0.99` | Decay factor for heatmap area removal after specific time | +| 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_txt_color | `RGB Color` | `(0, 0, 0)` | Foreground color for Object counts text | +| count_color | `RGB Color` | `(255, 255, 255)` | Background color for Object counts text | +| count_reg_color | `RGB Color` | `(255, 0, 255)` | Counting region color | +| region_thickness | `int` | `5` | Counting region thickness value | +| decay_factor | `float` | `0.99` | Decay factor for heatmap area removal after specific time | +| shape | `str` | `circle` | Heatmap shape for display "rect" or "circle" supported | +| line_dist_thresh | `int` | `15` | Euclidean Distance threshold for line counter | ### Arguments `model.track` diff --git a/docs/en/guides/object-counting.md b/docs/en/guides/object-counting.md index 520f7dd2..dcc4756d 100644 --- a/docs/en/guides/object-counting.md +++ b/docs/en/guides/object-counting.md @@ -34,9 +34,9 @@ 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" +!!! Example "Object Counting using YOLOv8 Example" - === "Object Counting" + === "Region" ```python from ultralytics import YOLO from ultralytics.solutions import object_counter @@ -46,75 +46,21 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly cap = cv2.VideoCapture("path/to/video/file.mp4") assert cap.isOpened(), "Error reading video file" - counter = object_counter.ObjectCounter() # Init Object Counter + # 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'), + int(cap.get(5)), + (int(cap.get(3)), int(cap.get(4)))) + + # 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) - - cv2.destroyAllWindows() - ``` - - === "Object Counting with Specific Classes" - ```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" - - classes_to_count = [0, 2] - counter = object_counter.ObjectCounter() # Init Object Counter - region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)] - 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, - classes=classes_to_count) - im0 = counter.start_counting(im0, tracks) - - cv2.destroyAllWindows() - ``` - - === "Object Counting with Save Output" - ```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" - - video_writer = cv2.VideoWriter("object_counting.avi", - cv2.VideoWriter_fourcc(*'mp4v'), - int(cap.get(5)), - (int(cap.get(3)), int(cap.get(4)))) - - counter = object_counter.ObjectCounter() # Init Object Counter - region_points = [(20, 400), (1080, 404), (1080, 360), (20, 360)] - counter.set_args(view_img=True, - reg_pts=region_points, - classes_names=model.names, - draw_tracks=True) + reg_pts=region_points, + classes_names=model.names, + draw_tracks=True) while cap.isOpened(): success, im0 = cap.read() @@ -122,9 +68,95 @@ 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) + cap.release() + video_writer.release() + cv2.destroyAllWindows() + + ``` + + === "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" + + # Define line points + line_points = [(20, 400), (1080, 400)] + + # Video writer + video_writer = cv2.VideoWriter("object_counting_output.avi", + cv2.VideoWriter_fourcc(*'mp4v'), + int(cap.get(5)), + (int(cap.get(3)), int(cap.get(4)))) + + # Init Object Counter + counter = object_counter.ObjectCounter() + counter.set_args(view_img=True, + reg_pts=line_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() + ``` + + === "Specific Classes" + ```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" + + line_points = [(20, 400), (1080, 400)] # line or region points + classes_to_count = [0, 2] # person and car classes for count + + # Video writer + video_writer = cv2.VideoWriter("object_counting_output.avi", + cv2.VideoWriter_fourcc(*'mp4v'), + int(cap.get(5)), + (int(cap.get(3)), int(cap.get(4)))) + + # Init Object Counter + counter = object_counter.ObjectCounter() + counter.set_args(view_img=True, + reg_pts=line_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, + classes=classes_to_count) + + im0 = counter.start_counting(im0, tracks) + video_writer.write(im0) + + cap.release() video_writer.release() cv2.destroyAllWindows() ``` @@ -135,15 +167,22 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly ### Optional Arguments `set_args` -| Name | Type | Default | Description | -|-----------------|---------|--------------------------------------------------|---------------------------------------| -| view_img | `bool` | `False` | Display the frame with counts | -| line_thickness | `int` | `2` | Increase the thickness of count value | -| reg_pts | `list` | `(20, 400), (1080, 404), (1080, 360), (20, 360)` | Region Area Points | -| classes_names | `dict` | `model.model.names` | Classes Names Dict | -| region_color | `tuple` | `(0, 255, 0)` | Region Area Color | -| track_thickness | `int` | `2` | Tracking line thickness | -| draw_tracks | `bool` | `False` | Draw Tracks lines | + +| Name | Type | Default | Description | +|---------------------|-------------|----------------------------|-----------------------------------------------| +| view_img | `bool` | `False` | Display frames with counts | +| line_thickness | `int` | `2` | Increase bounding boxes thickness | +| reg_pts | `list` | `[(20, 400), (1260, 400)]` | Points defining the Region Area | +| classes_names | `dict` | `model.model.names` | Dictionary of Class Names | +| region_color | `RGB Color` | `(255, 0, 255)` | Color of the Object counting Region or Line | +| track_thickness | `int` | `2` | Thickness of Tracking Lines | +| draw_tracks | `bool` | `False` | Enable drawing Track lines | +| track_color | `RGB Color` | `(0, 255, 0)` | Color for each track line | +| line_dist_thresh | `int` | `15` | Euclidean Distance threshold for line counter | +| count_txt_thickness | `int` | `2` | Thickness of Object counts text | +| count_txt_color | `RGB Color` | `(0, 0, 0)` | Foreground color for Object counts text | +| count_color | `RGB Color` | `(255, 255, 255)` | Background color for Object counts text | +| region_thickness | `int` | `5` | Thickness for object counter region or line | ### Arguments `model.track` @@ -155,3 +194,4 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly | `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] | +| `verbose` | `bool` | `True` | Display the object tracking results | diff --git a/ultralytics/solutions/heatmap.py b/ultralytics/solutions/heatmap.py index abcef69c..1131a5b1 100644 --- a/ultralytics/solutions/heatmap.py +++ b/ultralytics/solutions/heatmap.py @@ -10,8 +10,7 @@ from ultralytics.utils.plotting import Annotator check_requirements('shapely>=2.0.0') -from shapely.geometry import Polygon -from shapely.geometry.point import Point +from shapely.geometry import LineString, Point, Polygon class Heatmap: @@ -23,6 +22,7 @@ class Heatmap: # Visual information self.annotator = None self.view_img = False + self.shape = 'circle' # Image information self.imw = None @@ -38,17 +38,22 @@ class Heatmap: self.boxes = None self.track_ids = None self.clss = None - self.track_history = None + self.track_history = defaultdict(list) - # Counting info + # Region & Line Information self.count_reg_pts = None - self.count_region = None + self.counting_region = None + self.line_dist_thresh = 15 + self.region_thickness = 5 + self.region_color = (255, 0, 255) + + # Object Counting Information self.in_counts = 0 self.out_counts = 0 - self.count_list = [] + self.counting_list = [] self.count_txt_thickness = 0 - self.count_reg_color = (0, 255, 0) - self.region_thickness = 5 + self.count_txt_color = (0, 0, 0) + self.count_color = (255, 255, 255) # Decay factor self.decay_factor = 0.99 @@ -64,9 +69,13 @@ class Heatmap: view_img=False, count_reg_pts=None, count_txt_thickness=2, + count_txt_color=(0, 0, 0), + count_color=(255, 255, 255), count_reg_color=(255, 0, 255), region_thickness=5, - decay_factor=0.99): + line_dist_thresh=15, + decay_factor=0.99, + shape='circle'): """ Configures the heatmap colormap, width, height and display parameters. @@ -78,27 +87,55 @@ class Heatmap: view_img (bool): Flag indicating frame display count_reg_pts (list): Object counting region points count_txt_thickness (int): Text thickness for object counting display + count_txt_color (RGB color): count text color value + count_color (RGB color): count text background color value count_reg_color (RGB color): Color of object counting region region_thickness (int): Object counting Region thickness + line_dist_thresh (int): Euclidean Distance threshold for line counter decay_factor (float): value for removing heatmap area after object passed + shape (str): Heatmap shape, rect or circle shape supported """ self.imw = imw self.imh = imh - self.colormap = colormap self.heatmap_alpha = heatmap_alpha self.view_img = view_img + self.colormap = colormap - self.heatmap = np.zeros((int(self.imw), int(self.imh)), dtype=np.float32) # Heatmap new frame - + # Region and line selection if count_reg_pts is not None: - self.track_history = defaultdict(list) - self.count_reg_pts = count_reg_pts - self.count_region = Polygon(self.count_reg_pts) - self.count_txt_thickness = count_txt_thickness # Counting text thickness - self.count_reg_color = count_reg_color + if len(count_reg_pts) == 2: + print('Line Counter Initiated.') + self.count_reg_pts = count_reg_pts + self.counting_region = LineString(count_reg_pts) + + elif len(count_reg_pts) == 4: + print('Region Counter Initiated.') + self.count_reg_pts = count_reg_pts + self.counting_region = Polygon(self.count_reg_pts) + + else: + print('Region or line points Invalid, 2 or 4 points supported') + print('Using Line Counter Now') + self.counting_region = Polygon([(20, 400), (1260, 400)]) # dummy points + + # Heatmap new frame + self.heatmap = np.zeros((int(self.imw), int(self.imh)), dtype=np.float32) + + self.count_txt_thickness = count_txt_thickness + self.count_txt_color = count_txt_color + self.count_color = count_color + self.region_color = count_reg_color self.region_thickness = region_thickness self.decay_factor = decay_factor + self.line_dist_thresh = line_dist_thresh + self.shape = shape + + # shape of heatmap, if not selected + if self.shape not in ['circle', 'rect']: + print("Unknown shape value provided, 'circle' & 'rect' supported") + print('Using Circular shape now') + self.shape = 'circle' def extract_results(self, tracks): """ @@ -128,13 +165,26 @@ class Heatmap: self.annotator = Annotator(self.im0, self.count_txt_thickness, None) if self.count_reg_pts is not None: + # Draw counting region self.annotator.draw_region(reg_pts=self.count_reg_pts, - color=self.count_reg_color, + color=self.region_color, thickness=self.region_thickness) for box, cls, track_id in zip(self.boxes, self.clss, self.track_ids): - self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 1 + + if self.shape == 'circle': + center = (int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)) + radius = min(int(box[2]) - int(box[0]), int(box[3]) - int(box[1])) // 2 + + y, x = np.ogrid[0:self.heatmap.shape[0], 0:self.heatmap.shape[1]] + mask = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= radius ** 2 + + self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += \ + (2 * mask[int(box[1]):int(box[3]), int(box[0]):int(box[2])]) + + else: + self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 2 # Store tracking hist track_line = self.track_history[track_id] @@ -143,16 +193,39 @@ class Heatmap: track_line.pop(0) # Count objects - if self.count_region.contains(Point(track_line[-1])): - if track_id not in self.count_list: - self.count_list.append(track_id) - if box[0] < self.count_region.centroid.x: - self.out_counts += 1 - else: - self.in_counts += 1 + if len(self.count_reg_pts) == 4: + if self.counting_region.contains(Point(track_line[-1])): + if track_id not in self.counting_list: + self.counting_list.append(track_id) + if box[0] < self.counting_region.centroid.x: + self.out_counts += 1 + else: + self.in_counts += 1 + + elif len(self.count_reg_pts) == 2: + distance = Point(track_line[-1]).distance(self.counting_region) + if distance < self.line_dist_thresh: + if track_id not in self.counting_list: + self.counting_list.append(track_id) + if box[0] < self.counting_region.centroid.x: + self.out_counts += 1 + else: + self.in_counts += 1 else: for box, cls in zip(self.boxes, self.clss): - self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 1 + + if self.shape == 'circle': + center = (int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)) + radius = min(int(box[2]) - int(box[0]), int(box[3]) - int(box[1])) // 2 + + y, x = np.ogrid[0:self.heatmap.shape[0], 0:self.heatmap.shape[1]] + mask = (x - center[0]) ** 2 + (y - center[1]) ** 2 <= radius ** 2 + + self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += \ + (2 * mask[int(box[1]):int(box[3]), int(box[0]):int(box[2])]) + + else: + self.heatmap[int(box[1]):int(box[3]), int(box[0]):int(box[2])] += 2 # Normalize, apply colormap to heatmap and combine with original image heatmap_normalized = cv2.normalize(self.heatmap, None, 0, 255, cv2.NORM_MINMAX) @@ -161,7 +234,11 @@ class Heatmap: if self.count_reg_pts is not None: incount_label = 'InCount : ' + f'{self.in_counts}' outcount_label = 'OutCount : ' + f'{self.out_counts}' - self.annotator.count_labels(in_count=incount_label, out_count=outcount_label) + self.annotator.count_labels(in_count=incount_label, + out_count=outcount_label, + count_txt_size=self.count_txt_thickness, + txt_color=self.count_txt_color, + color=self.count_color) im0_with_heatmap = cv2.addWeighted(self.im0, 1 - self.heatmap_alpha, heatmap_colored, self.heatmap_alpha, 0) diff --git a/ultralytics/solutions/object_counter.py b/ultralytics/solutions/object_counter.py index fa48ebe7..5b6b4592 100644 --- a/ultralytics/solutions/object_counter.py +++ b/ultralytics/solutions/object_counter.py @@ -9,8 +9,7 @@ from ultralytics.utils.plotting import Annotator, colors check_requirements('shapely>=2.0.0') -from shapely.geometry import Polygon -from shapely.geometry.point import Point +from shapely.geometry import LineString, Point, Polygon class ObjectCounter: @@ -23,10 +22,12 @@ class ObjectCounter: self.is_drawing = False self.selected_point = None - # Region Information - self.reg_pts = None + # Region & Line Information + self.reg_pts = [(20, 400), (1260, 400)] + self.line_dist_thresh = 15 self.counting_region = None - self.region_color = (255, 255, 255) + self.region_color = (255, 0, 255) + self.region_thickness = 5 # Image and annotation Information self.im0 = None @@ -40,11 +41,15 @@ class ObjectCounter: self.in_counts = 0 self.out_counts = 0 self.counting_list = [] + self.count_txt_thickness = 0 + self.count_txt_color = (0, 0, 0) + self.count_color = (255, 255, 255) # Tracks info self.track_history = defaultdict(list) self.track_thickness = 2 self.draw_tracks = False + self.track_color = (0, 255, 0) # Check if environment support imshow self.env_check = check_imshow(warn=True) @@ -52,11 +57,17 @@ class ObjectCounter: def set_args(self, classes_names, reg_pts, - region_color=None, + count_reg_color=(255, 0, 255), line_thickness=2, track_thickness=2, view_img=False, - draw_tracks=False): + draw_tracks=False, + count_txt_thickness=2, + count_txt_color=(0, 0, 0), + count_color=(255, 255, 255), + track_color=(0, 255, 0), + region_thickness=5, + line_dist_thresh=15): """ Configures the Counter's image, bounding box line thickness, and counting region points. @@ -65,18 +76,43 @@ class ObjectCounter: view_img (bool): Flag to control whether to display the video stream. reg_pts (list): Initial list of points defining the counting region. classes_names (dict): Classes names - region_color (tuple): color for region line track_thickness (int): Track thickness draw_tracks (Bool): draw tracks + count_txt_thickness (int): Text thickness for object counting display + count_txt_color (RGB color): count text color value + count_color (RGB color): count text background color value + count_reg_color (RGB color): Color of object counting region + track_color (RGB color): color for tracks + region_thickness (int): Object counting Region thickness + line_dist_thresh (int): Euclidean Distance threshold for line counter """ self.tf = line_thickness self.view_img = view_img self.track_thickness = track_thickness self.draw_tracks = draw_tracks - self.reg_pts = reg_pts - self.counting_region = Polygon(self.reg_pts) + + # Region and line selection + if len(reg_pts) == 2: + print('Line Counter Initiated.') + self.reg_pts = reg_pts + self.counting_region = LineString(self.reg_pts) + elif len(reg_pts) == 4: + print('Region Counter Initiated.') + self.reg_pts = reg_pts + self.counting_region = Polygon(self.reg_pts) + else: + print('Invalid Region points provided, region_points can be 2 or 4') + print('Using Line Counter Now') + self.counting_region = LineString(self.reg_pts) + self.names = classes_names - self.region_color = region_color if region_color else self.region_color + self.track_color = track_color + self.count_txt_thickness = count_txt_thickness + self.count_txt_color = count_txt_color + self.count_color = count_color + self.region_color = count_reg_color + self.region_thickness = region_thickness + self.line_dist_thresh = line_dist_thresh def mouse_event_for_region(self, event, x, y, flags, params): """ @@ -113,11 +149,14 @@ class ObjectCounter: clss = tracks[0].boxes.cls.cpu().tolist() track_ids = tracks[0].boxes.id.int().cpu().tolist() + # Annotator Init and region drawing self.annotator = Annotator(self.im0, self.tf, self.names) - self.annotator.draw_region(reg_pts=self.reg_pts, color=(0, 255, 0)) + self.annotator.draw_region(reg_pts=self.reg_pts, color=self.region_color, thickness=self.region_thickness) + # Extract tracks for box, track_id, cls in zip(boxes, track_ids, clss): - self.annotator.box_label(box, label=self.names[cls], color=colors(int(cls), True)) # Draw bounding box + self.annotator.box_label(box, label=str(track_id) + ':' + self.names[cls], + color=colors(int(cls), True)) # Draw bounding box # Draw Tracks track_line = self.track_history[track_id] @@ -125,28 +164,45 @@ class ObjectCounter: if len(track_line) > 30: track_line.pop(0) + # Draw track trails if self.draw_tracks: self.annotator.draw_centroid_and_tracks(track_line, - color=(0, 255, 0), + color=self.track_color, track_thickness=self.track_thickness) # Count objects - if self.counting_region.contains(Point(track_line[-1])): - if track_id not in self.counting_list: - self.counting_list.append(track_id) - if box[0] < self.counting_region.centroid.x: - self.out_counts += 1 - else: - self.in_counts += 1 + if len(self.reg_pts) == 4: + if self.counting_region.contains(Point(track_line[-1])): + if track_id not in self.counting_list: + self.counting_list.append(track_id) + if box[0] < self.counting_region.centroid.x: + self.out_counts += 1 + else: + self.in_counts += 1 - incount_label = 'InCount : ' + f'{self.in_counts}' + elif len(self.reg_pts) == 2: + distance = Point(track_line[-1]).distance(self.counting_region) + if distance < self.line_dist_thresh: + if track_id not in self.counting_list: + self.counting_list.append(track_id) + if box[0] < self.counting_region.centroid.x: + self.out_counts += 1 + else: + self.in_counts += 1 + + incount_label = 'In Count : ' + f'{self.in_counts}' outcount_label = 'OutCount : ' + f'{self.out_counts}' - self.annotator.count_labels(in_count=incount_label, out_count=outcount_label) + self.annotator.count_labels(in_count=incount_label, + out_count=outcount_label, + count_txt_size=self.count_txt_thickness, + txt_color=self.count_txt_color, + color=self.count_color) if self.env_check and self.view_img: cv2.namedWindow('Ultralytics YOLOv8 Object Counter') - cv2.setMouseCallback('Ultralytics YOLOv8 Object Counter', self.mouse_event_for_region, - {'region_points': self.reg_pts}) + if len(self.reg_pts) == 4: # only add mouse event If user drawn region + cv2.setMouseCallback('Ultralytics YOLOv8 Object Counter', self.mouse_event_for_region, + {'region_points': self.reg_pts}) cv2.imshow('Ultralytics YOLOv8 Object Counter', self.im0) # Break Window if cv2.waitKey(1) & 0xFF == ord('q'): @@ -161,6 +217,7 @@ class ObjectCounter: tracks (list): List of tracks obtained from the object tracking process. """ self.im0 = im0 # store image + if tracks[0].boxes.id is None: return self.extract_and_process_tracks(tracks) diff --git a/ultralytics/utils/plotting.py b/ultralytics/utils/plotting.py index 1d6fade8..08d48cb7 100644 --- a/ultralytics/utils/plotting.py +++ b/ultralytics/utils/plotting.py @@ -260,19 +260,41 @@ class Annotator: # Object Counting Annotator def draw_region(self, reg_pts=None, color=(0, 255, 0), thickness=5): - # Draw region line + """ + Draw region line + Args: + reg_pts (list): Region Points (for line 2 points, for region 4 points) + color (tuple): Region Color value + thickness (int): Region area thickness value + """ cv2.polylines(self.im, [np.array(reg_pts, dtype=np.int32)], isClosed=True, color=color, thickness=thickness) def draw_centroid_and_tracks(self, track, color=(255, 0, 255), track_thickness=2): - # Draw region line + """ + Draw centroid point and track trails + Args: + track (list): object tracking points for trails display + color (tuple): tracks line color + track_thickness (int): track line thickness value + """ points = np.hstack(track).astype(np.int32).reshape((-1, 1, 2)) cv2.polylines(self.im, [points], isClosed=False, color=color, thickness=track_thickness) cv2.circle(self.im, (int(track[-1][0]), int(track[-1][1])), track_thickness * 2, color, -1) - def count_labels(self, in_count=0, out_count=0, color=(255, 255, 255), txt_color=(0, 0, 0)): + def count_labels(self, in_count=0, out_count=0, count_txt_size=2, color=(255, 255, 255), txt_color=(0, 0, 0)): + """ + Plot counts for object counter + Args: + in_count (int): in count value + out_count (int): out count value + count_txt_size (int): text size for counts display + color (tuple): background color of counts display + txt_color (tuple): text color of counts display + """ + self.tf = count_txt_size tl = self.tf or round(0.002 * (self.im.shape[0] + self.im.shape[1]) / 2) + 1 tf = max(tl - 1, 1) - gap = int(24 * tl) # Calculate the gap between in_count and out_count based on line_thickness + gap = int(24 * tl) # gap between in_count and out_count based on line_thickness # Get text size for in_count and out_count t_size_in = cv2.getTextSize(str(in_count), 0, fontScale=tl / 2, thickness=tf)[0] @@ -306,14 +328,13 @@ class Annotator: thickness=self.tf, lineType=cv2.LINE_AA) - # AI GYM Annotator - def estimate_pose_angle(self, a, b, c): + @staticmethod + def estimate_pose_angle(a, b, c): """Calculate the pose angle for object Args: a (float) : The value of pose point a b (float): The value of pose point b c (float): The value o pose point c - Returns: angle (degree): Degree value of angle between three points """ @@ -325,7 +346,15 @@ class Annotator: return angle def draw_specific_points(self, keypoints, indices=[2, 5, 7], shape=(640, 640), radius=2): - """Draw specific keypoints for gym steps counting.""" + """ + Draw specific keypoints for gym steps counting. + + Args: + keypoints (list): list of keypoints data to be plotted + indices (list): keypoints ids list to be plotted + shape (tuple): imgsz for model inference + radius (int): Keypoint radius value + """ nkpts, ndim = keypoints.shape nkpts == 17 and ndim == 3 for i, k in enumerate(keypoints): @@ -340,8 +369,17 @@ class Annotator: return self.im def plot_angle_and_count_and_stage(self, angle_text, count_text, stage_text, center_kpt, line_thickness=2): - """Plot the pose angle, count value and step stage.""" - angle_text, count_text, stage_text = f' {angle_text:.2f}', 'Steps : ' + f'{count_text}', f' {stage_text}' + """ + Plot the pose angle, count value and step stage. + + Args: + angle_text (str): angle value for workout monitoring + count_text (str): counts value for workout monitoring + stage_text (str): stage decision for workout monitoring + center_kpt (int): centroid pose index for workout monitoring + line_thickness (int): thickness for text display + """ + angle_text, count_text, stage_text = (f' {angle_text:.2f}', 'Steps : ' + f'{count_text}', f' {stage_text}') font_scale = 0.6 + (line_thickness / 10.0) # Draw angle @@ -378,18 +416,38 @@ class Annotator: cv2.putText(self.im, stage_text, stage_text_position, 0, font_scale, (0, 0, 0), line_thickness) def seg_bbox(self, mask, mask_color=(255, 0, 255), det_label=None, track_label=None): - """Function for drawing segmented object in bounding box shape.""" + """ + Function for drawing segmented object in bounding box shape. + + Args: + mask (list): masks data list for instance segmentation area plotting + mask_color (tuple): mask foreground color + det_label (str): Detection label text + track_label (str): Tracking label text + """ cv2.polylines(self.im, [np.int32([mask])], isClosed=True, color=mask_color, thickness=2) label = f'Track ID: {track_label}' if track_label else det_label text_size, _ = cv2.getTextSize(label, 0, 0.7, 1) + cv2.rectangle(self.im, (int(mask[0][0]) - text_size[0] // 2 - 10, int(mask[0][1]) - text_size[1] - 10), (int(mask[0][0]) + text_size[0] // 2 + 5, int(mask[0][1] + 5)), mask_color, -1) + cv2.putText(self.im, label, (int(mask[0][0]) - text_size[0] // 2, int(mask[0][1]) - 5), 0, 0.7, (255, 255, 255), 2) def visioneye(self, box, center_point, color=(235, 219, 11), pin_color=(255, 0, 255), thickness=2, pins_radius=10): - """Function for pinpoint human-vision eye mapping and plotting.""" + """ + Function for pinpoint human-vision eye mapping and plotting. + + Args: + box (list): Bounding box coordinates + center_point (tuple): center point for vision eye view + color (tuple): object centroid and line color value + pin_color (tuple): visioneye point color value + thickness (int): int value for line thickness + pins_radius (int): visioneye point radius value + """ center_bbox = int((box[0] + box[2]) / 2), int((box[1] + box[3]) / 2) cv2.circle(self.im, center_point, pins_radius, pin_color, -1) cv2.circle(self.im, center_bbox, pins_radius, color, -1)