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,1683 @@
1
+ """
2
+ People counting use case implementation.
3
+
4
+ This module provides a clean implementation of people counting functionality
5
+ with zone-based analysis, tracking, and alerting capabilities.
6
+ """
7
+
8
+ from typing import Any, Dict, List, Optional, Set
9
+ from dataclasses import asdict
10
+ import time
11
+ from datetime import datetime, timezone
12
+
13
+ from ..core.base import BaseProcessor, ProcessingContext, ProcessingResult, ConfigProtocol, ResultFormat
14
+ from ..core.config import PeopleCountingConfig, ZoneConfig, AlertConfig
15
+ from ..utils import (
16
+ filter_by_confidence,
17
+ filter_by_categories,
18
+ apply_category_mapping,
19
+ count_objects_by_category,
20
+ count_objects_in_zones,
21
+ calculate_counting_summary,
22
+ match_results_structure,
23
+ bbox_smoothing,
24
+ BBoxSmoothingConfig,
25
+ BBoxSmoothingTracker,
26
+ calculate_iou
27
+ )
28
+ from ..utils.geometry_utils import get_bbox_center, point_in_polygon, get_bbox_bottom25_center
29
+
30
+
31
+ class PeopleCountingUseCase(BaseProcessor):
32
+ """People counting use case with zone analysis and alerting."""
33
+
34
+ def __init__(self):
35
+ """Initialize people counting use case."""
36
+ super().__init__("people_counting")
37
+ self.category = "general"
38
+ self.CASE_TYPE: Optional[str] = 'People_Counting'
39
+ self.CASE_VERSION: Optional[str] = '1.3'
40
+
41
+ # Track ID storage for total count calculation
42
+ self._total_track_ids = set() # Store all unique track IDs seen across calls
43
+ self._current_frame_track_ids = set() # Store track IDs from current frame
44
+ self._total_count = 0 # Cached total count
45
+ self._last_update_time = time.time() # Track when last updated
46
+
47
+ # Zone-based tracking storage
48
+ self._zone_current_track_ids = {} # zone_name -> set of current track IDs in zone
49
+ self._zone_total_track_ids = {} # zone_name -> set of all track IDs that have been in zone
50
+ self._zone_current_counts = {} # zone_name -> current count in zone
51
+ self._zone_total_counts = {} # zone_name -> total count that have been in zone
52
+
53
+ # Frame counter for tracking total frames processed
54
+ self._total_frame_counter = 0 # Total frames processed across all calls
55
+
56
+ # Global frame offset for video chunk processing
57
+ self._global_frame_offset = 0 # Offset to add to local frame IDs for global frame numbering
58
+ self._frames_in_current_chunk = 0 # Number of frames in current chunk
59
+
60
+ # Initialize smoothing tracker
61
+ self.smoothing_tracker = None
62
+
63
+ # Track start time for "TOTAL SINCE" calculation
64
+ self._tracking_start_time = None
65
+
66
+ # --------------------------------------------------------------------- #
67
+ # Tracking aliasing structures to merge fragmented IDs #
68
+ # --------------------------------------------------------------------- #
69
+ # Maps raw tracker IDs generated by ByteTrack to a stable canonical ID
70
+ # that represents a real-world person. This helps avoid double counting
71
+ # when the tracker loses a target temporarily and assigns a new ID.
72
+ self._track_aliases: Dict[Any, Any] = {}
73
+
74
+ # Stores metadata about each canonical track such as its last seen
75
+ # bounding box, last update timestamp and all raw IDs that have been
76
+ # merged into it.
77
+ self._canonical_tracks: Dict[Any, Dict[str, Any]] = {}
78
+
79
+ # IoU threshold above which two bounding boxes are considered the same
80
+ # person for alias merging. Tuned for people (robust CCTV scenarios).
81
+ # Using a moderate IoU to handle jitter and perspective changes.
82
+ self._track_merge_iou_threshold: float = 0.3
83
+
84
+ # Merge window in seconds (people typically move slowly; shorter window
85
+ # reduces accidental merges across cuts).
86
+ self._track_merge_time_window: float = 3.0
87
+
88
+ self._ascending_alert_list: List[int] = []
89
+ self.current_incident_end_timestamp: str = "N/A"
90
+
91
+ self.start_timer = None
92
+
93
+ # Maintain last frame presence for consecutive confirmation logic
94
+ self._last_frame_track_ids: Set[Any] = set()
95
+
96
+ # Advanced tracking for single-frame detections
97
+ self.tracker = None
98
+ self._min_confirm_frames: int = 3 # require 3 consecutive frames before counting as unique
99
+ self._consecutive_track_frames: Dict[Any, int] = {}
100
+
101
+
102
+ def process(self, data: Any, config: ConfigProtocol,
103
+ context: Optional[ProcessingContext] = None, stream_info: Optional[Any] = None) -> ProcessingResult:
104
+ """
105
+ Process people counting use case - automatically detects single or multi-frame structure.
106
+
107
+ Args:
108
+ data: Raw model output (detection or tracking format)
109
+ config: People counting configuration
110
+ context: Processing context
111
+ stream_info: Stream information containing frame details (optional)
112
+
113
+ Returns:
114
+ ProcessingResult: Processing result with standardized agg_summary structure
115
+ """
116
+ start_time = time.time()
117
+
118
+ try:
119
+ # Ensure we have the right config type
120
+ if not isinstance(config, PeopleCountingConfig):
121
+ return self.create_error_result(
122
+ "Invalid configuration type for people counting",
123
+ usecase=self.name,
124
+ category=self.category,
125
+ context=context
126
+ )
127
+
128
+ # Initialize processing context if not provided
129
+ if context is None:
130
+ context = ProcessingContext()
131
+
132
+ # Detect input format and frame structure
133
+ input_format = match_results_structure(data)
134
+ context.input_format = input_format
135
+ context.confidence_threshold = config.confidence_threshold
136
+
137
+ is_multi_frame = self.detect_frame_structure(data)
138
+
139
+ # Apply smoothing if enabled
140
+ if config.enable_smoothing and input_format == ResultFormat.OBJECT_TRACKING:
141
+ data = self._apply_smoothing(data, config)
142
+
143
+ # Process based on frame structure
144
+ if is_multi_frame:
145
+
146
+ return self._process_multi_frame(data, config, context, stream_info)
147
+ else:
148
+ return self._process_single_frame(data, config, context, stream_info)
149
+
150
+ except Exception as e:
151
+ self.logger.error(f"People counting failed: {str(e)}", exc_info=True)
152
+
153
+ if context:
154
+ context.mark_completed()
155
+
156
+ return self.create_error_result(
157
+ str(e),
158
+ type(e).__name__,
159
+ usecase=self.name,
160
+ category=self.category,
161
+ context=context
162
+ )
163
+
164
+ def _process_multi_frame(self, data: Dict, config: PeopleCountingConfig, context: ProcessingContext, stream_info: Optional[Dict[str, Any]] = None) -> ProcessingResult:
165
+ """Process multi-frame data to generate frame-wise agg_summary."""
166
+
167
+ frame_incidents = {}
168
+ frame_tracking_stats = {}
169
+ frame_business_analytics = {}
170
+ frame_human_text = {}
171
+ frame_alerts = {}
172
+
173
+ # Increment total frame counter
174
+ frames_in_this_call = len(data)
175
+ self._total_frame_counter += frames_in_this_call
176
+
177
+ # Process each frame individually
178
+ for frame_key, frame_detections in data.items():
179
+ # Extract frame ID from tracking data
180
+ frame_id = self._extract_frame_id_from_tracking(frame_detections, frame_key)
181
+ global_frame_id = self.get_global_frame_id(frame_id)
182
+
183
+ # Process this single frame's detections
184
+ alerts, incidents_list, tracking_stats_list, business_analytics_list, summary_list = self._process_frame_detections(
185
+ frame_detections, config, global_frame_id, stream_info
186
+ )
187
+ incidents = incidents_list[0] if incidents_list else {}
188
+ tracking_stats = tracking_stats_list[0] if tracking_stats_list else {}
189
+ business_analytics = business_analytics_list[0] if business_analytics_list else {}
190
+ summary = summary_list[0] if summary_list else {}
191
+
192
+ # Store frame-wise results
193
+ if incidents:
194
+ frame_incidents[global_frame_id] = incidents
195
+ if tracking_stats:
196
+ frame_tracking_stats[global_frame_id] = tracking_stats
197
+ if business_analytics:
198
+ frame_business_analytics[global_frame_id] = business_analytics
199
+ if summary:
200
+ frame_human_text[global_frame_id] = summary
201
+ if alerts:
202
+ frame_alerts[global_frame_id] = alerts
203
+
204
+ # Update global frame offset after processing this chunk
205
+ self.update_global_frame_offset(frames_in_this_call)
206
+
207
+ # Create frame-wise agg_summary
208
+ agg_summary = self.create_frame_wise_agg_summary(
209
+ frame_incidents, frame_tracking_stats, frame_business_analytics, frame_alerts,
210
+ frame_human_text=frame_human_text
211
+ )
212
+
213
+ # Mark processing as completed
214
+ context.mark_completed()
215
+
216
+ # Create result with standardized agg_summary
217
+ return self.create_result(
218
+ data={"agg_summary": agg_summary},
219
+ usecase=self.name,
220
+ category=self.category,
221
+ context=context
222
+ )
223
+
224
+ def _process_single_frame(self, data: Any, config: PeopleCountingConfig, context: ProcessingContext, stream_info: Optional[Dict[str, Any]] = None) -> ProcessingResult:
225
+ """Process single frame data and return standardized agg_summary."""
226
+
227
+ current_frame = stream_info.get("input_settings", {}).get("start_frame", "current_frame")
228
+ # Process frame data
229
+ alerts, incidents_list, tracking_stats_list, business_analytics_list, summary_list = self._process_frame_detections(
230
+ data, config, current_frame, stream_info
231
+ )
232
+ incidents = incidents_list[0] if incidents_list else {}
233
+ tracking_stats = tracking_stats_list[0] if tracking_stats_list else {}
234
+ business_analytics = business_analytics_list[0] if business_analytics_list else {}
235
+ summary = summary_list[0] if summary_list else {}
236
+
237
+ # Create single-frame agg_summary
238
+ # agg_summary = self.create_agg_summary(
239
+ # current_frame, incidents, tracking_stats, business_analytics, alerts, human_text=summary
240
+ # )
241
+ agg_summary = {str(current_frame): {
242
+ "incidents": incidents,
243
+ "tracking_stats": tracking_stats,
244
+ "business_analytics": business_analytics,
245
+ "alerts": alerts,
246
+ "human_text": summary
247
+ }}
248
+
249
+ context.mark_completed()
250
+ result = self.create_result(
251
+ data={"agg_summary": agg_summary},
252
+ usecase=self.name,
253
+ category=self.category,
254
+ context=context
255
+ )
256
+
257
+ return result
258
+
259
+
260
+ def _process_frame_detections(self, frame_data: Any, config: PeopleCountingConfig, frame_id: str, stream_info: Optional[Dict[str, Any]] = None) -> tuple:
261
+ """Process detections from a single frame and return standardized components."""
262
+
263
+ # Convert frame_data to list if it's not already
264
+ if isinstance(frame_data, list):
265
+ frame_detections = frame_data
266
+ else:
267
+ # Handle other formats as needed
268
+ frame_detections = []
269
+
270
+ # Step 1: Apply confidence filtering to this frame
271
+ if config.confidence_threshold is not None:
272
+ frame_detections = [d for d in frame_detections if d.get("confidence", 0) >= config.confidence_threshold]
273
+
274
+ # Step 2: Apply category mapping if provided
275
+ if config.index_to_category:
276
+ frame_detections = apply_category_mapping(frame_detections, config.index_to_category)
277
+
278
+ # Step 3: Filter to person categories
279
+ if config.person_categories:
280
+ frame_detections = [d for d in frame_detections if d.get("category") in config.person_categories]
281
+ if config.target_categories:
282
+ frame_detections = [d for d in frame_detections if d.get('category') in config.target_categories]
283
+ self.logger.debug("Applied category filtering")
284
+
285
+ # Step 4: Track single-frame detections using AdvancedTracker to obtain stable track_ids
286
+ # Always apply when tracking is enabled in single-frame path
287
+ needs_tracking = bool(config.enable_tracking)
288
+ if self.tracker is None and needs_tracking:
289
+ try:
290
+ from ..advanced_tracker import AdvancedTracker
291
+ from ..advanced_tracker.config import TrackerConfig
292
+ # Configure tracker thresholds suitable for people
293
+ fps = 30
294
+ try:
295
+ fps = int(stream_info.get("input_settings", {}).get("original_fps", 30)) if stream_info else 30
296
+ if fps <= 0:
297
+ fps = 30
298
+ except Exception:
299
+ fps = 30
300
+ tracker_config = TrackerConfig(
301
+ track_high_thresh=0.4,
302
+ track_low_thresh=0.05,
303
+ new_track_thresh=0.3,
304
+ match_thresh=0.8,
305
+ track_buffer=int(3 * fps),
306
+ max_time_lost=int(3 * fps),
307
+ frame_rate=fps,
308
+ )
309
+ # Keep defaults for confidence thresholds; AdvancedTracker handles activation
310
+ self.tracker = AdvancedTracker(tracker_config)
311
+ self.logger.info("Initialized AdvancedTracker for People Counting (single-frame)")
312
+ except Exception as e:
313
+ self.logger.warning(f"AdvancedTracker init failed, falling back to IoU aliasing: {e}")
314
+
315
+ tracked_detections = frame_detections
316
+ if self.tracker is not None and needs_tracking:
317
+ try:
318
+ tracked_detections = self.tracker.update(frame_detections)
319
+ except Exception as e:
320
+ self.logger.warning(f"AdvancedTracker update failed, using raw detections: {e}")
321
+ tracked_detections = frame_detections
322
+
323
+ # Step 4: Create counting summary for this frame
324
+ counting_summary = {
325
+ "total_objects": len(tracked_detections),
326
+ "detections": tracked_detections,
327
+ "categories": {}
328
+ }
329
+
330
+ # Count by category
331
+ for detection in tracked_detections:
332
+ category = detection.get("category", "unknown")
333
+ counting_summary["categories"][category] = counting_summary["categories"].get(category, 0) + 1
334
+
335
+ # Step 4.5: Always update tracking state BEFORE zone enhancements so detections have track_ids
336
+ self._update_tracking_state(counting_summary)
337
+
338
+ # Step 5: Zone analysis for this frame
339
+ zone_analysis = {}
340
+ if config.zone_config and config.zone_config.zones:
341
+ # Convert single frame to format expected by count_objects_in_zones
342
+ frame_data = frame_detections #[frame_detections]
343
+ zone_analysis = count_objects_in_zones(frame_data, config.zone_config.zones)
344
+
345
+ # Update zone tracking with current frame data (now detections have canonical track_ids)
346
+ if zone_analysis and config.enable_tracking:
347
+ enhanced_zone_analysis = self._update_zone_tracking(zone_analysis, frame_detections, config)
348
+ # Merge enhanced zone analysis with original zone analysis
349
+ for zone_name, enhanced_data in enhanced_zone_analysis.items():
350
+ zone_analysis[zone_name] = enhanced_data
351
+
352
+ # Step 5: Generate insights and alerts for this frame
353
+ alerts = self._check_alerts(counting_summary, zone_analysis, config, frame_id)
354
+
355
+ # Step 6: Generate summary and standardized agg_summary components for this frame
356
+ incidents = self._generate_incidents(counting_summary, zone_analysis, alerts, config, frame_id, stream_info)
357
+ incidents = []
358
+ tracking_stats = self._generate_tracking_stats(counting_summary, zone_analysis, config, frame_id=frame_id, alerts=alerts, stream_info=stream_info)
359
+ business_analytics = self._generate_business_analytics(counting_summary, zone_analysis, config, frame_id, stream_info, is_empty=True)
360
+ summary = self._generate_summary(counting_summary, incidents, tracking_stats, business_analytics, alerts)
361
+
362
+ # Return standardized components as tuple
363
+ return alerts, incidents, tracking_stats, business_analytics, summary
364
+
365
+ def _generate_incidents(self, counting_summary: Dict, zone_analysis: Dict, alerts: List, config: PeopleCountingConfig, frame_id: str, stream_info: Optional[Dict[str, Any]] = None) -> List[Dict]:
366
+ """Generate standardized incidents for the agg_summary structure."""
367
+
368
+ camera_info = self.get_camera_info_from_stream(stream_info)
369
+ incidents = []
370
+ total_people = counting_summary.get("total_objects", 0)
371
+ current_timestamp = self._get_current_timestamp_str(stream_info, frame_id=frame_id)
372
+ self._ascending_alert_list = self._ascending_alert_list[-900:] if len(self._ascending_alert_list) > 900 else self._ascending_alert_list
373
+
374
+ alert_settings=[]
375
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
376
+ alert_settings.append({
377
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
378
+ "incident_category": self.CASE_TYPE,
379
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
380
+ "ascending": True,
381
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
382
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
383
+ }
384
+ })
385
+
386
+ if total_people > 0:
387
+ # Determine event level based on thresholds
388
+
389
+ level = "info"
390
+ intensity = 5.0
391
+ start_timestamp = self._get_start_timestamp_str(stream_info)
392
+ if start_timestamp and self.current_incident_end_timestamp=='N/A':
393
+ self.current_incident_end_timestamp = 'Incident still active'
394
+ elif start_timestamp and self.current_incident_end_timestamp=='Incident still active':
395
+ if len(self._ascending_alert_list) >= 15 and sum(self._ascending_alert_list[-15:]) / 15 < 1.5:
396
+ self.current_incident_end_timestamp = current_timestamp
397
+ elif self.current_incident_end_timestamp!='Incident still active' and self.current_incident_end_timestamp!='N/A':
398
+ self.current_incident_end_timestamp = 'N/A'
399
+
400
+ if config.alert_config and hasattr(config.alert_config, 'count_thresholds') and config.alert_config.count_thresholds:
401
+ threshold = config.alert_config.count_thresholds.get("all", 10)
402
+ intensity = min(10.0, (total_people / threshold) * 10)
403
+
404
+ if intensity >= 9:
405
+ level = "critical"
406
+ self._ascending_alert_list.append(3)
407
+ elif intensity >= 7:
408
+ level = "significant"
409
+ self._ascending_alert_list.append(2)
410
+ elif intensity >= 5:
411
+ level = "medium"
412
+ self._ascending_alert_list.append(1)
413
+ else:
414
+ level = "low"
415
+ self._ascending_alert_list.append(0)
416
+ else:
417
+ if total_people > 30:
418
+ level = "critical"
419
+ intensity = 10.0
420
+ self._ascending_alert_list.append(3)
421
+ elif total_people > 25:
422
+ level = "significant"
423
+ intensity = 9.0
424
+ self._ascending_alert_list.append(2)
425
+ elif total_people > 15:
426
+ level = "medium"
427
+ intensity = 7.0
428
+ self._ascending_alert_list.append(1)
429
+ else:
430
+ level = "low"
431
+ intensity = min(10.0, total_people / 3.0)
432
+ self._ascending_alert_list.append(0)
433
+
434
+ # Generate human text in new format
435
+ human_text_lines = [f"INCIDENTS DETECTED @ {current_timestamp}:"]
436
+ human_text_lines.append(f"\tSeverity Level: {(self.CASE_TYPE,level)}")
437
+ human_text = "\n".join(human_text_lines)
438
+
439
+ # Main people counting incident
440
+ event= self.create_incident(incident_id=self.CASE_TYPE+'_'+str(frame_id), incident_type=self.CASE_TYPE,
441
+ severity_level=level, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
442
+ start_time=start_timestamp, end_time=self.current_incident_end_timestamp,
443
+ level_settings= {"low": 1, "medium": 3, "significant":4, "critical": 7})
444
+ incidents.append(event)
445
+ else:
446
+ self._ascending_alert_list.append(0)
447
+ incidents.append({})
448
+
449
+ # Add zone-specific events if applicable
450
+ if zone_analysis:
451
+ human_text_lines.append(f"\t- ZONE EVENTS:")
452
+ for zone_name, zone_count in zone_analysis.items():
453
+ zone_current = zone_count.get("current_count", 0)
454
+ if zone_current > 0:
455
+ zone_intensity = min(10.0, zone_current / 5.0)
456
+ zone_level = "info"
457
+ if zone_intensity >= 9:
458
+ zone_level = "critical"
459
+ self._ascending_alert_list.append(3)
460
+ elif zone_intensity >= 7:
461
+ zone_level = "significant"
462
+ self._ascending_alert_list.append(2)
463
+ elif zone_intensity >= 5:
464
+ zone_level = "medium"
465
+ self._ascending_alert_list.append(1)
466
+ else:
467
+ zone_level = "low"
468
+ self._ascending_alert_list.append(0)
469
+
470
+ if zone_current > 0:
471
+ human_text_lines.append(f"\t\t- Zone name: {zone_name}")
472
+ human_text_lines.append(f"\t\t\t- Current people in zone: {zone_current}")
473
+ # Main people counting incident
474
+ event= self.create_incident(incident_id=self.CASE_TYPE+'_'+'zone_'+zone_name+str(frame_id), incident_type=self.CASE_TYPE,
475
+ severity_level=zone_level, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
476
+ start_time=start_timestamp, end_time=self.current_incident_end_timestamp,
477
+ level_settings= {"low": 1, "medium": 3, "significant":4, "critical": 7})
478
+ incidents.append(event)
479
+ return incidents
480
+
481
+ def _generate_tracking_stats(self, counting_summary: Dict, zone_analysis: Dict, config: PeopleCountingConfig, frame_id: str, alerts: Any=[], stream_info: Optional[Dict[str, Any]] = None) -> List[Dict]:
482
+ """Generate tracking stats using standardized methods."""
483
+
484
+ total_people = counting_summary.get("total_objects", 0)
485
+
486
+ # Get total count from cached tracking state
487
+ total_unique_count = self.get_total_count()
488
+ current_frame_count = self.get_current_frame_count()
489
+
490
+ # Get camera info using standardized method
491
+ camera_info = self.get_camera_info_from_stream(stream_info)
492
+
493
+ # Build total_counts using standardized method
494
+ total_counts = []
495
+ per_category_total = {}
496
+
497
+ for category in config.person_categories or ["person"]:
498
+ # Always use scene-wide unique count
499
+ category_total_count = total_unique_count
500
+
501
+ if category_total_count > 0:
502
+ total_counts.append(self.create_count_object(category, category_total_count))
503
+ per_category_total[category] = category_total_count
504
+
505
+ # Build current_counts using standardized method
506
+ current_counts = []
507
+ per_category_current = {}
508
+
509
+ for category in config.person_categories or ["person"]:
510
+ # Always use scene-wide current count
511
+ category_current_count = current_frame_count
512
+
513
+ if category_current_count > 0 or total_people > 0: # Include even if 0 when there are people
514
+ current_counts.append(self.create_count_object(category, category_current_count))
515
+ per_category_current[category] = category_current_count
516
+
517
+ # Prepare detections using standardized method (without confidence and track_id)
518
+ detections = []
519
+ for detection in counting_summary.get("detections", []):
520
+ bbox = detection.get("bounding_box", {})
521
+ category = detection.get("category", "person")
522
+ # Include segmentation if available (like in eg.json)
523
+ if detection.get("masks"):
524
+ segmentation= detection.get("masks", [])
525
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
526
+ elif detection.get("segmentation"):
527
+ segmentation= detection.get("segmentation")
528
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
529
+ elif detection.get("mask"):
530
+ segmentation= detection.get("mask")
531
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
532
+ else:
533
+ detection_obj = self.create_detection_object(category, bbox)
534
+ detections.append(detection_obj)
535
+
536
+ # Build alerts and alert_settings arrays
537
+ alert_settings = []
538
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
539
+ alert_settings.append({
540
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
541
+ "incident_category": self.CASE_TYPE,
542
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
543
+ "ascending": True,
544
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
545
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
546
+ }
547
+ })
548
+ if zone_analysis:
549
+ human_text_lines=[]
550
+ current_timestamp = self._get_current_timestamp_str(stream_info, frame_id=frame_id)
551
+ start_timestamp = self._get_start_timestamp_str(stream_info)
552
+ human_text_lines.append(f"CURRENT FRAME @ {current_timestamp}:")
553
+ human_text_lines.append(f"\t- People Detected: {total_people}")
554
+ for zone_name, zone_data in zone_analysis.items():
555
+ zone_current = zone_data.get("current_count", 0)
556
+ human_text_lines.append(f"\t- {zone_name}: {zone_current}")
557
+ human_text_lines.append("")
558
+ human_text_lines.append(f"TOTAL SINCE @ {start_timestamp}:")
559
+
560
+ for zone_name, zone_data in zone_analysis.items():
561
+ zone_total = zone_data.get("total_count", 0)
562
+ human_text_lines.append(f"\t- {zone_name}: {zone_total}")
563
+
564
+ if total_unique_count > 0:
565
+ human_text_lines.append(f"\t- Total unique people in the scene: {total_unique_count}")
566
+ if alerts:
567
+ for alert in alerts:
568
+ human_text_lines.append(f"Alerts: {alert.get('settings', {})} sent @ {current_timestamp}")
569
+ else:
570
+ human_text_lines.append("Alerts: None")
571
+ human_text = "\n".join(human_text_lines)
572
+ else:
573
+ human_text = self._generate_human_text_for_tracking(total_people, total_unique_count, config, frame_id, alerts, stream_info)
574
+
575
+ # Create high precision timestamps for input_timestamp and reset_timestamp
576
+ high_precision_start_timestamp = self._get_current_timestamp_str(stream_info, precision=True, frame_id=frame_id)
577
+ high_precision_reset_timestamp = self._get_start_timestamp_str(stream_info, precision=True)
578
+ # Create tracking_stat using standardized method
579
+ tracking_stat = self.create_tracking_stats(
580
+ total_counts, current_counts, detections, human_text, camera_info, alerts, alert_settings, start_time=high_precision_start_timestamp, reset_time=high_precision_reset_timestamp
581
+ )
582
+
583
+ return [tracking_stat]
584
+
585
+ def _generate_human_text_for_tracking(self, total_people: int, total_unique_count: int, config: PeopleCountingConfig, frame_id: str, alerts:Any=[], stream_info: Optional[Dict[str, Any]] = None) -> str:
586
+ """Generate human-readable text for tracking stats in old format."""
587
+ from datetime import datetime, timezone
588
+
589
+ human_text_lines=[]
590
+ current_timestamp = self._get_current_timestamp_str(stream_info, precision=True, frame_id=frame_id)
591
+ start_timestamp = self._get_start_timestamp_str(stream_info, precision=True)
592
+
593
+ human_text_lines.append(f"CURRENT FRAME @ {current_timestamp}:")
594
+ human_text_lines.append(f"\t- People Detected: {total_people}")
595
+
596
+ human_text_lines.append("")
597
+ #if total_unique_count > 0:
598
+ human_text_lines.append(f"TOTAL SINCE @ {start_timestamp}:")
599
+ human_text_lines.append(f"\t- Total unique people count: {total_unique_count}")
600
+
601
+ print('------------------HUMANNTEXTTT-------------------------')
602
+ print(human_text_lines)
603
+ print('------------------HUMANNTEXTTT-------------------------')
604
+
605
+ if alerts:
606
+ for alert in alerts:
607
+ human_text_lines.append(f"Alerts: {alert.get('settings', {})} sent @ {current_timestamp}")
608
+ else:
609
+ human_text_lines.append("Alerts: None")
610
+
611
+ return "\n".join(human_text_lines)
612
+
613
+ def _check_alerts(self, counting_summary: Dict, zone_analysis: Dict,
614
+ config: PeopleCountingConfig, frame_id: str) -> List[Dict]:
615
+ """Check for alert conditions and generate alerts."""
616
+ def get_trend(data, lookback=900, threshold=0.6):
617
+ '''
618
+ Determine if the trend is ascending or descending based on actual value progression.
619
+ Now works with values 0,1,2,3 (not just binary).
620
+ '''
621
+ window = data[-lookback:] if len(data) >= lookback else data
622
+ if len(window) < 2:
623
+ return True # not enough data to determine trend
624
+ increasing = 0
625
+ total = 0
626
+ for i in range(1, len(window)):
627
+ if window[i] >= window[i - 1]:
628
+ increasing += 1
629
+ total += 1
630
+ ratio = increasing / total
631
+ if ratio >= threshold:
632
+ return True
633
+ elif ratio <= (1 - threshold):
634
+ return False
635
+ alerts = []
636
+
637
+ if not config.alert_config:
638
+ return alerts
639
+
640
+ total_people = counting_summary.get("total_objects", 0)
641
+
642
+ # Count threshold alerts
643
+ if hasattr(config.alert_config, 'count_thresholds') and config.alert_config.count_thresholds:
644
+
645
+ for category, threshold in config.alert_config.count_thresholds.items():
646
+ if category == "all" and total_people >= threshold:
647
+
648
+ alerts.append({
649
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
650
+ "alert_id": "alert_"+category+'_'+frame_id,
651
+ "incident_category": self.CASE_TYPE,
652
+ "threshold_level": threshold,
653
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
654
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
655
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
656
+ }
657
+ })
658
+ elif category in counting_summary.get("by_category", {}):
659
+ count = counting_summary["by_category"][category]
660
+
661
+ if count >= threshold:
662
+ alerts.append({
663
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
664
+ "alert_id": "alert_"+category+'_'+frame_id,
665
+ "incident_category": self.CASE_TYPE,
666
+ "threshold_level": threshold,
667
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
668
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
669
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
670
+ }
671
+ })
672
+ else:
673
+ pass
674
+
675
+ # Zone occupancy threshold alerts
676
+ if hasattr(config.alert_config, 'occupancy_thresholds') and config.alert_config.occupancy_thresholds:
677
+ for zone_name, threshold in config.alert_config.occupancy_thresholds.items():
678
+ if zone_name in zone_analysis:
679
+ # Calculate zone_count robustly (supports int, list, dict values)
680
+ print('ZONEEE',zone_name, zone_analysis[zone_name])
681
+ zone_count = zone_analysis[zone_name].get("current_count", 0)
682
+ if zone_count >= threshold:
683
+ alerts.append({
684
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
685
+ "alert_id": f"alert_zone_{zone_name}_{frame_id}",
686
+ "incident_category": f"{self.CASE_TYPE}_{zone_name}",
687
+ "threshold_level": threshold,
688
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
689
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
690
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
691
+ }
692
+ })
693
+
694
+ return alerts
695
+
696
+ def _generate_business_analytics(self, counting_summary: Dict, zone_analysis: Dict, config: PeopleCountingConfig, frame_id: str, stream_info: Optional[Dict[str, Any]] = None, is_empty=False) -> List[Dict]:
697
+ """Generate standardized business analytics for the agg_summary structure."""
698
+ if is_empty:
699
+ return []
700
+ business_analytics = []
701
+
702
+ total_people = counting_summary.get("total_objects", 0)
703
+
704
+ # Get camera info using standardized method
705
+ camera_info = self.get_camera_info_from_stream(stream_info)
706
+
707
+ if total_people > 0 or config.enable_analytics:
708
+ # Calculate analytics statistics
709
+ analytics_stats = {
710
+ "people_count": total_people,
711
+ "unique_people_count": self.get_total_count(),
712
+ "current_frame_count": self.get_current_frame_count()
713
+ }
714
+
715
+ # Add zone analytics if available
716
+ if zone_analysis:
717
+ zone_stats = {}
718
+ for zone_name, zone_count in zone_analysis.items():
719
+ zone_total = zone_count.get("current_count", 0)
720
+ zone_stats[f"{zone_name}_occupancy"] = zone_total
721
+ analytics_stats.update(zone_stats)
722
+
723
+ # Generate human text for analytics
724
+ current_timestamp = self._get_current_timestamp_str(stream_info, frame_id=frame_id)
725
+ start_timestamp = self._get_start_timestamp_str(stream_info)
726
+
727
+ analytics_human_text = self.generate_analytics_human_text(
728
+ "people_counting_analytics", analytics_stats, current_timestamp, start_timestamp
729
+ )
730
+
731
+ # Create business analytics using standardized method
732
+ analytics = self.create_business_analytics(
733
+ "people_counting_analytics", analytics_stats, analytics_human_text, camera_info
734
+ )
735
+ business_analytics.append(analytics)
736
+
737
+ return business_analytics
738
+
739
+ def _generate_summary(self, summary: dict, incidents: List, tracking_stats: List, business_analytics: List, alerts: List) -> List[str]:
740
+ """
741
+ Generate a human_text string for the tracking_stat, incident, business analytics and alerts.
742
+ """
743
+ lines = []
744
+ lines.append("Application Name: "+self.CASE_TYPE)
745
+ lines.append("Application Version: "+self.CASE_VERSION)
746
+ if len(incidents) > 0:
747
+ lines.append("Incidents: "+f"\n\t{incidents[0].get('human_text', 'No incidents detected')}")
748
+ if len(tracking_stats) > 0:
749
+ lines.append("Tracking Statistics: "+f"\t{tracking_stats[0].get('human_text', 'No tracking statistics detected')}")
750
+ if len(business_analytics) > 0:
751
+ lines.append("Business Analytics: "+f"\t{business_analytics[0].get('human_text', 'No business analytics detected')}")
752
+
753
+ if len(incidents) == 0 and len(tracking_stats) == 0 and len(business_analytics) == 0:
754
+ lines.append("Summary: "+"No Summary Data")
755
+
756
+ return ["\n".join(lines)]
757
+
758
+ def _calculate_metrics(self, counting_summary: Dict, zone_analysis: Dict,
759
+ config: PeopleCountingConfig, context: ProcessingContext) -> Dict[str, Any]:
760
+ """Calculate detailed metrics for analytics."""
761
+ total_people = counting_summary.get("total_objects", 0)
762
+
763
+ metrics = {
764
+ "total_people": total_people,
765
+ "processing_time": context.processing_time or 0.0,
766
+ "input_format": context.input_format.value,
767
+ "confidence_threshold": config.confidence_threshold,
768
+ "zones_analyzed": len(zone_analysis),
769
+ "detection_rate": 0.0,
770
+ "coverage_percentage": 0.0
771
+ }
772
+
773
+ # Calculate detection rate
774
+ if config.time_window_minutes and config.time_window_minutes > 0:
775
+ metrics["detection_rate"] = (total_people / config.time_window_minutes) * 60
776
+
777
+ # Calculate zone coverage
778
+ if zone_analysis and total_people > 0:
779
+ people_in_zones = 0
780
+ for zone_counts in zone_analysis.values():
781
+ if isinstance(zone_counts, dict):
782
+ for v in zone_counts.values():
783
+ if isinstance(v, int):
784
+ people_in_zones += v
785
+ elif isinstance(v, list):
786
+ people_in_zones += len(v)
787
+ elif isinstance(zone_counts, list):
788
+ people_in_zones += len(zone_counts)
789
+ elif isinstance(zone_counts, int):
790
+ people_in_zones += zone_counts
791
+ metrics["coverage_percentage"] = (people_in_zones / total_people) * 100
792
+
793
+ # Unique tracking metrics
794
+ if config.enable_unique_counting:
795
+ unique_count = self._count_unique_tracks(counting_summary, config)
796
+ if unique_count is not None:
797
+ metrics["unique_people"] = unique_count
798
+ metrics["tracking_efficiency"] = (unique_count / total_people) * 100 if total_people > 0 else 0
799
+
800
+ # Per-zone metrics
801
+ if zone_analysis:
802
+ zone_metrics = {}
803
+ for zone_name, zone_counts in zone_analysis.items():
804
+ # Robustly sum counts, handling dicts with int or list values
805
+ if isinstance(zone_counts, dict):
806
+ zone_total = 0
807
+ for v in zone_counts.values():
808
+ if isinstance(v, int):
809
+ zone_total += v
810
+ elif isinstance(v, list):
811
+ zone_total += len(v)
812
+ elif isinstance(zone_counts, list):
813
+ zone_total = len(zone_counts)
814
+ elif isinstance(zone_counts, int):
815
+ zone_total = zone_counts
816
+ else:
817
+ zone_total = 0
818
+ zone_metrics[zone_name] = {
819
+ "count": zone_total,
820
+ "percentage": (zone_total / total_people) * 100 if total_people > 0 else 0
821
+ }
822
+ metrics["zone_metrics"] = zone_metrics
823
+
824
+ return metrics
825
+
826
+ def _extract_predictions(self, data: Any) -> List[Dict[str, Any]]:
827
+ """Extract predictions from processed data for API compatibility."""
828
+ predictions = []
829
+
830
+ try:
831
+ if isinstance(data, list):
832
+ # Detection format
833
+ for item in data:
834
+ prediction = self._normalize_prediction(item)
835
+ if prediction:
836
+ predictions.append(prediction)
837
+
838
+ elif isinstance(data, dict):
839
+ # Frame-based or tracking format
840
+ for frame_id, items in data.items():
841
+ if isinstance(items, list):
842
+ for item in items:
843
+ prediction = self._normalize_prediction(item)
844
+ if prediction:
845
+ prediction["frame_id"] = frame_id
846
+ predictions.append(prediction)
847
+
848
+ except Exception as e:
849
+ self.logger.warning(f"Failed to extract predictions: {str(e)}")
850
+
851
+ return predictions
852
+
853
+ def _normalize_prediction(self, item: Dict[str, Any]) -> Dict[str, Any]:
854
+ """Normalize a single prediction item."""
855
+ if not isinstance(item, dict):
856
+ return {}
857
+
858
+ return {
859
+ "category": item.get("category", item.get("class", "unknown")),
860
+ "confidence": item.get("confidence", item.get("score", 0.0)),
861
+ "bounding_box": item.get("bounding_box", item.get("bbox", {})),
862
+ "track_id": item.get("track_id")
863
+ }
864
+
865
+ def _get_detections_with_confidence(self, counting_summary: Dict) -> List[Dict]:
866
+ """Extract detection items with confidence scores."""
867
+ return counting_summary.get("detections", [])
868
+
869
+ def _count_unique_tracks(self, counting_summary: Dict, config: PeopleCountingConfig = None) -> Optional[int]:
870
+ """Count unique tracks if tracking is enabled."""
871
+ # Always update tracking state regardless of enable_unique_counting setting
872
+ self._update_tracking_state(counting_summary)
873
+
874
+ # Only return the count if unique counting is enabled
875
+ if config and config.enable_unique_counting:
876
+ return self._total_count if self._total_count > 0 else None
877
+ else:
878
+ return None
879
+
880
+ def _update_tracking_state(self, counting_summary: Dict) -> None:
881
+ """Update tracking state with current frame data with 3-frame confirmation.
882
+
883
+ Behavior:
884
+ - Prefer tracker-provided track_id when available (from AdvancedTracker).
885
+ - Otherwise use IoU-based canonical aliasing with tight person-specific thresholds.
886
+ - Only add a canonical_id to cumulative total after it appears in 3 consecutive frames.
887
+ - Cumulative totals never decrease.
888
+ """
889
+ detections = self._get_detections_with_confidence(counting_summary)
890
+
891
+ if not detections:
892
+ # If no detections this frame, decay consecutive counters softly rather than clearing,
893
+ # so brief detector dropouts don't reset confirmation progress.
894
+ for tid in list(self._consecutive_track_frames.keys()):
895
+ self._consecutive_track_frames[tid] = max(0, self._consecutive_track_frames[tid] - 1)
896
+ self._current_frame_track_ids = set()
897
+ self._last_update_time = time.time()
898
+ return
899
+
900
+ current_frame_tracks: Set[Any] = set()
901
+
902
+ ephemeral_seq = 0
903
+ for detection in detections:
904
+ raw_track_id = detection.get("track_id")
905
+ bbox = detection.get("bounding_box", detection.get("bbox"))
906
+ if not bbox:
907
+ continue
908
+
909
+ # If no tracker id yet, generate ephemeral then alias-merge by IoU
910
+ if raw_track_id is None:
911
+ raw_track_id = self._generate_ephemeral_track_id(bbox, ephemeral_seq)
912
+ ephemeral_seq += 1
913
+
914
+ canonical_id = self._merge_or_register_track(raw_track_id, bbox)
915
+ detection["track_id"] = canonical_id
916
+ current_frame_tracks.add(canonical_id)
917
+
918
+ # Update consecutive presence counts for confirmation
919
+ updated_consecutive: Dict[Any, int] = {}
920
+ for tid in current_frame_tracks:
921
+ prev = self._consecutive_track_frames.get(tid, 0)
922
+ updated_consecutive[tid] = min(self._min_confirm_frames, prev + 1)
923
+ # carry over decayed counts for those not seen this frame (bounded by 0)
924
+ for tid, prev in self._consecutive_track_frames.items():
925
+ if tid not in updated_consecutive:
926
+ updated_consecutive[tid] = max(0, prev - 1)
927
+ self._consecutive_track_frames = updated_consecutive
928
+
929
+ # Promote confirmed tracks to cumulative unique set
930
+ for tid, count in self._consecutive_track_frames.items():
931
+ if count >= self._min_confirm_frames:
932
+ if tid not in self._total_track_ids:
933
+ self._total_track_ids.add(tid)
934
+
935
+ # Overwrite current-frame set
936
+ self._current_frame_track_ids = current_frame_tracks
937
+ self._last_update_time = time.time()
938
+
939
+ # Cumulative total never decreases
940
+ self._total_count = len(self._total_track_ids)
941
+
942
+ def _generate_ephemeral_track_id(self, bbox: Any, seq: int) -> str:
943
+ """Create a short-lived raw track id for detections without a track_id.
944
+
945
+ Combines a coarse hash of the bbox geometry with a per-call sequence and
946
+ a millisecond timestamp, so the same person across adjacent frames will
947
+ still be merged to the same canonical track via IoU and time window,
948
+ while avoiding long-lived ID collisions across distant calls.
949
+ """
950
+ try:
951
+ # Normalize bbox to xyxy list for hashing
952
+ if isinstance(bbox, dict):
953
+ if "x1" in bbox:
954
+ xyxy = [bbox.get("x1"), bbox.get("y1"), bbox.get("x2"), bbox.get("y2")]
955
+ elif "xmin" in bbox:
956
+ xyxy = [bbox.get("xmin"), bbox.get("ymin"), bbox.get("xmax"), bbox.get("ymax")]
957
+ else:
958
+ values = list(bbox.values())
959
+ xyxy = values[:4] if len(values) >= 4 else []
960
+ elif isinstance(bbox, list):
961
+ xyxy = bbox[:4]
962
+ else:
963
+ xyxy = []
964
+
965
+ if len(xyxy) < 4:
966
+ xyxy = [0, 0, 0, 0]
967
+
968
+ x1, y1, x2, y2 = xyxy
969
+ # Coarse-quantize geometry to stabilize hash across minor jitter
970
+ cx = int(round((float(x1) + float(x2)) / 2.0))
971
+ cy = int(round((float(y1) + float(y2)) / 2.0))
972
+ w = int(round(abs(float(x2) - float(x1))))
973
+ h = int(round(abs(float(y2) - float(y1))))
974
+ geom_token = f"{cx}_{cy}_{w}_{h}"
975
+ except Exception:
976
+ geom_token = "0_0_0_0"
977
+
978
+ ms = int(time.time() * 1000)
979
+ return f"tmp_{ms}_{seq}_{abs(hash(geom_token)) % 1000003}"
980
+
981
+ def get_total_count(self) -> int:
982
+ """Get the total count of unique people tracked across all calls."""
983
+ return self._total_count
984
+
985
+ def get_current_frame_count(self) -> int:
986
+ """Get the count of people in the current frame."""
987
+ return len(self._current_frame_track_ids)
988
+
989
+ def get_total_frames_processed(self) -> int:
990
+ """Get the total number of frames processed across all calls."""
991
+ return self._total_frame_counter
992
+
993
+ def set_global_frame_offset(self, offset: int) -> None:
994
+ """Set the global frame offset for video chunk processing."""
995
+ self._global_frame_offset = offset
996
+ self.logger.info(f"Global frame offset set to: {offset}")
997
+
998
+ def get_global_frame_offset(self) -> int:
999
+ """Get the current global frame offset."""
1000
+ return self._global_frame_offset
1001
+
1002
+ def update_global_frame_offset(self, frames_in_chunk: int) -> None:
1003
+ """Update global frame offset after processing a chunk."""
1004
+ old_offset = self._global_frame_offset
1005
+ self._global_frame_offset += frames_in_chunk
1006
+ self.logger.info(f"Global frame offset updated: {old_offset} -> {self._global_frame_offset} (added {frames_in_chunk} frames)")
1007
+
1008
+ def get_global_frame_id(self, local_frame_id: str) -> str:
1009
+ """Convert local frame ID to global frame ID."""
1010
+ try:
1011
+ # Try to convert local_frame_id to integer
1012
+ local_frame_num = int(local_frame_id)
1013
+ global_frame_num = local_frame_num #+ self._global_frame_offset
1014
+ return str(global_frame_num)
1015
+ except (ValueError, TypeError):
1016
+ # If local_frame_id is not a number (e.g., timestamp), return as is
1017
+ return local_frame_id
1018
+
1019
+ def get_track_ids_info(self) -> Dict[str, Any]:
1020
+ """Get detailed information about track IDs."""
1021
+ return {
1022
+ "total_count": self._total_count,
1023
+ "current_frame_count": len(self._current_frame_track_ids),
1024
+ "total_unique_track_ids": len(self._total_track_ids),
1025
+ "current_frame_track_ids": list(self._current_frame_track_ids),
1026
+ "last_update_time": self._last_update_time,
1027
+ "total_frames_processed": self._total_frame_counter
1028
+ }
1029
+
1030
+ def get_tracking_debug_info(self) -> Dict[str, Any]:
1031
+ """Get detailed debugging information about tracking state."""
1032
+ return {
1033
+ "total_track_ids": list(self._total_track_ids),
1034
+ "current_frame_track_ids": list(self._current_frame_track_ids),
1035
+ "total_count": self._total_count,
1036
+ "current_frame_count": len(self._current_frame_track_ids),
1037
+ "total_frames_processed": self._total_frame_counter,
1038
+ "last_update_time": self._last_update_time,
1039
+ "zone_current_track_ids": {zone: list(tracks) for zone, tracks in self._zone_current_track_ids.items()},
1040
+ "zone_total_track_ids": {zone: list(tracks) for zone, tracks in self._zone_total_track_ids.items()},
1041
+ "zone_current_counts": self._zone_current_counts.copy(),
1042
+ "zone_total_counts": self._zone_total_counts.copy(),
1043
+ "global_frame_offset": self._global_frame_offset,
1044
+ "frames_in_current_chunk": self._frames_in_current_chunk
1045
+ }
1046
+
1047
+ def get_frame_info(self) -> Dict[str, Any]:
1048
+ """Get detailed information about frame processing and global frame offset."""
1049
+ return {
1050
+ "global_frame_offset": self._global_frame_offset,
1051
+ "total_frames_processed": self._total_frame_counter,
1052
+ "frames_in_current_chunk": self._frames_in_current_chunk,
1053
+ "next_global_frame": self._global_frame_offset + self._frames_in_current_chunk
1054
+ }
1055
+
1056
+ def reset_tracking_state(self) -> None:
1057
+ """
1058
+ WARNING: This completely resets ALL tracking data including cumulative totals!
1059
+
1060
+ This should ONLY be used when:
1061
+ - Starting a completely new tracking session
1062
+ - Switching to a different video/stream
1063
+ - Manual reset requested by user
1064
+
1065
+ For clearing expired/stale tracks, use clear_current_frame_tracking() instead.
1066
+ """
1067
+ self._total_track_ids.clear()
1068
+ self._current_frame_track_ids.clear()
1069
+ self._total_count = 0
1070
+ self._last_update_time = time.time()
1071
+
1072
+ # Clear zone tracking data
1073
+ self._zone_current_track_ids.clear()
1074
+ self._zone_total_track_ids.clear()
1075
+ self._zone_current_counts.clear()
1076
+ self._zone_total_counts.clear()
1077
+
1078
+ # Reset frame counter and global frame offset
1079
+ self._total_frame_counter = 0
1080
+ self._global_frame_offset = 0
1081
+ self._frames_in_current_chunk = 0
1082
+
1083
+ # Clear aliasing information
1084
+ self._canonical_tracks.clear()
1085
+ self._track_aliases.clear()
1086
+ self._tracking_start_time = None
1087
+
1088
+ self.logger.warning(" FULL tracking state reset - all track IDs, zone data, frame counter, and global frame offset cleared. Cumulative totals lost!")
1089
+
1090
+ def clear_current_frame_tracking(self) -> int:
1091
+ """
1092
+ MANUAL USE ONLY: Clear only current frame tracking data while preserving cumulative totals.
1093
+
1094
+ This method is NOT called automatically anywhere in the code.
1095
+
1096
+ This is the SAFE method to use for manual clearing of stale/expired current frame data.
1097
+ The cumulative total (self._total_count) is always preserved.
1098
+
1099
+ In streaming scenarios, you typically don't need to call this at all.
1100
+
1101
+ Returns:
1102
+ Number of current frame tracks cleared
1103
+ """
1104
+ old_current_count = len(self._current_frame_track_ids)
1105
+ self._current_frame_track_ids.clear()
1106
+
1107
+ # Clear current zone tracking (but keep total zone tracking)
1108
+ cleared_zone_tracks = 0
1109
+ for zone_name in list(self._zone_current_track_ids.keys()):
1110
+ cleared_zone_tracks += len(self._zone_current_track_ids[zone_name])
1111
+ self._zone_current_track_ids[zone_name].clear()
1112
+ self._zone_current_counts[zone_name] = 0
1113
+
1114
+ # Update timestamp
1115
+ self._last_update_time = time.time()
1116
+
1117
+ self.logger.info(f"Cleared {old_current_count} current frame tracks and {cleared_zone_tracks} zone current tracks. Cumulative total preserved: {self._total_count}")
1118
+ return old_current_count
1119
+
1120
+ def reset_frame_counter(self) -> None:
1121
+ """Reset only the frame counter."""
1122
+ old_count = self._total_frame_counter
1123
+ self._total_frame_counter = 0
1124
+ self.logger.info(f"Frame counter reset from {old_count} to 0")
1125
+
1126
+ def clear_expired_tracks(self, max_age_seconds: float = 300.0) -> int:
1127
+ """
1128
+ MANUAL USE ONLY: Clear current frame tracking data if no updates for a while.
1129
+
1130
+ This method is NOT called automatically anywhere in the code.
1131
+ It's provided as a utility function for manual cleanup if needed.
1132
+
1133
+ In streaming scenarios, you typically don't need to call this at all.
1134
+ The cumulative total should keep growing as new unique people are detected.
1135
+
1136
+ This method only clears current frame tracking data while preserving
1137
+ the cumulative total count. The cumulative total should never decrease.
1138
+
1139
+ Args:
1140
+ max_age_seconds: Maximum age in seconds before clearing current frame tracks
1141
+
1142
+ Returns:
1143
+ Number of current frame tracks cleared
1144
+ """
1145
+ current_time = time.time()
1146
+ if current_time - self._last_update_time > max_age_seconds:
1147
+ # Use the safe method that preserves cumulative totals
1148
+ cleared_count = self.clear_current_frame_tracking()
1149
+ self.logger.info(f"Manual cleanup: cleared {cleared_count} expired current frame tracks (age > {max_age_seconds}s)")
1150
+ return cleared_count
1151
+ return 0
1152
+
1153
+ def _update_zone_tracking(self, zone_analysis: Dict[str, Dict[str, int]], detections: List[Dict], config: PeopleCountingConfig) -> Dict[str, Dict[str, Any]]:
1154
+ """
1155
+ Update zone tracking with current frame data.
1156
+
1157
+ Args:
1158
+ zone_analysis: Current zone analysis results
1159
+ detections: List of detections with track IDs
1160
+ config: People counting configuration with zone polygons
1161
+
1162
+ Returns:
1163
+ Enhanced zone analysis with tracking information
1164
+ """
1165
+ if not zone_analysis or not config.zone_config or not config.zone_config.zones:
1166
+ return {}
1167
+
1168
+ enhanced_zone_analysis = {}
1169
+ zones = config.zone_config.zones
1170
+
1171
+ # Get current frame track IDs in each zone
1172
+ current_frame_zone_tracks = {}
1173
+
1174
+ # Initialize zone tracking for all zones
1175
+ for zone_name in zones.keys():
1176
+ current_frame_zone_tracks[zone_name] = set()
1177
+ if zone_name not in self._zone_current_track_ids:
1178
+ self._zone_current_track_ids[zone_name] = set()
1179
+ if zone_name not in self._zone_total_track_ids:
1180
+ self._zone_total_track_ids[zone_name] = set()
1181
+
1182
+ # Check each detection against each zone
1183
+ for detection in detections:
1184
+ track_id = detection.get("track_id")
1185
+ if track_id is None:
1186
+ continue
1187
+
1188
+ # Get detection bbox
1189
+ bbox = detection.get("bounding_box", detection.get("bbox"))
1190
+ if not bbox:
1191
+ continue
1192
+
1193
+ # Get detection center point
1194
+ center_point = get_bbox_bottom25_center(bbox) #get_bbox_center(bbox)
1195
+
1196
+ # Check which zone this detection is in using actual zone polygons
1197
+ for zone_name, zone_polygon in zones.items():
1198
+ # Convert polygon points to tuples for point_in_polygon function
1199
+ # zone_polygon format: [[x1, y1], [x2, y2], [x3, y3], ...]
1200
+ polygon_points = [(point[0], point[1]) for point in zone_polygon]
1201
+
1202
+ # Check if detection center is inside the zone polygon using ray casting algorithm
1203
+ if point_in_polygon(center_point, polygon_points):
1204
+ current_frame_zone_tracks[zone_name].add(track_id)
1205
+
1206
+ # Update zone tracking for each zone
1207
+ for zone_name, zone_counts in zone_analysis.items():
1208
+ # Get current frame tracks for this zone
1209
+ current_tracks = current_frame_zone_tracks.get(zone_name, set())
1210
+
1211
+ # Update current zone tracks
1212
+ self._zone_current_track_ids[zone_name] = current_tracks
1213
+
1214
+ # Update total zone tracks (accumulate all track IDs that have been in this zone)
1215
+ self._zone_total_track_ids[zone_name].update(current_tracks)
1216
+
1217
+ # Update counts
1218
+ self._zone_current_counts[zone_name] = len(current_tracks)
1219
+ self._zone_total_counts[zone_name] = len(self._zone_total_track_ids[zone_name])
1220
+
1221
+ # Create enhanced zone analysis
1222
+ enhanced_zone_analysis[zone_name] = {
1223
+ "current_count": self._zone_current_counts[zone_name],
1224
+ "total_count": self._zone_total_counts[zone_name],
1225
+ "current_track_ids": list(current_tracks),
1226
+ "total_track_ids": list(self._zone_total_track_ids[zone_name]),
1227
+ "original_counts": zone_counts # Preserve original zone counts
1228
+ }
1229
+
1230
+ return enhanced_zone_analysis
1231
+
1232
+ def get_zone_tracking_info(self) -> Dict[str, Dict[str, Any]]:
1233
+ """Get detailed zone tracking information."""
1234
+ return {
1235
+ zone_name: {
1236
+ "current_count": self._zone_current_counts.get(zone_name, 0),
1237
+ "total_count": self._zone_total_counts.get(zone_name, 0),
1238
+ "current_track_ids": list(self._zone_current_track_ids.get(zone_name, set())),
1239
+ "total_track_ids": list(self._zone_total_track_ids.get(zone_name, set()))
1240
+ }
1241
+ for zone_name in set(self._zone_current_counts.keys()) | set(self._zone_total_counts.keys())
1242
+ }
1243
+
1244
+ def get_zone_current_count(self, zone_name: str) -> int:
1245
+ """Get current count of people in a specific zone."""
1246
+ return self._zone_current_counts.get(zone_name, 0)
1247
+
1248
+ def get_zone_total_count(self, zone_name: str) -> int:
1249
+ """Get total count of people who have been in a specific zone."""
1250
+ return self._zone_total_counts.get(zone_name, 0)
1251
+
1252
+ def get_all_zone_counts(self) -> Dict[str, Dict[str, int]]:
1253
+ """Get current and total counts for all zones."""
1254
+ return {
1255
+ zone_name: {
1256
+ "current": self._zone_current_counts.get(zone_name, 0),
1257
+ "total": self._zone_total_counts.get(zone_name, 0)
1258
+ }
1259
+ for zone_name in set(self._zone_current_counts.keys()) | set(self._zone_total_counts.keys())
1260
+ }
1261
+
1262
+ def _format_timestamp_for_stream(self, timestamp: float) -> str:
1263
+ """Format timestamp for streams (YYYY:MM:DD HH:MM:SS format)."""
1264
+ dt = datetime.fromtimestamp(float(timestamp), tz=timezone.utc)
1265
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
1266
+
1267
+ def _format_timestamp_for_video(self, timestamp: float) -> str:
1268
+ """Format timestamp for video chunks (HH:MM:SS.ms format)."""
1269
+ hours = int(timestamp // 3600)
1270
+ minutes = int((timestamp % 3600) // 60)
1271
+ seconds = round(float(timestamp % 60),2)
1272
+ return f"{hours:02d}:{minutes:02d}:{seconds:.1f}"
1273
+
1274
+ def _get_current_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False, frame_id: Optional[str]=None) -> str:
1275
+ """Get formatted current timestamp based on stream type."""
1276
+
1277
+ if not stream_info:
1278
+ return "00:00:00.00"
1279
+ if precision:
1280
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
1281
+ if frame_id:
1282
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
1283
+ else:
1284
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
1285
+ stream_time_str = self._format_timestamp_for_video(start_time)
1286
+
1287
+
1288
+ return self._format_timestamp(stream_info.get("input_settings", {}).get("stream_time", "NA"))
1289
+ else:
1290
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC")
1291
+
1292
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
1293
+ if frame_id:
1294
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
1295
+ else:
1296
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
1297
+
1298
+ stream_time_str = self._format_timestamp_for_video(start_time)
1299
+
1300
+ return self._format_timestamp(stream_info.get("input_settings", {}).get("stream_time", "NA"))
1301
+ else:
1302
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
1303
+ if stream_time_str:
1304
+ try:
1305
+ timestamp_str = stream_time_str.replace(" UTC", "")
1306
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
1307
+ timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
1308
+ return self._format_timestamp_for_stream(timestamp)
1309
+ except:
1310
+ return self._format_timestamp_for_stream(time.time())
1311
+ else:
1312
+ return self._format_timestamp_for_stream(time.time())
1313
+
1314
+ def _get_start_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False) -> str:
1315
+ """Get formatted start timestamp for 'TOTAL SINCE' based on stream type."""
1316
+ if not stream_info:
1317
+ return "00:00:00"
1318
+
1319
+ if precision:
1320
+ if self.start_timer is None:
1321
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC"))
1322
+ return self._format_timestamp(self.start_timer)
1323
+ elif stream_info.get("input_settings", {}).get("start_frame", "na") == 1:
1324
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC"))
1325
+ return self._format_timestamp(self.start_timer)
1326
+ else:
1327
+ return self._format_timestamp(self.start_timer)
1328
+
1329
+ if self.start_timer is None:
1330
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC"))
1331
+ return self._format_timestamp(self.start_timer)
1332
+ elif stream_info.get("input_settings", {}).get("start_frame", "na") == 1:
1333
+ self.start_timer = stream_info.get("input_settings", {}).get("stream_time", datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC"))
1334
+ return self._format_timestamp(self.start_timer)
1335
+
1336
+ else:
1337
+ if self.start_timer is not None:
1338
+ return self._format_timestamp(self.start_timer)
1339
+
1340
+ if self._tracking_start_time is None:
1341
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
1342
+ if stream_time_str:
1343
+ try:
1344
+ timestamp_str = stream_time_str.replace(" UTC", "")
1345
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
1346
+ self._tracking_start_time = dt.replace(tzinfo=timezone.utc).timestamp()
1347
+ except:
1348
+ self._tracking_start_time = time.time()
1349
+ else:
1350
+ self._tracking_start_time = time.time()
1351
+
1352
+ dt = datetime.fromtimestamp(self._tracking_start_time, tz=timezone.utc)
1353
+ dt = dt.replace(minute=0, second=0, microsecond=0)
1354
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
1355
+
1356
+ def _extract_frame_id_from_tracking(self, frame_detections: List[Dict], frame_key: str) -> str:
1357
+ """Extract frame ID from tracking data."""
1358
+ # Priority 1: Check if detections have frame information
1359
+ if frame_detections and len(frame_detections) > 0:
1360
+ first_detection = frame_detections[0]
1361
+ if "frame" in first_detection:
1362
+ return str(first_detection["frame"])
1363
+ elif "frame_id" in first_detection:
1364
+ return str(first_detection["frame_id"])
1365
+ # Priority 2: Use frame_key from input data
1366
+ return str(frame_key)
1367
+
1368
+ def _robust_zone_total(self, zone_count):
1369
+ """Helper method to robustly calculate zone total."""
1370
+ if isinstance(zone_count, dict):
1371
+ total = 0
1372
+ for v in zone_count.values():
1373
+ if isinstance(v, int):
1374
+ total += v
1375
+ elif isinstance(v, list):
1376
+ total += len(v)
1377
+ return total
1378
+ elif isinstance(zone_count, list):
1379
+ return len(zone_count)
1380
+ elif isinstance(zone_count, int):
1381
+ return zone_count
1382
+ else:
1383
+ return 0
1384
+
1385
+ # --------------------------------------------------------------------- #
1386
+ # Private helpers for canonical track aliasing #
1387
+ # --------------------------------------------------------------------- #
1388
+
1389
+ def _compute_iou(self, box1: Any, box2: Any) -> float:
1390
+ """Compute IoU between two bounding boxes that may be either list or dict.
1391
+ Falls back to geometry_utils.calculate_iou when both boxes are dicts.
1392
+ """
1393
+ # Handle dict format directly with calculate_iou (supports many keys)
1394
+ if isinstance(box1, dict) and isinstance(box2, dict):
1395
+ return calculate_iou(box1, box2)
1396
+
1397
+ # Helper to convert bbox (dict or list) to a list [x1,y1,x2,y2]
1398
+ def _bbox_to_list(bbox):
1399
+ if bbox is None:
1400
+ return []
1401
+ if isinstance(bbox, list):
1402
+ return bbox[:4] if len(bbox) >= 4 else []
1403
+ if isinstance(bbox, dict):
1404
+ if "xmin" in bbox:
1405
+ return [bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]]
1406
+ if "x1" in bbox:
1407
+ return [bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"]]
1408
+ # Fallback: take first four values in insertion order
1409
+ values = list(bbox.values())
1410
+ return values[:4] if len(values) >= 4 else []
1411
+ # Unsupported type
1412
+ return []
1413
+
1414
+ list1 = _bbox_to_list(box1)
1415
+ list2 = _bbox_to_list(box2)
1416
+
1417
+ if len(list1) < 4 or len(list2) < 4:
1418
+ return 0.0
1419
+
1420
+ x1_min, y1_min, x1_max, y1_max = list1
1421
+ x2_min, y2_min, x2_max, y2_max = list2
1422
+
1423
+ # Ensure correct ordering of coordinates
1424
+ x1_min, x1_max = min(x1_min, x1_max), max(x1_min, x1_max)
1425
+ y1_min, y1_max = min(y1_min, y1_max), max(y1_min, y1_max)
1426
+ x2_min, x2_max = min(x2_min, x2_max), max(x2_min, x2_max)
1427
+ y2_min, y2_max = min(y2_min, y2_max), max(y2_min, y2_max)
1428
+
1429
+ inter_x_min = max(x1_min, x2_min)
1430
+ inter_y_min = max(y1_min, y2_min)
1431
+ inter_x_max = min(x1_max, x2_max)
1432
+ inter_y_max = min(y1_max, y2_max)
1433
+
1434
+ inter_w = max(0.0, inter_x_max - inter_x_min)
1435
+ inter_h = max(0.0, inter_y_max - inter_y_min)
1436
+ inter_area = inter_w * inter_h
1437
+
1438
+ area1 = (x1_max - x1_min) * (y1_max - y1_min)
1439
+ area2 = (x2_max - x2_min) * (y2_max - y2_min)
1440
+ union_area = area1 + area2 - inter_area
1441
+
1442
+ return (inter_area / union_area) if union_area > 0 else 0.0
1443
+
1444
+ def _get_canonical_id(self, raw_id: Any) -> Any:
1445
+ """Return the canonical ID for a raw tracker-generated ID."""
1446
+ return self._track_aliases.get(raw_id, raw_id)
1447
+
1448
+ def _merge_or_register_track(self, raw_id: Any, bbox: List[float]) -> Any:
1449
+ """Merge the raw track into an existing canonical track if possible,
1450
+ otherwise register it as a new canonical track. Returns the canonical
1451
+ ID to use for counting.
1452
+ """
1453
+ now = time.time()
1454
+
1455
+ # Fast path: raw_id already mapped
1456
+ if raw_id in self._track_aliases:
1457
+ canonical_id = self._track_aliases[raw_id]
1458
+ track_info = self._canonical_tracks.get(canonical_id)
1459
+ if track_info is not None:
1460
+ track_info["last_bbox"] = bbox
1461
+ track_info["last_update"] = now
1462
+ track_info["raw_ids"].add(raw_id)
1463
+ return canonical_id
1464
+
1465
+ # Attempt to merge with an existing canonical track
1466
+ for canonical_id, info in self._canonical_tracks.items():
1467
+ # Only consider recently updated tracks to avoid stale matches
1468
+ if now - info["last_update"] > self._track_merge_time_window:
1469
+ continue
1470
+
1471
+ iou = self._compute_iou(bbox, info["last_bbox"])
1472
+ if iou >= self._track_merge_iou_threshold:
1473
+ # Merge raw_id into canonical track
1474
+ self._track_aliases[raw_id] = canonical_id
1475
+ info["last_bbox"] = bbox
1476
+ info["last_update"] = now
1477
+ info["raw_ids"].add(raw_id)
1478
+ self.logger.debug(
1479
+ f"Merged raw track {raw_id} into canonical track {canonical_id} (IoU={iou:.2f})")
1480
+ return canonical_id
1481
+
1482
+ # No match found – create a new canonical track
1483
+ canonical_id = raw_id
1484
+ self._track_aliases[raw_id] = canonical_id
1485
+ self._canonical_tracks[canonical_id] = {
1486
+ "last_bbox": bbox,
1487
+ "last_update": now,
1488
+ "raw_ids": {raw_id},
1489
+ }
1490
+ self.logger.debug(f"Registered new canonical track {canonical_id}")
1491
+ return canonical_id
1492
+
1493
+ def _format_timestamp(self, timestamp: Any) -> str:
1494
+ """Format a timestamp so that exactly two digits follow the decimal point (milliseconds).
1495
+
1496
+ The input can be either:
1497
+ 1. A numeric Unix timestamp (``float`` / ``int``) – it will first be converted to a
1498
+ string in the format ``YYYY-MM-DD-HH:MM:SS.ffffff UTC``.
1499
+ 2. A string already following the same layout.
1500
+
1501
+ The returned value preserves the overall format of the input but truncates or pads
1502
+ the fractional seconds portion to **exactly two digits**.
1503
+
1504
+ Example
1505
+ -------
1506
+ >>> self._format_timestamp("2025-08-19-04:22:47.187574 UTC")
1507
+ '2025-08-19-04:22:47.18 UTC'
1508
+ """
1509
+
1510
+ # Convert numeric timestamps to the expected string representation first
1511
+ if isinstance(timestamp, (int, float)):
1512
+ timestamp = datetime.fromtimestamp(timestamp, timezone.utc).strftime(
1513
+ '%Y-%m-%d-%H:%M:%S.%f UTC'
1514
+ )
1515
+
1516
+ # Ensure we are working with a string from here on
1517
+ if not isinstance(timestamp, str):
1518
+ return str(timestamp)
1519
+
1520
+ # If there is no fractional component, simply return the original string
1521
+ if '.' not in timestamp:
1522
+ return timestamp
1523
+
1524
+ # Split out the main portion (up to the decimal point)
1525
+ main_part, fractional_and_suffix = timestamp.split('.', 1)
1526
+
1527
+ # Separate fractional digits from the suffix (typically ' UTC')
1528
+ if ' ' in fractional_and_suffix:
1529
+ fractional_part, suffix = fractional_and_suffix.split(' ', 1)
1530
+ suffix = ' ' + suffix # Re-attach the space removed by split
1531
+ else:
1532
+ fractional_part, suffix = fractional_and_suffix, ''
1533
+
1534
+ # Guarantee exactly two digits for the fractional part
1535
+ fractional_part = (fractional_part + '00')[:2]
1536
+
1537
+ return f"{main_part}.{fractional_part}{suffix}"
1538
+
1539
+ def _get_tracking_start_time(self) -> str:
1540
+ """Get the tracking start time, formatted as a string."""
1541
+ if self._tracking_start_time is None:
1542
+ return "N/A"
1543
+ return self._format_timestamp(self._tracking_start_time)
1544
+
1545
+ def _set_tracking_start_time(self) -> None:
1546
+ """Set the tracking start time to the current time."""
1547
+ self._tracking_start_time = time.time()
1548
+
1549
+ def get_config_schema(self) -> Dict[str, Any]:
1550
+ """Get configuration schema for people counting."""
1551
+ return {
1552
+ "type": "object",
1553
+ "properties": {
1554
+ "confidence_threshold": {
1555
+ "type": "number",
1556
+ "minimum": 0.0,
1557
+ "maximum": 1.0,
1558
+ "default": 0.5,
1559
+ "description": "Minimum confidence threshold for detections"
1560
+ },
1561
+ "enable_tracking": {
1562
+ "type": "boolean",
1563
+ "default": False,
1564
+ "description": "Enable tracking for unique counting"
1565
+ },
1566
+ "zone_config": {
1567
+ "type": "object",
1568
+ "properties": {
1569
+ "zones": {
1570
+ "type": "object",
1571
+ "additionalProperties": {
1572
+ "type": "array",
1573
+ "items": {
1574
+ "type": "array",
1575
+ "items": {"type": "number"},
1576
+ "minItems": 2,
1577
+ "maxItems": 2
1578
+ },
1579
+ "minItems": 3
1580
+ },
1581
+ "description": "Zone definitions as polygons"
1582
+ },
1583
+ "zone_confidence_thresholds": {
1584
+ "type": "object",
1585
+ "additionalProperties": {"type": "number", "minimum": 0.0, "maximum": 1.0},
1586
+ "description": "Per-zone confidence thresholds"
1587
+ }
1588
+ }
1589
+ },
1590
+ "person_categories": {
1591
+ "type": "array",
1592
+ "items": {"type": "string"},
1593
+ "default": ["person", "people"],
1594
+ "description": "Category names that represent people"
1595
+ },
1596
+ "target_categories": {
1597
+ "type": "array",
1598
+ "items": {"type": "string"},
1599
+ "default": ["person", "people"],
1600
+ "description": "Category names that represent people"
1601
+ },
1602
+ "enable_unique_counting": {
1603
+ "type": "boolean",
1604
+ "default": True,
1605
+ "description": "Enable unique people counting using tracking"
1606
+ },
1607
+ "time_window_minutes": {
1608
+ "type": "integer",
1609
+ "minimum": 1,
1610
+ "default": 60,
1611
+ "description": "Time window for counting analysis in minutes"
1612
+ },
1613
+ "alert_config": {
1614
+ "type": "object",
1615
+ "properties": {
1616
+ "count_thresholds": {
1617
+ "type": "object",
1618
+ "additionalProperties": {"type": "integer", "minimum": 1},
1619
+ "description": "Count thresholds for alerts"
1620
+ },
1621
+ "occupancy_thresholds": {
1622
+ "type": "object",
1623
+ "additionalProperties": {"type": "integer", "minimum": 1},
1624
+ "description": "Zone occupancy thresholds for alerts"
1625
+ },
1626
+ "alert_type": {
1627
+ "type": "array",
1628
+ "items": {"type": "string"},
1629
+ "default": ["Default"],
1630
+ "description": "To pass the type of alert. EG: email, sms, etc."
1631
+ },
1632
+ "alert_value": {
1633
+ "type": "array",
1634
+ "items": {"type": "string"},
1635
+ "default": ["JSON"],
1636
+ "description": "Alert value to pass the value based on type. EG: email id if type is email."
1637
+ },
1638
+ "alert_incident_category": {
1639
+ "type": "array",
1640
+ "items": {"type": "string"},
1641
+ "default": ["Incident Detection Alert"],
1642
+ "description": "Group and name the Alert category Type"
1643
+ },
1644
+ }
1645
+ }
1646
+ },
1647
+ "required": ["confidence_threshold"],
1648
+ "additionalProperties": False
1649
+ }
1650
+
1651
+ def create_default_config(self, **overrides) -> PeopleCountingConfig:
1652
+ """Create default configuration with optional overrides."""
1653
+ defaults = {
1654
+ "category": self.category,
1655
+ "usecase": self.name,
1656
+ "confidence_threshold": 0.5,
1657
+ "enable_tracking": False,
1658
+ "enable_analytics": True,
1659
+ "enable_unique_counting": True,
1660
+ "time_window_minutes": 60,
1661
+ "person_categories": ["person", "people"],
1662
+ "target_categories": ["person", "people", "human", "man", "woman", "male", "female"]
1663
+ }
1664
+ defaults.update(overrides)
1665
+ return PeopleCountingConfig(**defaults)
1666
+
1667
+ def _apply_smoothing(self, data: Any, config: PeopleCountingConfig) -> Any:
1668
+ """Apply smoothing to tracking data if enabled."""
1669
+ if self.smoothing_tracker is None:
1670
+ smoothing_config = BBoxSmoothingConfig(
1671
+ smoothing_algorithm=config.smoothing_algorithm,
1672
+ window_size=config.smoothing_window_size,
1673
+ cooldown_frames=config.smoothing_cooldown_frames,
1674
+ confidence_threshold=config.confidence_threshold or 0.5,
1675
+ confidence_range_factor=config.smoothing_confidence_range_factor,
1676
+ enable_smoothing=True
1677
+ )
1678
+ self.smoothing_tracker = BBoxSmoothingTracker(smoothing_config)
1679
+
1680
+ smoothed_data = bbox_smoothing(data, self.smoothing_tracker.config, self.smoothing_tracker)
1681
+ self.logger.debug(f"Applied bbox smoothing to tracking results")
1682
+ return smoothed_data
1683
+