Solutions updates (#9548)
Co-authored-by: UltralyticsAssistant <web@ultralytics.com>
This commit is contained in:
parent
207bba108f
commit
e597bed2f2
5 changed files with 98 additions and 115 deletions
|
|
@ -293,27 +293,26 @@ A heatmap generated with [Ultralytics YOLOv8](https://github.com/ultralytics/ult
|
||||||
|
|
||||||
### Arguments `set_args`
|
### Arguments `set_args`
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|-----------------------|----------------|---------------------|-----------------------------------------------------------|
|
|----------------------|----------------|---------------------|-----------------------------------------------------------|
|
||||||
| `view_img` | `bool` | `False` | Display the frame with heatmap |
|
| `view_img` | `bool` | `False` | Display the frame with heatmap |
|
||||||
| `colormap` | `cv2.COLORMAP` | `None` | cv2.COLORMAP for heatmap |
|
| `colormap` | `cv2.COLORMAP` | `None` | cv2.COLORMAP for heatmap |
|
||||||
| `imw` | `int` | `None` | Width of Heatmap |
|
| `imw` | `int` | `None` | Width of Heatmap |
|
||||||
| `imh` | `int` | `None` | Height of Heatmap |
|
| `imh` | `int` | `None` | Height of Heatmap |
|
||||||
| `view_in_counts` | `bool` | `True` | Display in-counts only on video frame |
|
| `line_thickness` | `int` | `2` | Increase bounding boxes and count text thickness |
|
||||||
| `view_out_counts` | `bool` | `True` | Display out-counts only on video frame |
|
| `view_in_counts` | `bool` | `True` | Display in-counts only on video frame |
|
||||||
| `classes_names` | `dict` | `model.model.names` | Dictionary of Class Names |
|
| `view_out_counts` | `bool` | `True` | Display out-counts only on video frame |
|
||||||
| `heatmap_alpha` | `float` | `0.5` | Heatmap alpha value |
|
| `classes_names` | `dict` | `model.model.names` | Dictionary of Class Names |
|
||||||
| `count_reg_pts` | `list` | `None` | Object counting region points |
|
| `heatmap_alpha` | `float` | `0.5` | Heatmap alpha value |
|
||||||
| `count_txt_thickness` | `int` | `2` | Count values text size |
|
| `count_reg_pts` | `list` | `None` | Object counting region points |
|
||||||
| `count_txt_color` | `RGB Color` | `(0, 0, 0)` | Foreground color for Object counts text |
|
| `count_txt_color` | `RGB Color` | `(0, 0, 0)` | Foreground color for Object counts text |
|
||||||
| `count_reg_color` | `RGB Color` | `(255, 0, 255)` | Counting region color |
|
| `count_reg_color` | `RGB Color` | `(255, 0, 255)` | Counting region color |
|
||||||
| `region_thickness` | `int` | `5` | Counting region thickness value |
|
| `region_thickness` | `int` | `5` | Counting region thickness value |
|
||||||
| `decay_factor` | `float` | `0.99` | Decay factor for heatmap area removal after specific time |
|
| `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 |
|
| `shape` | `str` | `circle` | Heatmap shape for display "rect" or "circle" supported |
|
||||||
| `line_dist_thresh` | `int` | `15` | Euclidean Distance threshold for line counter |
|
| `line_dist_thresh` | `int` | `15` | Euclidean Distance threshold for line counter |
|
||||||
| `fontsize` | `float` | `0.6` | Font size of counting text |
|
| `count_bg_color` | `RGB Color` | `(255, 255, 255)` | Count highlighter color |
|
||||||
| `line_color` | `RGB Color` | `(255, 255, 255)` | Count highlighter color |
|
| `cls_txtdisplay_gap` | `int` | `50` | Display gap between each class count |
|
||||||
| `cls_txtdisplay_gap` | `int` | `50` | Display gap between each class count |
|
|
||||||
|
|
||||||
### Arguments `model.track`
|
### Arguments `model.track`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -62,7 +62,8 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
counter.set_args(view_img=True,
|
counter.set_args(view_img=True,
|
||||||
reg_pts=region_points,
|
reg_pts=region_points,
|
||||||
classes_names=model.names,
|
classes_names=model.names,
|
||||||
draw_tracks=True)
|
draw_tracks=True,
|
||||||
|
line_thickness=2)
|
||||||
|
|
||||||
while cap.isOpened():
|
while cap.isOpened():
|
||||||
success, im0 = cap.read()
|
success, im0 = cap.read()
|
||||||
|
|
@ -105,7 +106,8 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
counter.set_args(view_img=True,
|
counter.set_args(view_img=True,
|
||||||
reg_pts=region_points,
|
reg_pts=region_points,
|
||||||
classes_names=model.names,
|
classes_names=model.names,
|
||||||
draw_tracks=True)
|
draw_tracks=True,
|
||||||
|
line_thickness=2)
|
||||||
|
|
||||||
while cap.isOpened():
|
while cap.isOpened():
|
||||||
success, im0 = cap.read()
|
success, im0 = cap.read()
|
||||||
|
|
@ -148,7 +150,8 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
counter.set_args(view_img=True,
|
counter.set_args(view_img=True,
|
||||||
reg_pts=line_points,
|
reg_pts=line_points,
|
||||||
classes_names=model.names,
|
classes_names=model.names,
|
||||||
draw_tracks=True)
|
draw_tracks=True,
|
||||||
|
line_thickness=2)
|
||||||
|
|
||||||
while cap.isOpened():
|
while cap.isOpened():
|
||||||
success, im0 = cap.read()
|
success, im0 = cap.read()
|
||||||
|
|
@ -191,7 +194,8 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
counter.set_args(view_img=True,
|
counter.set_args(view_img=True,
|
||||||
reg_pts=line_points,
|
reg_pts=line_points,
|
||||||
classes_names=model.names,
|
classes_names=model.names,
|
||||||
draw_tracks=True)
|
draw_tracks=True,
|
||||||
|
line_thickness=2)
|
||||||
|
|
||||||
while cap.isOpened():
|
while cap.isOpened():
|
||||||
success, im0 = cap.read()
|
success, im0 = cap.read()
|
||||||
|
|
@ -215,25 +219,22 @@ Object counting with [Ultralytics YOLOv8](https://github.com/ultralytics/ultraly
|
||||||
|
|
||||||
### Optional Arguments `set_args`
|
### Optional Arguments `set_args`
|
||||||
|
|
||||||
| Name | Type | Default | Description |
|
| Name | Type | Default | Description |
|
||||||
|-----------------------|-------------|----------------------------|-----------------------------------------------|
|
|-----------------------|-------------|----------------------------|--------------------------------------------------|
|
||||||
| `view_img` | `bool` | `False` | Display frames with counts |
|
| `view_img` | `bool` | `False` | Display frames with counts |
|
||||||
| `view_in_counts` | `bool` | `True` | Display in-counts only on video frame |
|
| `view_in_counts` | `bool` | `True` | Display in-counts only on video frame |
|
||||||
| `view_out_counts` | `bool` | `True` | Display out-counts only on video frame |
|
| `view_out_counts` | `bool` | `True` | Display out-counts only on video frame |
|
||||||
| `line_thickness` | `int` | `2` | Increase bounding boxes thickness |
|
| `line_thickness` | `int` | `2` | Increase bounding boxes and count text thickness |
|
||||||
| `reg_pts` | `list` | `[(20, 400), (1260, 400)]` | Points defining the Region Area |
|
| `reg_pts` | `list` | `[(20, 400), (1260, 400)]` | Points defining the Region Area |
|
||||||
| `classes_names` | `dict` | `model.model.names` | Dictionary of Class Names |
|
| `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 |
|
| `count_reg_color` | `RGB Color` | `(255, 0, 255)` | Color of the Object counting Region or Line |
|
||||||
| `track_thickness` | `int` | `2` | Thickness of Tracking Lines |
|
| `track_thickness` | `int` | `2` | Thickness of Tracking Lines |
|
||||||
| `draw_tracks` | `bool` | `False` | Enable drawing Track lines |
|
| `draw_tracks` | `bool` | `False` | Enable drawing Track lines |
|
||||||
| `track_color` | `RGB Color` | `(0, 255, 0)` | Color for each track line |
|
| `track_color` | `RGB Color` | `(0, 255, 0)` | Color for each track line |
|
||||||
| `line_dist_thresh` | `int` | `15` | Euclidean Distance threshold for line counter |
|
| `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` | `(255, 255, 255)` | Foreground color for Object counts text |
|
||||||
| `count_txt_color` | `RGB Color` | `(255, 255, 255)` | Foreground color for Object counts text |
|
| `region_thickness` | `int` | `5` | Thickness for object counter region or line |
|
||||||
| `region_thickness` | `int` | `5` | Thickness for object counter region or line |
|
| `count_bg_color` | `RGB Color` | `(255, 255, 255)` | Count highlighter color |
|
||||||
| `fontsize` | `float` | `0.6` | Font size of counting text |
|
|
||||||
| `line_color` | `RGB Color` | `(255, 255, 255)` | Count highlighter color |
|
|
||||||
| `cls_txtdisplay_gap` | `int` | `50` | Display gap between each class count |
|
|
||||||
|
|
||||||
### Arguments `model.track`
|
### Arguments `model.track`
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ class Heatmap:
|
||||||
self.imw = None
|
self.imw = None
|
||||||
self.imh = None
|
self.imh = None
|
||||||
self.im0 = None
|
self.im0 = None
|
||||||
|
self.tf = 2
|
||||||
self.view_in_counts = True
|
self.view_in_counts = True
|
||||||
self.view_out_counts = True
|
self.view_out_counts = True
|
||||||
|
|
||||||
|
|
@ -56,11 +57,9 @@ class Heatmap:
|
||||||
self.out_counts = 0
|
self.out_counts = 0
|
||||||
self.count_ids = []
|
self.count_ids = []
|
||||||
self.class_wise_count = {}
|
self.class_wise_count = {}
|
||||||
self.count_txt_thickness = 0
|
self.count_txt_color = (0, 0, 0)
|
||||||
self.count_txt_color = (255, 255, 255)
|
self.count_bg_color = (255, 255, 255)
|
||||||
self.line_color = (255, 255, 255)
|
|
||||||
self.cls_txtdisplay_gap = 50
|
self.cls_txtdisplay_gap = 50
|
||||||
self.fontsize = 0.6
|
|
||||||
|
|
||||||
# Decay factor
|
# Decay factor
|
||||||
self.decay_factor = 0.99
|
self.decay_factor = 0.99
|
||||||
|
|
@ -79,16 +78,14 @@ class Heatmap:
|
||||||
view_in_counts=True,
|
view_in_counts=True,
|
||||||
view_out_counts=True,
|
view_out_counts=True,
|
||||||
count_reg_pts=None,
|
count_reg_pts=None,
|
||||||
count_txt_thickness=2,
|
count_txt_color=(0, 0, 0),
|
||||||
count_txt_color=(255, 255, 255),
|
count_bg_color=(255, 255, 255),
|
||||||
fontsize=0.8,
|
|
||||||
line_color=(255, 255, 255),
|
|
||||||
count_reg_color=(255, 0, 255),
|
count_reg_color=(255, 0, 255),
|
||||||
region_thickness=5,
|
region_thickness=5,
|
||||||
line_dist_thresh=15,
|
line_dist_thresh=15,
|
||||||
|
line_thickness=2,
|
||||||
decay_factor=0.99,
|
decay_factor=0.99,
|
||||||
shape="circle",
|
shape="circle",
|
||||||
cls_txtdisplay_gap=50,
|
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Configures the heatmap colormap, width, height and display parameters.
|
Configures the heatmap colormap, width, height and display parameters.
|
||||||
|
|
@ -98,22 +95,21 @@ class Heatmap:
|
||||||
imw (int): The width of the frame.
|
imw (int): The width of the frame.
|
||||||
imh (int): The height of the frame.
|
imh (int): The height of the frame.
|
||||||
classes_names (dict): Classes names
|
classes_names (dict): Classes names
|
||||||
|
line_thickness (int): Line thickness for bounding boxes.
|
||||||
heatmap_alpha (float): alpha value for heatmap display
|
heatmap_alpha (float): alpha value for heatmap display
|
||||||
view_img (bool): Flag indicating frame display
|
view_img (bool): Flag indicating frame display
|
||||||
view_in_counts (bool): Flag to control whether to display the incounts on video stream.
|
view_in_counts (bool): Flag to control whether to display the incounts on video stream.
|
||||||
view_out_counts (bool): Flag to control whether to display the outcounts on video stream.
|
view_out_counts (bool): Flag to control whether to display the outcounts on video stream.
|
||||||
count_reg_pts (list): Object counting region points
|
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_txt_color (RGB color): count text color value
|
||||||
fontsize (float): Text display font size
|
count_bg_color (RGB color): count highlighter line color
|
||||||
line_color (RGB color): count highlighter line color
|
|
||||||
count_reg_color (RGB color): Color of object counting region
|
count_reg_color (RGB color): Color of object counting region
|
||||||
region_thickness (int): Object counting Region thickness
|
region_thickness (int): Object counting Region thickness
|
||||||
line_dist_thresh (int): Euclidean Distance threshold for line counter
|
line_dist_thresh (int): Euclidean Distance threshold for line counter
|
||||||
decay_factor (float): value for removing heatmap area after object passed
|
decay_factor (float): value for removing heatmap area after object passed
|
||||||
shape (str): Heatmap shape, rect or circle shape supported
|
shape (str): Heatmap shape, rect or circle shape supported
|
||||||
cls_txtdisplay_gap (int): Display gap between each class count
|
|
||||||
"""
|
"""
|
||||||
|
self.tf = line_thickness
|
||||||
self.names = classes_names
|
self.names = classes_names
|
||||||
self.imw = imw
|
self.imw = imw
|
||||||
self.imh = imh
|
self.imh = imh
|
||||||
|
|
@ -141,16 +137,13 @@ class Heatmap:
|
||||||
# Heatmap new frame
|
# Heatmap new frame
|
||||||
self.heatmap = np.zeros((int(self.imh), int(self.imw)), dtype=np.float32)
|
self.heatmap = np.zeros((int(self.imh), int(self.imw)), dtype=np.float32)
|
||||||
|
|
||||||
self.count_txt_thickness = count_txt_thickness
|
|
||||||
self.count_txt_color = count_txt_color
|
self.count_txt_color = count_txt_color
|
||||||
self.fontsize = fontsize
|
self.count_bg_color = count_bg_color
|
||||||
self.line_color = line_color
|
|
||||||
self.region_color = count_reg_color
|
self.region_color = count_reg_color
|
||||||
self.region_thickness = region_thickness
|
self.region_thickness = region_thickness
|
||||||
self.decay_factor = decay_factor
|
self.decay_factor = decay_factor
|
||||||
self.line_dist_thresh = line_dist_thresh
|
self.line_dist_thresh = line_dist_thresh
|
||||||
self.shape = shape
|
self.shape = shape
|
||||||
self.cls_txtdisplay_gap = cls_txtdisplay_gap
|
|
||||||
|
|
||||||
# shape of heatmap, if not selected
|
# shape of heatmap, if not selected
|
||||||
if self.shape not in {"circle", "rect"}:
|
if self.shape not in {"circle", "rect"}:
|
||||||
|
|
@ -185,7 +178,7 @@ class Heatmap:
|
||||||
return im0
|
return im0
|
||||||
self.heatmap *= self.decay_factor # decay factor
|
self.heatmap *= self.decay_factor # decay factor
|
||||||
self.extract_results(tracks)
|
self.extract_results(tracks)
|
||||||
self.annotator = Annotator(self.im0, self.count_txt_thickness, None)
|
self.annotator = Annotator(self.im0, self.tf, None)
|
||||||
|
|
||||||
if self.count_reg_pts is not None:
|
if self.count_reg_pts is not None:
|
||||||
# Draw counting region
|
# Draw counting region
|
||||||
|
|
@ -239,11 +232,8 @@ class Heatmap:
|
||||||
|
|
||||||
# Count objects using line
|
# Count objects using line
|
||||||
elif len(self.count_reg_pts) == 2:
|
elif len(self.count_reg_pts) == 2:
|
||||||
is_inside = (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0
|
if prev_position is not None and track_id not in self.count_ids:
|
||||||
|
|
||||||
if prev_position is not None and is_inside and track_id not in self.count_ids:
|
|
||||||
distance = Point(track_line[-1]).distance(self.counting_region)
|
distance = Point(track_line[-1]).distance(self.counting_region)
|
||||||
|
|
||||||
if distance < self.line_dist_thresh and track_id not in self.count_ids:
|
if distance < self.line_dist_thresh and track_id not in self.count_ids:
|
||||||
self.count_ids.append(track_id)
|
self.count_ids.append(track_id)
|
||||||
|
|
||||||
|
|
@ -293,11 +283,8 @@ class Heatmap:
|
||||||
if self.count_reg_pts is not None and label is not None:
|
if self.count_reg_pts is not None and label is not None:
|
||||||
self.annotator.display_counts(
|
self.annotator.display_counts(
|
||||||
counts=label,
|
counts=label,
|
||||||
tf=self.count_txt_thickness,
|
count_txt_color=self.count_txt_color,
|
||||||
fontScale=self.fontsize,
|
count_bg_color=self.count_bg_color,
|
||||||
txt_color=self.count_txt_color,
|
|
||||||
line_color=self.line_color,
|
|
||||||
classwise_txtgap=self.cls_txtdisplay_gap,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
self.im0 = cv2.addWeighted(self.im0, 1 - self.heatmap_alpha, heatmap_colored, self.heatmap_alpha, 0)
|
self.im0 = cv2.addWeighted(self.im0, 1 - self.heatmap_alpha, heatmap_colored, self.heatmap_alpha, 0)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
# Ultralytics YOLO 🚀, AGPL-3.0 license
|
||||||
|
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
from ultralytics.utils.checks import check_imshow, check_requirements
|
from ultralytics.utils.checks import check_imshow, check_requirements
|
||||||
|
|
@ -47,7 +46,7 @@ class ObjectCounter:
|
||||||
self.class_wise_count = {}
|
self.class_wise_count = {}
|
||||||
self.count_txt_thickness = 0
|
self.count_txt_thickness = 0
|
||||||
self.count_txt_color = (255, 255, 255)
|
self.count_txt_color = (255, 255, 255)
|
||||||
self.line_color = (255, 255, 255)
|
self.count_bg_color = (255, 255, 255)
|
||||||
self.cls_txtdisplay_gap = 50
|
self.cls_txtdisplay_gap = 50
|
||||||
self.fontsize = 0.6
|
self.fontsize = 0.6
|
||||||
|
|
||||||
|
|
@ -65,16 +64,14 @@ class ObjectCounter:
|
||||||
classes_names,
|
classes_names,
|
||||||
reg_pts,
|
reg_pts,
|
||||||
count_reg_color=(255, 0, 255),
|
count_reg_color=(255, 0, 255),
|
||||||
|
count_txt_color=(0, 0, 0),
|
||||||
|
count_bg_color=(255, 255, 255),
|
||||||
line_thickness=2,
|
line_thickness=2,
|
||||||
track_thickness=2,
|
track_thickness=2,
|
||||||
view_img=False,
|
view_img=False,
|
||||||
view_in_counts=True,
|
view_in_counts=True,
|
||||||
view_out_counts=True,
|
view_out_counts=True,
|
||||||
draw_tracks=False,
|
draw_tracks=False,
|
||||||
count_txt_thickness=3,
|
|
||||||
count_txt_color=(255, 255, 255),
|
|
||||||
fontsize=0.8,
|
|
||||||
line_color=(255, 255, 255),
|
|
||||||
track_color=None,
|
track_color=None,
|
||||||
region_thickness=5,
|
region_thickness=5,
|
||||||
line_dist_thresh=15,
|
line_dist_thresh=15,
|
||||||
|
|
@ -92,10 +89,8 @@ class ObjectCounter:
|
||||||
classes_names (dict): Classes names
|
classes_names (dict): Classes names
|
||||||
track_thickness (int): Track thickness
|
track_thickness (int): Track thickness
|
||||||
draw_tracks (Bool): draw tracks
|
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_txt_color (RGB color): count text color value
|
||||||
fontsize (float): Text display font size
|
count_bg_color (RGB color): count highlighter line color
|
||||||
line_color (RGB color): count highlighter line color
|
|
||||||
count_reg_color (RGB color): Color of object counting region
|
count_reg_color (RGB color): Color of object counting region
|
||||||
track_color (RGB color): color for tracks
|
track_color (RGB color): color for tracks
|
||||||
region_thickness (int): Object counting Region thickness
|
region_thickness (int): Object counting Region thickness
|
||||||
|
|
@ -125,10 +120,8 @@ class ObjectCounter:
|
||||||
|
|
||||||
self.names = classes_names
|
self.names = classes_names
|
||||||
self.track_color = track_color
|
self.track_color = track_color
|
||||||
self.count_txt_thickness = count_txt_thickness
|
|
||||||
self.count_txt_color = count_txt_color
|
self.count_txt_color = count_txt_color
|
||||||
self.fontsize = fontsize
|
self.count_bg_color = count_bg_color
|
||||||
self.line_color = line_color
|
|
||||||
self.region_color = count_reg_color
|
self.region_color = count_reg_color
|
||||||
self.region_thickness = region_thickness
|
self.region_thickness = region_thickness
|
||||||
self.line_dist_thresh = line_dist_thresh
|
self.line_dist_thresh = line_dist_thresh
|
||||||
|
|
@ -172,6 +165,9 @@ class ObjectCounter:
|
||||||
# Annotator Init and region drawing
|
# Annotator Init and region drawing
|
||||||
self.annotator = Annotator(self.im0, self.tf, self.names)
|
self.annotator = Annotator(self.im0, self.tf, self.names)
|
||||||
|
|
||||||
|
# Draw region or line
|
||||||
|
self.annotator.draw_region(reg_pts=self.reg_pts, color=self.region_color, thickness=self.region_thickness)
|
||||||
|
|
||||||
if tracks[0].boxes.id is not None:
|
if tracks[0].boxes.id is not None:
|
||||||
boxes = tracks[0].boxes.xyxy.cpu()
|
boxes = tracks[0].boxes.xyxy.cpu()
|
||||||
clss = tracks[0].boxes.cls.cpu().tolist()
|
clss = tracks[0].boxes.cls.cpu().tolist()
|
||||||
|
|
@ -220,17 +216,14 @@ class ObjectCounter:
|
||||||
|
|
||||||
# Count objects using line
|
# Count objects using line
|
||||||
elif len(self.reg_pts) == 2:
|
elif len(self.reg_pts) == 2:
|
||||||
is_inside = (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0
|
if prev_position is not None and track_id not in self.count_ids:
|
||||||
|
|
||||||
if prev_position is not None and is_inside and track_id not in self.count_ids:
|
|
||||||
distance = Point(track_line[-1]).distance(self.counting_region)
|
distance = Point(track_line[-1]).distance(self.counting_region)
|
||||||
|
|
||||||
if distance < self.line_dist_thresh and track_id not in self.count_ids:
|
if distance < self.line_dist_thresh and track_id not in self.count_ids:
|
||||||
self.count_ids.append(track_id)
|
self.count_ids.append(track_id)
|
||||||
|
|
||||||
if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0:
|
if (box[0] - prev_position[0]) * (self.counting_region.centroid.x - prev_position[0]) > 0:
|
||||||
self.in_counts += 1
|
self.in_counts += 1
|
||||||
self.class_wise_count[self.names[cls]]["in"] += 1
|
self.class_wise_count[self.names[cls]]["in"] += 2
|
||||||
else:
|
else:
|
||||||
self.out_counts += 1
|
self.out_counts += 1
|
||||||
self.class_wise_count[self.names[cls]]["out"] += 1
|
self.class_wise_count[self.names[cls]]["out"] += 1
|
||||||
|
|
@ -254,17 +247,13 @@ class ObjectCounter:
|
||||||
if label is not None:
|
if label is not None:
|
||||||
self.annotator.display_counts(
|
self.annotator.display_counts(
|
||||||
counts=label,
|
counts=label,
|
||||||
tf=self.count_txt_thickness,
|
count_txt_color=self.count_txt_color,
|
||||||
fontScale=self.fontsize,
|
count_bg_color=self.count_bg_color,
|
||||||
txt_color=self.count_txt_color,
|
|
||||||
line_color=self.line_color,
|
|
||||||
classwise_txtgap=self.cls_txtdisplay_gap,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def display_frames(self):
|
def display_frames(self):
|
||||||
"""Display frame."""
|
"""Display frame."""
|
||||||
if self.env_check:
|
if self.env_check:
|
||||||
self.annotator.draw_region(reg_pts=self.reg_pts, color=self.region_color, thickness=self.region_thickness)
|
|
||||||
cv2.namedWindow(self.window_name)
|
cv2.namedWindow(self.window_name)
|
||||||
if len(self.reg_pts) == 4: # only add mouse event If user drawn region
|
if len(self.reg_pts) == 4: # only add mouse event If user drawn region
|
||||||
cv2.setMouseCallback(self.window_name, self.mouse_event_for_region, {"region_points": self.reg_pts})
|
cv2.setMouseCallback(self.window_name, self.mouse_event_for_region, {"region_points": self.reg_pts})
|
||||||
|
|
|
||||||
|
|
@ -395,49 +395,56 @@ class Annotator:
|
||||||
lineType=cv2.LINE_AA,
|
lineType=cv2.LINE_AA,
|
||||||
)
|
)
|
||||||
|
|
||||||
def display_counts(
|
def display_counts(self, counts=None, count_bg_color=(0, 0, 0), count_txt_color=(255, 255, 255)):
|
||||||
self, counts=None, tf=2, fontScale=0.6, line_color=(0, 0, 0), txt_color=(255, 255, 255), classwise_txtgap=55
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Display counts on im0.
|
Display counts on im0 with text background and border.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
counts (str): objects count data
|
counts (str): objects count data
|
||||||
tf (int): text thickness for display
|
count_bg_color (RGB Color): counts highlighter color
|
||||||
fontScale (float): text fontsize for display
|
count_txt_color (RGB Color): counts display color
|
||||||
line_color (RGB Color): counts highlighter color
|
|
||||||
txt_color (RGB Color): counts display color
|
|
||||||
classwise_txtgap (int): Gap between each class count data
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
tl = tf or round(0.002 * (self.im.shape[0] + self.im.shape[1]) / 2) + 1
|
tl = self.tf or round(0.002 * (self.im.shape[0] + self.im.shape[1]) / 2) + 1
|
||||||
tf = max(tl - 1, 1)
|
tf = max(tl - 1, 1)
|
||||||
|
|
||||||
t_sizes = [cv2.getTextSize(str(count), 0, fontScale=0.8, thickness=tf)[0] for count in counts]
|
t_sizes = [cv2.getTextSize(str(count), 0, fontScale=self.sf, thickness=self.tf)[0] for count in counts]
|
||||||
|
|
||||||
max_text_width = max([size[0] for size in t_sizes])
|
max_text_width = max([size[0] for size in t_sizes])
|
||||||
max_text_height = max([size[1] for size in t_sizes])
|
max_text_height = max([size[1] for size in t_sizes])
|
||||||
|
|
||||||
text_x = self.im.shape[1] - max_text_width - 20
|
text_x = self.im.shape[1] - int(self.im.shape[1] * 0.025 + max_text_width)
|
||||||
text_y = classwise_txtgap
|
text_y = int(self.im.shape[0] * 0.025)
|
||||||
|
|
||||||
|
# Calculate dynamic gap between each count value based on the width of the image
|
||||||
|
dynamic_gap = max(1, self.im.shape[1] // 100) * tf
|
||||||
|
|
||||||
for i, count in enumerate(counts):
|
for i, count in enumerate(counts):
|
||||||
text_x_pos = text_x
|
text_x_pos = text_x
|
||||||
text_y_pos = text_y + i * classwise_txtgap
|
text_y_pos = text_y + i * dynamic_gap # Adjust vertical position with dynamic gap
|
||||||
|
|
||||||
|
# Draw the border
|
||||||
|
cv2.rectangle(
|
||||||
|
self.im,
|
||||||
|
(text_x_pos - (10 * tf), text_y_pos - (10 * tf)),
|
||||||
|
(text_x_pos + max_text_width + (10 * tf), text_y_pos + max_text_height + (10 * tf)),
|
||||||
|
count_bg_color,
|
||||||
|
-1,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Draw the count text
|
||||||
cv2.putText(
|
cv2.putText(
|
||||||
self.im,
|
self.im,
|
||||||
str(count),
|
str(count),
|
||||||
(text_x_pos, text_y_pos),
|
(text_x_pos, text_y_pos + max_text_height),
|
||||||
cv2.FONT_HERSHEY_SIMPLEX,
|
0,
|
||||||
fontScale=fontScale,
|
fontScale=self.sf,
|
||||||
color=txt_color,
|
color=count_txt_color,
|
||||||
thickness=tf,
|
thickness=self.tf,
|
||||||
lineType=cv2.LINE_AA,
|
lineType=cv2.LINE_AA,
|
||||||
)
|
)
|
||||||
|
|
||||||
line_y_pos = text_y_pos + max_text_height + 5
|
text_y_pos += tf * max_text_height
|
||||||
cv2.line(self.im, (text_x_pos, line_y_pos), (text_x_pos + max_text_width, line_y_pos), line_color, tf)
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def estimate_pose_angle(a, b, c):
|
def estimate_pose_angle(a, b, c):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue