dgenerate-ultralytics-headless 8.3.134__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dgenerate_ultralytics_headless-8.3.134.dist-info/METADATA +400 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/RECORD +272 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/WHEEL +5 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/entry_points.txt +3 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/licenses/LICENSE +661 -0
- dgenerate_ultralytics_headless-8.3.134.dist-info/top_level.txt +1 -0
- tests/__init__.py +22 -0
- tests/conftest.py +83 -0
- tests/test_cli.py +138 -0
- tests/test_cuda.py +215 -0
- tests/test_engine.py +131 -0
- tests/test_exports.py +236 -0
- tests/test_integrations.py +154 -0
- tests/test_python.py +694 -0
- tests/test_solutions.py +187 -0
- ultralytics/__init__.py +30 -0
- ultralytics/assets/bus.jpg +0 -0
- ultralytics/assets/zidane.jpg +0 -0
- ultralytics/cfg/__init__.py +1023 -0
- ultralytics/cfg/datasets/Argoverse.yaml +77 -0
- ultralytics/cfg/datasets/DOTAv1.5.yaml +37 -0
- ultralytics/cfg/datasets/DOTAv1.yaml +36 -0
- ultralytics/cfg/datasets/GlobalWheat2020.yaml +68 -0
- ultralytics/cfg/datasets/HomeObjects-3K.yaml +33 -0
- ultralytics/cfg/datasets/ImageNet.yaml +2025 -0
- ultralytics/cfg/datasets/Objects365.yaml +443 -0
- ultralytics/cfg/datasets/SKU-110K.yaml +58 -0
- ultralytics/cfg/datasets/VOC.yaml +106 -0
- ultralytics/cfg/datasets/VisDrone.yaml +77 -0
- ultralytics/cfg/datasets/african-wildlife.yaml +25 -0
- ultralytics/cfg/datasets/brain-tumor.yaml +23 -0
- ultralytics/cfg/datasets/carparts-seg.yaml +44 -0
- ultralytics/cfg/datasets/coco-pose.yaml +42 -0
- ultralytics/cfg/datasets/coco.yaml +118 -0
- ultralytics/cfg/datasets/coco128-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco128.yaml +101 -0
- ultralytics/cfg/datasets/coco8-multispectral.yaml +104 -0
- ultralytics/cfg/datasets/coco8-pose.yaml +26 -0
- ultralytics/cfg/datasets/coco8-seg.yaml +101 -0
- ultralytics/cfg/datasets/coco8.yaml +101 -0
- ultralytics/cfg/datasets/crack-seg.yaml +22 -0
- ultralytics/cfg/datasets/dog-pose.yaml +24 -0
- ultralytics/cfg/datasets/dota8-multispectral.yaml +38 -0
- ultralytics/cfg/datasets/dota8.yaml +35 -0
- ultralytics/cfg/datasets/hand-keypoints.yaml +26 -0
- ultralytics/cfg/datasets/lvis.yaml +1240 -0
- ultralytics/cfg/datasets/medical-pills.yaml +22 -0
- ultralytics/cfg/datasets/open-images-v7.yaml +666 -0
- ultralytics/cfg/datasets/package-seg.yaml +22 -0
- ultralytics/cfg/datasets/signature.yaml +21 -0
- ultralytics/cfg/datasets/tiger-pose.yaml +25 -0
- ultralytics/cfg/datasets/xView.yaml +155 -0
- ultralytics/cfg/default.yaml +127 -0
- ultralytics/cfg/models/11/yolo11-cls-resnet18.yaml +17 -0
- ultralytics/cfg/models/11/yolo11-cls.yaml +33 -0
- ultralytics/cfg/models/11/yolo11-obb.yaml +50 -0
- ultralytics/cfg/models/11/yolo11-pose.yaml +51 -0
- ultralytics/cfg/models/11/yolo11-seg.yaml +50 -0
- ultralytics/cfg/models/11/yolo11.yaml +50 -0
- ultralytics/cfg/models/11/yoloe-11-seg.yaml +48 -0
- ultralytics/cfg/models/11/yoloe-11.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-cls.yaml +32 -0
- ultralytics/cfg/models/12/yolo12-obb.yaml +48 -0
- ultralytics/cfg/models/12/yolo12-pose.yaml +49 -0
- ultralytics/cfg/models/12/yolo12-seg.yaml +48 -0
- ultralytics/cfg/models/12/yolo12.yaml +48 -0
- ultralytics/cfg/models/rt-detr/rtdetr-l.yaml +53 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet101.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml +45 -0
- ultralytics/cfg/models/rt-detr/rtdetr-x.yaml +57 -0
- ultralytics/cfg/models/v10/yolov10b.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10l.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10m.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10n.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10s.yaml +45 -0
- ultralytics/cfg/models/v10/yolov10x.yaml +45 -0
- ultralytics/cfg/models/v3/yolov3-spp.yaml +49 -0
- ultralytics/cfg/models/v3/yolov3-tiny.yaml +40 -0
- ultralytics/cfg/models/v3/yolov3.yaml +49 -0
- ultralytics/cfg/models/v5/yolov5-p6.yaml +62 -0
- ultralytics/cfg/models/v5/yolov5.yaml +51 -0
- ultralytics/cfg/models/v6/yolov6.yaml +56 -0
- ultralytics/cfg/models/v8/yoloe-v8-seg.yaml +45 -0
- ultralytics/cfg/models/v8/yoloe-v8.yaml +45 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet101.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls-resnet50.yaml +28 -0
- ultralytics/cfg/models/v8/yolov8-cls.yaml +32 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p2.yaml +58 -0
- ultralytics/cfg/models/v8/yolov8-ghost-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-ghost.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-obb.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-p2.yaml +57 -0
- ultralytics/cfg/models/v8/yolov8-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-pose-p6.yaml +60 -0
- ultralytics/cfg/models/v8/yolov8-pose.yaml +50 -0
- ultralytics/cfg/models/v8/yolov8-rtdetr.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-seg-p6.yaml +59 -0
- ultralytics/cfg/models/v8/yolov8-seg.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8-world.yaml +51 -0
- ultralytics/cfg/models/v8/yolov8-worldv2.yaml +49 -0
- ultralytics/cfg/models/v8/yolov8.yaml +49 -0
- ultralytics/cfg/models/v9/yolov9c-seg.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9c.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9e-seg.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9e.yaml +64 -0
- ultralytics/cfg/models/v9/yolov9m.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9s.yaml +41 -0
- ultralytics/cfg/models/v9/yolov9t.yaml +41 -0
- ultralytics/cfg/trackers/botsort.yaml +22 -0
- ultralytics/cfg/trackers/bytetrack.yaml +14 -0
- ultralytics/data/__init__.py +26 -0
- ultralytics/data/annotator.py +66 -0
- ultralytics/data/augment.py +2945 -0
- ultralytics/data/base.py +438 -0
- ultralytics/data/build.py +258 -0
- ultralytics/data/converter.py +754 -0
- ultralytics/data/dataset.py +834 -0
- ultralytics/data/loaders.py +676 -0
- ultralytics/data/scripts/download_weights.sh +18 -0
- ultralytics/data/scripts/get_coco.sh +61 -0
- ultralytics/data/scripts/get_coco128.sh +18 -0
- ultralytics/data/scripts/get_imagenet.sh +52 -0
- ultralytics/data/split.py +125 -0
- ultralytics/data/split_dota.py +325 -0
- ultralytics/data/utils.py +777 -0
- ultralytics/engine/__init__.py +1 -0
- ultralytics/engine/exporter.py +1519 -0
- ultralytics/engine/model.py +1156 -0
- ultralytics/engine/predictor.py +502 -0
- ultralytics/engine/results.py +1840 -0
- ultralytics/engine/trainer.py +853 -0
- ultralytics/engine/tuner.py +243 -0
- ultralytics/engine/validator.py +377 -0
- ultralytics/hub/__init__.py +168 -0
- ultralytics/hub/auth.py +137 -0
- ultralytics/hub/google/__init__.py +176 -0
- ultralytics/hub/session.py +446 -0
- ultralytics/hub/utils.py +248 -0
- ultralytics/models/__init__.py +9 -0
- ultralytics/models/fastsam/__init__.py +7 -0
- ultralytics/models/fastsam/model.py +61 -0
- ultralytics/models/fastsam/predict.py +181 -0
- ultralytics/models/fastsam/utils.py +24 -0
- ultralytics/models/fastsam/val.py +40 -0
- ultralytics/models/nas/__init__.py +7 -0
- ultralytics/models/nas/model.py +102 -0
- ultralytics/models/nas/predict.py +58 -0
- ultralytics/models/nas/val.py +39 -0
- ultralytics/models/rtdetr/__init__.py +7 -0
- ultralytics/models/rtdetr/model.py +63 -0
- ultralytics/models/rtdetr/predict.py +84 -0
- ultralytics/models/rtdetr/train.py +85 -0
- ultralytics/models/rtdetr/val.py +191 -0
- ultralytics/models/sam/__init__.py +6 -0
- ultralytics/models/sam/amg.py +260 -0
- ultralytics/models/sam/build.py +358 -0
- ultralytics/models/sam/model.py +170 -0
- ultralytics/models/sam/modules/__init__.py +1 -0
- ultralytics/models/sam/modules/blocks.py +1129 -0
- ultralytics/models/sam/modules/decoders.py +515 -0
- ultralytics/models/sam/modules/encoders.py +854 -0
- ultralytics/models/sam/modules/memory_attention.py +299 -0
- ultralytics/models/sam/modules/sam.py +1006 -0
- ultralytics/models/sam/modules/tiny_encoder.py +1002 -0
- ultralytics/models/sam/modules/transformer.py +351 -0
- ultralytics/models/sam/modules/utils.py +394 -0
- ultralytics/models/sam/predict.py +1605 -0
- ultralytics/models/utils/__init__.py +1 -0
- ultralytics/models/utils/loss.py +455 -0
- ultralytics/models/utils/ops.py +268 -0
- ultralytics/models/yolo/__init__.py +7 -0
- ultralytics/models/yolo/classify/__init__.py +7 -0
- ultralytics/models/yolo/classify/predict.py +88 -0
- ultralytics/models/yolo/classify/train.py +233 -0
- ultralytics/models/yolo/classify/val.py +215 -0
- ultralytics/models/yolo/detect/__init__.py +7 -0
- ultralytics/models/yolo/detect/predict.py +124 -0
- ultralytics/models/yolo/detect/train.py +217 -0
- ultralytics/models/yolo/detect/val.py +451 -0
- ultralytics/models/yolo/model.py +354 -0
- ultralytics/models/yolo/obb/__init__.py +7 -0
- ultralytics/models/yolo/obb/predict.py +66 -0
- ultralytics/models/yolo/obb/train.py +81 -0
- ultralytics/models/yolo/obb/val.py +283 -0
- ultralytics/models/yolo/pose/__init__.py +7 -0
- ultralytics/models/yolo/pose/predict.py +79 -0
- ultralytics/models/yolo/pose/train.py +154 -0
- ultralytics/models/yolo/pose/val.py +394 -0
- ultralytics/models/yolo/segment/__init__.py +7 -0
- ultralytics/models/yolo/segment/predict.py +113 -0
- ultralytics/models/yolo/segment/train.py +123 -0
- ultralytics/models/yolo/segment/val.py +428 -0
- ultralytics/models/yolo/world/__init__.py +5 -0
- ultralytics/models/yolo/world/train.py +119 -0
- ultralytics/models/yolo/world/train_world.py +176 -0
- ultralytics/models/yolo/yoloe/__init__.py +22 -0
- ultralytics/models/yolo/yoloe/predict.py +169 -0
- ultralytics/models/yolo/yoloe/train.py +298 -0
- ultralytics/models/yolo/yoloe/train_seg.py +124 -0
- ultralytics/models/yolo/yoloe/val.py +191 -0
- ultralytics/nn/__init__.py +29 -0
- ultralytics/nn/autobackend.py +842 -0
- ultralytics/nn/modules/__init__.py +182 -0
- ultralytics/nn/modules/activation.py +53 -0
- ultralytics/nn/modules/block.py +1966 -0
- ultralytics/nn/modules/conv.py +712 -0
- ultralytics/nn/modules/head.py +880 -0
- ultralytics/nn/modules/transformer.py +713 -0
- ultralytics/nn/modules/utils.py +164 -0
- ultralytics/nn/tasks.py +1627 -0
- ultralytics/nn/text_model.py +351 -0
- ultralytics/solutions/__init__.py +41 -0
- ultralytics/solutions/ai_gym.py +116 -0
- ultralytics/solutions/analytics.py +252 -0
- ultralytics/solutions/config.py +106 -0
- ultralytics/solutions/distance_calculation.py +124 -0
- ultralytics/solutions/heatmap.py +127 -0
- ultralytics/solutions/instance_segmentation.py +84 -0
- ultralytics/solutions/object_blurrer.py +90 -0
- ultralytics/solutions/object_counter.py +195 -0
- ultralytics/solutions/object_cropper.py +84 -0
- ultralytics/solutions/parking_management.py +273 -0
- ultralytics/solutions/queue_management.py +93 -0
- ultralytics/solutions/region_counter.py +120 -0
- ultralytics/solutions/security_alarm.py +154 -0
- ultralytics/solutions/similarity_search.py +172 -0
- ultralytics/solutions/solutions.py +724 -0
- ultralytics/solutions/speed_estimation.py +110 -0
- ultralytics/solutions/streamlit_inference.py +196 -0
- ultralytics/solutions/templates/similarity-search.html +160 -0
- ultralytics/solutions/trackzone.py +88 -0
- ultralytics/solutions/vision_eye.py +68 -0
- ultralytics/trackers/__init__.py +7 -0
- ultralytics/trackers/basetrack.py +124 -0
- ultralytics/trackers/bot_sort.py +260 -0
- ultralytics/trackers/byte_tracker.py +480 -0
- ultralytics/trackers/track.py +125 -0
- ultralytics/trackers/utils/__init__.py +1 -0
- ultralytics/trackers/utils/gmc.py +376 -0
- ultralytics/trackers/utils/kalman_filter.py +493 -0
- ultralytics/trackers/utils/matching.py +157 -0
- ultralytics/utils/__init__.py +1435 -0
- ultralytics/utils/autobatch.py +106 -0
- ultralytics/utils/autodevice.py +174 -0
- ultralytics/utils/benchmarks.py +695 -0
- ultralytics/utils/callbacks/__init__.py +5 -0
- ultralytics/utils/callbacks/base.py +234 -0
- ultralytics/utils/callbacks/clearml.py +153 -0
- ultralytics/utils/callbacks/comet.py +552 -0
- ultralytics/utils/callbacks/dvc.py +205 -0
- ultralytics/utils/callbacks/hub.py +108 -0
- ultralytics/utils/callbacks/mlflow.py +138 -0
- ultralytics/utils/callbacks/neptune.py +140 -0
- ultralytics/utils/callbacks/raytune.py +43 -0
- ultralytics/utils/callbacks/tensorboard.py +132 -0
- ultralytics/utils/callbacks/wb.py +185 -0
- ultralytics/utils/checks.py +897 -0
- ultralytics/utils/dist.py +119 -0
- ultralytics/utils/downloads.py +499 -0
- ultralytics/utils/errors.py +43 -0
- ultralytics/utils/export.py +219 -0
- ultralytics/utils/files.py +221 -0
- ultralytics/utils/instance.py +499 -0
- ultralytics/utils/loss.py +813 -0
- ultralytics/utils/metrics.py +1356 -0
- ultralytics/utils/ops.py +885 -0
- ultralytics/utils/patches.py +143 -0
- ultralytics/utils/plotting.py +1011 -0
- ultralytics/utils/tal.py +416 -0
- ultralytics/utils/torch_utils.py +990 -0
- ultralytics/utils/triton.py +116 -0
- ultralytics/utils/tuner.py +159 -0
@@ -0,0 +1,84 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
from ultralytics.engine.results import Results
|
4
|
+
from ultralytics.solutions.solutions import BaseSolution, SolutionResults
|
5
|
+
|
6
|
+
|
7
|
+
class InstanceSegmentation(BaseSolution):
|
8
|
+
"""
|
9
|
+
A class to manage instance segmentation in images or video streams.
|
10
|
+
|
11
|
+
This class extends the BaseSolution class and provides functionality for performing instance segmentation, including
|
12
|
+
drawing segmented masks with bounding boxes and labels.
|
13
|
+
|
14
|
+
Attributes:
|
15
|
+
model (str): The segmentation model to use for inference.
|
16
|
+
line_width (int): Width of the bounding box and text lines.
|
17
|
+
names (Dict[int, str]): Dictionary mapping class indices to class names.
|
18
|
+
clss (List[int]): List of detected class indices.
|
19
|
+
track_ids (List[int]): List of track IDs for detected instances.
|
20
|
+
masks (List[numpy.ndarray]): List of segmentation masks for detected instances.
|
21
|
+
|
22
|
+
Methods:
|
23
|
+
process: Process the input image to perform instance segmentation and annotate results.
|
24
|
+
extract_tracks: Extract tracks including bounding boxes, classes, and masks from model predictions.
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
>>> segmenter = InstanceSegmentation()
|
28
|
+
>>> frame = cv2.imread("frame.jpg")
|
29
|
+
>>> results = segmenter.segment(frame)
|
30
|
+
>>> print(f"Total segmented instances: {results['total_tracks']}")
|
31
|
+
"""
|
32
|
+
|
33
|
+
def __init__(self, **kwargs):
|
34
|
+
"""
|
35
|
+
Initialize the InstanceSegmentation class for detecting and annotating segmented instances.
|
36
|
+
|
37
|
+
Args:
|
38
|
+
**kwargs (Any): Keyword arguments passed to the BaseSolution parent class.
|
39
|
+
model (str): Model name or path, defaults to "yolo11n-seg.pt".
|
40
|
+
"""
|
41
|
+
kwargs["model"] = kwargs.get("model", "yolo11n-seg.pt")
|
42
|
+
super().__init__(**kwargs)
|
43
|
+
|
44
|
+
self.show_conf = self.CFG.get("show_conf", True)
|
45
|
+
self.show_labels = self.CFG.get("show_labels", True)
|
46
|
+
self.show_boxes = self.CFG.get("show_boxes", True)
|
47
|
+
|
48
|
+
def process(self, im0):
|
49
|
+
"""
|
50
|
+
Perform instance segmentation on the input image and annotate the results.
|
51
|
+
|
52
|
+
Args:
|
53
|
+
im0 (numpy.ndarray): The input image for segmentation.
|
54
|
+
|
55
|
+
Returns:
|
56
|
+
(SolutionResults): Object containing the annotated image and total number of tracked instances.
|
57
|
+
|
58
|
+
Examples:
|
59
|
+
>>> segmenter = InstanceSegmentation()
|
60
|
+
>>> frame = cv2.imread("image.jpg")
|
61
|
+
>>> summary = segmenter.segment(frame)
|
62
|
+
>>> print(summary)
|
63
|
+
"""
|
64
|
+
self.extract_tracks(im0) # Extract tracks (bounding boxes, classes, and masks)
|
65
|
+
self.masks = getattr(self.tracks[0], "masks", None)
|
66
|
+
|
67
|
+
# Iterate over detected classes, track IDs, and segmentation masks
|
68
|
+
if self.masks is None:
|
69
|
+
self.LOGGER.warning("No masks detected! Ensure you're using a supported Ultralytics segmentation model.")
|
70
|
+
plot_im = im0
|
71
|
+
else:
|
72
|
+
results = Results(im0, path=None, names=self.names, boxes=self.track_data.data, masks=self.masks.data)
|
73
|
+
plot_im = results.plot(
|
74
|
+
line_width=self.line_width,
|
75
|
+
boxes=self.show_boxes,
|
76
|
+
conf=self.show_conf,
|
77
|
+
labels=self.show_labels,
|
78
|
+
color_mode="instance",
|
79
|
+
)
|
80
|
+
|
81
|
+
self.display_output(plot_im) # Display the annotated output using the base class function
|
82
|
+
|
83
|
+
# Return SolutionResults
|
84
|
+
return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
import cv2
|
4
|
+
|
5
|
+
from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
|
6
|
+
from ultralytics.utils import LOGGER
|
7
|
+
from ultralytics.utils.plotting import colors
|
8
|
+
|
9
|
+
|
10
|
+
class ObjectBlurrer(BaseSolution):
|
11
|
+
"""
|
12
|
+
A class to manage the blurring of detected objects in a real-time video stream.
|
13
|
+
|
14
|
+
This class extends the BaseSolution class and provides functionality for blurring objects based on detected bounding
|
15
|
+
boxes. The blurred areas are updated directly in the input image, allowing for privacy preservation or other effects.
|
16
|
+
|
17
|
+
Attributes:
|
18
|
+
blur_ratio (int): The intensity of the blur effect applied to detected objects (higher values create more blur).
|
19
|
+
iou (float): Intersection over Union threshold for object detection.
|
20
|
+
conf (float): Confidence threshold for object detection.
|
21
|
+
|
22
|
+
Methods:
|
23
|
+
process: Applies a blurring effect to detected objects in the input image.
|
24
|
+
extract_tracks: Extracts tracking information from detected objects.
|
25
|
+
display_output: Displays the processed output image.
|
26
|
+
|
27
|
+
Examples:
|
28
|
+
>>> blurrer = ObjectBlurrer()
|
29
|
+
>>> frame = cv2.imread("frame.jpg")
|
30
|
+
>>> processed_results = blurrer.process(frame)
|
31
|
+
>>> print(f"Total blurred objects: {processed_results.total_tracks}")
|
32
|
+
"""
|
33
|
+
|
34
|
+
def __init__(self, **kwargs):
|
35
|
+
"""
|
36
|
+
Initialize the ObjectBlurrer class for applying a blur effect to objects detected in video streams or images.
|
37
|
+
|
38
|
+
Args:
|
39
|
+
**kwargs (Any): Keyword arguments passed to the parent class and for configuration.
|
40
|
+
blur_ratio (float): Intensity of the blur effect (0.1-1.0, default=0.5).
|
41
|
+
"""
|
42
|
+
super().__init__(**kwargs)
|
43
|
+
blur_ratio = self.CFG["blur_ratio"]
|
44
|
+
if blur_ratio < 0.1:
|
45
|
+
LOGGER.warning("blur ratio cannot be less than 0.1, updating it to default value 0.5")
|
46
|
+
blur_ratio = 0.5
|
47
|
+
self.blur_ratio = int(blur_ratio * 100)
|
48
|
+
|
49
|
+
def process(self, im0):
|
50
|
+
"""
|
51
|
+
Apply a blurring effect to detected objects in the input image.
|
52
|
+
|
53
|
+
This method extracts tracking information, applies blur to regions corresponding to detected objects,
|
54
|
+
and annotates the image with bounding boxes.
|
55
|
+
|
56
|
+
Args:
|
57
|
+
im0 (numpy.ndarray): The input image containing detected objects.
|
58
|
+
|
59
|
+
Returns:
|
60
|
+
(SolutionResults): Object containing the processed image and number of tracked objects.
|
61
|
+
- plot_im (numpy.ndarray): The annotated output image with blurred objects.
|
62
|
+
- total_tracks (int): The total number of tracked objects in the frame.
|
63
|
+
|
64
|
+
Examples:
|
65
|
+
>>> blurrer = ObjectBlurrer()
|
66
|
+
>>> frame = cv2.imread("image.jpg")
|
67
|
+
>>> results = blurrer.process(frame)
|
68
|
+
>>> print(f"Blurred {results.total_tracks} objects")
|
69
|
+
"""
|
70
|
+
self.extract_tracks(im0) # Extract tracks
|
71
|
+
annotator = SolutionAnnotator(im0, self.line_width)
|
72
|
+
|
73
|
+
# Iterate over bounding boxes and classes
|
74
|
+
for box, cls, conf in zip(self.boxes, self.clss, self.confs):
|
75
|
+
# Crop and blur the detected object
|
76
|
+
blur_obj = cv2.blur(
|
77
|
+
im0[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])],
|
78
|
+
(self.blur_ratio, self.blur_ratio),
|
79
|
+
)
|
80
|
+
# Update the blurred area in the original image
|
81
|
+
im0[int(box[1]) : int(box[3]), int(box[0]) : int(box[2])] = blur_obj
|
82
|
+
annotator.box_label(
|
83
|
+
box, label=self.adjust_box_label(cls, conf), color=colors(cls, True)
|
84
|
+
) # Annotate bounding box
|
85
|
+
|
86
|
+
plot_im = annotator.result()
|
87
|
+
self.display_output(plot_im) # Display the output using the base class function
|
88
|
+
|
89
|
+
# Return a SolutionResults
|
90
|
+
return SolutionResults(plot_im=plot_im, total_tracks=len(self.track_ids))
|
@@ -0,0 +1,195 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
from collections import defaultdict
|
4
|
+
|
5
|
+
from ultralytics.solutions.solutions import BaseSolution, SolutionAnnotator, SolutionResults
|
6
|
+
from ultralytics.utils.plotting import colors
|
7
|
+
|
8
|
+
|
9
|
+
class ObjectCounter(BaseSolution):
|
10
|
+
"""
|
11
|
+
A class to manage the counting of objects in a real-time video stream based on their tracks.
|
12
|
+
|
13
|
+
This class extends the BaseSolution class and provides functionality for counting objects moving in and out of a
|
14
|
+
specified region in a video stream. It supports both polygonal and linear regions for counting.
|
15
|
+
|
16
|
+
Attributes:
|
17
|
+
in_count (int): Counter for objects moving inward.
|
18
|
+
out_count (int): Counter for objects moving outward.
|
19
|
+
counted_ids (List[int]): List of IDs of objects that have been counted.
|
20
|
+
classwise_counts (Dict[str, Dict[str, int]]): Dictionary for counts, categorized by object class.
|
21
|
+
region_initialized (bool): Flag indicating whether the counting region has been initialized.
|
22
|
+
show_in (bool): Flag to control display of inward count.
|
23
|
+
show_out (bool): Flag to control display of outward count.
|
24
|
+
|
25
|
+
Methods:
|
26
|
+
count_objects: Counts objects within a polygonal or linear region.
|
27
|
+
display_counts: Displays object counts on the frame.
|
28
|
+
process: Processes input data (frames or object tracks) and updates counts.
|
29
|
+
|
30
|
+
Examples:
|
31
|
+
>>> counter = ObjectCounter()
|
32
|
+
>>> frame = cv2.imread("frame.jpg")
|
33
|
+
>>> results = counter.process(frame)
|
34
|
+
>>> print(f"Inward count: {counter.in_count}, Outward count: {counter.out_count}")
|
35
|
+
"""
|
36
|
+
|
37
|
+
def __init__(self, **kwargs):
|
38
|
+
"""Initializes the ObjectCounter class for real-time object counting in video streams."""
|
39
|
+
super().__init__(**kwargs)
|
40
|
+
|
41
|
+
self.in_count = 0 # Counter for objects moving inward
|
42
|
+
self.out_count = 0 # Counter for objects moving outward
|
43
|
+
self.counted_ids = [] # List of IDs of objects that have been counted
|
44
|
+
self.classwise_counts = defaultdict(lambda: {"IN": 0, "OUT": 0}) # Dictionary for counts, categorized by class
|
45
|
+
self.region_initialized = False # Flag indicating whether the region has been initialized
|
46
|
+
|
47
|
+
self.show_in = self.CFG["show_in"]
|
48
|
+
self.show_out = self.CFG["show_out"]
|
49
|
+
self.margin = self.line_width * 2 # Scales the background rectangle size to display counts properly
|
50
|
+
|
51
|
+
def count_objects(self, current_centroid, track_id, prev_position, cls):
|
52
|
+
"""
|
53
|
+
Counts objects within a polygonal or linear region based on their tracks.
|
54
|
+
|
55
|
+
Args:
|
56
|
+
current_centroid (Tuple[float, float]): Current centroid coordinates (x, y) in the current frame.
|
57
|
+
track_id (int): Unique identifier for the tracked object.
|
58
|
+
prev_position (Tuple[float, float]): Last frame position coordinates (x, y) of the track.
|
59
|
+
cls (int): Class index for classwise count updates.
|
60
|
+
|
61
|
+
Examples:
|
62
|
+
>>> counter = ObjectCounter()
|
63
|
+
>>> track_line = {1: [100, 200], 2: [110, 210], 3: [120, 220]}
|
64
|
+
>>> box = [130, 230, 150, 250]
|
65
|
+
>>> track_id_num = 1
|
66
|
+
>>> previous_position = (120, 220)
|
67
|
+
>>> class_to_count = 0 # In COCO model, class 0 = person
|
68
|
+
>>> counter.count_objects((140, 240), track_id_num, previous_position, class_to_count)
|
69
|
+
"""
|
70
|
+
if prev_position is None or track_id in self.counted_ids:
|
71
|
+
return
|
72
|
+
|
73
|
+
if len(self.region) == 2: # Linear region (defined as a line segment)
|
74
|
+
line = self.LineString(self.region) # Check if the line intersects the trajectory of the object
|
75
|
+
if line.intersects(self.LineString([prev_position, current_centroid])):
|
76
|
+
# Determine orientation of the region (vertical or horizontal)
|
77
|
+
if abs(self.region[0][0] - self.region[1][0]) < abs(self.region[0][1] - self.region[1][1]):
|
78
|
+
# Vertical region: Compare x-coordinates to determine direction
|
79
|
+
if current_centroid[0] > prev_position[0]: # Moving right
|
80
|
+
self.in_count += 1
|
81
|
+
self.classwise_counts[self.names[cls]]["IN"] += 1
|
82
|
+
else: # Moving left
|
83
|
+
self.out_count += 1
|
84
|
+
self.classwise_counts[self.names[cls]]["OUT"] += 1
|
85
|
+
# Horizontal region: Compare y-coordinates to determine direction
|
86
|
+
elif current_centroid[1] > prev_position[1]: # Moving downward
|
87
|
+
self.in_count += 1
|
88
|
+
self.classwise_counts[self.names[cls]]["IN"] += 1
|
89
|
+
else: # Moving upward
|
90
|
+
self.out_count += 1
|
91
|
+
self.classwise_counts[self.names[cls]]["OUT"] += 1
|
92
|
+
self.counted_ids.append(track_id)
|
93
|
+
|
94
|
+
elif len(self.region) > 2: # Polygonal region
|
95
|
+
polygon = self.Polygon(self.region)
|
96
|
+
if polygon.contains(self.Point(current_centroid)):
|
97
|
+
# Determine motion direction for vertical or horizontal polygons
|
98
|
+
region_width = max(p[0] for p in self.region) - min(p[0] for p in self.region)
|
99
|
+
region_height = max(p[1] for p in self.region) - min(p[1] for p in self.region)
|
100
|
+
|
101
|
+
if (
|
102
|
+
region_width < region_height
|
103
|
+
and current_centroid[0] > prev_position[0]
|
104
|
+
or region_width >= region_height
|
105
|
+
and current_centroid[1] > prev_position[1]
|
106
|
+
): # Moving right or downward
|
107
|
+
self.in_count += 1
|
108
|
+
self.classwise_counts[self.names[cls]]["IN"] += 1
|
109
|
+
else: # Moving left or upward
|
110
|
+
self.out_count += 1
|
111
|
+
self.classwise_counts[self.names[cls]]["OUT"] += 1
|
112
|
+
self.counted_ids.append(track_id)
|
113
|
+
|
114
|
+
def display_counts(self, plot_im):
|
115
|
+
"""
|
116
|
+
Display object counts on the input image or frame.
|
117
|
+
|
118
|
+
Args:
|
119
|
+
plot_im (numpy.ndarray): The image or frame to display counts on.
|
120
|
+
|
121
|
+
Examples:
|
122
|
+
>>> counter = ObjectCounter()
|
123
|
+
>>> frame = cv2.imread("image.jpg")
|
124
|
+
>>> counter.display_counts(frame)
|
125
|
+
"""
|
126
|
+
labels_dict = {
|
127
|
+
str.capitalize(key): f"{'IN ' + str(value['IN']) if self.show_in else ''} "
|
128
|
+
f"{'OUT ' + str(value['OUT']) if self.show_out else ''}".strip()
|
129
|
+
for key, value in self.classwise_counts.items()
|
130
|
+
if value["IN"] != 0 or value["OUT"] != 0
|
131
|
+
}
|
132
|
+
if labels_dict:
|
133
|
+
self.annotator.display_analytics(plot_im, labels_dict, (104, 31, 17), (255, 255, 255), self.margin)
|
134
|
+
|
135
|
+
def process(self, im0):
|
136
|
+
"""
|
137
|
+
Process input data (frames or object tracks) and update object counts.
|
138
|
+
|
139
|
+
This method initializes the counting region, extracts tracks, draws bounding boxes and regions, updates
|
140
|
+
object counts, and displays the results on the input image.
|
141
|
+
|
142
|
+
Args:
|
143
|
+
im0 (numpy.ndarray): The input image or frame to be processed.
|
144
|
+
|
145
|
+
Returns:
|
146
|
+
(SolutionResults): Contains processed image `im0`, 'in_count' (int, count of objects entering the region),
|
147
|
+
'out_count' (int, count of objects exiting the region), 'classwise_count' (dict, per-class object count),
|
148
|
+
and 'total_tracks' (int, total number of tracked objects).
|
149
|
+
|
150
|
+
Examples:
|
151
|
+
>>> counter = ObjectCounter()
|
152
|
+
>>> frame = cv2.imread("path/to/image.jpg")
|
153
|
+
>>> results = counter.process(frame)
|
154
|
+
"""
|
155
|
+
if not self.region_initialized:
|
156
|
+
self.initialize_region()
|
157
|
+
self.region_initialized = True
|
158
|
+
|
159
|
+
self.extract_tracks(im0) # Extract tracks
|
160
|
+
self.annotator = SolutionAnnotator(im0, line_width=self.line_width) # Initialize annotator
|
161
|
+
|
162
|
+
is_obb = getattr(self.tracks[0], "obb", None) is not None # True if OBB results exist
|
163
|
+
if is_obb:
|
164
|
+
self.boxes = self.track_data.xyxyxyxy.reshape(-1, 4, 2).cpu()
|
165
|
+
|
166
|
+
self.annotator.draw_region(
|
167
|
+
reg_pts=self.region, color=(104, 0, 123), thickness=self.line_width * 2
|
168
|
+
) # Draw region
|
169
|
+
|
170
|
+
# Iterate over bounding boxes, track ids and classes index
|
171
|
+
for box, track_id, cls, conf in zip(self.boxes, self.track_ids, self.clss, self.confs):
|
172
|
+
# Draw bounding box and counting region
|
173
|
+
self.annotator.box_label(
|
174
|
+
box, label=self.adjust_box_label(cls, conf, track_id), color=colors(cls, True), rotated=is_obb
|
175
|
+
)
|
176
|
+
self.store_tracking_history(track_id, box, is_obb=is_obb) # Store track history
|
177
|
+
|
178
|
+
# Store previous position of track for object counting
|
179
|
+
prev_position = None
|
180
|
+
if len(self.track_history[track_id]) > 1:
|
181
|
+
prev_position = self.track_history[track_id][-2]
|
182
|
+
self.count_objects(self.track_history[track_id][-1], track_id, prev_position, cls) # object counting
|
183
|
+
|
184
|
+
plot_im = self.annotator.result()
|
185
|
+
self.display_counts(plot_im) # Display the counts on the frame
|
186
|
+
self.display_output(plot_im) # Display output with base class function
|
187
|
+
|
188
|
+
# Return SolutionResults
|
189
|
+
return SolutionResults(
|
190
|
+
plot_im=plot_im,
|
191
|
+
in_count=self.in_count,
|
192
|
+
out_count=self.out_count,
|
193
|
+
classwise_count=dict(self.classwise_counts),
|
194
|
+
total_tracks=len(self.track_ids),
|
195
|
+
)
|
@@ -0,0 +1,84 @@
|
|
1
|
+
# Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license
|
2
|
+
|
3
|
+
import os
|
4
|
+
from pathlib import Path
|
5
|
+
|
6
|
+
from ultralytics.solutions.solutions import BaseSolution, SolutionResults
|
7
|
+
from ultralytics.utils.plotting import save_one_box
|
8
|
+
|
9
|
+
|
10
|
+
class ObjectCropper(BaseSolution):
|
11
|
+
"""
|
12
|
+
A class to manage the cropping of detected objects in a real-time video stream or images.
|
13
|
+
|
14
|
+
This class extends the BaseSolution class and provides functionality for cropping objects based on detected bounding
|
15
|
+
boxes. The cropped images are saved to a specified directory for further analysis or usage.
|
16
|
+
|
17
|
+
Attributes:
|
18
|
+
crop_dir (str): Directory where cropped object images are stored.
|
19
|
+
crop_idx (int): Counter for the total number of cropped objects.
|
20
|
+
iou (float): IoU (Intersection over Union) threshold for non-maximum suppression.
|
21
|
+
conf (float): Confidence threshold for filtering detections.
|
22
|
+
|
23
|
+
Methods:
|
24
|
+
process: Crops detected objects from the input image and saves them to the output directory.
|
25
|
+
|
26
|
+
Examples:
|
27
|
+
>>> cropper = ObjectCropper()
|
28
|
+
>>> frame = cv2.imread("frame.jpg")
|
29
|
+
>>> processed_results = cropper.process(frame)
|
30
|
+
>>> print(f"Total cropped objects: {cropper.crop_idx}")
|
31
|
+
"""
|
32
|
+
|
33
|
+
def __init__(self, **kwargs):
|
34
|
+
"""
|
35
|
+
Initialize the ObjectCropper class for cropping objects from detected bounding boxes.
|
36
|
+
|
37
|
+
Args:
|
38
|
+
**kwargs (Any): Keyword arguments passed to the parent class and used for configuration.
|
39
|
+
crop_dir (str): Path to the directory for saving cropped object images.
|
40
|
+
"""
|
41
|
+
super().__init__(**kwargs)
|
42
|
+
|
43
|
+
self.crop_dir = self.CFG["crop_dir"] # Directory for storing cropped detections
|
44
|
+
if not os.path.exists(self.crop_dir):
|
45
|
+
os.mkdir(self.crop_dir) # Create directory if it does not exist
|
46
|
+
if self.CFG["show"]:
|
47
|
+
self.LOGGER.warning(
|
48
|
+
f"show=True disabled for crop solution, results will be saved in the directory named: {self.crop_dir}"
|
49
|
+
)
|
50
|
+
self.crop_idx = 0 # Initialize counter for total cropped objects
|
51
|
+
self.iou = self.CFG["iou"]
|
52
|
+
self.conf = self.CFG["conf"]
|
53
|
+
|
54
|
+
def process(self, im0):
|
55
|
+
"""
|
56
|
+
Crop detected objects from the input image and save them as separate images.
|
57
|
+
|
58
|
+
Args:
|
59
|
+
im0 (numpy.ndarray): The input image containing detected objects.
|
60
|
+
|
61
|
+
Returns:
|
62
|
+
(SolutionResults): A SolutionResults object containing the total number of cropped objects and processed image.
|
63
|
+
|
64
|
+
Examples:
|
65
|
+
>>> cropper = ObjectCropper()
|
66
|
+
>>> frame = cv2.imread("image.jpg")
|
67
|
+
>>> results = cropper.process(frame)
|
68
|
+
>>> print(f"Total cropped objects: {results.total_crop_objects}")
|
69
|
+
"""
|
70
|
+
results = self.model.predict(
|
71
|
+
im0, classes=self.classes, conf=self.conf, iou=self.iou, device=self.CFG["device"]
|
72
|
+
)[0]
|
73
|
+
|
74
|
+
for box in results.boxes:
|
75
|
+
self.crop_idx += 1
|
76
|
+
save_one_box(
|
77
|
+
box.xyxy,
|
78
|
+
im0,
|
79
|
+
file=Path(self.crop_dir) / f"crop_{self.crop_idx}.jpg",
|
80
|
+
BGR=True,
|
81
|
+
)
|
82
|
+
|
83
|
+
# Return SolutionResults
|
84
|
+
return SolutionResults(plot_im=im0, total_crop_objects=self.crop_idx)
|