mustafa2ak commited on
Commit
5df2291
·
verified ·
1 Parent(s): 67fefa1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -63,7 +63,7 @@ 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 colored tracking boxes and IDs"""
67
  try:
68
  cap = cv2.VideoCapture(video_path)
69
  if not cap.isOpened():
@@ -76,7 +76,7 @@ class DatasetCollectionApp:
76
 
77
  output_path = f"visualization_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
78
 
79
- # Try multiple codecs in order of preference
80
  codecs = ['mp4v', 'XVID', 'MJPG']
81
  out = None
82
 
@@ -91,23 +91,30 @@ class DatasetCollectionApp:
91
  out.release()
92
  out = None
93
 
94
- # If no codec worked, return None
95
  if out is None or not out.isOpened():
96
  print("ERROR: Could not initialize VideoWriter with any codec")
97
  cap.release()
98
  return None
99
 
 
100
  viz_tracker = DeepSORTTracker(
101
  max_iou_distance=0.5,
102
  max_age=90,
103
  n_init=1,
104
- use_appearance=False
105
  )
106
 
 
 
 
 
 
 
107
  track_colors = {}
 
108
  frame_num = 0
109
 
110
- print("\nCreating visualization video...")
111
 
112
  while cap.isOpened():
113
  ret, frame = cap.read()
@@ -115,47 +122,60 @@ class DatasetCollectionApp:
115
  break
116
 
117
  if frame_num % sample_rate == 0:
 
118
  detections = self.detector.detect(frame)
 
 
119
  tracks = viz_tracker.update(detections)
120
 
 
121
  for track in tracks:
122
- if track.track_id not in track_colors:
123
- track_colors[track.track_id] = (
 
 
 
 
 
 
 
 
124
  np.random.randint(50, 255),
125
  np.random.randint(50, 255),
126
  np.random.randint(50, 255)
127
  )
128
 
129
  x1, y1, x2, y2 = map(int, track.bbox)
130
- color = track_colors[track.track_id]
131
 
132
- # Bold box (thickness = 6)
133
  cv2.rectangle(frame, (x1, y1), (x2, y2), color, 6)
134
 
135
  # Add ID label
136
- label = f"ID: {track.track_id}"
137
  font = cv2.FONT_HERSHEY_SIMPLEX
138
- font_scale = 1.2
139
  font_thickness = 3
140
 
 
141
  (text_width, text_height), baseline = cv2.getTextSize(
142
  label, font, font_scale, font_thickness
143
  )
144
 
145
- # Background rectangle for text
146
  cv2.rectangle(
147
  frame,
148
- (x1, y1 - text_height - 10),
149
- (x1 + text_width + 10, y1),
150
  color,
151
  -1
152
  )
153
 
154
- # White text
155
  cv2.putText(
156
  frame,
157
  label,
158
- (x1 + 5, y1 - 5),
159
  font,
160
  font_scale,
161
  (255, 255, 255),
@@ -172,9 +192,10 @@ class DatasetCollectionApp:
172
  cap.release()
173
  out.release()
174
 
175
- # Verify file was created and has content
176
  if os.path.exists(output_path) and os.path.getsize(output_path) > 1000:
177
  print(f"Visualization video saved: {output_path}")
 
178
  return output_path
179
  else:
180
  print("ERROR: Video file not created or is empty")
 
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():
 
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
 
 
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()
 
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),
 
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")