matrice-analytics 0.1.60__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.
- matrice_analytics/__init__.py +28 -0
- matrice_analytics/boundary_drawing_internal/README.md +305 -0
- matrice_analytics/boundary_drawing_internal/__init__.py +45 -0
- matrice_analytics/boundary_drawing_internal/boundary_drawing_internal.py +1207 -0
- matrice_analytics/boundary_drawing_internal/boundary_drawing_tool.py +429 -0
- matrice_analytics/boundary_drawing_internal/boundary_tool_template.html +1036 -0
- matrice_analytics/boundary_drawing_internal/data/.gitignore +12 -0
- matrice_analytics/boundary_drawing_internal/example_usage.py +206 -0
- matrice_analytics/boundary_drawing_internal/usage/README.md +110 -0
- matrice_analytics/boundary_drawing_internal/usage/boundary_drawer_launcher.py +102 -0
- matrice_analytics/boundary_drawing_internal/usage/simple_boundary_launcher.py +107 -0
- matrice_analytics/post_processing/README.md +455 -0
- matrice_analytics/post_processing/__init__.py +732 -0
- matrice_analytics/post_processing/advanced_tracker/README.md +650 -0
- matrice_analytics/post_processing/advanced_tracker/__init__.py +17 -0
- matrice_analytics/post_processing/advanced_tracker/base.py +99 -0
- matrice_analytics/post_processing/advanced_tracker/config.py +77 -0
- matrice_analytics/post_processing/advanced_tracker/kalman_filter.py +370 -0
- matrice_analytics/post_processing/advanced_tracker/matching.py +195 -0
- matrice_analytics/post_processing/advanced_tracker/strack.py +230 -0
- matrice_analytics/post_processing/advanced_tracker/tracker.py +367 -0
- matrice_analytics/post_processing/config.py +146 -0
- matrice_analytics/post_processing/core/__init__.py +63 -0
- matrice_analytics/post_processing/core/base.py +704 -0
- matrice_analytics/post_processing/core/config.py +3291 -0
- matrice_analytics/post_processing/core/config_utils.py +925 -0
- matrice_analytics/post_processing/face_reg/__init__.py +43 -0
- matrice_analytics/post_processing/face_reg/compare_similarity.py +556 -0
- matrice_analytics/post_processing/face_reg/embedding_manager.py +950 -0
- matrice_analytics/post_processing/face_reg/face_recognition.py +2234 -0
- matrice_analytics/post_processing/face_reg/face_recognition_client.py +606 -0
- matrice_analytics/post_processing/face_reg/people_activity_logging.py +321 -0
- matrice_analytics/post_processing/ocr/__init__.py +0 -0
- matrice_analytics/post_processing/ocr/easyocr_extractor.py +250 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/__init__.py +9 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/__init__.py +4 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/cli.py +33 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/dataset_stats.py +139 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/export.py +398 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/train.py +447 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/utils.py +129 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/valid.py +93 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/validate_dataset.py +240 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/visualize_augmentation.py +176 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/visualize_predictions.py +96 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/__init__.py +3 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/process.py +246 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/types.py +60 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/utils.py +87 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/__init__.py +3 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/config.py +82 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/hub.py +141 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/plate_recognizer.py +323 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/py.typed +0 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/__init__.py +0 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/__init__.py +0 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/augmentation.py +101 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/dataset.py +97 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/__init__.py +0 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/config.py +114 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/layers.py +553 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/loss.py +55 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/metric.py +86 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/model_builders.py +95 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/model_schema.py +395 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/__init__.py +0 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/backend_utils.py +38 -0
- matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/utils.py +214 -0
- matrice_analytics/post_processing/ocr/postprocessing.py +270 -0
- matrice_analytics/post_processing/ocr/preprocessing.py +52 -0
- matrice_analytics/post_processing/post_processor.py +1175 -0
- matrice_analytics/post_processing/test_cases/__init__.py +1 -0
- matrice_analytics/post_processing/test_cases/run_tests.py +143 -0
- matrice_analytics/post_processing/test_cases/test_advanced_customer_service.py +841 -0
- matrice_analytics/post_processing/test_cases/test_basic_counting_tracking.py +523 -0
- matrice_analytics/post_processing/test_cases/test_comprehensive.py +531 -0
- matrice_analytics/post_processing/test_cases/test_config.py +852 -0
- matrice_analytics/post_processing/test_cases/test_customer_service.py +585 -0
- matrice_analytics/post_processing/test_cases/test_data_generators.py +583 -0
- matrice_analytics/post_processing/test_cases/test_people_counting.py +510 -0
- matrice_analytics/post_processing/test_cases/test_processor.py +524 -0
- matrice_analytics/post_processing/test_cases/test_usecases.py +165 -0
- matrice_analytics/post_processing/test_cases/test_utilities.py +356 -0
- matrice_analytics/post_processing/test_cases/test_utils.py +743 -0
- matrice_analytics/post_processing/usecases/Histopathological_Cancer_Detection_img.py +604 -0
- matrice_analytics/post_processing/usecases/__init__.py +267 -0
- matrice_analytics/post_processing/usecases/abandoned_object_detection.py +797 -0
- matrice_analytics/post_processing/usecases/advanced_customer_service.py +1601 -0
- matrice_analytics/post_processing/usecases/age_detection.py +842 -0
- matrice_analytics/post_processing/usecases/age_gender_detection.py +1085 -0
- matrice_analytics/post_processing/usecases/anti_spoofing_detection.py +656 -0
- matrice_analytics/post_processing/usecases/assembly_line_detection.py +841 -0
- matrice_analytics/post_processing/usecases/banana_defect_detection.py +624 -0
- matrice_analytics/post_processing/usecases/basic_counting_tracking.py +667 -0
- matrice_analytics/post_processing/usecases/blood_cancer_detection_img.py +881 -0
- matrice_analytics/post_processing/usecases/car_damage_detection.py +834 -0
- matrice_analytics/post_processing/usecases/car_part_segmentation.py +946 -0
- matrice_analytics/post_processing/usecases/car_service.py +1601 -0
- matrice_analytics/post_processing/usecases/cardiomegaly_classification.py +864 -0
- matrice_analytics/post_processing/usecases/cell_microscopy_segmentation.py +897 -0
- matrice_analytics/post_processing/usecases/chicken_pose_detection.py +648 -0
- matrice_analytics/post_processing/usecases/child_monitoring.py +814 -0
- matrice_analytics/post_processing/usecases/color/clip.py +660 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/merges.txt +48895 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/preprocessor_config.json +28 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/special_tokens_map.json +30 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/tokenizer.json +245079 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/tokenizer_config.json +32 -0
- matrice_analytics/post_processing/usecases/color/clip_processor/vocab.json +1 -0
- matrice_analytics/post_processing/usecases/color/color_map_utils.py +70 -0
- matrice_analytics/post_processing/usecases/color/color_mapper.py +468 -0
- matrice_analytics/post_processing/usecases/color_detection.py +1936 -0
- matrice_analytics/post_processing/usecases/color_map_utils.py +70 -0
- matrice_analytics/post_processing/usecases/concrete_crack_detection.py +827 -0
- matrice_analytics/post_processing/usecases/crop_weed_detection.py +781 -0
- matrice_analytics/post_processing/usecases/customer_service.py +1008 -0
- matrice_analytics/post_processing/usecases/defect_detection_products.py +936 -0
- matrice_analytics/post_processing/usecases/distracted_driver_detection.py +822 -0
- matrice_analytics/post_processing/usecases/drone_traffic_monitoring.py +585 -0
- matrice_analytics/post_processing/usecases/drowsy_driver_detection.py +829 -0
- matrice_analytics/post_processing/usecases/dwell_detection.py +829 -0
- matrice_analytics/post_processing/usecases/emergency_vehicle_detection.py +827 -0
- matrice_analytics/post_processing/usecases/face_emotion.py +813 -0
- matrice_analytics/post_processing/usecases/face_recognition.py +827 -0
- matrice_analytics/post_processing/usecases/fashion_detection.py +835 -0
- matrice_analytics/post_processing/usecases/field_mapping.py +902 -0
- matrice_analytics/post_processing/usecases/fire_detection.py +1146 -0
- matrice_analytics/post_processing/usecases/flare_analysis.py +836 -0
- matrice_analytics/post_processing/usecases/flower_segmentation.py +1006 -0
- matrice_analytics/post_processing/usecases/gas_leak_detection.py +837 -0
- matrice_analytics/post_processing/usecases/gender_detection.py +832 -0
- matrice_analytics/post_processing/usecases/human_activity_recognition.py +871 -0
- matrice_analytics/post_processing/usecases/intrusion_detection.py +1672 -0
- matrice_analytics/post_processing/usecases/leaf.py +821 -0
- matrice_analytics/post_processing/usecases/leaf_disease.py +840 -0
- matrice_analytics/post_processing/usecases/leak_detection.py +837 -0
- matrice_analytics/post_processing/usecases/license_plate_detection.py +1188 -0
- matrice_analytics/post_processing/usecases/license_plate_monitoring.py +1781 -0
- matrice_analytics/post_processing/usecases/litter_monitoring.py +717 -0
- matrice_analytics/post_processing/usecases/mask_detection.py +869 -0
- matrice_analytics/post_processing/usecases/natural_disaster.py +907 -0
- matrice_analytics/post_processing/usecases/parking.py +787 -0
- matrice_analytics/post_processing/usecases/parking_space_detection.py +822 -0
- matrice_analytics/post_processing/usecases/pcb_defect_detection.py +888 -0
- matrice_analytics/post_processing/usecases/pedestrian_detection.py +808 -0
- matrice_analytics/post_processing/usecases/people_counting.py +706 -0
- matrice_analytics/post_processing/usecases/people_counting_bckp.py +1683 -0
- matrice_analytics/post_processing/usecases/people_tracking.py +1842 -0
- matrice_analytics/post_processing/usecases/pipeline_detection.py +605 -0
- matrice_analytics/post_processing/usecases/plaque_segmentation_img.py +874 -0
- matrice_analytics/post_processing/usecases/pothole_segmentation.py +915 -0
- matrice_analytics/post_processing/usecases/ppe_compliance.py +645 -0
- matrice_analytics/post_processing/usecases/price_tag_detection.py +822 -0
- matrice_analytics/post_processing/usecases/proximity_detection.py +1901 -0
- matrice_analytics/post_processing/usecases/road_lane_detection.py +623 -0
- matrice_analytics/post_processing/usecases/road_traffic_density.py +832 -0
- matrice_analytics/post_processing/usecases/road_view_segmentation.py +915 -0
- matrice_analytics/post_processing/usecases/shelf_inventory_detection.py +583 -0
- matrice_analytics/post_processing/usecases/shoplifting_detection.py +822 -0
- matrice_analytics/post_processing/usecases/shopping_cart_analysis.py +899 -0
- matrice_analytics/post_processing/usecases/skin_cancer_classification_img.py +864 -0
- matrice_analytics/post_processing/usecases/smoker_detection.py +833 -0
- matrice_analytics/post_processing/usecases/solar_panel.py +810 -0
- matrice_analytics/post_processing/usecases/suspicious_activity_detection.py +1030 -0
- matrice_analytics/post_processing/usecases/template_usecase.py +380 -0
- matrice_analytics/post_processing/usecases/theft_detection.py +648 -0
- matrice_analytics/post_processing/usecases/traffic_sign_monitoring.py +724 -0
- matrice_analytics/post_processing/usecases/underground_pipeline_defect_detection.py +775 -0
- matrice_analytics/post_processing/usecases/underwater_pollution_detection.py +842 -0
- matrice_analytics/post_processing/usecases/vehicle_monitoring.py +1029 -0
- matrice_analytics/post_processing/usecases/warehouse_object_segmentation.py +899 -0
- matrice_analytics/post_processing/usecases/waterbody_segmentation.py +923 -0
- matrice_analytics/post_processing/usecases/weapon_detection.py +771 -0
- matrice_analytics/post_processing/usecases/weld_defect_detection.py +615 -0
- matrice_analytics/post_processing/usecases/wildlife_monitoring.py +898 -0
- matrice_analytics/post_processing/usecases/windmill_maintenance.py +834 -0
- matrice_analytics/post_processing/usecases/wound_segmentation.py +856 -0
- matrice_analytics/post_processing/utils/__init__.py +150 -0
- matrice_analytics/post_processing/utils/advanced_counting_utils.py +400 -0
- matrice_analytics/post_processing/utils/advanced_helper_utils.py +317 -0
- matrice_analytics/post_processing/utils/advanced_tracking_utils.py +461 -0
- matrice_analytics/post_processing/utils/alerting_utils.py +213 -0
- matrice_analytics/post_processing/utils/category_mapping_utils.py +94 -0
- matrice_analytics/post_processing/utils/color_utils.py +592 -0
- matrice_analytics/post_processing/utils/counting_utils.py +182 -0
- matrice_analytics/post_processing/utils/filter_utils.py +261 -0
- matrice_analytics/post_processing/utils/format_utils.py +293 -0
- matrice_analytics/post_processing/utils/geometry_utils.py +300 -0
- matrice_analytics/post_processing/utils/smoothing_utils.py +358 -0
- matrice_analytics/post_processing/utils/tracking_utils.py +234 -0
- matrice_analytics/py.typed +0 -0
- matrice_analytics-0.1.60.dist-info/METADATA +481 -0
- matrice_analytics-0.1.60.dist-info/RECORD +196 -0
- matrice_analytics-0.1.60.dist-info/WHEEL +5 -0
- matrice_analytics-0.1.60.dist-info/licenses/LICENSE.txt +21 -0
- matrice_analytics-0.1.60.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,648 @@
|
|
|
1
|
+
from typing import Any, Dict, List, Optional
|
|
2
|
+
from dataclasses import asdict
|
|
3
|
+
import time
|
|
4
|
+
from datetime import datetime, timezone
|
|
5
|
+
|
|
6
|
+
from ..core.base import BaseProcessor, ProcessingContext, ProcessingResult, ConfigProtocol, ResultFormat
|
|
7
|
+
from ..utils import (
|
|
8
|
+
filter_by_confidence,
|
|
9
|
+
filter_by_categories,
|
|
10
|
+
apply_category_mapping,
|
|
11
|
+
count_objects_by_category,
|
|
12
|
+
count_objects_in_zones,
|
|
13
|
+
calculate_counting_summary,
|
|
14
|
+
match_results_structure,
|
|
15
|
+
bbox_smoothing,
|
|
16
|
+
BBoxSmoothingConfig,
|
|
17
|
+
BBoxSmoothingTracker
|
|
18
|
+
)
|
|
19
|
+
from dataclasses import dataclass, field
|
|
20
|
+
from ..core.config import BaseConfig, AlertConfig, ZoneConfig
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass
|
|
24
|
+
class TheftDetectionConfig(BaseConfig):
|
|
25
|
+
"""Configuration for theft detection use case in theft monitoring."""
|
|
26
|
+
# Smoothing configuration
|
|
27
|
+
enable_smoothing: bool = True
|
|
28
|
+
smoothing_algorithm: str = "observability" # "window" or "observability"
|
|
29
|
+
smoothing_window_size: int = 20
|
|
30
|
+
smoothing_cooldown_frames: int = 5
|
|
31
|
+
smoothing_confidence_range_factor: float = 0.5
|
|
32
|
+
|
|
33
|
+
# Confidence thresholds
|
|
34
|
+
confidence_threshold: float = 0.4
|
|
35
|
+
|
|
36
|
+
usecase_categories: List[str] = field(
|
|
37
|
+
default_factory=lambda: ['normal','shoplifting']
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
target_categories: List[str] = field(
|
|
41
|
+
default_factory=lambda: ['normal','shoplifting']
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
alert_config: Optional[AlertConfig] = None
|
|
45
|
+
|
|
46
|
+
index_to_category: Optional[Dict[int, str]] = field(
|
|
47
|
+
default_factory=lambda: {
|
|
48
|
+
-1: "normal",
|
|
49
|
+
0: "shoplifting"
|
|
50
|
+
}
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class TheftDetectionUseCase(BaseProcessor):
|
|
55
|
+
def _get_track_ids_info(self, detections: list) -> Dict[str, Any]:
|
|
56
|
+
"""
|
|
57
|
+
Get detailed information about track IDs (per frame).
|
|
58
|
+
"""
|
|
59
|
+
frame_track_ids = set()
|
|
60
|
+
for det in detections:
|
|
61
|
+
tid = det.get('track_id')
|
|
62
|
+
if tid is not None:
|
|
63
|
+
frame_track_ids.add(tid)
|
|
64
|
+
total_track_ids = set()
|
|
65
|
+
for s in getattr(self, '_per_category_total_track_ids', {}).values():
|
|
66
|
+
total_track_ids.update(s)
|
|
67
|
+
return {
|
|
68
|
+
"total_count": len(total_track_ids),
|
|
69
|
+
"current_frame_count": len(frame_track_ids),
|
|
70
|
+
"total_unique_track_ids": len(total_track_ids),
|
|
71
|
+
"current_frame_track_ids": list(frame_track_ids),
|
|
72
|
+
"last_update_time": time.time(),
|
|
73
|
+
"total_frames_processed": getattr(self, '_total_frame_counter', 0)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
def _update_tracking_state(self, detections: list):
|
|
77
|
+
"""
|
|
78
|
+
Track unique categories track_ids per category for total count after tracking.
|
|
79
|
+
Applies canonical ID merging to avoid duplicate counting.
|
|
80
|
+
"""
|
|
81
|
+
if not hasattr(self, "_per_category_total_track_ids"):
|
|
82
|
+
self._per_category_total_track_ids = {cat: set() for cat in self.target_categories}
|
|
83
|
+
self._current_frame_track_ids = {cat: set() for cat in self.target_categories}
|
|
84
|
+
|
|
85
|
+
for det in detections:
|
|
86
|
+
cat = det.get("category")
|
|
87
|
+
raw_track_id = det.get("track_id")
|
|
88
|
+
if cat not in self.target_categories or raw_track_id is None:
|
|
89
|
+
continue
|
|
90
|
+
bbox = det.get("bounding_box", det.get("bbox"))
|
|
91
|
+
canonical_id = self._merge_or_register_track(raw_track_id, bbox)
|
|
92
|
+
det["track_id"] = canonical_id
|
|
93
|
+
self._per_category_total_track_ids.setdefault(cat, set()).add(canonical_id)
|
|
94
|
+
self._current_frame_track_ids[cat].add(canonical_id)
|
|
95
|
+
|
|
96
|
+
def get_total_counts(self):
|
|
97
|
+
"""
|
|
98
|
+
Return total unique track_id count for each category.
|
|
99
|
+
"""
|
|
100
|
+
return {cat: len(ids) for cat, ids in getattr(self, '_per_category_total_track_ids', {}).items()}
|
|
101
|
+
|
|
102
|
+
def _format_timestamp_for_video(self, timestamp: float) -> str:
|
|
103
|
+
"""Format timestamp for video chunks (HH:MM:SS.ms format)."""
|
|
104
|
+
hours = int(timestamp // 3600)
|
|
105
|
+
minutes = int((timestamp % 3600) // 60)
|
|
106
|
+
seconds = timestamp % 60
|
|
107
|
+
return f"{hours:02d}:{minutes:02d}:{seconds:06.2f}"
|
|
108
|
+
|
|
109
|
+
def _format_timestamp_for_stream(self, timestamp: float) -> str:
|
|
110
|
+
"""Format timestamp for streams (YYYY:MM:DD HH:MM:SS format)."""
|
|
111
|
+
dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
|
|
112
|
+
return dt.strftime('%Y:%m:%d %H:%M:%S')
|
|
113
|
+
|
|
114
|
+
def _get_current_timestamp_str(self, stream_info: Optional[Dict[str, Any]]) -> str:
|
|
115
|
+
"""Get formatted current timestamp based on stream type."""
|
|
116
|
+
if not stream_info:
|
|
117
|
+
return "00:00:00.00"
|
|
118
|
+
if stream_info.get("input_settings", {}).get("stream_type", "video_file") == "video_file":
|
|
119
|
+
stream_time_str = stream_info.get("video_timestamp", "")
|
|
120
|
+
return stream_time_str[:8]
|
|
121
|
+
else:
|
|
122
|
+
stream_time_str = stream_info.get("stream_time", "")
|
|
123
|
+
if stream_time_str:
|
|
124
|
+
try:
|
|
125
|
+
timestamp_str = stream_time_str.replace(" UTC", "")
|
|
126
|
+
dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
|
|
127
|
+
timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
|
|
128
|
+
return self._format_timestamp_for_stream(timestamp)
|
|
129
|
+
except:
|
|
130
|
+
return self._format_timestamp_for_stream(time.time())
|
|
131
|
+
else:
|
|
132
|
+
return self._format_timestamp_for_stream(time.time())
|
|
133
|
+
|
|
134
|
+
def _get_start_timestamp_str(self, stream_info: Optional[Dict[str, Any]]) -> str:
|
|
135
|
+
"""Get formatted start timestamp for 'TOTAL SINCE' based on stream type."""
|
|
136
|
+
if not stream_info:
|
|
137
|
+
return "00:00:00"
|
|
138
|
+
is_video_chunk = stream_info.get("input_settings", {}).get("is_video_chunk", False)
|
|
139
|
+
if is_video_chunk or stream_info.get("input_settings", {}).get("stream_type", "video_file") == "video_file":
|
|
140
|
+
return "00:00:00"
|
|
141
|
+
else:
|
|
142
|
+
if self._tracking_start_time is None:
|
|
143
|
+
stream_time_str = stream_info.get("stream_time", "")
|
|
144
|
+
if stream_time_str:
|
|
145
|
+
try:
|
|
146
|
+
timestamp_str = stream_time_str.replace(" UTC", "")
|
|
147
|
+
dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
|
|
148
|
+
self._tracking_start_time = dt.replace(tzinfo=timezone.utc).timestamp()
|
|
149
|
+
except:
|
|
150
|
+
self._tracking_start_time = time.time()
|
|
151
|
+
else:
|
|
152
|
+
self._tracking_start_time = time.time()
|
|
153
|
+
dt = datetime.fromtimestamp(self._tracking_start_time, tz=timezone.utc)
|
|
154
|
+
dt = dt.replace(minute=0, second=0, microsecond=0)
|
|
155
|
+
return dt.strftime('%Y:%m:%d %H:%M:%S')
|
|
156
|
+
|
|
157
|
+
""" Theft monitoring use case with smoothing and alerting."""
|
|
158
|
+
|
|
159
|
+
def __init__(self):
|
|
160
|
+
super().__init__("theft_detection")
|
|
161
|
+
self.category = "security"
|
|
162
|
+
self.target_categories = ["normal","shoplifting"]
|
|
163
|
+
self.smoothing_tracker = None
|
|
164
|
+
self.tracker = None
|
|
165
|
+
self._total_frame_counter = 0
|
|
166
|
+
self._global_frame_offset = 0
|
|
167
|
+
self._tracking_start_time = None
|
|
168
|
+
self._track_aliases: Dict[Any, Any] = {}
|
|
169
|
+
self._canonical_tracks: Dict[Any, Dict[str, Any]] = {}
|
|
170
|
+
self._track_merge_iou_threshold: float = 0.05
|
|
171
|
+
self._track_merge_time_window: float = 7.0
|
|
172
|
+
|
|
173
|
+
def process(self, data: Any, config: ConfigProtocol, context: Optional[ProcessingContext] = None,
|
|
174
|
+
stream_info: Optional[Dict[str, Any]] = None) -> ProcessingResult:
|
|
175
|
+
"""
|
|
176
|
+
Main entry point for theft post-processing.
|
|
177
|
+
Applies category mapping, smoothing, counting, alerting, and summary generation.
|
|
178
|
+
"""
|
|
179
|
+
start_time = time.time()
|
|
180
|
+
if not isinstance(config, TheftDetectionConfig):
|
|
181
|
+
return self.create_error_result("Invalid config type", usecase=self.name, category=self.category, context=context)
|
|
182
|
+
if context is None:
|
|
183
|
+
context = ProcessingContext()
|
|
184
|
+
|
|
185
|
+
input_format = match_results_structure(data)
|
|
186
|
+
context.input_format = input_format
|
|
187
|
+
context.confidence_threshold = config.confidence_threshold
|
|
188
|
+
|
|
189
|
+
if config.confidence_threshold is not None:
|
|
190
|
+
processed_data = filter_by_confidence(data, config.confidence_threshold)
|
|
191
|
+
self.logger.debug(f"Applied confidence filtering with threshold {config.confidence_threshold}")
|
|
192
|
+
else:
|
|
193
|
+
processed_data = data
|
|
194
|
+
self.logger.debug(f"Did not apply confidence filtering with threshold since nothing was provided")
|
|
195
|
+
|
|
196
|
+
if config.index_to_category:
|
|
197
|
+
processed_data = apply_category_mapping(processed_data, config.index_to_category)
|
|
198
|
+
self.logger.debug("Applied category mapping")
|
|
199
|
+
|
|
200
|
+
if config.target_categories:
|
|
201
|
+
processed_data = [d for d in processed_data if d.get('category') in self.target_categories]
|
|
202
|
+
self.logger.debug(f"Applied category filtering")
|
|
203
|
+
|
|
204
|
+
if config.enable_smoothing:
|
|
205
|
+
if self.smoothing_tracker is None:
|
|
206
|
+
smoothing_config = BBoxSmoothingConfig(
|
|
207
|
+
smoothing_algorithm=config.smoothing_algorithm,
|
|
208
|
+
window_size=config.smoothing_window_size,
|
|
209
|
+
cooldown_frames=config.smoothing_cooldown_frames,
|
|
210
|
+
confidence_threshold=config.confidence_threshold,
|
|
211
|
+
confidence_range_factor=config.smoothing_confidence_range_factor,
|
|
212
|
+
enable_smoothing=True
|
|
213
|
+
)
|
|
214
|
+
self.smoothing_tracker = BBoxSmoothingTracker(smoothing_config)
|
|
215
|
+
processed_data = bbox_smoothing(processed_data, self.smoothing_tracker.config, self.smoothing_tracker)
|
|
216
|
+
|
|
217
|
+
try:
|
|
218
|
+
from ..advanced_tracker import AdvancedTracker
|
|
219
|
+
from ..advanced_tracker.config import TrackerConfig
|
|
220
|
+
if self.tracker is None:
|
|
221
|
+
tracker_config = TrackerConfig()
|
|
222
|
+
self.tracker = AdvancedTracker(tracker_config)
|
|
223
|
+
self.logger.info("Initialized AdvancedTracker for Theft Monitoring and tracking")
|
|
224
|
+
processed_data = self.tracker.update(processed_data)
|
|
225
|
+
except Exception as e:
|
|
226
|
+
self.logger.warning(f"AdvancedTracker failed: {e}")
|
|
227
|
+
|
|
228
|
+
self._update_tracking_state(processed_data)
|
|
229
|
+
self._total_frame_counter += 1
|
|
230
|
+
|
|
231
|
+
frame_number = None
|
|
232
|
+
if stream_info:
|
|
233
|
+
input_settings = stream_info.get("input_settings", {})
|
|
234
|
+
start_frame = input_settings.get("start_frame")
|
|
235
|
+
end_frame = input_settings.get("end_frame")
|
|
236
|
+
if start_frame is not None and end_frame is not None and start_frame == end_frame:
|
|
237
|
+
frame_number = start_frame
|
|
238
|
+
|
|
239
|
+
general_counting_summary = calculate_counting_summary(data)
|
|
240
|
+
counting_summary = self._count_categories(processed_data, config)
|
|
241
|
+
total_counts = self.get_total_counts()
|
|
242
|
+
counting_summary['total_counts'] = total_counts
|
|
243
|
+
insights = self._generate_insights(counting_summary, config)
|
|
244
|
+
alerts = self._check_alerts(counting_summary, config)
|
|
245
|
+
predictions = self._extract_predictions(processed_data)
|
|
246
|
+
summary = self._generate_summary(counting_summary, alerts)
|
|
247
|
+
|
|
248
|
+
events_list = self._generate_events(counting_summary, alerts, config, frame_number, stream_info)
|
|
249
|
+
tracking_stats_list = self._generate_tracking_stats(counting_summary, insights, summary, config, frame_number, stream_info)
|
|
250
|
+
|
|
251
|
+
events = events_list[0] if events_list else {}
|
|
252
|
+
tracking_stats = tracking_stats_list[0] if tracking_stats_list else {}
|
|
253
|
+
|
|
254
|
+
context.mark_completed()
|
|
255
|
+
|
|
256
|
+
result = self.create_result(
|
|
257
|
+
data={
|
|
258
|
+
"counting_summary": counting_summary,
|
|
259
|
+
"general_counting_summary": general_counting_summary,
|
|
260
|
+
"alerts": alerts,
|
|
261
|
+
"total_detections": counting_summary.get("total_count", 0),
|
|
262
|
+
"events": events,
|
|
263
|
+
"tracking_stats": tracking_stats,
|
|
264
|
+
},
|
|
265
|
+
usecase=self.name,
|
|
266
|
+
category=self.category,
|
|
267
|
+
context=context
|
|
268
|
+
)
|
|
269
|
+
result.summary = summary
|
|
270
|
+
result.insights = insights
|
|
271
|
+
result.predictions = predictions
|
|
272
|
+
return result
|
|
273
|
+
|
|
274
|
+
def _generate_events(self, counting_summary: Dict, alerts: List, config: TheftDetectionConfig,frame_number: Optional[int] = None, stream_info: Optional[Dict[str, Any]] = None) -> List[Dict]:
|
|
275
|
+
"""Generate structured events for the output format with frame-based keys."""
|
|
276
|
+
frame_key = str(frame_number) if frame_number is not None else "current_frame"
|
|
277
|
+
events = [{frame_key: []}]
|
|
278
|
+
frame_events = events[0][frame_key]
|
|
279
|
+
total_detections = counting_summary.get("total_count", 0)
|
|
280
|
+
|
|
281
|
+
if total_detections > 0:
|
|
282
|
+
level = "info"
|
|
283
|
+
intensity = 5.0
|
|
284
|
+
if config.alert_config and config.alert_config.count_thresholds:
|
|
285
|
+
threshold = config.alert_config.count_thresholds.get("all", 15)
|
|
286
|
+
intensity = min(10.0, (total_detections / threshold) * 10)
|
|
287
|
+
if intensity >= 7:
|
|
288
|
+
level = "critical"
|
|
289
|
+
elif intensity >= 5:
|
|
290
|
+
level = "warning"
|
|
291
|
+
else:
|
|
292
|
+
level = "info"
|
|
293
|
+
else:
|
|
294
|
+
if total_detections > 25:
|
|
295
|
+
level = "critical"
|
|
296
|
+
intensity = 9.0
|
|
297
|
+
elif total_detections > 15:
|
|
298
|
+
level = "warning"
|
|
299
|
+
intensity = 7.0
|
|
300
|
+
else:
|
|
301
|
+
level = "info"
|
|
302
|
+
intensity = min(10.0, total_detections / 3.0)
|
|
303
|
+
|
|
304
|
+
human_text_lines = ["EVENTS DETECTED:"]
|
|
305
|
+
human_text_lines.append(f" - {total_detections} activities detected [INFO]")
|
|
306
|
+
human_text = "\n".join(human_text_lines)
|
|
307
|
+
|
|
308
|
+
event = {
|
|
309
|
+
"type": "theft_detection",
|
|
310
|
+
"stream_time": datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S UTC"),
|
|
311
|
+
"level": level,
|
|
312
|
+
"intensity": round(intensity, 1),
|
|
313
|
+
"config": {
|
|
314
|
+
"min_value": 0,
|
|
315
|
+
"max_value": 10,
|
|
316
|
+
"level_settings": {"info": 2, "warming": 5, "critical": 7}
|
|
317
|
+
},
|
|
318
|
+
"application_name": "Theft Detection System",
|
|
319
|
+
"application_version": "1.2",
|
|
320
|
+
"location_info": None,
|
|
321
|
+
"human_text": human_text
|
|
322
|
+
}
|
|
323
|
+
frame_events.append(event)
|
|
324
|
+
|
|
325
|
+
for alert in alerts:
|
|
326
|
+
total_detections = counting_summary.get("total_count", 0)
|
|
327
|
+
intensity_message = "ALERT: Low activity in the scene"
|
|
328
|
+
if config.alert_config and config.alert_config.count_thresholds:
|
|
329
|
+
threshold = config.alert_config.count_thresholds.get("all", 15)
|
|
330
|
+
percentage = (total_detections / threshold) * 100 if threshold > 0 else 0
|
|
331
|
+
if percentage < 20:
|
|
332
|
+
intensity_message = "ALERT: Low activity in the scene"
|
|
333
|
+
elif percentage <= 50:
|
|
334
|
+
intensity_message = "ALERT: Moderate activity in the scene"
|
|
335
|
+
elif percentage <= 70:
|
|
336
|
+
intensity_message = "ALERT: High activity in the scene"
|
|
337
|
+
else:
|
|
338
|
+
intensity_message = "ALERT: Severe activity in the scene"
|
|
339
|
+
else:
|
|
340
|
+
if total_detections > 15:
|
|
341
|
+
intensity_message = "ALERT: High activity in the scene"
|
|
342
|
+
elif total_detections == 1:
|
|
343
|
+
intensity_message = "ALERT: Low activity in the scene"
|
|
344
|
+
else:
|
|
345
|
+
intensity_message = "ALERT: Moderate activity in the scene"
|
|
346
|
+
|
|
347
|
+
alert_event = {
|
|
348
|
+
"type": alert.get("type", "activity_alert"),
|
|
349
|
+
"stream_time": datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S UTC"),
|
|
350
|
+
"level": alert.get("severity", "warning"),
|
|
351
|
+
"intensity": 8.0,
|
|
352
|
+
"config": {
|
|
353
|
+
"min_value": 0,
|
|
354
|
+
"max_value": 10,
|
|
355
|
+
"level_settings": {"info": 2, "warning": 5, "critical": 7}
|
|
356
|
+
},
|
|
357
|
+
"application_name": "Activity Alert System",
|
|
358
|
+
"application_version": "1.2",
|
|
359
|
+
"location_info": alert.get("zone"),
|
|
360
|
+
"human_text": f"{datetime.now(timezone.utc).strftime('%Y-%m-%d-%H:%M:%S UTC')} : {intensity_message}"
|
|
361
|
+
}
|
|
362
|
+
frame_events.append(alert_event)
|
|
363
|
+
|
|
364
|
+
return events
|
|
365
|
+
|
|
366
|
+
def _generate_tracking_stats(
|
|
367
|
+
self,
|
|
368
|
+
counting_summary: Dict,
|
|
369
|
+
insights: List[str],
|
|
370
|
+
summary: str,
|
|
371
|
+
config: TheftDetectionConfig,
|
|
372
|
+
frame_number: Optional[int] = None,
|
|
373
|
+
stream_info: Optional[Dict[str, Any]] = None
|
|
374
|
+
) -> List[Dict]:
|
|
375
|
+
"""Generate structured tracking stats for the output format with frame-based keys, including track_ids_info."""
|
|
376
|
+
frame_key = str(frame_number) if frame_number is not None else "current_frame"
|
|
377
|
+
tracking_stats = [{frame_key: []}]
|
|
378
|
+
frame_tracking_stats = tracking_stats[0][frame_key]
|
|
379
|
+
|
|
380
|
+
total_detections = counting_summary.get("total_count", 0)
|
|
381
|
+
total_counts = counting_summary.get("total_counts", {})
|
|
382
|
+
cumulative_total = sum(total_counts.values()) if total_counts else 0
|
|
383
|
+
per_category_count = counting_summary.get("per_category_count", {})
|
|
384
|
+
|
|
385
|
+
track_ids_info = self._get_track_ids_info(counting_summary.get("detections", []))
|
|
386
|
+
|
|
387
|
+
current_timestamp = self._get_current_timestamp_str(stream_info)
|
|
388
|
+
start_timestamp = self._get_start_timestamp_str(stream_info)
|
|
389
|
+
|
|
390
|
+
human_text_lines = []
|
|
391
|
+
human_text_lines.append(f"CURRENT FRAME @ {current_timestamp}:")
|
|
392
|
+
if total_detections > 0:
|
|
393
|
+
category_counts = [f"{count} {cat}" for cat, count in per_category_count.items()]
|
|
394
|
+
if len(category_counts) == 1:
|
|
395
|
+
detection_text = category_counts[0] + " detected"
|
|
396
|
+
elif len(category_counts) == 2:
|
|
397
|
+
detection_text = f"{category_counts[0]} and {category_counts[1]} detected"
|
|
398
|
+
else:
|
|
399
|
+
detection_text = f"{', '.join(category_counts[:-1])}, and {category_counts[-1]} detected"
|
|
400
|
+
human_text_lines.append(f"\t- {detection_text}")
|
|
401
|
+
else:
|
|
402
|
+
human_text_lines.append(f"\t- No detections")
|
|
403
|
+
|
|
404
|
+
human_text_lines.append("")
|
|
405
|
+
human_text_lines.append(f"TOTAL SINCE {start_timestamp}:")
|
|
406
|
+
human_text_lines.append(f"\t- Total Activities Detected: {cumulative_total}")
|
|
407
|
+
if total_counts:
|
|
408
|
+
for cat, count in total_counts.items():
|
|
409
|
+
if count > 0:
|
|
410
|
+
human_text_lines.append(f"\t- {cat}: {count}")
|
|
411
|
+
|
|
412
|
+
human_text = "\n".join(human_text_lines)
|
|
413
|
+
|
|
414
|
+
tracking_stat = {
|
|
415
|
+
"type": "theft_detection",
|
|
416
|
+
"category": "security",
|
|
417
|
+
"count": total_detections,
|
|
418
|
+
"insights": insights,
|
|
419
|
+
"summary": summary,
|
|
420
|
+
"timestamp": datetime.now(timezone.utc).strftime('%Y-%m-%d-%H:%M:%S UTC'),
|
|
421
|
+
"human_text": human_text,
|
|
422
|
+
"track_ids_info": track_ids_info,
|
|
423
|
+
"global_frame_offset": getattr(self, '_global_frame_offset', 0),
|
|
424
|
+
"local_frame_id": frame_key,
|
|
425
|
+
"detections": counting_summary.get("detections", [])
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
frame_tracking_stats.append(tracking_stat)
|
|
429
|
+
return tracking_stats
|
|
430
|
+
|
|
431
|
+
def _count_categories(self, detections: list, config: TheftDetectionConfig) -> dict:
|
|
432
|
+
"""
|
|
433
|
+
Count the number of detections per category and return a summary dict.
|
|
434
|
+
"""
|
|
435
|
+
counts = {}
|
|
436
|
+
for det in detections:
|
|
437
|
+
cat = det.get('category', 'unknown')
|
|
438
|
+
counts[cat] = counts.get(cat, 0) + 1
|
|
439
|
+
return {
|
|
440
|
+
"total_count": sum(counts.values()),
|
|
441
|
+
"per_category_count": counts,
|
|
442
|
+
"detections": [
|
|
443
|
+
{
|
|
444
|
+
"bounding_box": det.get("bounding_box"),
|
|
445
|
+
"category": det.get("category"),
|
|
446
|
+
"confidence": det.get("confidence"),
|
|
447
|
+
"track_id": det.get("track_id"),
|
|
448
|
+
"frame_id": det.get("frame_id")
|
|
449
|
+
}
|
|
450
|
+
for det in detections
|
|
451
|
+
]
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
CATEGORY_DISPLAY = {
|
|
455
|
+
"normal": "normal",
|
|
456
|
+
"shoplifting": "shoplifting"
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
def _generate_insights(self, summary: dict, config: TheftDetectionConfig) -> List[str]:
|
|
460
|
+
"""
|
|
461
|
+
Generate human-readable insights for each category.
|
|
462
|
+
"""
|
|
463
|
+
insights = []
|
|
464
|
+
per_cat = summary.get("per_category_count", {})
|
|
465
|
+
total_detections = summary.get("total_count", 0)
|
|
466
|
+
|
|
467
|
+
if total_detections == 0:
|
|
468
|
+
insights.append("No activities detected in the scene")
|
|
469
|
+
return insights
|
|
470
|
+
insights.append(f"EVENT: Detected {total_detections} activities in the scene")
|
|
471
|
+
intensity_threshold = None
|
|
472
|
+
if config.alert_config and config.alert_config.count_thresholds and "all" in config.alert_config.count_thresholds:
|
|
473
|
+
intensity_threshold = config.alert_config.count_thresholds["all"]
|
|
474
|
+
|
|
475
|
+
if intensity_threshold is not None:
|
|
476
|
+
percentage = (total_detections / intensity_threshold) * 100
|
|
477
|
+
if percentage < 20:
|
|
478
|
+
insights.append(f"INTENSITY: Low activity in the scene ({percentage:.1f}% of capacity)")
|
|
479
|
+
elif percentage <= 50:
|
|
480
|
+
insights.append(f"INTENSITY: Moderate activity in the scene ({percentage:.1f}% of capacity)")
|
|
481
|
+
elif percentage <= 70:
|
|
482
|
+
insights.append(f"INTENSITY: High activity in the scene ({percentage:.1f}% of capacity)")
|
|
483
|
+
else:
|
|
484
|
+
insights.append(f"INTENSITY: Severe activity in the scene ({percentage:.1f}% of capacity)")
|
|
485
|
+
|
|
486
|
+
for cat, count in per_cat.items():
|
|
487
|
+
display = self.CATEGORY_DISPLAY.get(cat, cat)
|
|
488
|
+
insights.append(f"{display}:{count}")
|
|
489
|
+
return insights
|
|
490
|
+
|
|
491
|
+
def _check_alerts(self, summary: dict, config: TheftDetectionConfig) -> List[Dict]:
|
|
492
|
+
"""
|
|
493
|
+
Check if any alert thresholds are exceeded and return alert dicts.
|
|
494
|
+
"""
|
|
495
|
+
alerts = []
|
|
496
|
+
if not config.alert_config:
|
|
497
|
+
return alerts
|
|
498
|
+
total = summary.get("total_count", 0)
|
|
499
|
+
if config.alert_config.count_thresholds:
|
|
500
|
+
for category, threshold in config.alert_config.count_thresholds.items():
|
|
501
|
+
if category == "all" and total >= threshold:
|
|
502
|
+
alerts.append({
|
|
503
|
+
"type": "count_threshold",
|
|
504
|
+
"severity": "warning",
|
|
505
|
+
"message": f"Total activities count ({total}) exceeds threshold ({threshold})",
|
|
506
|
+
"category": category,
|
|
507
|
+
"current_count": total,
|
|
508
|
+
"threshold": threshold
|
|
509
|
+
})
|
|
510
|
+
elif category in summary.get("per_category_count", {}):
|
|
511
|
+
count = summary.get("per_category_count", {})[category]
|
|
512
|
+
if count >= threshold:
|
|
513
|
+
alerts.append({
|
|
514
|
+
"type": "count_threshold",
|
|
515
|
+
"severity": "warning",
|
|
516
|
+
"message": f"{category} count ({count}) exceeds threshold ({threshold})",
|
|
517
|
+
"category": category,
|
|
518
|
+
"current_count": count,
|
|
519
|
+
"threshold": threshold
|
|
520
|
+
})
|
|
521
|
+
return alerts
|
|
522
|
+
|
|
523
|
+
def _extract_predictions(self, detections: list) -> List[Dict[str, Any]]:
|
|
524
|
+
"""
|
|
525
|
+
Extract prediction details for output (category, confidence, bounding box).
|
|
526
|
+
"""
|
|
527
|
+
return [
|
|
528
|
+
{
|
|
529
|
+
"category": det.get("category", "unknown"),
|
|
530
|
+
"confidence": det.get("confidence", 0.0),
|
|
531
|
+
"bounding_box": det.get("bounding_box", {})
|
|
532
|
+
}
|
|
533
|
+
for det in detections
|
|
534
|
+
]
|
|
535
|
+
|
|
536
|
+
def _generate_summary(self, summary: dict, alerts: List) -> str:
|
|
537
|
+
"""
|
|
538
|
+
Generate a human_text string for the result, including per-category insights.
|
|
539
|
+
"""
|
|
540
|
+
total = summary.get("total_count", 0)
|
|
541
|
+
per_cat = summary.get("per_category_count", {})
|
|
542
|
+
cumulative = summary.get("total_counts", {})
|
|
543
|
+
cumulative_total = sum(cumulative.values()) if cumulative else 0
|
|
544
|
+
lines = []
|
|
545
|
+
if total > 0:
|
|
546
|
+
lines.append(f"{total} activities detected")
|
|
547
|
+
if per_cat:
|
|
548
|
+
lines.append("Activities:")
|
|
549
|
+
for cat, count in per_cat.items():
|
|
550
|
+
lines.append(f"\t{cat}:{count}")
|
|
551
|
+
else:
|
|
552
|
+
lines.append("No activities detected")
|
|
553
|
+
lines.append(f"Total activities: {cumulative_total}")
|
|
554
|
+
if alerts:
|
|
555
|
+
lines.append(f"{len(alerts)} alert(s)")
|
|
556
|
+
return "\n".join(lines)
|
|
557
|
+
|
|
558
|
+
def _compute_iou(self, box1: Any, box2: Any) -> float:
|
|
559
|
+
"""Compute IoU between two bounding boxes which may be dicts or lists."""
|
|
560
|
+
def _bbox_to_list(bbox):
|
|
561
|
+
if bbox is None:
|
|
562
|
+
return []
|
|
563
|
+
if isinstance(bbox, list):
|
|
564
|
+
return bbox[:4] if len(bbox) >= 4 else []
|
|
565
|
+
if isinstance(bbox, dict):
|
|
566
|
+
if "xmin" in bbox:
|
|
567
|
+
return [bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]]
|
|
568
|
+
if "x1" in bbox:
|
|
569
|
+
return [bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"]]
|
|
570
|
+
values = [v for v in bbox.values() if isinstance(v, (int, float))]
|
|
571
|
+
return values[:4] if len(values) >= 4 else []
|
|
572
|
+
return []
|
|
573
|
+
|
|
574
|
+
l1 = _bbox_to_list(box1)
|
|
575
|
+
l2 = _bbox_to_list(box2)
|
|
576
|
+
if len(l1) < 4 or len(l2) < 4:
|
|
577
|
+
return 0.0
|
|
578
|
+
x1_min, y1_min, x1_max, y1_max = l1
|
|
579
|
+
x2_min, y2_min, x2_max, y2_max = l2
|
|
580
|
+
|
|
581
|
+
x1_min, x1_max = min(x1_min, x1_max), max(x1_min, x1_max)
|
|
582
|
+
y1_min, y1_max = min(y1_min, y1_max), max(y1_min, y1_max)
|
|
583
|
+
x2_min, x2_max = min(x2_min, x2_max), max(x2_min, x2_max)
|
|
584
|
+
y2_min, y2_max = min(y2_min, y2_max), max(y2_min, y2_max)
|
|
585
|
+
|
|
586
|
+
inter_x_min = max(x1_min, x2_min)
|
|
587
|
+
inter_y_min = max(y1_min, y2_min)
|
|
588
|
+
inter_x_max = min(x1_max, x2_max)
|
|
589
|
+
inter_y_max = min(y1_max, y2_max)
|
|
590
|
+
|
|
591
|
+
inter_w = max(0.0, inter_x_max - inter_x_min)
|
|
592
|
+
inter_h = max(0.0, inter_y_max - inter_y_min)
|
|
593
|
+
inter_area = inter_w * inter_h
|
|
594
|
+
|
|
595
|
+
area1 = (x1_max - x1_min) * (y1_max - y1_min)
|
|
596
|
+
area2 = (x2_max - x2_min) * (y2_max - y2_min)
|
|
597
|
+
union_area = area1 + area2 - inter_area
|
|
598
|
+
|
|
599
|
+
return (inter_area / union_area) if union_area > 0 else 0.0
|
|
600
|
+
|
|
601
|
+
def _merge_or_register_track(self, raw_id: Any, bbox: Any) -> Any:
|
|
602
|
+
"""Return a stable canonical ID for a raw tracker ID."""
|
|
603
|
+
if raw_id is None or bbox is None:
|
|
604
|
+
return raw_id
|
|
605
|
+
|
|
606
|
+
now = time.time()
|
|
607
|
+
if raw_id in self._track_aliases:
|
|
608
|
+
canonical_id = self._track_aliases[raw_id]
|
|
609
|
+
track_info = self._canonical_tracks.get(canonical_id)
|
|
610
|
+
if track_info is not None:
|
|
611
|
+
track_info["last_bbox"] = bbox
|
|
612
|
+
track_info["last_update"] = now
|
|
613
|
+
track_info["raw_ids"].add(raw_id)
|
|
614
|
+
return canonical_id
|
|
615
|
+
|
|
616
|
+
for canonical_id, info in self._canonical_tracks.items():
|
|
617
|
+
if now - info["last_update"] > self._track_merge_time_window:
|
|
618
|
+
continue
|
|
619
|
+
iou = self._compute_iou(bbox, info["last_bbox"])
|
|
620
|
+
if iou >= self._track_merge_iou_threshold:
|
|
621
|
+
self._track_aliases[raw_id] = canonical_id
|
|
622
|
+
info["last_bbox"] = bbox
|
|
623
|
+
info["last_update"] = now
|
|
624
|
+
info["raw_ids"].add(raw_id)
|
|
625
|
+
return canonical_id
|
|
626
|
+
|
|
627
|
+
canonical_id = raw_id
|
|
628
|
+
self._track_aliases[raw_id] = canonical_id
|
|
629
|
+
self._canonical_tracks[canonical_id] = {
|
|
630
|
+
"last_bbox": bbox,
|
|
631
|
+
"last_update": now,
|
|
632
|
+
"raw_ids": {raw_id},
|
|
633
|
+
}
|
|
634
|
+
return canonical_id
|
|
635
|
+
|
|
636
|
+
def _format_timestamp(self, timestamp: float) -> str:
|
|
637
|
+
"""Format a timestamp for human-readable output."""
|
|
638
|
+
return datetime.fromtimestamp(timestamp, timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')
|
|
639
|
+
|
|
640
|
+
def _get_tracking_start_time(self) -> str:
|
|
641
|
+
"""Get the tracking start time, formatted as a string."""
|
|
642
|
+
if self._tracking_start_time is None:
|
|
643
|
+
return "N/A"
|
|
644
|
+
return self._format_timestamp(self._tracking_start_time)
|
|
645
|
+
|
|
646
|
+
def _set_tracking_start_time(self) -> None:
|
|
647
|
+
"""Set the tracking start time to the current time."""
|
|
648
|
+
self._tracking_start_time = time.time()
|