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.
Files changed (196) hide show
  1. matrice_analytics/__init__.py +28 -0
  2. matrice_analytics/boundary_drawing_internal/README.md +305 -0
  3. matrice_analytics/boundary_drawing_internal/__init__.py +45 -0
  4. matrice_analytics/boundary_drawing_internal/boundary_drawing_internal.py +1207 -0
  5. matrice_analytics/boundary_drawing_internal/boundary_drawing_tool.py +429 -0
  6. matrice_analytics/boundary_drawing_internal/boundary_tool_template.html +1036 -0
  7. matrice_analytics/boundary_drawing_internal/data/.gitignore +12 -0
  8. matrice_analytics/boundary_drawing_internal/example_usage.py +206 -0
  9. matrice_analytics/boundary_drawing_internal/usage/README.md +110 -0
  10. matrice_analytics/boundary_drawing_internal/usage/boundary_drawer_launcher.py +102 -0
  11. matrice_analytics/boundary_drawing_internal/usage/simple_boundary_launcher.py +107 -0
  12. matrice_analytics/post_processing/README.md +455 -0
  13. matrice_analytics/post_processing/__init__.py +732 -0
  14. matrice_analytics/post_processing/advanced_tracker/README.md +650 -0
  15. matrice_analytics/post_processing/advanced_tracker/__init__.py +17 -0
  16. matrice_analytics/post_processing/advanced_tracker/base.py +99 -0
  17. matrice_analytics/post_processing/advanced_tracker/config.py +77 -0
  18. matrice_analytics/post_processing/advanced_tracker/kalman_filter.py +370 -0
  19. matrice_analytics/post_processing/advanced_tracker/matching.py +195 -0
  20. matrice_analytics/post_processing/advanced_tracker/strack.py +230 -0
  21. matrice_analytics/post_processing/advanced_tracker/tracker.py +367 -0
  22. matrice_analytics/post_processing/config.py +146 -0
  23. matrice_analytics/post_processing/core/__init__.py +63 -0
  24. matrice_analytics/post_processing/core/base.py +704 -0
  25. matrice_analytics/post_processing/core/config.py +3291 -0
  26. matrice_analytics/post_processing/core/config_utils.py +925 -0
  27. matrice_analytics/post_processing/face_reg/__init__.py +43 -0
  28. matrice_analytics/post_processing/face_reg/compare_similarity.py +556 -0
  29. matrice_analytics/post_processing/face_reg/embedding_manager.py +950 -0
  30. matrice_analytics/post_processing/face_reg/face_recognition.py +2234 -0
  31. matrice_analytics/post_processing/face_reg/face_recognition_client.py +606 -0
  32. matrice_analytics/post_processing/face_reg/people_activity_logging.py +321 -0
  33. matrice_analytics/post_processing/ocr/__init__.py +0 -0
  34. matrice_analytics/post_processing/ocr/easyocr_extractor.py +250 -0
  35. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/__init__.py +9 -0
  36. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/__init__.py +4 -0
  37. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/cli.py +33 -0
  38. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/dataset_stats.py +139 -0
  39. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/export.py +398 -0
  40. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/train.py +447 -0
  41. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/utils.py +129 -0
  42. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/valid.py +93 -0
  43. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/validate_dataset.py +240 -0
  44. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/visualize_augmentation.py +176 -0
  45. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/cli/visualize_predictions.py +96 -0
  46. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/__init__.py +3 -0
  47. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/process.py +246 -0
  48. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/types.py +60 -0
  49. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/core/utils.py +87 -0
  50. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/__init__.py +3 -0
  51. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/config.py +82 -0
  52. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/hub.py +141 -0
  53. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/inference/plate_recognizer.py +323 -0
  54. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/py.typed +0 -0
  55. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/__init__.py +0 -0
  56. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/__init__.py +0 -0
  57. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/augmentation.py +101 -0
  58. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/data/dataset.py +97 -0
  59. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/__init__.py +0 -0
  60. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/config.py +114 -0
  61. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/layers.py +553 -0
  62. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/loss.py +55 -0
  63. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/metric.py +86 -0
  64. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/model_builders.py +95 -0
  65. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/model/model_schema.py +395 -0
  66. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/__init__.py +0 -0
  67. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/backend_utils.py +38 -0
  68. matrice_analytics/post_processing/ocr/fast_plate_ocr_py38/train/utilities/utils.py +214 -0
  69. matrice_analytics/post_processing/ocr/postprocessing.py +270 -0
  70. matrice_analytics/post_processing/ocr/preprocessing.py +52 -0
  71. matrice_analytics/post_processing/post_processor.py +1175 -0
  72. matrice_analytics/post_processing/test_cases/__init__.py +1 -0
  73. matrice_analytics/post_processing/test_cases/run_tests.py +143 -0
  74. matrice_analytics/post_processing/test_cases/test_advanced_customer_service.py +841 -0
  75. matrice_analytics/post_processing/test_cases/test_basic_counting_tracking.py +523 -0
  76. matrice_analytics/post_processing/test_cases/test_comprehensive.py +531 -0
  77. matrice_analytics/post_processing/test_cases/test_config.py +852 -0
  78. matrice_analytics/post_processing/test_cases/test_customer_service.py +585 -0
  79. matrice_analytics/post_processing/test_cases/test_data_generators.py +583 -0
  80. matrice_analytics/post_processing/test_cases/test_people_counting.py +510 -0
  81. matrice_analytics/post_processing/test_cases/test_processor.py +524 -0
  82. matrice_analytics/post_processing/test_cases/test_usecases.py +165 -0
  83. matrice_analytics/post_processing/test_cases/test_utilities.py +356 -0
  84. matrice_analytics/post_processing/test_cases/test_utils.py +743 -0
  85. matrice_analytics/post_processing/usecases/Histopathological_Cancer_Detection_img.py +604 -0
  86. matrice_analytics/post_processing/usecases/__init__.py +267 -0
  87. matrice_analytics/post_processing/usecases/abandoned_object_detection.py +797 -0
  88. matrice_analytics/post_processing/usecases/advanced_customer_service.py +1601 -0
  89. matrice_analytics/post_processing/usecases/age_detection.py +842 -0
  90. matrice_analytics/post_processing/usecases/age_gender_detection.py +1085 -0
  91. matrice_analytics/post_processing/usecases/anti_spoofing_detection.py +656 -0
  92. matrice_analytics/post_processing/usecases/assembly_line_detection.py +841 -0
  93. matrice_analytics/post_processing/usecases/banana_defect_detection.py +624 -0
  94. matrice_analytics/post_processing/usecases/basic_counting_tracking.py +667 -0
  95. matrice_analytics/post_processing/usecases/blood_cancer_detection_img.py +881 -0
  96. matrice_analytics/post_processing/usecases/car_damage_detection.py +834 -0
  97. matrice_analytics/post_processing/usecases/car_part_segmentation.py +946 -0
  98. matrice_analytics/post_processing/usecases/car_service.py +1601 -0
  99. matrice_analytics/post_processing/usecases/cardiomegaly_classification.py +864 -0
  100. matrice_analytics/post_processing/usecases/cell_microscopy_segmentation.py +897 -0
  101. matrice_analytics/post_processing/usecases/chicken_pose_detection.py +648 -0
  102. matrice_analytics/post_processing/usecases/child_monitoring.py +814 -0
  103. matrice_analytics/post_processing/usecases/color/clip.py +660 -0
  104. matrice_analytics/post_processing/usecases/color/clip_processor/merges.txt +48895 -0
  105. matrice_analytics/post_processing/usecases/color/clip_processor/preprocessor_config.json +28 -0
  106. matrice_analytics/post_processing/usecases/color/clip_processor/special_tokens_map.json +30 -0
  107. matrice_analytics/post_processing/usecases/color/clip_processor/tokenizer.json +245079 -0
  108. matrice_analytics/post_processing/usecases/color/clip_processor/tokenizer_config.json +32 -0
  109. matrice_analytics/post_processing/usecases/color/clip_processor/vocab.json +1 -0
  110. matrice_analytics/post_processing/usecases/color/color_map_utils.py +70 -0
  111. matrice_analytics/post_processing/usecases/color/color_mapper.py +468 -0
  112. matrice_analytics/post_processing/usecases/color_detection.py +1936 -0
  113. matrice_analytics/post_processing/usecases/color_map_utils.py +70 -0
  114. matrice_analytics/post_processing/usecases/concrete_crack_detection.py +827 -0
  115. matrice_analytics/post_processing/usecases/crop_weed_detection.py +781 -0
  116. matrice_analytics/post_processing/usecases/customer_service.py +1008 -0
  117. matrice_analytics/post_processing/usecases/defect_detection_products.py +936 -0
  118. matrice_analytics/post_processing/usecases/distracted_driver_detection.py +822 -0
  119. matrice_analytics/post_processing/usecases/drone_traffic_monitoring.py +585 -0
  120. matrice_analytics/post_processing/usecases/drowsy_driver_detection.py +829 -0
  121. matrice_analytics/post_processing/usecases/dwell_detection.py +829 -0
  122. matrice_analytics/post_processing/usecases/emergency_vehicle_detection.py +827 -0
  123. matrice_analytics/post_processing/usecases/face_emotion.py +813 -0
  124. matrice_analytics/post_processing/usecases/face_recognition.py +827 -0
  125. matrice_analytics/post_processing/usecases/fashion_detection.py +835 -0
  126. matrice_analytics/post_processing/usecases/field_mapping.py +902 -0
  127. matrice_analytics/post_processing/usecases/fire_detection.py +1146 -0
  128. matrice_analytics/post_processing/usecases/flare_analysis.py +836 -0
  129. matrice_analytics/post_processing/usecases/flower_segmentation.py +1006 -0
  130. matrice_analytics/post_processing/usecases/gas_leak_detection.py +837 -0
  131. matrice_analytics/post_processing/usecases/gender_detection.py +832 -0
  132. matrice_analytics/post_processing/usecases/human_activity_recognition.py +871 -0
  133. matrice_analytics/post_processing/usecases/intrusion_detection.py +1672 -0
  134. matrice_analytics/post_processing/usecases/leaf.py +821 -0
  135. matrice_analytics/post_processing/usecases/leaf_disease.py +840 -0
  136. matrice_analytics/post_processing/usecases/leak_detection.py +837 -0
  137. matrice_analytics/post_processing/usecases/license_plate_detection.py +1188 -0
  138. matrice_analytics/post_processing/usecases/license_plate_monitoring.py +1781 -0
  139. matrice_analytics/post_processing/usecases/litter_monitoring.py +717 -0
  140. matrice_analytics/post_processing/usecases/mask_detection.py +869 -0
  141. matrice_analytics/post_processing/usecases/natural_disaster.py +907 -0
  142. matrice_analytics/post_processing/usecases/parking.py +787 -0
  143. matrice_analytics/post_processing/usecases/parking_space_detection.py +822 -0
  144. matrice_analytics/post_processing/usecases/pcb_defect_detection.py +888 -0
  145. matrice_analytics/post_processing/usecases/pedestrian_detection.py +808 -0
  146. matrice_analytics/post_processing/usecases/people_counting.py +706 -0
  147. matrice_analytics/post_processing/usecases/people_counting_bckp.py +1683 -0
  148. matrice_analytics/post_processing/usecases/people_tracking.py +1842 -0
  149. matrice_analytics/post_processing/usecases/pipeline_detection.py +605 -0
  150. matrice_analytics/post_processing/usecases/plaque_segmentation_img.py +874 -0
  151. matrice_analytics/post_processing/usecases/pothole_segmentation.py +915 -0
  152. matrice_analytics/post_processing/usecases/ppe_compliance.py +645 -0
  153. matrice_analytics/post_processing/usecases/price_tag_detection.py +822 -0
  154. matrice_analytics/post_processing/usecases/proximity_detection.py +1901 -0
  155. matrice_analytics/post_processing/usecases/road_lane_detection.py +623 -0
  156. matrice_analytics/post_processing/usecases/road_traffic_density.py +832 -0
  157. matrice_analytics/post_processing/usecases/road_view_segmentation.py +915 -0
  158. matrice_analytics/post_processing/usecases/shelf_inventory_detection.py +583 -0
  159. matrice_analytics/post_processing/usecases/shoplifting_detection.py +822 -0
  160. matrice_analytics/post_processing/usecases/shopping_cart_analysis.py +899 -0
  161. matrice_analytics/post_processing/usecases/skin_cancer_classification_img.py +864 -0
  162. matrice_analytics/post_processing/usecases/smoker_detection.py +833 -0
  163. matrice_analytics/post_processing/usecases/solar_panel.py +810 -0
  164. matrice_analytics/post_processing/usecases/suspicious_activity_detection.py +1030 -0
  165. matrice_analytics/post_processing/usecases/template_usecase.py +380 -0
  166. matrice_analytics/post_processing/usecases/theft_detection.py +648 -0
  167. matrice_analytics/post_processing/usecases/traffic_sign_monitoring.py +724 -0
  168. matrice_analytics/post_processing/usecases/underground_pipeline_defect_detection.py +775 -0
  169. matrice_analytics/post_processing/usecases/underwater_pollution_detection.py +842 -0
  170. matrice_analytics/post_processing/usecases/vehicle_monitoring.py +1029 -0
  171. matrice_analytics/post_processing/usecases/warehouse_object_segmentation.py +899 -0
  172. matrice_analytics/post_processing/usecases/waterbody_segmentation.py +923 -0
  173. matrice_analytics/post_processing/usecases/weapon_detection.py +771 -0
  174. matrice_analytics/post_processing/usecases/weld_defect_detection.py +615 -0
  175. matrice_analytics/post_processing/usecases/wildlife_monitoring.py +898 -0
  176. matrice_analytics/post_processing/usecases/windmill_maintenance.py +834 -0
  177. matrice_analytics/post_processing/usecases/wound_segmentation.py +856 -0
  178. matrice_analytics/post_processing/utils/__init__.py +150 -0
  179. matrice_analytics/post_processing/utils/advanced_counting_utils.py +400 -0
  180. matrice_analytics/post_processing/utils/advanced_helper_utils.py +317 -0
  181. matrice_analytics/post_processing/utils/advanced_tracking_utils.py +461 -0
  182. matrice_analytics/post_processing/utils/alerting_utils.py +213 -0
  183. matrice_analytics/post_processing/utils/category_mapping_utils.py +94 -0
  184. matrice_analytics/post_processing/utils/color_utils.py +592 -0
  185. matrice_analytics/post_processing/utils/counting_utils.py +182 -0
  186. matrice_analytics/post_processing/utils/filter_utils.py +261 -0
  187. matrice_analytics/post_processing/utils/format_utils.py +293 -0
  188. matrice_analytics/post_processing/utils/geometry_utils.py +300 -0
  189. matrice_analytics/post_processing/utils/smoothing_utils.py +358 -0
  190. matrice_analytics/post_processing/utils/tracking_utils.py +234 -0
  191. matrice_analytics/py.typed +0 -0
  192. matrice_analytics-0.1.60.dist-info/METADATA +481 -0
  193. matrice_analytics-0.1.60.dist-info/RECORD +196 -0
  194. matrice_analytics-0.1.60.dist-info/WHEEL +5 -0
  195. matrice_analytics-0.1.60.dist-info/licenses/LICENSE.txt +21 -0
  196. matrice_analytics-0.1.60.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1030 @@
1
+ from typing import Any, Dict, List, Optional
2
+ from dataclasses import asdict, dataclass, field
3
+ import time
4
+ from datetime import datetime, timezone
5
+ import copy
6
+ import tempfile
7
+ import os
8
+ import json
9
+ import pandas as pd
10
+ import cv2
11
+ import numpy as np
12
+ import logging
13
+ import zipfile
14
+ import os
15
+ import requests
16
+ try:
17
+ import xgboost as xgb
18
+ except ImportError:
19
+ logging.warning("xgboost is not installed")
20
+ xgb = None
21
+ from PIL import Image
22
+
23
+ from io import BytesIO
24
+ from collections import Counter, defaultdict
25
+ from ..core.base import BaseProcessor, ProcessingContext, ProcessingResult, ConfigProtocol, ResultFormat
26
+ from ..utils import (
27
+ filter_by_confidence,
28
+ filter_by_categories,
29
+ # apply_category_mapping,
30
+ count_objects_by_category,
31
+ count_objects_in_zones,
32
+ calculate_counting_summary,
33
+ match_results_structure,
34
+ bbox_smoothing,
35
+ BBoxSmoothingConfig,
36
+ BBoxSmoothingTracker
37
+ )
38
+ from dataclasses import dataclass, field
39
+ from ..core.config import BaseConfig, AlertConfig, ZoneConfig
40
+
41
+ def apply_category_mapping(results: Any, index_to_category: Dict[str, str]) -> Any:
42
+ """
43
+ Apply category index to name mapping.
44
+
45
+ Args:
46
+ results: Detection or tracking results
47
+ index_to_category: Mapping from category index to category name
48
+
49
+ Returns:
50
+ Results with mapped category names
51
+ """
52
+
53
+ def map_detection(
54
+ detection: Dict[str, Any], index_to_category: Dict[str, str]
55
+ ) -> Dict[str, Any]:
56
+ """Map a single detection."""
57
+ detection = detection.copy()
58
+ category_id = str(detection.get("class_id", detection.get("class_id")))
59
+ index_to_category = {str(k): str(v) for k, v in index_to_category.items()}
60
+ if category_id in index_to_category:
61
+ detection["category"] = index_to_category[category_id]
62
+ detection["class_id"] = category_id
63
+ return detection
64
+
65
+ if isinstance(results, list):
66
+ # Detection format
67
+ return [map_detection(r, index_to_category) for r in results]
68
+
69
+ elif isinstance(results, dict):
70
+ # Check if it's a simple classification result
71
+ if "category" in results or "class_id" in results:
72
+ return map_detection(results, index_to_category)
73
+
74
+ # Frame-based format
75
+ mapped_results = {}
76
+ for frame_id, detections in results.items():
77
+ if isinstance(detections, list):
78
+ mapped_results[frame_id] = [
79
+ map_detection(d, index_to_category) for d in detections
80
+ ]
81
+ else:
82
+ mapped_results[frame_id] = detections
83
+
84
+ return mapped_results
85
+
86
+ return results
87
+
88
+ def load_model_from_checkpoint(checkpoint_path,local_path):
89
+ """
90
+ Load a model from checkpoint URL
91
+ """
92
+ try:
93
+ print(f"Loading model from checkpoint: {checkpoint_path}")
94
+
95
+ # Check if checkpoint is a URL
96
+ if checkpoint_path.startswith(('http://', 'https://')):
97
+ # Download checkpoint from URL
98
+ response = requests.get(checkpoint_path, timeout = (30,200))
99
+ if response.status_code == 200:
100
+ with open(local_path, 'wb') as f:
101
+ f.write(response.content)
102
+ checkpoint_path = local_path
103
+ print(f"Downloaded checkpoint to {local_path}")
104
+ else:
105
+ print(f"Failed to download checkpoint from {checkpoint_path}")
106
+ return None
107
+
108
+ # Load the model from the checkpoint
109
+ model = xgb.Booster()
110
+ model.load_model(checkpoint_path)
111
+ print(f"{local_path} Model loaded successfully from checkpoint")
112
+ return model
113
+
114
+ except Exception as e:
115
+ print(f"Error loading model from checkpoint: {e}")
116
+ return None
117
+
118
+
119
+ @dataclass
120
+ class SusActivityConfig(BaseConfig):
121
+ """Configuration for PCB Defect Detection use case."""
122
+ # Smoothing configuration
123
+ enable_smoothing: bool = True
124
+ smoothing_algorithm: str = "observability" # "window" or "observability"
125
+ smoothing_window_size: int = 20
126
+ smoothing_cooldown_frames: int = 5
127
+ smoothing_confidence_range_factor: float = 0.5
128
+ usecase:str = "suspicious_activity_detection"
129
+ model_url:str = "https://s3.us-west-2.amazonaws.com/testing.resources/datasets/suspicous_detection_xgb-model_weights.json"
130
+
131
+ #confidence thresholds
132
+ confidence_threshold: float = 0.3
133
+
134
+ usecase_categories: List[str] = field(
135
+ default_factory=lambda: ['Person']
136
+ )
137
+
138
+ target_categories: List[str] = field(
139
+ default_factory=lambda: ['Person']
140
+ )
141
+
142
+ alert_config: Optional[AlertConfig] = None
143
+
144
+ index_to_category: Optional[Dict[int, str]] = field(
145
+ default_factory=lambda: {
146
+ 0: 'Person'
147
+ }
148
+ )
149
+ def __post_init__(self):
150
+ if self.model_url:
151
+ self.model_url = load_model_from_checkpoint(self.model_url, "suspicous_detection_xgb-model_weights.json")
152
+ else:
153
+ raise ValueError("Age and Gender model URL must be passed!")
154
+
155
+
156
+ class SusActivityUseCase(BaseProcessor):
157
+ # Human-friendly display names for categories
158
+
159
+ def __init__(self):
160
+ super().__init__("suspicious_activity_detection")
161
+ self.category = "security"
162
+
163
+ self.CASE_TYPE: Optional[str] = 'suspicious_activity_detection'
164
+ self.CASE_VERSION: Optional[str] = '1.2'
165
+ # List of categories to track
166
+ self.target_categories = ['Person']
167
+
168
+
169
+ # Initialize smoothing tracker
170
+ self.smoothing_tracker = None
171
+
172
+ # Initialize advanced tracker (will be created on first use)
173
+ self.tracker = None
174
+ # Initialize tracking state variables
175
+ self._total_frame_counter = 0
176
+ self._global_frame_offset = 0
177
+
178
+ # Track start time for "TOTAL SINCE" calculation
179
+ self._tracking_start_time = None
180
+
181
+ self._track_aliases: Dict[Any, Any] = {}
182
+ self._canonical_tracks: Dict[Any, Dict[str, Any]] = {}
183
+ # Tunable parameters – adjust if necessary for specific scenarios
184
+ self._track_merge_iou_threshold: float = 0.05 # IoU ≥ 0.05 →
185
+ self._track_merge_time_window: float = 7.0 # seconds within which to merge
186
+
187
+ self._ascending_alert_list: List[int] = []
188
+ self.current_incident_end_timestamp: str = "N/A"
189
+ self.start_timer = None
190
+ self.sus_dict: Dict[Any, Any] = {}
191
+
192
+ def helper(self, data, config):
193
+ curr_sus_data = {}
194
+ for det in data:
195
+ bound_box = det.get("bounding_box")
196
+ conf = det.get("confidence")
197
+ keypoints = det.get("keypoints")
198
+ index = det.get("track_id")
199
+ if conf > 0.75:
200
+ data_dict = {}
201
+ for j, kp in enumerate(keypoints):
202
+ data_dict[f'x{j}'] = kp[0]
203
+ data_dict[f'y{j}'] = kp[1]
204
+
205
+ # print(f'Bounding Box {index}: {data_dict}')
206
+ df = pd.DataFrame(data_dict, index=[0])
207
+ dmatrix = xgb.DMatrix(df)
208
+ cut = config.model_url.predict(dmatrix)
209
+ binary_predictions = (cut > 0.5).astype(int)
210
+ print(f'Prediction: {binary_predictions}')
211
+
212
+ if binary_predictions == 0:
213
+ conf_text = f'Suspicious {index} ({conf:.2f})'
214
+ curr_sus_data[index] = "SUSPICIOUS"
215
+ if binary_predictions == 1:
216
+ conf_text = f'Normal {index} ({conf:.2f})'
217
+
218
+ if index not in self.sus_dict and binary_predictions==0:
219
+ self.sus_dict[index] = "Suspicious"
220
+
221
+ return {"sus_data":self.sus_dict, "curr_data": curr_sus_data}
222
+
223
+
224
+ def process(self, data: Any, config: ConfigProtocol, context: Optional[ProcessingContext] = None,
225
+ stream_info: Optional[Dict[str, Any]] = None) -> ProcessingResult:
226
+ """
227
+ Main entry point for post-processing.
228
+ Applies category mapping, smoothing, counting, alerting, and summary generation.
229
+ Returns a ProcessingResult with all relevant outputs.
230
+ """
231
+ start_time = time.time()
232
+ if config.model_url is None:
233
+ return self.create_error_result("Model not loaded",
234
+ usecase=self.name, category=self.category, context=context)
235
+ # Ensure config is correct type
236
+ if not isinstance(config, SusActivityConfig):
237
+ return self.create_error_result("Invalid config type", usecase=self.name, category=self.category,
238
+ context=context)
239
+ if context is None:
240
+ context = ProcessingContext()
241
+
242
+ # Detect input format and store in context
243
+ input_format = match_results_structure(data)
244
+ context.input_format = input_format
245
+ context.confidence_threshold = config.confidence_threshold
246
+
247
+ if config.confidence_threshold is not None:
248
+ processed_data = filter_by_confidence(data, config.confidence_threshold)
249
+ self.logger.debug(f"Applied confidence filtering with threshold {config.confidence_threshold}")
250
+ else:
251
+ processed_data = data
252
+
253
+ self.logger.debug(f"Did not apply confidence filtering with threshold since nothing was provided")
254
+
255
+ print("---------DATA1--------------",processed_data)
256
+ # Step 2: Apply category mapping if provided
257
+ if config.index_to_category:
258
+ processed_data = apply_category_mapping(processed_data, config.index_to_category)
259
+ self.logger.debug("Applied category mapping")
260
+ print("---------DATA2--------------",processed_data)
261
+ # print(processed_data)
262
+
263
+ if self.target_categories:
264
+ processed_data = [d for d in processed_data if d.get('category') in self.target_categories]
265
+ self.logger.debug(f"Applied category filtering")
266
+
267
+ # Apply bbox smoothing if enabled
268
+ print("---------DATA2--------------",processed_data)
269
+ if config.enable_smoothing:
270
+ if self.smoothing_tracker is None:
271
+ smoothing_config = BBoxSmoothingConfig(
272
+ smoothing_algorithm=config.smoothing_algorithm,
273
+ window_size=config.smoothing_window_size,
274
+ cooldown_frames=config.smoothing_cooldown_frames,
275
+ confidence_threshold=config.confidence_threshold, # Use mask threshold as default
276
+ confidence_range_factor=config.smoothing_confidence_range_factor,
277
+ enable_smoothing=True
278
+ )
279
+ self.smoothing_tracker = BBoxSmoothingTracker(smoothing_config)
280
+ processed_data = bbox_smoothing(processed_data, self.smoothing_tracker.config, self.smoothing_tracker)
281
+
282
+ # Advanced tracking (BYTETracker-like)
283
+ try:
284
+ from ..advanced_tracker import AdvancedTracker
285
+ from ..advanced_tracker.config import TrackerConfig
286
+
287
+ # Create tracker instance if it doesn't exist (preserves state across frames)
288
+ if self.tracker is None:
289
+ # Configure tracker thresholds based on the use-case confidence threshold so that
290
+ # low-confidence detections (e.g. < 0.7) can still be initialised as tracks when
291
+ # the user passes a lower `confidence_threshold` in the post-processing config.
292
+ if config.confidence_threshold is not None:
293
+ tracker_config = TrackerConfig(
294
+ track_high_thresh=float(config.confidence_threshold),
295
+ # Allow even lower detections to participate in secondary association
296
+ track_low_thresh=max(0.05, float(config.confidence_threshold) / 2),
297
+ new_track_thresh=float(config.confidence_threshold)
298
+ )
299
+ else:
300
+ tracker_config = TrackerConfig()
301
+ self.tracker = AdvancedTracker(tracker_config)
302
+ self.logger.info(
303
+ "Initialized AdvancedTracker for Monitoring and tracking with thresholds: "
304
+ f"high={tracker_config.track_high_thresh}, "
305
+ f"low={tracker_config.track_low_thresh}, "
306
+ f"new={tracker_config.new_track_thresh}"
307
+ )
308
+
309
+ # The tracker expects the data in the same format as input
310
+ # It will add track_id and frame_id to each detection
311
+ processed_data = self.tracker.update(processed_data)
312
+
313
+ except Exception as e:
314
+ # If advanced tracker fails, fallback to unsmoothed detections
315
+ self.logger.warning(f"AdvancedTracker failed: {e}")
316
+
317
+ print("---------DATA3--------------",processed_data)
318
+ # Update tracking state for total count per label
319
+ self._update_tracking_state(processed_data)
320
+ print("---------DATA4--------------",processed_data)
321
+
322
+ # Update frame counter
323
+ self._total_frame_counter += 1
324
+
325
+ # Extract frame information from stream_info
326
+ frame_number = None
327
+ if stream_info:
328
+ input_settings = stream_info.get("input_settings", {})
329
+ start_frame = input_settings.get("start_frame")
330
+ end_frame = input_settings.get("end_frame")
331
+ # If start and end frame are the same, it's a single frame
332
+ if start_frame is not None and end_frame is not None and start_frame == end_frame:
333
+ frame_number = start_frame
334
+
335
+ # Call helper method for additional processing
336
+ det = self.helper(processed_data, config)
337
+ print("----------------------HELPER--------------------------------")
338
+ print(det)
339
+ print("----------------------HELPER--------------------------------")
340
+ # Compute summaries and alerts
341
+ general_counting_summary = calculate_counting_summary(data)
342
+ counting_summary = self._count_categories(processed_data, config)
343
+
344
+ # Add total unique counts after tracking using only local state
345
+ total_counts = self.get_total_counts()
346
+ counting_summary['total_counts'] = total_counts
347
+ counting_summary['sus_data'] = det
348
+ print("---------------------------------COUNTING SUMMARY------------------------------")
349
+ print(counting_summary)
350
+ print("---------------------------------COUNTING SUMMARY------------------------------")
351
+
352
+ alerts = self._check_alerts(counting_summary, frame_number, config)
353
+ predictions = self._extract_predictions(processed_data)
354
+
355
+ # Step: Generate structured incidents, tracking stats and business analytics with frame-based keys
356
+ incidents_list = self._generate_incidents(counting_summary, alerts, config, frame_number, stream_info)
357
+ tracking_stats_list = self._generate_tracking_stats(counting_summary, alerts, config, frame_number, stream_info)
358
+ business_analytics_list = self._generate_business_analytics(counting_summary, alerts, config, stream_info, is_empty=True)
359
+ summary_list = self._generate_summary(counting_summary, incidents_list, tracking_stats_list, business_analytics_list, alerts)
360
+
361
+ # Extract frame-based dictionaries from the lists
362
+ incidents = incidents_list[0] if incidents_list else {}
363
+ tracking_stats = tracking_stats_list[0] if tracking_stats_list else {}
364
+ business_analytics = business_analytics_list[0] if business_analytics_list else {}
365
+ summary = summary_list[0] if summary_list else {}
366
+ agg_summary = {str(frame_number): {
367
+ "incidents": incidents,
368
+ "tracking_stats": tracking_stats,
369
+ "business_analytics": business_analytics,
370
+ "alerts": alerts,
371
+ "human_text": summary}
372
+ }
373
+
374
+
375
+ context.mark_completed()
376
+
377
+ # Build result object following the new pattern
378
+
379
+ result = self.create_result(
380
+ data={"agg_summary": agg_summary},
381
+ usecase=self.name,
382
+ category=self.category,
383
+ context=context
384
+ )
385
+
386
+ return result
387
+
388
+ def _check_alerts(self, summary: dict, frame_number:Any, config: SusActivityConfig) -> List[Dict]:
389
+ """
390
+ Check if any alert thresholds are exceeded and return alert dicts.
391
+ """
392
+ def get_trend(data, lookback=900, threshold=0.6):
393
+ '''
394
+ Determine if the trend is ascending or descending based on actual value progression.
395
+ Now works with values 0,1,2,3 (not just binary).
396
+ '''
397
+ window = data[-lookback:] if len(data) >= lookback else data
398
+ if len(window) < 2:
399
+ return True # not enough data to determine trend
400
+ increasing = 0
401
+ total = 0
402
+ for i in range(1, len(window)):
403
+ if window[i] >= window[i - 1]:
404
+ increasing += 1
405
+ total += 1
406
+ ratio = increasing / total
407
+ if ratio >= threshold:
408
+ return True
409
+ elif ratio <= (1 - threshold):
410
+ return False
411
+
412
+ frame_key = str(frame_number) if frame_number is not None else "current_frame"
413
+ alerts = []
414
+ total_detections = summary.get("total_count", 0) #CURRENT combined total count of all classes
415
+ total_counts_dict = summary.get("total_counts", {}) #TOTAL cumulative counts per class
416
+ cumulative_total = sum(total_counts_dict.values()) if total_counts_dict else 0 #TOTAL combined cumulative count
417
+ per_category_count = summary.get("per_category_count", {}) #CURRENT count per class
418
+
419
+ if not config.alert_config:
420
+ return alerts
421
+
422
+ total = summary.get("total_count", 0)
423
+ #self._ascending_alert_list
424
+ if hasattr(config.alert_config, 'count_thresholds') and config.alert_config.count_thresholds:
425
+
426
+ for category, threshold in config.alert_config.count_thresholds.items():
427
+ if category == "all" and total > threshold:
428
+
429
+ alerts.append({
430
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
431
+ "alert_id": "alert_"+category+'_'+frame_key,
432
+ "incident_category": self.CASE_TYPE,
433
+ "threshold_level": threshold,
434
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
435
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
436
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
437
+ }
438
+ })
439
+ elif category in summary.get("per_category_count", {}):
440
+ count = summary.get("per_category_count", {})[category]
441
+ if count > threshold: # Fixed logic: alert when EXCEEDING threshold
442
+ alerts.append({
443
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
444
+ "alert_id": "alert_"+category+'_'+frame_key,
445
+ "incident_category": self.CASE_TYPE,
446
+ "threshold_level": threshold,
447
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
448
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
449
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
450
+ }
451
+ })
452
+ else:
453
+ pass
454
+ return alerts
455
+
456
+ def _generate_incidents(self, counting_summary: Dict, alerts: List, config: SusActivityConfig,
457
+ frame_number: Optional[int] = None, stream_info: Optional[Dict[str, Any]] = None) -> List[
458
+ Dict]:
459
+ """Generate structured incidents for the output format with frame-based keys."""
460
+
461
+ incidents = []
462
+ total_detections = counting_summary.get("total_count", 0)
463
+ current_timestamp = self._get_current_timestamp_str(stream_info)
464
+ camera_info = self.get_camera_info_from_stream(stream_info)
465
+
466
+ self._ascending_alert_list = self._ascending_alert_list[-900:] if len(self._ascending_alert_list) > 900 else self._ascending_alert_list
467
+
468
+ if total_detections > 0:
469
+ # Determine event level based on thresholds
470
+ level = "low"
471
+ intensity = 5.0
472
+ start_timestamp = self._get_start_timestamp_str(stream_info)
473
+ if start_timestamp and self.current_incident_end_timestamp=='N/A':
474
+ self.current_incident_end_timestamp = 'Incident still active'
475
+ elif start_timestamp and self.current_incident_end_timestamp=='Incident still active':
476
+ if len(self._ascending_alert_list) >= 15 and sum(self._ascending_alert_list[-15:]) / 15 < 1.5:
477
+ self.current_incident_end_timestamp = current_timestamp
478
+ elif self.current_incident_end_timestamp!='Incident still active' and self.current_incident_end_timestamp!='N/A':
479
+ self.current_incident_end_timestamp = 'N/A'
480
+
481
+ if config.alert_config and config.alert_config.count_thresholds:
482
+ threshold = config.alert_config.count_thresholds.get("all", 15)
483
+ intensity = min(10.0, (total_detections / threshold) * 10)
484
+
485
+ if intensity >= 9:
486
+ level = "critical"
487
+ self._ascending_alert_list.append(3)
488
+ elif intensity >= 7:
489
+ level = "significant"
490
+ self._ascending_alert_list.append(2)
491
+ elif intensity >= 5:
492
+ level = "medium"
493
+ self._ascending_alert_list.append(1)
494
+ else:
495
+ level = "low"
496
+ self._ascending_alert_list.append(0)
497
+ else:
498
+ if total_detections > 30:
499
+ level = "critical"
500
+ intensity = 10.0
501
+ self._ascending_alert_list.append(3)
502
+ elif total_detections > 25:
503
+ level = "significant"
504
+ intensity = 9.0
505
+ self._ascending_alert_list.append(2)
506
+ elif total_detections > 15:
507
+ level = "medium"
508
+ intensity = 7.0
509
+ self._ascending_alert_list.append(1)
510
+ else:
511
+ level = "low"
512
+ intensity = min(10.0, total_detections / 3.0)
513
+ self._ascending_alert_list.append(0)
514
+
515
+ # Generate human text in new format
516
+ human_text_lines = [f"INCIDENTS DETECTED @ {current_timestamp}:"]
517
+ human_text_lines.append(f"\tSeverity Level: {(self.CASE_TYPE,level)}")
518
+ human_text = "\n".join(human_text_lines)
519
+
520
+ alert_settings=[]
521
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
522
+ alert_settings.append({
523
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
524
+ "incident_category": self.CASE_TYPE,
525
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
526
+ "ascending": True,
527
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
528
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
529
+ }
530
+ })
531
+
532
+ event= self.create_incident(incident_id=self.CASE_TYPE+'_'+str(frame_number), incident_type=self.CASE_TYPE,
533
+ severity_level=level, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
534
+ start_time=start_timestamp, end_time=self.current_incident_end_timestamp,
535
+ level_settings= {"low": 1, "medium": 3, "significant":4, "critical": 7})
536
+ incidents.append(event)
537
+
538
+ else:
539
+ self._ascending_alert_list.append(0)
540
+ incidents.append({})
541
+
542
+ return incidents
543
+ def _generate_tracking_stats(
544
+ self,
545
+ counting_summary: Dict,
546
+ alerts: List,
547
+ config: SusActivityConfig,
548
+ frame_number: Optional[int] = None,
549
+ stream_info: Optional[Dict[str, Any]] = None
550
+ ) -> List[Dict]:
551
+ """Generate structured tracking stats matching eg.json format."""
552
+ camera_info = self.get_camera_info_from_stream(stream_info)
553
+
554
+ # frame_key = str(frame_number) if frame_number is not None else "current_frame"
555
+ # tracking_stats = [{frame_key: []}]
556
+ # frame_tracking_stats = tracking_stats[0][frame_key]
557
+ tracking_stats = []
558
+
559
+ total_detections = counting_summary.get("total_count", 0) #CURRENT total count of all classes
560
+ total_counts_dict = counting_summary.get("total_counts", {}) #TOTAL cumulative counts per class
561
+ cumulative_total = sum(total_counts_dict.values()) if total_counts_dict else 0 #TOTAL combined cumulative count
562
+ per_category_count = counting_summary.get("per_category_count", {}) #CURRENT count per class
563
+ sus_data = counting_summary.get("sus_data")
564
+
565
+ current_timestamp = self._get_current_timestamp_str(stream_info, precision=False)
566
+ start_timestamp = self._get_start_timestamp_str(stream_info, precision=False)
567
+
568
+ # Create high precision timestamps for input_timestamp and reset_timestamp
569
+ high_precision_start_timestamp = self._get_current_timestamp_str(stream_info, precision=True)
570
+ high_precision_reset_timestamp = self._get_start_timestamp_str(stream_info, precision=True)
571
+
572
+
573
+ # Build total_counts array in expected format
574
+ total_counts = []
575
+ for cat, count in total_counts_dict.items():
576
+ if count > 0:
577
+ total_counts.append({
578
+ "category": cat,
579
+ "count": count
580
+ })
581
+
582
+ # Build current_counts array in expected format
583
+ current_counts = []
584
+ for cat, count in per_category_count.items():
585
+ if count > 0 or total_detections > 0: # Include even if 0 when there are detections
586
+ current_counts.append({
587
+ "category": cat,
588
+ "count": count
589
+ })
590
+
591
+ # Prepare detections without confidence scores (as per eg.json)
592
+ detections = []
593
+ for detection in counting_summary.get("detections", []):
594
+ bbox = detection.get("bounding_box", {})
595
+ category = detection.get("category", "person")
596
+ # Include segmentation if available (like in eg.json)
597
+ if detection.get("masks"):
598
+ segmentation= detection.get("masks", [])
599
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
600
+ elif detection.get("segmentation"):
601
+ segmentation= detection.get("segmentation")
602
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
603
+ elif detection.get("mask"):
604
+ segmentation= detection.get("mask")
605
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
606
+ else:
607
+ detection_obj = self.create_detection_object(category, bbox)
608
+ detections.append(detection_obj)
609
+
610
+ # Build alert_settings array in expected format
611
+ alert_settings = []
612
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
613
+ alert_settings.append({
614
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
615
+ "incident_category": self.CASE_TYPE,
616
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
617
+ "ascending": True,
618
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
619
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
620
+ }
621
+ })
622
+
623
+ # Generate human_text in expected format
624
+ human_text_lines = [f"Tracking Statistics:"]
625
+ human_text_lines.append(f"CURRENT FRAME @ {current_timestamp}:")
626
+ if len(sus_data['curr_data']) > 0:
627
+ human_text_lines.append(f"\t Suspicious Activity Detected - {len(sus_data['curr_data'])}")
628
+ else:
629
+ human_text_lines.append(f"\t- No Suspicious Activity Detected")
630
+
631
+ human_text_lines.append(f"TOTAL SINCE {start_timestamp}")
632
+ human_text_lines.append(f"Total Suspicious Activity Detected:- {len(sus_data['sus_data'])}")
633
+ # for cat, count in total_counts_dict.items():
634
+ # if count > 0:
635
+ # human_text_lines.append(f"\t{cat}: {count}")
636
+
637
+ if alerts:
638
+ for alert in alerts:
639
+ human_text_lines.append(f"Alerts: {alert.get('settings', {})} sent @ {current_timestamp}")
640
+ else:
641
+ human_text_lines.append("Alerts: None")
642
+
643
+ human_text = "\n".join(human_text_lines)
644
+ reset_settings=[
645
+ {
646
+ "interval_type": "daily",
647
+ "reset_time": {
648
+ "value": 9,
649
+ "time_unit": "hour"
650
+ }
651
+ }
652
+ ]
653
+
654
+ tracking_stat=self.create_tracking_stats(total_counts=total_counts, current_counts=current_counts,
655
+ detections=detections, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
656
+ reset_settings=reset_settings, start_time=high_precision_start_timestamp ,
657
+ reset_time=high_precision_reset_timestamp)
658
+
659
+ tracking_stats.append(tracking_stat)
660
+ return tracking_stats
661
+
662
+ def _generate_business_analytics(self, counting_summary: Dict, alerts:Any, config: SusActivityConfig, stream_info: Optional[Dict[str, Any]] = None, is_empty=False) -> List[Dict]:
663
+ """Generate standardized business analytics for the agg_summary structure."""
664
+ if is_empty:
665
+ return []
666
+
667
+ #-----IF YOUR USECASE NEEDS BUSINESS ANALYTICS, YOU CAN USE THIS FUNCTION------#
668
+ #camera_info = self.get_camera_info_from_stream(stream_info)
669
+ # business_analytics = self.create_business_analytics(nalysis_name, statistics,
670
+ # human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
671
+ # reset_settings)
672
+ # return business_analytics
673
+
674
+ def _generate_summary(self, summary: dict, incidents: List, tracking_stats: List, business_analytics: List, alerts: List) -> List[str]:
675
+ """
676
+ Generate a human_text string for the tracking_stat, incident, business analytics and alerts.
677
+ """
678
+ lines = {}
679
+ lines["Application Name"] = self.CASE_TYPE
680
+ lines["Application Version"] = self.CASE_VERSION
681
+ if len(incidents) > 0:
682
+ lines["Incidents:"]=f"\n\t{incidents[0].get('human_text', 'No incidents detected')}\n"
683
+ if len(tracking_stats) > 0:
684
+ lines["Tracking Statistics:"]=f"\t{tracking_stats[0].get('human_text', 'No tracking statistics detected')}\n"
685
+ if len(business_analytics) > 0:
686
+ lines["Business Analytics:"]=f"\t{business_analytics[0].get('human_text', 'No business analytics detected')}\n"
687
+
688
+ if len(incidents) == 0 and len(tracking_stats) == 0 and len(business_analytics) == 0:
689
+ lines["Summary"] = "No Summary Data"
690
+
691
+ return [lines]
692
+
693
+ def _get_track_ids_info(self, detections: list) -> Dict[str, Any]:
694
+ """
695
+ Get detailed information about track IDs (per frame).
696
+ """
697
+ # Collect all track_ids in this frame
698
+ frame_track_ids = set()
699
+ for det in detections:
700
+ tid = det.get('track_id')
701
+ if tid is not None:
702
+ frame_track_ids.add(tid)
703
+ # Use persistent total set for unique counting
704
+ total_track_ids = set()
705
+ for s in getattr(self, '_per_category_total_track_ids', {}).values():
706
+ total_track_ids.update(s)
707
+ return {
708
+ "total_count": len(total_track_ids),
709
+ "current_frame_count": len(frame_track_ids),
710
+ "total_unique_track_ids": len(total_track_ids),
711
+ "current_frame_track_ids": list(frame_track_ids),
712
+ "last_update_time": time.time(),
713
+ "total_frames_processed": getattr(self, '_total_frame_counter', 0)
714
+ }
715
+
716
+ def _update_tracking_state(self, detections: list):
717
+ """
718
+ Track unique categories track_ids per category for total count after tracking.
719
+ Applies canonical ID merging to avoid duplicate counting when the underlying
720
+ tracker loses an object temporarily and assigns a new ID.
721
+ """
722
+ # Lazily initialise storage dicts
723
+ if not hasattr(self, "_per_category_total_track_ids"):
724
+ self._per_category_total_track_ids = {cat: set() for cat in self.target_categories}
725
+ self._current_frame_track_ids = {cat: set() for cat in self.target_categories}
726
+
727
+ for det in detections:
728
+ cat = det.get("category")
729
+ raw_track_id = det.get("track_id")
730
+ if cat not in self.target_categories or raw_track_id is None:
731
+ continue
732
+ bbox = det.get("bounding_box", det.get("bbox"))
733
+ canonical_id = self._merge_or_register_track(raw_track_id, bbox)
734
+ # Propagate canonical ID back to detection so downstream logic uses it
735
+ det["track_id"] = canonical_id
736
+
737
+ self._per_category_total_track_ids.setdefault(cat, set()).add(canonical_id)
738
+ self._current_frame_track_ids[cat].add(canonical_id)
739
+
740
+ def get_total_counts(self):
741
+ """
742
+ Return total unique track_id count for each category.
743
+ """
744
+ return {cat: len(ids) for cat, ids in getattr(self, '_per_category_total_track_ids', {}).items()}
745
+
746
+
747
+ def _format_timestamp(self, timestamp: Any) -> str:
748
+ """Format a timestamp so that exactly two digits follow the decimal point (milliseconds).
749
+
750
+ The input can be either:
751
+ 1. A numeric Unix timestamp (``float`` / ``int``) – it will first be converted to a
752
+ string in the format ``YYYY-MM-DD-HH:MM:SS.ffffff UTC``.
753
+ 2. A string already following the same layout.
754
+
755
+ The returned value preserves the overall format of the input but truncates or pads
756
+ the fractional seconds portion to **exactly two digits**.
757
+
758
+ Example
759
+ -------
760
+ >>> self._format_timestamp("2025-08-19-04:22:47.187574 UTC")
761
+ '2025-08-19-04:22:47.18 UTC'
762
+ """
763
+
764
+ # Convert numeric timestamps to the expected string representation first
765
+ if isinstance(timestamp, (int, float)):
766
+ timestamp = datetime.fromtimestamp(timestamp, timezone.utc).strftime(
767
+ '%Y-%m-%d-%H:%M:%S.%f UTC'
768
+ )
769
+
770
+ # Ensure we are working with a string from here on
771
+ if not isinstance(timestamp, str):
772
+ return str(timestamp)
773
+
774
+ # If there is no fractional component, simply return the original string
775
+ if '.' not in timestamp:
776
+ return timestamp
777
+
778
+ # Split out the main portion (up to the decimal point)
779
+ main_part, fractional_and_suffix = timestamp.split('.', 1)
780
+
781
+ # Separate fractional digits from the suffix (typically ' UTC')
782
+ if ' ' in fractional_and_suffix:
783
+ fractional_part, suffix = fractional_and_suffix.split(' ', 1)
784
+ suffix = ' ' + suffix # Re-attach the space removed by split
785
+ else:
786
+ fractional_part, suffix = fractional_and_suffix, ''
787
+
788
+ # Guarantee exactly two digits for the fractional part
789
+ fractional_part = (fractional_part + '00')[:2]
790
+
791
+ return f"{main_part}.{fractional_part}{suffix}"
792
+
793
+ def _format_timestamp_for_stream(self, timestamp: float) -> str:
794
+ """Format timestamp for streams (YYYY:MM:DD HH:MM:SS format)."""
795
+ dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
796
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
797
+
798
+ def _format_timestamp_for_video(self, timestamp: float) -> str:
799
+ """Format timestamp for video chunks (HH:MM:SS.ms format)."""
800
+ hours = int(timestamp // 3600)
801
+ minutes = int((timestamp % 3600) // 60)
802
+ seconds = round(float(timestamp % 60), 2)
803
+ return f"{hours:02d}:{minutes:02d}:{seconds:.1f}"
804
+
805
+ def _get_current_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False, frame_id: Optional[str]=None) -> str:
806
+ """Get formatted current timestamp based on stream type."""
807
+
808
+ if not stream_info:
809
+ return "00:00:00.00"
810
+ if precision:
811
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
812
+ if frame_id:
813
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
814
+ else:
815
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
816
+ stream_time_str = self._format_timestamp_for_video(start_time)
817
+
818
+ return self._format_timestamp(stream_info.get("input_settings", {}).get("stream_time", "NA"))
819
+ else:
820
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC")
821
+
822
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
823
+ if frame_id:
824
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
825
+ else:
826
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
827
+
828
+ stream_time_str = self._format_timestamp_for_video(start_time)
829
+
830
+
831
+ return self._format_timestamp(stream_info.get("input_settings", {}).get("stream_time", "NA"))
832
+ else:
833
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
834
+ if stream_time_str:
835
+ try:
836
+ timestamp_str = stream_time_str.replace(" UTC", "")
837
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
838
+ timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
839
+ return self._format_timestamp_for_stream(timestamp)
840
+ except:
841
+ return self._format_timestamp_for_stream(time.time())
842
+ else:
843
+ return self._format_timestamp_for_stream(time.time())
844
+
845
+ def _get_start_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False) -> str:
846
+ """Get formatted start timestamp for 'TOTAL SINCE' based on stream type."""
847
+ if not stream_info:
848
+ return "00:00:00"
849
+
850
+ if precision:
851
+ if self.start_timer is None:
852
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", "NA")
853
+ return self._format_timestamp(self.start_timer)
854
+ elif stream_info.get("input_settings", {}).get("start_frame", "na") == 1:
855
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", "NA")
856
+ return self._format_timestamp(self.start_timer)
857
+ else:
858
+ return self._format_timestamp(self.start_timer)
859
+
860
+ if self.start_timer is None:
861
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", "NA")
862
+ return self._format_timestamp(self.start_timer)
863
+ elif stream_info.get("input_settings", {}).get("start_frame", "na") == 1:
864
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", "NA")
865
+ return self._format_timestamp(self.start_timer)
866
+
867
+ else:
868
+ if self.start_timer is not None:
869
+ return self._format_timestamp(self.start_timer)
870
+
871
+ if self._tracking_start_time is None:
872
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
873
+ if stream_time_str:
874
+ try:
875
+ timestamp_str = stream_time_str.replace(" UTC", "")
876
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
877
+ self._tracking_start_time = dt.replace(tzinfo=timezone.utc).timestamp()
878
+ except:
879
+ self._tracking_start_time = time.time()
880
+ else:
881
+ self._tracking_start_time = time.time()
882
+
883
+ dt = datetime.fromtimestamp(self._tracking_start_time, tz=timezone.utc)
884
+ dt = dt.replace(minute=0, second=0, microsecond=0)
885
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
886
+
887
+ def _get_tracking_start_time(self) -> str:
888
+ """Get the tracking start time, formatted as a string."""
889
+ if self._tracking_start_time is None:
890
+ return "N/A"
891
+ return self._format_timestamp(self._tracking_start_time)
892
+
893
+ def _set_tracking_start_time(self) -> None:
894
+ """Set the tracking start time to the current time."""
895
+ self._tracking_start_time = time.time()
896
+
897
+
898
+ def _count_categories(self, detections: list, config: SusActivityConfig) -> dict:
899
+ """
900
+ Count the number of detections per category and return a summary dict.
901
+ The detections list is expected to have 'track_id' (from tracker), 'category', 'bounding_box', etc.
902
+ Output structure will include 'track_id' for each detection as per AdvancedTracker output.
903
+ """
904
+ counts = {}
905
+ for det in detections:
906
+ cat = det.get('category', 'unknown')
907
+ counts[cat] = counts.get(cat, 0) + 1
908
+ # Each detection dict will now include 'track_id' (and possibly 'frame_id')
909
+ return {
910
+ "total_count": sum(counts.values()),
911
+ "per_category_count": counts,
912
+ "detections": [
913
+ {
914
+ "bounding_box": det.get("bounding_box"),
915
+ "category": det.get("category"),
916
+ "confidence": det.get("confidence"),
917
+ "track_id": det.get("track_id"),
918
+ "frame_id": det.get("frame_id")
919
+ }
920
+ for det in detections
921
+ ]
922
+ }
923
+
924
+ def _extract_predictions(self, detections: list) -> List[Dict[str, Any]]:
925
+ """
926
+ Extract prediction details for output (category, confidence, bounding box).
927
+ """
928
+ return [
929
+ {
930
+ "category": det.get("category", "unknown"),
931
+ "confidence": det.get("confidence", 0.0),
932
+ "bounding_box": det.get("bounding_box", {})
933
+ }
934
+ for det in detections
935
+ ]
936
+
937
+ # ------------------------------------------------------------------ #
938
+ # Canonical ID helpers #
939
+ # ------------------------------------------------------------------ #
940
+ def _compute_iou(self, box1: Any, box2: Any) -> float:
941
+ """Compute IoU between two bounding boxes which may be dicts or lists.
942
+ Falls back to 0 when insufficient data is available."""
943
+
944
+ # Helper to convert bbox (dict or list) to [x1, y1, x2, y2]
945
+ def _bbox_to_list(bbox):
946
+ if bbox is None:
947
+ return []
948
+ if isinstance(bbox, list):
949
+ return bbox[:4] if len(bbox) >= 4 else []
950
+ if isinstance(bbox, dict):
951
+ if "xmin" in bbox:
952
+ return [bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]]
953
+ if "x1" in bbox:
954
+ return [bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"]]
955
+ # Fallback: first four numeric values
956
+ values = [v for v in bbox.values() if isinstance(v, (int, float))]
957
+ return values[:4] if len(values) >= 4 else []
958
+ return []
959
+
960
+ l1 = _bbox_to_list(box1)
961
+ l2 = _bbox_to_list(box2)
962
+ if len(l1) < 4 or len(l2) < 4:
963
+ return 0.0
964
+ x1_min, y1_min, x1_max, y1_max = l1
965
+ x2_min, y2_min, x2_max, y2_max = l2
966
+
967
+ # Ensure correct order
968
+ x1_min, x1_max = min(x1_min, x1_max), max(x1_min, x1_max)
969
+ y1_min, y1_max = min(y1_min, y1_max), max(y1_min, y1_max)
970
+ x2_min, x2_max = min(x2_min, x2_max), max(x2_min, x2_max)
971
+ y2_min, y2_max = min(y2_min, y2_max), max(y2_min, y2_max)
972
+
973
+ inter_x_min = max(x1_min, x2_min)
974
+ inter_y_min = max(y1_min, y2_min)
975
+ inter_x_max = min(x1_max, x2_max)
976
+ inter_y_max = min(y1_max, y2_max)
977
+
978
+ inter_w = max(0.0, inter_x_max - inter_x_min)
979
+ inter_h = max(0.0, inter_y_max - inter_y_min)
980
+ inter_area = inter_w * inter_h
981
+
982
+ area1 = (x1_max - x1_min) * (y1_max - y1_min)
983
+ area2 = (x2_max - x2_min) * (y2_max - y2_min)
984
+ union_area = area1 + area2 - inter_area
985
+
986
+ return (inter_area / union_area) if union_area > 0 else 0.0
987
+
988
+ def _merge_or_register_track(self, raw_id: Any, bbox: Any) -> Any:
989
+ """Return a stable canonical ID for a raw tracker ID, merging fragmented
990
+ tracks when IoU and temporal constraints indicate they represent the
991
+ same physical."""
992
+ if raw_id is None or bbox is None:
993
+ # Nothing to merge
994
+ return raw_id
995
+
996
+ now = time.time()
997
+
998
+ # Fast path – raw_id already mapped
999
+ if raw_id in self._track_aliases:
1000
+ canonical_id = self._track_aliases[raw_id]
1001
+ track_info = self._canonical_tracks.get(canonical_id)
1002
+ if track_info is not None:
1003
+ track_info["last_bbox"] = bbox
1004
+ track_info["last_update"] = now
1005
+ track_info["raw_ids"].add(raw_id)
1006
+ return canonical_id
1007
+
1008
+ # Attempt to merge with an existing canonical track
1009
+ for canonical_id, info in self._canonical_tracks.items():
1010
+ # Only consider recently updated tracks
1011
+ if now - info["last_update"] > self._track_merge_time_window:
1012
+ continue
1013
+ iou = self._compute_iou(bbox, info["last_bbox"])
1014
+ if iou >= self._track_merge_iou_threshold:
1015
+ # Merge
1016
+ self._track_aliases[raw_id] = canonical_id
1017
+ info["last_bbox"] = bbox
1018
+ info["last_update"] = now
1019
+ info["raw_ids"].add(raw_id)
1020
+ return canonical_id
1021
+
1022
+ # No match – register new canonical track
1023
+ canonical_id = raw_id
1024
+ self._track_aliases[raw_id] = canonical_id
1025
+ self._canonical_tracks[canonical_id] = {
1026
+ "last_bbox": bbox,
1027
+ "last_update": now,
1028
+ "raw_ids": {raw_id},
1029
+ }
1030
+ return canonical_id