mustafa2ak commited on
Commit
a22aaed
·
verified ·
1 Parent(s): 8344942

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -81
app.py CHANGED
@@ -63,144 +63,89 @@ class DatasetCollectionApp:
63
  print(f"Database has {len(self.db.get_all_dogs())} dogs")
64
 
65
  def create_visualization_video(self, video_path: str, sample_rate: int) -> str:
66
- """Create visualization video with boxes matching ReID temp_ids with ID labels"""
67
  try:
68
  cap = cv2.VideoCapture(video_path)
69
  if not cap.isOpened():
70
  print("ERROR: Cannot open input video")
71
  return None
72
-
73
  fps = cap.get(cv2.CAP_PROP_FPS) or 30
74
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
75
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
76
-
77
  output_path = f"visualization_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
78
-
79
- # Try multiple codecs
80
  codecs = ['mp4v', 'XVID', 'MJPG']
81
  out = None
82
-
83
  for codec in codecs:
84
  fourcc = cv2.VideoWriter_fourcc(*codec)
85
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
86
-
87
  if out.isOpened():
88
  print(f"Using codec: {codec}")
89
  break
90
  else:
91
  out.release()
92
  out = None
93
-
94
  if out is None or not out.isOpened():
95
  print("ERROR: Could not initialize VideoWriter with any codec")
96
  cap.release()
97
  return None
98
-
99
- # Create separate tracker for visualization
100
  viz_tracker = DeepSORTTracker(
101
  max_iou_distance=0.5,
102
  max_age=90,
103
  n_init=1,
104
- use_appearance=True
105
  )
106
-
107
- # Create separate ReID instance for visualization
108
- viz_reid = SimplifiedReID(device=self.device)
109
- viz_reid.set_threshold(self.reid.threshold)
110
- viz_reid.set_video_source(video_path)
111
-
112
- # Color mapping for temp_ids
113
  track_colors = {}
114
-
115
  frame_num = 0
116
-
117
- print("\nCreating visualization video with ReID matching and ID labels...")
118
-
119
  while cap.isOpened():
120
  ret, frame = cap.read()
121
  if not ret:
122
  break
123
-
124
  if frame_num % sample_rate == 0:
125
- # Detect dogs
126
  detections = self.detector.detect(frame)
127
-
128
- # Track dogs
129
  tracks = viz_tracker.update(detections)
130
-
131
- # Match with ReID to get temp_ids
132
  for track in tracks:
133
- # Get ReID temp_id (matches gallery)
134
- result = viz_reid.match_or_register(track)
135
- temp_id = result['temp_id']
136
-
137
- if temp_id == 0:
138
- continue
139
-
140
- # Assign color based on temp_id
141
- if temp_id not in track_colors:
142
- track_colors[temp_id] = (
143
  np.random.randint(50, 255),
144
  np.random.randint(50, 255),
145
  np.random.randint(50, 255)
146
  )
147
-
148
  x1, y1, x2, y2 = map(int, track.bbox)
149
- color = track_colors[temp_id]
150
-
151
- # Draw bold box
152
  cv2.rectangle(frame, (x1, y1), (x2, y2), color, 6)
153
-
154
- # Add ID label
155
- label = f"#{temp_id}"
156
- font = cv2.FONT_HERSHEY_SIMPLEX
157
- font_scale = 1.5
158
- font_thickness = 3
159
-
160
- # Get text size
161
- (text_width, text_height), baseline = cv2.getTextSize(
162
- label, font, font_scale, font_thickness
163
- )
164
-
165
- # Draw filled rectangle background for text
166
- cv2.rectangle(
167
- frame,
168
- (x1, y1 - text_height - 15),
169
- (x1 + text_width + 15, y1),
170
- color,
171
- -1
172
- )
173
-
174
- # Draw white text
175
- cv2.putText(
176
- frame,
177
- label,
178
- (x1 + 7, y1 - 7),
179
- font,
180
- font_scale,
181
- (255, 255, 255),
182
- font_thickness,
183
- cv2.LINE_AA
184
- )
185
-
186
  out.write(frame)
187
  frame_num += 1
188
-
189
  if frame_num % 30 == 0:
190
  print(f"Visualization progress: {frame_num} frames")
191
-
192
  cap.release()
193
  out.release()
194
-
195
- # Verify file
196
  if os.path.exists(output_path) and os.path.getsize(output_path) > 1000:
197
  print(f"Visualization video saved: {output_path}")
198
- print(f"Gallery temp_ids shown: {sorted(track_colors.keys())}")
199
  return output_path
200
  else:
201
  print("ERROR: Video file not created or is empty")
202
  return None
203
-
204
  except Exception as e:
205
  print(f"Visualization video error: {e}")
206
  import traceback
 
63
  print(f"Database has {len(self.db.get_all_dogs())} dogs")
64
 
65
  def create_visualization_video(self, video_path: str, sample_rate: int) -> str:
66
+ """Create visualization video with bold boxes from tracking.py only (no ReID, no labels)"""
67
  try:
68
  cap = cv2.VideoCapture(video_path)
69
  if not cap.isOpened():
70
  print("ERROR: Cannot open input video")
71
  return None
72
+
73
  fps = cap.get(cv2.CAP_PROP_FPS) or 30
74
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
75
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
76
+
77
  output_path = f"visualization_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
78
+
79
+ # Try codecs
80
  codecs = ['mp4v', 'XVID', 'MJPG']
81
  out = None
 
82
  for codec in codecs:
83
  fourcc = cv2.VideoWriter_fourcc(*codec)
84
  out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
 
85
  if out.isOpened():
86
  print(f"Using codec: {codec}")
87
  break
88
  else:
89
  out.release()
90
  out = None
91
+
92
  if out is None or not out.isOpened():
93
  print("ERROR: Could not initialize VideoWriter with any codec")
94
  cap.release()
95
  return None
96
+
97
+ # Tracker only (no ReID)
98
  viz_tracker = DeepSORTTracker(
99
  max_iou_distance=0.5,
100
  max_age=90,
101
  n_init=1,
102
+ use_appearance=False
103
  )
104
+
 
 
 
 
 
 
105
  track_colors = {}
 
106
  frame_num = 0
107
+
108
+ print("\nCreating visualization video (tracking IDs only)...")
109
+
110
  while cap.isOpened():
111
  ret, frame = cap.read()
112
  if not ret:
113
  break
114
+
115
  if frame_num % sample_rate == 0:
 
116
  detections = self.detector.detect(frame)
 
 
117
  tracks = viz_tracker.update(detections)
118
+
 
119
  for track in tracks:
120
+ tid = track.track_id
121
+ if tid not in track_colors:
122
+ track_colors[tid] = (
 
 
 
 
 
 
 
123
  np.random.randint(50, 255),
124
  np.random.randint(50, 255),
125
  np.random.randint(50, 255)
126
  )
127
+ color = track_colors[tid]
128
  x1, y1, x2, y2 = map(int, track.bbox)
129
+
130
+ # Bold box only
 
131
  cv2.rectangle(frame, (x1, y1), (x2, y2), color, 6)
132
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133
  out.write(frame)
134
  frame_num += 1
135
+
136
  if frame_num % 30 == 0:
137
  print(f"Visualization progress: {frame_num} frames")
138
+
139
  cap.release()
140
  out.release()
141
+
 
142
  if os.path.exists(output_path) and os.path.getsize(output_path) > 1000:
143
  print(f"Visualization video saved: {output_path}")
 
144
  return output_path
145
  else:
146
  print("ERROR: Video file not created or is empty")
147
  return None
148
+
149
  except Exception as e:
150
  print(f"Visualization video error: {e}")
151
  import traceback