matrice 1.0.99217__py3-none-any.whl → 1.0.99219__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.
@@ -0,0 +1,837 @@
1
+ from typing import Any, Dict, List, Optional
2
+ from dataclasses import asdict
3
+ import time
4
+ from datetime import datetime, timezone
5
+
6
+ from ..core.base import BaseProcessor, ProcessingContext, ProcessingResult, ConfigProtocol, ResultFormat
7
+ from ..utils import (
8
+ filter_by_confidence,
9
+ filter_by_categories,
10
+ apply_category_mapping,
11
+ count_objects_by_category,
12
+ count_objects_in_zones,
13
+ calculate_counting_summary,
14
+ match_results_structure,
15
+ bbox_smoothing,
16
+ BBoxSmoothingConfig,
17
+ BBoxSmoothingTracker
18
+ )
19
+ from dataclasses import dataclass, field
20
+ from ..core.config import BaseConfig, AlertConfig, ZoneConfig
21
+
22
+
23
+ @dataclass
24
+ class HumanActivityRecognitionConfig(BaseConfig):
25
+ """Configuration for human activity detection use case."""
26
+ # Smoothing configuration
27
+ enable_smoothing: bool = True
28
+ smoothing_algorithm: str = "observability" # "window" or "observability"
29
+ smoothing_window_size: int = 20
30
+ smoothing_cooldown_frames: int = 5
31
+ smoothing_confidence_range_factor: float = 0.5
32
+
33
+ #confidence thresholds
34
+ confidence_threshold: float = 0.3
35
+
36
+ usecase_categories: List[str] = field(
37
+ default_factory=lambda: ['Drinking', 'Fall-Detected', 'Fall_down', 'Lying_down', 'Nearly_fall', 'Sit Down', 'Sitting', 'Standing', 'Walking', 'Walking_on_Stairs', 'crawling', 'falling', 'sitting', 'standing', 'walking']
38
+ )
39
+
40
+ target_categories: List[str] = field(
41
+ default_factory=lambda: ['Drinking', 'Fall-Detected', 'Fall_down', 'Lying_down', 'Nearly_fall', 'Sit Down', 'Sitting', 'Standing', 'Walking', 'Walking_on_Stairs', 'crawling', 'falling', 'sitting', 'standing', 'walking']
42
+ )
43
+
44
+ alert_config: Optional[AlertConfig] = None
45
+
46
+ index_to_category: Optional[Dict[int, str]] = field(
47
+ default_factory=lambda: {
48
+ 0: 'Drinking',
49
+ 1: 'Fall-Detected',
50
+ 2: 'Fall_down',
51
+ 3: 'Lying_down',
52
+ 4: 'Nearly_fall',
53
+ 5: 'Sit Down',
54
+ 6: 'Sitting',
55
+ 7: 'Standing',
56
+ 8: 'Walking',
57
+ 9: 'Walking_on_Stairs',
58
+ 10: 'crawling',
59
+ 11: 'falling',
60
+ 12: 'sitting',
61
+ 13: 'standing',
62
+ 14: 'walking'
63
+ }
64
+ )
65
+
66
+
67
+ class HumanActivityRecognitionUseCase(BaseProcessor):
68
+
69
+
70
+ def __init__(self):
71
+ super().__init__("human_activity")
72
+ self.category = "general"
73
+
74
+ self.CASE_TYPE: Optional[str] = 'human_activity'
75
+ self.CASE_VERSION: Optional[str] = '1.2'
76
+ # List of categories to track
77
+ self.target_categories = ['Drinking', 'Fall-Detected', 'Fall_down', 'Lying_down', 'Nearly_fall', 'Sit Down', 'Sitting', 'Standing', 'Walking', 'Walking_on_Stairs', 'crawling', 'falling', 'sitting', 'standing', 'walking']
78
+
79
+
80
+ # Initialize smoothing tracker
81
+ self.smoothing_tracker = None
82
+
83
+ # Initialize advanced tracker (will be created on first use)
84
+ self.tracker = None
85
+ # Initialize tracking state variables
86
+ self._total_frame_counter = 0
87
+ self._global_frame_offset = 0
88
+
89
+ # Track start time for "TOTAL SINCE" calculation
90
+ self._tracking_start_time = None
91
+
92
+ self._track_aliases: Dict[Any, Any] = {}
93
+ self._canonical_tracks: Dict[Any, Dict[str, Any]] = {}
94
+ # Tunable parameters – adjust if necessary for specific scenarios
95
+ self._track_merge_iou_threshold: float = 0.05 # IoU ≥ 0.05 →
96
+ self._track_merge_time_window: float = 7.0 # seconds within which to merge
97
+
98
+ self._ascending_alert_list: List[int] = []
99
+ self.current_incident_end_timestamp: str = "N/A"
100
+
101
+
102
+ def process(self, data: Any, config: ConfigProtocol, context: Optional[ProcessingContext] = None,
103
+ stream_info: Optional[Dict[str, Any]] = None) -> ProcessingResult:
104
+ """
105
+ Main entry point for post-processing.
106
+ Applies category mapping, smoothing, counting, alerting, and summary generation.
107
+ Returns a ProcessingResult with all relevant outputs.
108
+ """
109
+ start_time = time.time()
110
+ # Ensure config is correct type
111
+ if not isinstance(config, HumanActivityRecognitionConfig):
112
+ return self.create_error_result("Invalid config type", usecase=self.name, category=self.category,
113
+ context=context)
114
+ if context is None:
115
+ context = ProcessingContext()
116
+
117
+ # Detect input format and store in context
118
+ input_format = match_results_structure(data)
119
+ context.input_format = input_format
120
+ context.confidence_threshold = config.confidence_threshold
121
+
122
+ if config.confidence_threshold is not None:
123
+ processed_data = filter_by_confidence(data, config.confidence_threshold)
124
+ self.logger.debug(f"Applied confidence filtering with threshold {config.confidence_threshold}")
125
+ else:
126
+ processed_data = data
127
+
128
+ self.logger.debug(f"Did not apply confidence filtering with threshold since nothing was provided")
129
+
130
+ # Step 2: Apply category mapping if provided
131
+ if config.index_to_category:
132
+ processed_data = apply_category_mapping(processed_data, config.index_to_category)
133
+ self.logger.debug("Applied category mapping")
134
+
135
+ if config.target_categories:
136
+ processed_data = [d for d in processed_data if d.get('category') in self.target_categories]
137
+ self.logger.debug(f"Applied category filtering")
138
+
139
+ # Apply bbox smoothing if enabled
140
+ if config.enable_smoothing:
141
+ if self.smoothing_tracker is None:
142
+ smoothing_config = BBoxSmoothingConfig(
143
+ smoothing_algorithm=config.smoothing_algorithm,
144
+ window_size=config.smoothing_window_size,
145
+ cooldown_frames=config.smoothing_cooldown_frames,
146
+ confidence_threshold=config.confidence_threshold, # Use mask threshold as default
147
+ confidence_range_factor=config.smoothing_confidence_range_factor,
148
+ enable_smoothing=True
149
+ )
150
+ self.smoothing_tracker = BBoxSmoothingTracker(smoothing_config)
151
+ processed_data = bbox_smoothing(processed_data, self.smoothing_tracker.config, self.smoothing_tracker)
152
+
153
+ # Advanced tracking (BYTETracker-like)
154
+ try:
155
+ from ..advanced_tracker import AdvancedTracker
156
+ from ..advanced_tracker.config import TrackerConfig
157
+
158
+ # Create tracker instance if it doesn't exist (preserves state across frames)
159
+ if self.tracker is None:
160
+ # Configure tracker thresholds based on the use-case confidence threshold so that
161
+ # low-confidence detections (e.g. < 0.7) can still be initialised as tracks when
162
+ # the user passes a lower `confidence_threshold` in the post-processing config.
163
+ if config.confidence_threshold is not None:
164
+ tracker_config = TrackerConfig(
165
+ track_high_thresh=float(config.confidence_threshold),
166
+ # Allow even lower detections to participate in secondary association
167
+ track_low_thresh=max(0.05, float(config.confidence_threshold) / 2),
168
+ new_track_thresh=float(config.confidence_threshold)
169
+ )
170
+ else:
171
+ tracker_config = TrackerConfig()
172
+ self.tracker = AdvancedTracker(tracker_config)
173
+ self.logger.info(
174
+ "Initialized AdvancedTracker for Monitoring and tracking with thresholds: "
175
+ f"high={tracker_config.track_high_thresh}, "
176
+ f"low={tracker_config.track_low_thresh}, "
177
+ f"new={tracker_config.new_track_thresh}"
178
+ )
179
+
180
+ # The tracker expects the data in the same format as input
181
+ # It will add track_id and frame_id to each detection
182
+ processed_data = self.tracker.update(processed_data)
183
+
184
+ except Exception as e:
185
+ # If advanced tracker fails, fallback to unsmoothed detections
186
+ self.logger.warning(f"AdvancedTracker failed: {e}")
187
+
188
+ # Update tracking state for total count per label
189
+ self._update_tracking_state(processed_data)
190
+
191
+ # Update frame counter
192
+ self._total_frame_counter += 1
193
+
194
+ # Extract frame information from stream_info
195
+ frame_number = None
196
+ if stream_info:
197
+ input_settings = stream_info.get("input_settings", {})
198
+ start_frame = input_settings.get("start_frame")
199
+ end_frame = input_settings.get("end_frame")
200
+ # If start and end frame are the same, it's a single frame
201
+ if start_frame is not None and end_frame is not None and start_frame == end_frame:
202
+ frame_number = start_frame
203
+
204
+ # Compute summaries and alerts
205
+ general_counting_summary = calculate_counting_summary(data)
206
+ counting_summary = self._count_categories(processed_data, config)
207
+ # Add total unique counts after tracking using only local state
208
+ total_counts = self.get_total_counts()
209
+ counting_summary['total_counts'] = total_counts
210
+
211
+ alerts = self._check_alerts(counting_summary, frame_number, config)
212
+ predictions = self._extract_predictions(processed_data)
213
+
214
+ # Step: Generate structured incidents, tracking stats and business analytics with frame-based keys
215
+ incidents_list = self._generate_incidents(counting_summary, alerts, config, frame_number, stream_info)
216
+ tracking_stats_list = self._generate_tracking_stats(counting_summary, alerts, config, frame_number, stream_info)
217
+ business_analytics_list = self._generate_business_analytics(counting_summary, alerts, config, stream_info, is_empty=True)
218
+ summary_list = self._generate_summary(counting_summary, incidents_list, tracking_stats_list, business_analytics_list, alerts)
219
+
220
+ # Extract frame-based dictionaries from the lists
221
+ incidents = incidents_list[0] if incidents_list else {}
222
+ tracking_stats = tracking_stats_list[0] if tracking_stats_list else {}
223
+ business_analytics = business_analytics_list[0] if business_analytics_list else {}
224
+ summary = summary_list[0] if summary_list else {}
225
+ agg_summary = {str(frame_number): {
226
+ "incidents": incidents,
227
+ "tracking_stats": tracking_stats,
228
+ "business_analytics": business_analytics,
229
+ "alerts": alerts,
230
+ "human_text": summary}
231
+ }
232
+
233
+
234
+ context.mark_completed()
235
+
236
+ # Build result object following the new pattern
237
+
238
+ result = self.create_result(
239
+ data={"agg_summary": agg_summary},
240
+ usecase=self.name,
241
+ category=self.category,
242
+ context=context
243
+ )
244
+
245
+ return result
246
+
247
+ def _check_alerts(self, summary: dict, frame_number:Any, config: HumanActivityRecognitionConfig) -> List[Dict]:
248
+ """
249
+ Check if any alert thresholds are exceeded and return alert dicts.
250
+ """
251
+ def get_trend(data, lookback=900, threshold=0.6):
252
+ '''
253
+ Determine if the trend is ascending or descending based on actual value progression.
254
+ Now works with values 0,1,2,3 (not just binary).
255
+ '''
256
+ window = data[-lookback:] if len(data) >= lookback else data
257
+ if len(window) < 2:
258
+ return True # not enough data to determine trend
259
+ increasing = 0
260
+ total = 0
261
+ for i in range(1, len(window)):
262
+ if window[i] >= window[i - 1]:
263
+ increasing += 1
264
+ total += 1
265
+ ratio = increasing / total
266
+ if ratio >= threshold:
267
+ return True
268
+ elif ratio <= (1 - threshold):
269
+ return False
270
+
271
+ frame_key = str(frame_number) if frame_number is not None else "current_frame"
272
+ alerts = []
273
+ total_detections = summary.get("total_count", 0) #CURRENT combined total count of all classes
274
+ total_counts_dict = summary.get("total_counts", {}) #TOTAL cumulative counts per class
275
+ cumulative_total = sum(total_counts_dict.values()) if total_counts_dict else 0 #TOTAL combined cumulative count
276
+ per_category_count = summary.get("per_category_count", {}) #CURRENT count per class
277
+
278
+ if not config.alert_config:
279
+ return alerts
280
+
281
+ total = summary.get("total_count", 0)
282
+ #self._ascending_alert_list
283
+ if hasattr(config.alert_config, 'count_thresholds') and config.alert_config.count_thresholds:
284
+
285
+ for category, threshold in config.alert_config.count_thresholds.items():
286
+ if category == "all" and total > threshold:
287
+
288
+ alerts.append({
289
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
290
+ "alert_id": "alert_"+category+'_'+frame_key,
291
+ "incident_category": self.CASE_TYPE,
292
+ "threshold_level": threshold,
293
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
294
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
295
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
296
+ }
297
+ })
298
+ elif category in summary.get("per_category_count", {}):
299
+ count = summary.get("per_category_count", {})[category]
300
+ if count > threshold: # Fixed logic: alert when EXCEEDING threshold
301
+ alerts.append({
302
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
303
+ "alert_id": "alert_"+category+'_'+frame_key,
304
+ "incident_category": self.CASE_TYPE,
305
+ "threshold_level": threshold,
306
+ "ascending": get_trend(self._ascending_alert_list, lookback=900, threshold=0.8),
307
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
308
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
309
+ }
310
+ })
311
+ else:
312
+ pass
313
+ return alerts
314
+
315
+ def _generate_incidents(self, counting_summary: Dict, alerts: List, config: HumanActivityRecognitionConfig,
316
+ frame_number: Optional[int] = None, stream_info: Optional[Dict[str, Any]] = None) -> List[
317
+ Dict]:
318
+ """Generate structured incidents for the output format with frame-based keys."""
319
+
320
+ incidents = []
321
+ total_detections = counting_summary.get("total_count", 0)
322
+ current_timestamp = self._get_current_timestamp_str(stream_info)
323
+ camera_info = self.get_camera_info_from_stream(stream_info)
324
+
325
+ self._ascending_alert_list = self._ascending_alert_list[-900:] if len(self._ascending_alert_list) > 900 else self._ascending_alert_list
326
+
327
+ if total_detections > 0:
328
+ # Determine event level based on thresholds
329
+ level = "low"
330
+ intensity = 5.0
331
+ start_timestamp = self._get_start_timestamp_str(stream_info)
332
+ if start_timestamp and self.current_incident_end_timestamp=='N/A':
333
+ self.current_incident_end_timestamp = 'Incident still active'
334
+ elif start_timestamp and self.current_incident_end_timestamp=='Incident still active':
335
+ if len(self._ascending_alert_list) >= 15 and sum(self._ascending_alert_list[-15:]) / 15 < 1.5:
336
+ self.current_incident_end_timestamp = current_timestamp
337
+ elif self.current_incident_end_timestamp!='Incident still active' and self.current_incident_end_timestamp!='N/A':
338
+ self.current_incident_end_timestamp = 'N/A'
339
+
340
+ if config.alert_config and config.alert_config.count_thresholds:
341
+ threshold = config.alert_config.count_thresholds.get("all", 15)
342
+ intensity = min(10.0, (total_detections / threshold) * 10)
343
+
344
+ if intensity >= 9:
345
+ level = "critical"
346
+ self._ascending_alert_list.append(3)
347
+ elif intensity >= 7:
348
+ level = "significant"
349
+ self._ascending_alert_list.append(2)
350
+ elif intensity >= 5:
351
+ level = "medium"
352
+ self._ascending_alert_list.append(1)
353
+ else:
354
+ level = "low"
355
+ self._ascending_alert_list.append(0)
356
+ else:
357
+ if total_detections > 30:
358
+ level = "critical"
359
+ intensity = 10.0
360
+ self._ascending_alert_list.append(3)
361
+ elif total_detections > 25:
362
+ level = "significant"
363
+ intensity = 9.0
364
+ self._ascending_alert_list.append(2)
365
+ elif total_detections > 15:
366
+ level = "medium"
367
+ intensity = 7.0
368
+ self._ascending_alert_list.append(1)
369
+ else:
370
+ level = "low"
371
+ intensity = min(10.0, total_detections / 3.0)
372
+ self._ascending_alert_list.append(0)
373
+
374
+ # Generate human text in new format
375
+ human_text_lines = [f"INCIDENTS DETECTED @ {current_timestamp}:"]
376
+ human_text_lines.append(f"\tSeverity Level: {(self.CASE_TYPE,level)}")
377
+ human_text = "\n".join(human_text_lines)
378
+
379
+ alert_settings=[]
380
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
381
+ alert_settings.append({
382
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
383
+ "incident_category": self.CASE_TYPE,
384
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
385
+ "ascending": True,
386
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
387
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
388
+ }
389
+ })
390
+
391
+ event= self.create_incident(incident_id=self.CASE_TYPE+'_'+str(frame_number), incident_type=self.CASE_TYPE,
392
+ severity_level=level, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
393
+ start_time=start_timestamp, end_time=self.current_incident_end_timestamp,
394
+ level_settings= {"low": 1, "medium": 3, "significant":4, "critical": 7})
395
+ incidents.append(event)
396
+
397
+ else:
398
+ self._ascending_alert_list.append(0)
399
+ incidents.append({})
400
+
401
+ return incidents
402
+ def _generate_tracking_stats(
403
+ self,
404
+ counting_summary: Dict,
405
+ alerts: List,
406
+ config: HumanActivityRecognitionConfig,
407
+ frame_number: Optional[int] = None,
408
+ stream_info: Optional[Dict[str, Any]] = None
409
+ ) -> List[Dict]:
410
+ """Generate structured tracking stats matching eg.json format."""
411
+ camera_info = self.get_camera_info_from_stream(stream_info)
412
+
413
+ # frame_key = str(frame_number) if frame_number is not None else "current_frame"
414
+ # tracking_stats = [{frame_key: []}]
415
+ # frame_tracking_stats = tracking_stats[0][frame_key]
416
+ tracking_stats = []
417
+
418
+ total_detections = counting_summary.get("total_count", 0) #CURRENT total count of all classes
419
+ total_counts_dict = counting_summary.get("total_counts", {}) #TOTAL cumulative counts per class
420
+ cumulative_total = sum(total_counts_dict.values()) if total_counts_dict else 0 #TOTAL combined cumulative count
421
+ per_category_count = counting_summary.get("per_category_count", {}) #CURRENT count per class
422
+
423
+ current_timestamp = self._get_current_timestamp_str(stream_info, precision=False)
424
+ start_timestamp = self._get_start_timestamp_str(stream_info, precision=False)
425
+
426
+ # Create high precision timestamps for input_timestamp and reset_timestamp
427
+ high_precision_start_timestamp = self._get_current_timestamp_str(stream_info, precision=True)
428
+ high_precision_reset_timestamp = self._get_start_timestamp_str(stream_info, precision=True)
429
+
430
+
431
+ # Build total_counts array in expected format
432
+ total_counts = []
433
+ for cat, count in total_counts_dict.items():
434
+ if count > 0:
435
+ total_counts.append({
436
+ "category": cat,
437
+ "count": count
438
+ })
439
+
440
+ # Build current_counts array in expected format
441
+ current_counts = []
442
+ for cat, count in per_category_count.items():
443
+ if count > 0 or total_detections > 0: # Include even if 0 when there are detections
444
+ current_counts.append({
445
+ "category": cat,
446
+ "count": count
447
+ })
448
+
449
+ # Prepare detections without confidence scores (as per eg.json)
450
+ detections = []
451
+ for detection in counting_summary.get("detections", []):
452
+ bbox = detection.get("bounding_box", {})
453
+ category = detection.get("category", "person")
454
+ # Include segmentation if available (like in eg.json)
455
+ if detection.get("masks"):
456
+ segmentation= detection.get("masks", [])
457
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
458
+ elif detection.get("segmentation"):
459
+ segmentation= detection.get("segmentation")
460
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
461
+ elif detection.get("mask"):
462
+ segmentation= detection.get("mask")
463
+ detection_obj = self.create_detection_object(category, bbox, segmentation=segmentation)
464
+ else:
465
+ detection_obj = self.create_detection_object(category, bbox)
466
+ detections.append(detection_obj)
467
+
468
+ # Build alert_settings array in expected format
469
+ alert_settings = []
470
+ if config.alert_config and hasattr(config.alert_config, 'alert_type'):
471
+ alert_settings.append({
472
+ "alert_type": getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
473
+ "incident_category": self.CASE_TYPE,
474
+ "threshold_level": config.alert_config.count_thresholds if hasattr(config.alert_config, 'count_thresholds') else {},
475
+ "ascending": True,
476
+ "settings": {t: v for t, v in zip(getattr(config.alert_config, 'alert_type', ['Default']) if hasattr(config.alert_config, 'alert_type') else ['Default'],
477
+ getattr(config.alert_config, 'alert_value', ['JSON']) if hasattr(config.alert_config, 'alert_value') else ['JSON'])
478
+ }
479
+ })
480
+
481
+ # Generate human_text in expected format
482
+ human_text_lines = [f"Tracking Statistics:"]
483
+ human_text_lines.append(f"CURRENT FRAME @ {current_timestamp}")
484
+
485
+ for cat, count in per_category_count.items():
486
+ human_text_lines.append(f"\t{cat}: {count}")
487
+
488
+ human_text_lines.append(f"TOTAL SINCE {start_timestamp}")
489
+ for cat, count in total_counts_dict.items():
490
+ if count > 0:
491
+ human_text_lines.append(f"\t{cat}: {count}")
492
+
493
+ if alerts:
494
+ for alert in alerts:
495
+ human_text_lines.append(f"Alerts: {alert.get('settings', {})} sent @ {current_timestamp}")
496
+ else:
497
+ human_text_lines.append("Alerts: None")
498
+
499
+ human_text = "\n".join(human_text_lines)
500
+ reset_settings=[
501
+ {
502
+ "interval_type": "daily",
503
+ "reset_time": {
504
+ "value": 9,
505
+ "time_unit": "hour"
506
+ }
507
+ }
508
+ ]
509
+
510
+ tracking_stat=self.create_tracking_stats(total_counts=total_counts, current_counts=current_counts,
511
+ detections=detections, human_text=human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
512
+ reset_settings=reset_settings, start_time=high_precision_start_timestamp ,
513
+ reset_time=high_precision_reset_timestamp)
514
+
515
+ tracking_stats.append(tracking_stat)
516
+ return tracking_stats
517
+
518
+ def _generate_business_analytics(self, counting_summary: Dict, alerts:Any, config: HumanActivityRecognitionConfig, stream_info: Optional[Dict[str, Any]] = None, is_empty=False) -> List[Dict]:
519
+ """Generate standardized business analytics for the agg_summary structure."""
520
+ if is_empty:
521
+ return []
522
+
523
+ #-----IF YOUR USECASE NEEDS BUSINESS ANALYTICS, YOU CAN USE THIS FUNCTION------#
524
+ #camera_info = self.get_camera_info_from_stream(stream_info)
525
+ # business_analytics = self.create_business_analytics(nalysis_name, statistics,
526
+ # human_text, camera_info=camera_info, alerts=alerts, alert_settings=alert_settings,
527
+ # reset_settings)
528
+ # return business_analytics
529
+
530
+ def _generate_summary(self, summary: dict, incidents: List, tracking_stats: List, business_analytics: List, alerts: List) -> List[str]:
531
+ """
532
+ Generate a human_text string for the tracking_stat, incident, business analytics and alerts.
533
+ """
534
+ lines = {}
535
+ lines["Application Name"] = self.CASE_TYPE
536
+ lines["Application Version"] = self.CASE_VERSION
537
+ if len(incidents) > 0:
538
+ lines["Incidents:"]=f"\n\t{incidents[0].get('human_text', 'No incidents detected')}\n"
539
+ if len(tracking_stats) > 0:
540
+ lines["Tracking Statistics:"]=f"\t{tracking_stats[0].get('human_text', 'No tracking statistics detected')}\n"
541
+ if len(business_analytics) > 0:
542
+ lines["Business Analytics:"]=f"\t{business_analytics[0].get('human_text', 'No business analytics detected')}\n"
543
+
544
+ if len(incidents) == 0 and len(tracking_stats) == 0 and len(business_analytics) == 0:
545
+ lines["Summary"] = "No Summary Data"
546
+
547
+ return [lines]
548
+
549
+ def _get_track_ids_info(self, detections: list) -> Dict[str, Any]:
550
+ """
551
+ Get detailed information about track IDs (per frame).
552
+ """
553
+ # Collect all track_ids in this frame
554
+ frame_track_ids = set()
555
+ for det in detections:
556
+ tid = det.get('track_id')
557
+ if tid is not None:
558
+ frame_track_ids.add(tid)
559
+ # Use persistent total set for unique counting
560
+ total_track_ids = set()
561
+ for s in getattr(self, '_per_category_total_track_ids', {}).values():
562
+ total_track_ids.update(s)
563
+ return {
564
+ "total_count": len(total_track_ids),
565
+ "current_frame_count": len(frame_track_ids),
566
+ "total_unique_track_ids": len(total_track_ids),
567
+ "current_frame_track_ids": list(frame_track_ids),
568
+ "last_update_time": time.time(),
569
+ "total_frames_processed": getattr(self, '_total_frame_counter', 0)
570
+ }
571
+
572
+ def _update_tracking_state(self, detections: list):
573
+ """
574
+ Track unique categories track_ids per category for total count after tracking.
575
+ Applies canonical ID merging to avoid duplicate counting when the underlying
576
+ tracker loses an object temporarily and assigns a new ID.
577
+ """
578
+ # Lazily initialise storage dicts
579
+ if not hasattr(self, "_per_category_total_track_ids"):
580
+ self._per_category_total_track_ids = {cat: set() for cat in self.target_categories}
581
+ self._current_frame_track_ids = {cat: set() for cat in self.target_categories}
582
+
583
+ for det in detections:
584
+ cat = det.get("category")
585
+ raw_track_id = det.get("track_id")
586
+ if cat not in self.target_categories or raw_track_id is None:
587
+ continue
588
+ bbox = det.get("bounding_box", det.get("bbox"))
589
+ canonical_id = self._merge_or_register_track(raw_track_id, bbox)
590
+ # Propagate canonical ID back to detection so downstream logic uses it
591
+ det["track_id"] = canonical_id
592
+
593
+ self._per_category_total_track_ids.setdefault(cat, set()).add(canonical_id)
594
+ self._current_frame_track_ids[cat].add(canonical_id)
595
+
596
+ def get_total_counts(self):
597
+ """
598
+ Return total unique track_id count for each category.
599
+ """
600
+ return {cat: len(ids) for cat, ids in getattr(self, '_per_category_total_track_ids', {}).items()}
601
+
602
+
603
+ def _format_timestamp_for_stream(self, timestamp: float) -> str:
604
+ """Format timestamp for streams (YYYY:MM:DD HH:MM:SS format)."""
605
+ dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)
606
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
607
+
608
+ def _format_timestamp_for_video(self, timestamp: float) -> str:
609
+ """Format timestamp for video chunks (HH:MM:SS.ms format)."""
610
+ hours = int(timestamp // 3600)
611
+ minutes = int((timestamp % 3600) // 60)
612
+ seconds = round(float(timestamp % 60),2)
613
+ return f"{hours:02d}:{minutes:02d}:{seconds:.1f}"
614
+
615
+ def _get_current_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False, frame_id: Optional[str]=None) -> str:
616
+ """Get formatted current timestamp based on stream type."""
617
+ if not stream_info:
618
+ return "00:00:00.00"
619
+ # is_video_chunk = stream_info.get("input_settings", {}).get("is_video_chunk", False)
620
+ if precision:
621
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
622
+ if frame_id:
623
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
624
+ else:
625
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
626
+ stream_time_str = self._format_timestamp_for_video(start_time)
627
+ return stream_time_str
628
+ else:
629
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC")
630
+
631
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
632
+ if frame_id:
633
+ start_time = int(frame_id)/stream_info.get("input_settings", {}).get("original_fps", 30)
634
+ else:
635
+ start_time = stream_info.get("input_settings", {}).get("start_frame", 30)/stream_info.get("input_settings", {}).get("original_fps", 30)
636
+ stream_time_str = self._format_timestamp_for_video(start_time)
637
+ return stream_time_str
638
+ else:
639
+ # For streams, use stream_time from stream_info
640
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
641
+ if stream_time_str:
642
+ # Parse the high precision timestamp string to get timestamp
643
+ try:
644
+ # Remove " UTC" suffix and parse
645
+ timestamp_str = stream_time_str.replace(" UTC", "")
646
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
647
+ timestamp = dt.replace(tzinfo=timezone.utc).timestamp()
648
+ return self._format_timestamp_for_stream(timestamp)
649
+ except:
650
+ # Fallback to current time if parsing fails
651
+ return self._format_timestamp_for_stream(time.time())
652
+ else:
653
+ return self._format_timestamp_for_stream(time.time())
654
+
655
+ def _get_start_timestamp_str(self, stream_info: Optional[Dict[str, Any]], precision=False) -> str:
656
+ """Get formatted start timestamp for 'TOTAL SINCE' based on stream type."""
657
+ if not stream_info:
658
+ return "00:00:00"
659
+ if precision:
660
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
661
+ return "00:00:00"
662
+ else:
663
+ return datetime.now(timezone.utc).strftime("%Y-%m-%d-%H:%M:%S.%f UTC")
664
+
665
+ if stream_info.get("input_settings", {}).get("start_frame", "na") != "na":
666
+ # If video format, start from 00:00:00
667
+ return "00:00:00"
668
+ else:
669
+ # For streams, use tracking start time or current time with minutes/seconds reset
670
+ if self._tracking_start_time is None:
671
+ # Try to extract timestamp from stream_time string
672
+ stream_time_str = stream_info.get("input_settings", {}).get("stream_info", {}).get("stream_time", "")
673
+ if stream_time_str:
674
+ try:
675
+ # Remove " UTC" suffix and parse
676
+ timestamp_str = stream_time_str.replace(" UTC", "")
677
+ dt = datetime.strptime(timestamp_str, "%Y-%m-%d-%H:%M:%S.%f")
678
+ self._tracking_start_time = dt.replace(tzinfo=timezone.utc).timestamp()
679
+ except:
680
+ # Fallback to current time if parsing fails
681
+ self._tracking_start_time = time.time()
682
+ else:
683
+ self._tracking_start_time = time.time()
684
+
685
+ dt = datetime.fromtimestamp(self._tracking_start_time, tz=timezone.utc)
686
+ # Reset minutes and seconds to 00:00 for "TOTAL SINCE" format
687
+ dt = dt.replace(minute=0, second=0, microsecond=0)
688
+ return dt.strftime('%Y:%m:%d %H:%M:%S')
689
+
690
+
691
+ def _count_categories(self, detections: list, config: HumanActivityRecognitionConfig) -> dict:
692
+ """
693
+ Count the number of detections per category and return a summary dict.
694
+ The detections list is expected to have 'track_id' (from tracker), 'category', 'bounding_box', etc.
695
+ Output structure will include 'track_id' for each detection as per AdvancedTracker output.
696
+ """
697
+ counts = {}
698
+ for det in detections:
699
+ cat = det.get('category', 'unknown')
700
+ counts[cat] = counts.get(cat, 0) + 1
701
+ # Each detection dict will now include 'track_id' (and possibly 'frame_id')
702
+ return {
703
+ "total_count": sum(counts.values()),
704
+ "per_category_count": counts,
705
+ "detections": [
706
+ {
707
+ "bounding_box": det.get("bounding_box"),
708
+ "category": det.get("category"),
709
+ "confidence": det.get("confidence"),
710
+ "track_id": det.get("track_id"),
711
+ "frame_id": det.get("frame_id")
712
+ }
713
+ for det in detections
714
+ ]
715
+ }
716
+
717
+ def _extract_predictions(self, detections: list) -> List[Dict[str, Any]]:
718
+ """
719
+ Extract prediction details for output (category, confidence, bounding box).
720
+ """
721
+ return [
722
+ {
723
+ "category": det.get("category", "unknown"),
724
+ "confidence": det.get("confidence", 0.0),
725
+ "bounding_box": det.get("bounding_box", {})
726
+ }
727
+ for det in detections
728
+ ]
729
+
730
+ # ------------------------------------------------------------------ #
731
+ # Canonical ID helpers #
732
+ # ------------------------------------------------------------------ #
733
+ def _compute_iou(self, box1: Any, box2: Any) -> float:
734
+ """Compute IoU between two bounding boxes which may be dicts or lists.
735
+ Falls back to 0 when insufficient data is available."""
736
+
737
+ # Helper to convert bbox (dict or list) to [x1, y1, x2, y2]
738
+ def _bbox_to_list(bbox):
739
+ if bbox is None:
740
+ return []
741
+ if isinstance(bbox, list):
742
+ return bbox[:4] if len(bbox) >= 4 else []
743
+ if isinstance(bbox, dict):
744
+ if "xmin" in bbox:
745
+ return [bbox["xmin"], bbox["ymin"], bbox["xmax"], bbox["ymax"]]
746
+ if "x1" in bbox:
747
+ return [bbox["x1"], bbox["y1"], bbox["x2"], bbox["y2"]]
748
+ # Fallback: first four numeric values
749
+ values = [v for v in bbox.values() if isinstance(v, (int, float))]
750
+ return values[:4] if len(values) >= 4 else []
751
+ return []
752
+
753
+ l1 = _bbox_to_list(box1)
754
+ l2 = _bbox_to_list(box2)
755
+ if len(l1) < 4 or len(l2) < 4:
756
+ return 0.0
757
+ x1_min, y1_min, x1_max, y1_max = l1
758
+ x2_min, y2_min, x2_max, y2_max = l2
759
+
760
+ # Ensure correct order
761
+ x1_min, x1_max = min(x1_min, x1_max), max(x1_min, x1_max)
762
+ y1_min, y1_max = min(y1_min, y1_max), max(y1_min, y1_max)
763
+ x2_min, x2_max = min(x2_min, x2_max), max(x2_min, x2_max)
764
+ y2_min, y2_max = min(y2_min, y2_max), max(y2_min, y2_max)
765
+
766
+ inter_x_min = max(x1_min, x2_min)
767
+ inter_y_min = max(y1_min, y2_min)
768
+ inter_x_max = min(x1_max, x2_max)
769
+ inter_y_max = min(y1_max, y2_max)
770
+
771
+ inter_w = max(0.0, inter_x_max - inter_x_min)
772
+ inter_h = max(0.0, inter_y_max - inter_y_min)
773
+ inter_area = inter_w * inter_h
774
+
775
+ area1 = (x1_max - x1_min) * (y1_max - y1_min)
776
+ area2 = (x2_max - x2_min) * (y2_max - y2_min)
777
+ union_area = area1 + area2 - inter_area
778
+
779
+ return (inter_area / union_area) if union_area > 0 else 0.0
780
+
781
+ def _merge_or_register_track(self, raw_id: Any, bbox: Any) -> Any:
782
+ """Return a stable canonical ID for a raw tracker ID, merging fragmented
783
+ tracks when IoU and temporal constraints indicate they represent the
784
+ same physical."""
785
+ if raw_id is None or bbox is None:
786
+ # Nothing to merge
787
+ return raw_id
788
+
789
+ now = time.time()
790
+
791
+ # Fast path – raw_id already mapped
792
+ if raw_id in self._track_aliases:
793
+ canonical_id = self._track_aliases[raw_id]
794
+ track_info = self._canonical_tracks.get(canonical_id)
795
+ if track_info is not None:
796
+ track_info["last_bbox"] = bbox
797
+ track_info["last_update"] = now
798
+ track_info["raw_ids"].add(raw_id)
799
+ return canonical_id
800
+
801
+ # Attempt to merge with an existing canonical track
802
+ for canonical_id, info in self._canonical_tracks.items():
803
+ # Only consider recently updated tracks
804
+ if now - info["last_update"] > self._track_merge_time_window:
805
+ continue
806
+ iou = self._compute_iou(bbox, info["last_bbox"])
807
+ if iou >= self._track_merge_iou_threshold:
808
+ # Merge
809
+ self._track_aliases[raw_id] = canonical_id
810
+ info["last_bbox"] = bbox
811
+ info["last_update"] = now
812
+ info["raw_ids"].add(raw_id)
813
+ return canonical_id
814
+
815
+ # No match – register new canonical track
816
+ canonical_id = raw_id
817
+ self._track_aliases[raw_id] = canonical_id
818
+ self._canonical_tracks[canonical_id] = {
819
+ "last_bbox": bbox,
820
+ "last_update": now,
821
+ "raw_ids": {raw_id},
822
+ }
823
+ return canonical_id
824
+
825
+ def _format_timestamp(self, timestamp: float) -> str:
826
+ """Format a timestamp for human-readable output."""
827
+ return datetime.fromtimestamp(timestamp, timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')
828
+
829
+ def _get_tracking_start_time(self) -> str:
830
+ """Get the tracking start time, formatted as a string."""
831
+ if self._tracking_start_time is None:
832
+ return "N/A"
833
+ return self._format_timestamp(self._tracking_start_time)
834
+
835
+ def _set_tracking_start_time(self) -> None:
836
+ """Set the tracking start time to the current time."""
837
+ self._tracking_start_time = time.time()