ayushsaun commited on
Commit
9b02776
·
1 Parent(s): ffdb6f1

Fix video output generation and HF deployment issues

Browse files
Files changed (1) hide show
  1. inference.py +21 -6
inference.py CHANGED
@@ -187,12 +187,13 @@ class ObjectTrackerInference:
187
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
188
  print(f"Video: {w}x{h}, {total_frames} frames")
189
 
190
- fourcc = cv2.VideoWriter_fourcc(*'avc1')
191
- out=cv2.VideoWriter(output_path, fourcc, fps, (w,h))
192
-
 
 
193
  if not out.isOpened():
194
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
195
- out=cv2.VideoWriter(output_path, fourcc, fps, (w,h))
196
 
197
  # Reset state
198
  self.motion.prev_frame = None
@@ -215,7 +216,17 @@ class ObjectTrackerInference:
215
 
216
  pos=self.position_model.predict(self.position_scaler.transform(feats))
217
  size=self.size_model.predict(self.size_scaler.transform(feats))
218
- pred=[int(pos[0,0]),int(pos[0,1]),int(size[0,0]),int(size[0,1])]
 
 
 
 
 
 
 
 
 
 
219
 
220
  self.template_update_counter+=1
221
  if self.template_update_counter>=5 and self.prev_bbox is not None:
@@ -241,6 +252,10 @@ class ObjectTrackerInference:
241
 
242
  # Draw tracked bounding box
243
  x,y,w1,h1=pred
 
 
 
 
244
  cv2.rectangle(frame,(x,y),(x+w1,y+h1),(0,255,0),2)
245
  cv2.putText(frame,f'Frame: {frame_idx}',(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
246
 
 
187
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
188
  print(f"Video: {w}x{h}, {total_frames} frames")
189
 
190
+ fourcc = cv2.VideoWriter_fourcc(*'mp4v')
191
+ output_path = os.path.abspath(output_path)
192
+ out = cv2.VideoWriter(output_path, fourcc, fps, (w, h))
193
+
194
+
195
  if not out.isOpened():
196
+ raise RuntimeError("VideoWriter failed to open")
 
197
 
198
  # Reset state
199
  self.motion.prev_frame = None
 
216
 
217
  pos=self.position_model.predict(self.position_scaler.transform(feats))
218
  size=self.size_model.predict(self.size_scaler.transform(feats))
219
+ x = int(pos[0,0])
220
+ y = int(pos[0,1])
221
+ w1 = int(size[0,0])
222
+ h1 = int(size[0,1])
223
+
224
+ x = max(0, min(x, w - 1))
225
+ y = max(0, min(y, h - 1))
226
+ w1 = max(10, min(w1, w - x))
227
+ h1 = max(10, min(h1, h - y))
228
+
229
+ pred = [x, y, w1, h1]
230
 
231
  self.template_update_counter+=1
232
  if self.template_update_counter>=5 and self.prev_bbox is not None:
 
252
 
253
  # Draw tracked bounding box
254
  x,y,w1,h1=pred
255
+ x = max(0, min(x, w - 1))
256
+ y = max(0, min(y, h - 1))
257
+ w1 = max(1, min(w1, w - x))
258
+ h1 = max(1, min(h1, h - y))
259
  cv2.rectangle(frame,(x,y),(x+w1,y+h1),(0,255,0),2)
260
  cv2.putText(frame,f'Frame: {frame_idx}',(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,255),2)
261