PrashanthB461 commited on
Commit
c2789c6
·
verified ·
1 Parent(s): 3677731

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -66
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import os
2
  import sys
3
  import subprocess
4
  import logging
@@ -38,7 +38,6 @@ class BYTETracker:
38
  self.tracks = {} # Store active tracks
39
  self.worker_history = {} # Track worker positions over time
40
  self.last_positions = {} # Last known positions of workers
41
- self.violation_history = {} # Track violations per worker: {worker_id: set(violation_types)}
42
 
43
  def update(self, dets, scores, cls):
44
  tracks = []
@@ -118,7 +117,6 @@ class BYTETracker:
118
  }
119
  self.worker_history[self.next_id] = [[x, y]]
120
  self.last_positions[self.next_id] = [x, y]
121
- self.violation_history[self.next_id] = set() # Initialize violation set for new worker
122
  tracks.append({
123
  'id': self.next_id,
124
  'bbox': [x, y, w, h],
@@ -140,21 +138,9 @@ class BYTETracker:
140
  del self.worker_history[track_id]
141
  if track_id in self.last_positions:
142
  del self.last_positions[track_id]
143
- if track_id in self.violation_history:
144
- del self.violation_history[track_id]
145
 
146
  return tracks
147
 
148
- def has_violation(self, worker_id, violation_type):
149
- """Check if this worker already has this violation type recorded"""
150
- return worker_id in self.violation_history and violation_type in self.violation_history[worker_id]
151
-
152
- def record_violation(self, worker_id, violation_type):
153
- """Record that this worker has this violation type"""
154
- if worker_id not in self.violation_history:
155
- self.violation_history[worker_id] = set()
156
- self.violation_history[worker_id].add(violation_type)
157
-
158
  def _calculate_iou(self, box1, box2):
159
  """Calculate IOU between two boxes"""
160
  x1, y1, w1, h1 = box1
@@ -627,61 +613,58 @@ def process_video(video_data):
627
  if label is None:
628
  continue
629
 
630
- # Skip if this worker already has this violation recorded
631
- if tracker.has_violation(worker_id, label):
632
- continue
633
-
634
  # Initialize worker if not seen before
635
  if worker_id not in unique_violations:
636
  unique_violations[worker_id] = {}
637
 
638
- # Record this violation for this worker
639
- tracker.record_violation(worker_id, label)
640
- unique_violations[worker_id][label] = current_time
641
-
642
- # Create detection object
643
- detection = {
644
- "worker_id": worker_id,
645
- "violation": label,
646
- "confidence": round(conf, 2),
647
- "bounding_box": bbox,
648
- "timestamp": current_time
649
- }
650
-
651
- # Take snapshot for the new violation
652
- snapshot_frame = batch_frames[i].copy()
653
- snapshot_frame = draw_detections(snapshot_frame, [detection])
654
-
655
- # Add timestamp to snapshot
656
- cv2.putText(
657
- snapshot_frame,
658
- f"Time: {current_time:.2f}s",
659
- (10, 30),
660
- cv2.FONT_HERSHEY_SIMPLEX,
661
- 0.7,
662
- (255, 255, 255),
663
- 2
664
- )
665
-
666
- # Save snapshot with high quality
667
- snapshot_filename = f"violation_{label}_worker{worker_id}_{int(current_time*100)}.jpg"
668
- snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], snapshot_filename)
669
-
670
- cv2.imwrite(
671
- snapshot_path,
672
- snapshot_frame,
673
- [cv2.IMWRITE_JPEG_QUALITY, CONFIG["SNAPSHOT_QUALITY"]]
674
- )
675
-
676
- snapshots.append({
677
- "violation": label,
678
- "worker_id": worker_id,
679
- "timestamp": current_time,
680
- "snapshot_path": snapshot_path,
681
- "snapshot_url": f"{CONFIG['PUBLIC_URL_BASE']}{snapshot_filename}"
682
- })
683
-
684
- logger.info(f"Captured snapshot for {label} violation by worker {worker_id} at {current_time:.2f}s")
 
685
 
686
  cap.release()
687
  if os.path.exists(video_path):
 
1
+ import os
2
  import sys
3
  import subprocess
4
  import logging
 
38
  self.tracks = {} # Store active tracks
39
  self.worker_history = {} # Track worker positions over time
40
  self.last_positions = {} # Last known positions of workers
 
41
 
42
  def update(self, dets, scores, cls):
43
  tracks = []
 
117
  }
118
  self.worker_history[self.next_id] = [[x, y]]
119
  self.last_positions[self.next_id] = [x, y]
 
120
  tracks.append({
121
  'id': self.next_id,
122
  'bbox': [x, y, w, h],
 
138
  del self.worker_history[track_id]
139
  if track_id in self.last_positions:
140
  del self.last_positions[track_id]
 
 
141
 
142
  return tracks
143
 
 
 
 
 
 
 
 
 
 
 
144
  def _calculate_iou(self, box1, box2):
145
  """Calculate IOU between two boxes"""
146
  x1, y1, w1, h1 = box1
 
613
  if label is None:
614
  continue
615
 
 
 
 
 
616
  # Initialize worker if not seen before
617
  if worker_id not in unique_violations:
618
  unique_violations[worker_id] = {}
619
 
620
+ # Check if this violation type has been recorded for this worker
621
+ if label not in unique_violations[worker_id]:
622
+ # This is a new violation type for this worker
623
+ unique_violations[worker_id][label] = current_time
624
+
625
+ # Create detection object
626
+ detection = {
627
+ "worker_id": worker_id,
628
+ "violation": label,
629
+ "confidence": round(conf, 2),
630
+ "bounding_box": bbox,
631
+ "timestamp": current_time
632
+ }
633
+
634
+ # Take snapshot for the new violation
635
+ snapshot_frame = batch_frames[i].copy()
636
+ snapshot_frame = draw_detections(snapshot_frame, [detection])
637
+
638
+ # Add timestamp to snapshot
639
+ cv2.putText(
640
+ snapshot_frame,
641
+ f"Time: {current_time:.2f}s",
642
+ (10, 30),
643
+ cv2.FONT_HERSHEY_SIMPLEX,
644
+ 0.7,
645
+ (255, 255, 255),
646
+ 2
647
+ )
648
+
649
+ # Save snapshot with high quality
650
+ snapshot_filename = f"violation_{label}_worker{worker_id}_{int(current_time*100)}.jpg"
651
+ snapshot_path = os.path.join(CONFIG["OUTPUT_DIR"], snapshot_filename)
652
+
653
+ cv2.imwrite(
654
+ snapshot_path,
655
+ snapshot_frame,
656
+ [cv2.IMWRITE_JPEG_QUALITY, CONFIG["SNAPSHOT_QUALITY"]]
657
+ )
658
+
659
+ snapshots.append({
660
+ "violation": label,
661
+ "worker_id": worker_id,
662
+ "timestamp": current_time,
663
+ "snapshot_path": snapshot_path,
664
+ "snapshot_url": f"{CONFIG['PUBLIC_URL_BASE']}{snapshot_filename}"
665
+ })
666
+
667
+ logger.info(f"Captured snapshot for {label} violation by worker {worker_id} at {current_time:.2f}s")
668
 
669
  cap.release()
670
  if os.path.exists(video_path):