Sean Carnahan commited on
Commit
e7825c1
·
1 Parent(s): 1a0875b

Add robust logging and checks to ensure output video is valid and not empty

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -183,7 +183,6 @@ def process_video_movenet(video_path):
183
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
184
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
185
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
186
-
187
  print(f"[DEBUG] Video properties - FPS: {fps}, Width: {width}, Height: {height}, Total Frames: {total_frames}")
188
 
189
  # Initialize MoveNet model on GPU if available
@@ -216,17 +215,18 @@ def process_video_movenet(video_path):
216
  ret, frame = cap.read()
217
  if not ret:
218
  break
219
-
220
  frame_count += 1
221
  if frame_count % 10 != 0: # Process every 10th frame
222
  continue
223
-
224
  try:
 
 
 
 
225
  # Resize and pad the image to keep aspect ratio
226
  img = frame.copy()
227
  img = tf.image.resize_with_pad(tf.expand_dims(img, axis=0), 192, 192)
228
  img = tf.cast(img, dtype=tf.int32)
229
-
230
  # Run inference on GPU if available
231
  if gpus:
232
  with tf.device('/GPU:0'):
@@ -236,31 +236,31 @@ def process_video_movenet(video_path):
236
  with tf.device('/CPU:0'):
237
  results = movenet(img)
238
  keypoints = results['output_0'].numpy()
239
-
240
  # Process keypoints and draw on frame
241
  y, x, c = frame.shape
242
  shaped = np.squeeze(keypoints)
243
-
244
  for kp in range(17):
245
  ky, kx, kp_conf = shaped[kp]
246
  if kp_conf > 0.3:
247
  cx, cy = int(kx * x), int(ky * y)
248
  cv2.circle(frame, (cx, cy), 6, (0, 255, 0), -1)
249
-
250
  out.write(frame)
251
  processed_frames += 1
252
-
253
  except Exception as e:
254
  print(f"[ERROR] Error processing frame {frame_count}: {str(e)}")
255
  continue
256
-
257
  cap.release()
258
  out.release()
259
-
260
  print(f"[DEBUG] Processed {processed_frames} frames out of {total_frames} total frames")
261
- # Return the correct URL for the output video
 
 
 
 
 
 
262
  return url_for('serve_video', filename=output_filename, _external=False)
263
-
264
  except Exception as e:
265
  print(f"[ERROR] Error in process_video_movenet: {str(e)}")
266
  traceback.print_exc()
 
183
  width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
184
  height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
185
  total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
 
186
  print(f"[DEBUG] Video properties - FPS: {fps}, Width: {width}, Height: {height}, Total Frames: {total_frames}")
187
 
188
  # Initialize MoveNet model on GPU if available
 
215
  ret, frame = cap.read()
216
  if not ret:
217
  break
 
218
  frame_count += 1
219
  if frame_count % 10 != 0: # Process every 10th frame
220
  continue
 
221
  try:
222
+ # Ensure frame size matches VideoWriter
223
+ if frame.shape[1] != width or frame.shape[0] != height:
224
+ print(f"[WARNING] Frame size {frame.shape[1]}x{frame.shape[0]} does not match VideoWriter size {width}x{height}. Resizing.")
225
+ frame = cv2.resize(frame, (width, height))
226
  # Resize and pad the image to keep aspect ratio
227
  img = frame.copy()
228
  img = tf.image.resize_with_pad(tf.expand_dims(img, axis=0), 192, 192)
229
  img = tf.cast(img, dtype=tf.int32)
 
230
  # Run inference on GPU if available
231
  if gpus:
232
  with tf.device('/GPU:0'):
 
236
  with tf.device('/CPU:0'):
237
  results = movenet(img)
238
  keypoints = results['output_0'].numpy()
 
239
  # Process keypoints and draw on frame
240
  y, x, c = frame.shape
241
  shaped = np.squeeze(keypoints)
 
242
  for kp in range(17):
243
  ky, kx, kp_conf = shaped[kp]
244
  if kp_conf > 0.3:
245
  cx, cy = int(kx * x), int(ky * y)
246
  cv2.circle(frame, (cx, cy), 6, (0, 255, 0), -1)
 
247
  out.write(frame)
248
  processed_frames += 1
249
+ print(f"[DEBUG] Wrote frame {frame_count} to output video.")
250
  except Exception as e:
251
  print(f"[ERROR] Error processing frame {frame_count}: {str(e)}")
252
  continue
 
253
  cap.release()
254
  out.release()
 
255
  print(f"[DEBUG] Processed {processed_frames} frames out of {total_frames} total frames")
256
+ # Check output file size
257
+ if not os.path.exists(output_path):
258
+ raise ValueError(f"Output video file was not created: {output_path}")
259
+ file_size = os.path.getsize(output_path)
260
+ print(f"[DEBUG] Output video file size: {file_size} bytes")
261
+ if processed_frames == 0 or file_size < 1000:
262
+ raise ValueError(f"Output video file is empty or too small: {output_path}")
263
  return url_for('serve_video', filename=output_filename, _external=False)
 
264
  except Exception as e:
265
  print(f"[ERROR] Error in process_video_movenet: {str(e)}")
266
  traceback.print_exc()