Spaces:
Runtime error
Runtime error
add more debug
Browse files
app.py
CHANGED
|
@@ -65,41 +65,46 @@ class ChaplinGradio:
|
|
| 65 |
def process_frame(self, frame):
|
| 66 |
"""Process frames with buffering"""
|
| 67 |
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if current_time - self.last_frame_time < self.frame_interval:
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
self.last_frame_time = current_time
|
| 73 |
|
| 74 |
if frame is None:
|
| 75 |
-
|
| 76 |
-
return "No video input detected"
|
| 77 |
|
| 78 |
try:
|
| 79 |
-
|
| 80 |
|
| 81 |
# Convert frame to grayscale if it's not already
|
| 82 |
if len(frame.shape) == 3:
|
| 83 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 84 |
-
|
| 85 |
|
| 86 |
# Add frame to buffer
|
| 87 |
self.frame_buffer.append(frame)
|
| 88 |
-
|
| 89 |
|
| 90 |
# Process when we have enough frames
|
| 91 |
if len(self.frame_buffer) >= self.min_frames:
|
| 92 |
-
|
| 93 |
# Create temp directory if it doesn't exist
|
| 94 |
os.makedirs("temp", exist_ok=True)
|
| 95 |
|
| 96 |
# Generate temporary video file path
|
| 97 |
temp_video = f"temp/frames_{time.time_ns()}.mp4"
|
| 98 |
-
|
| 99 |
|
| 100 |
# Get frame dimensions from first frame
|
| 101 |
frame_height, frame_width = self.frame_buffer[0].shape[:2]
|
| 102 |
-
|
| 103 |
|
| 104 |
# Create video writer
|
| 105 |
out = cv2.VideoWriter(
|
|
@@ -113,36 +118,38 @@ class ChaplinGradio:
|
|
| 113 |
# Write all frames to video
|
| 114 |
for i, f in enumerate(self.frame_buffer):
|
| 115 |
out.write(f)
|
| 116 |
-
|
| 117 |
out.release()
|
| 118 |
|
| 119 |
# Clear buffer but keep last few frames for continuity
|
| 120 |
self.frame_buffer = self.frame_buffer[-8:] # Keep last 0.5 seconds
|
| 121 |
-
|
| 122 |
|
| 123 |
try:
|
| 124 |
# Process the video file using the pipeline
|
| 125 |
-
|
| 126 |
predicted_text = self.vsr_model(temp_video)
|
| 127 |
-
|
| 128 |
if predicted_text:
|
| 129 |
self.last_prediction = predicted_text
|
| 130 |
-
return self.last_prediction
|
| 131 |
|
| 132 |
except Exception as e:
|
| 133 |
-
|
| 134 |
-
|
|
|
|
| 135 |
finally:
|
| 136 |
# Clean up temp file
|
| 137 |
if os.path.exists(temp_video):
|
| 138 |
os.remove(temp_video)
|
| 139 |
-
|
| 140 |
|
| 141 |
-
return self.last_prediction or "Waiting for speech..."
|
| 142 |
|
| 143 |
except Exception as e:
|
| 144 |
-
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
|
| 148 |
# Create Gradio interface
|
|
|
|
| 65 |
def process_frame(self, frame):
|
| 66 |
"""Process frames with buffering"""
|
| 67 |
current_time = time.time()
|
| 68 |
+
debug_log = [] # List to collect debug messages
|
| 69 |
+
|
| 70 |
+
# Add initial debug info
|
| 71 |
+
debug_log.append(f"Current time: {current_time}")
|
| 72 |
|
| 73 |
if current_time - self.last_frame_time < self.frame_interval:
|
| 74 |
+
debug_log.append("Skipping frame - too soon")
|
| 75 |
+
return self.last_prediction, "\n".join(debug_log) # Make sure we return both values
|
| 76 |
|
| 77 |
self.last_frame_time = current_time
|
| 78 |
|
| 79 |
if frame is None:
|
| 80 |
+
debug_log.append("Received None frame")
|
| 81 |
+
return "No video input detected", "\n".join(debug_log)
|
| 82 |
|
| 83 |
try:
|
| 84 |
+
debug_log.append(f"Received frame with shape: {frame.shape}")
|
| 85 |
|
| 86 |
# Convert frame to grayscale if it's not already
|
| 87 |
if len(frame.shape) == 3:
|
| 88 |
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 89 |
+
debug_log.append("Converted frame to grayscale")
|
| 90 |
|
| 91 |
# Add frame to buffer
|
| 92 |
self.frame_buffer.append(frame)
|
| 93 |
+
debug_log.append(f"Buffer size now: {len(self.frame_buffer)}/{self.min_frames}")
|
| 94 |
|
| 95 |
# Process when we have enough frames
|
| 96 |
if len(self.frame_buffer) >= self.min_frames:
|
| 97 |
+
debug_log.append("Processing buffer - have enough frames")
|
| 98 |
# Create temp directory if it doesn't exist
|
| 99 |
os.makedirs("temp", exist_ok=True)
|
| 100 |
|
| 101 |
# Generate temporary video file path
|
| 102 |
temp_video = f"temp/frames_{time.time_ns()}.mp4"
|
| 103 |
+
debug_log.append(f"Created temp video path: {temp_video}")
|
| 104 |
|
| 105 |
# Get frame dimensions from first frame
|
| 106 |
frame_height, frame_width = self.frame_buffer[0].shape[:2]
|
| 107 |
+
debug_log.append(f"Video dimensions: {frame_width}x{frame_height}")
|
| 108 |
|
| 109 |
# Create video writer
|
| 110 |
out = cv2.VideoWriter(
|
|
|
|
| 118 |
# Write all frames to video
|
| 119 |
for i, f in enumerate(self.frame_buffer):
|
| 120 |
out.write(f)
|
| 121 |
+
debug_log.append(f"Wrote {i+1} frames to video")
|
| 122 |
out.release()
|
| 123 |
|
| 124 |
# Clear buffer but keep last few frames for continuity
|
| 125 |
self.frame_buffer = self.frame_buffer[-8:] # Keep last 0.5 seconds
|
| 126 |
+
debug_log.append(f"Cleared buffer, kept {len(self.frame_buffer)} frames")
|
| 127 |
|
| 128 |
try:
|
| 129 |
# Process the video file using the pipeline
|
| 130 |
+
debug_log.append("Starting model inference...")
|
| 131 |
predicted_text = self.vsr_model(temp_video)
|
| 132 |
+
debug_log.append(f"Model prediction: {predicted_text}")
|
| 133 |
if predicted_text:
|
| 134 |
self.last_prediction = predicted_text
|
| 135 |
+
return (self.last_prediction or "Waiting for speech..."), "\n".join(debug_log)
|
| 136 |
|
| 137 |
except Exception as e:
|
| 138 |
+
error_msg = f"Error during inference: {str(e)}"
|
| 139 |
+
debug_log.append(error_msg)
|
| 140 |
+
return f"Error processing frames: {str(e)}", "\n".join(debug_log)
|
| 141 |
finally:
|
| 142 |
# Clean up temp file
|
| 143 |
if os.path.exists(temp_video):
|
| 144 |
os.remove(temp_video)
|
| 145 |
+
debug_log.append("Cleaned up temp video file")
|
| 146 |
|
| 147 |
+
return (self.last_prediction or "Waiting for speech..."), "\n".join(debug_log)
|
| 148 |
|
| 149 |
except Exception as e:
|
| 150 |
+
error_msg = f"Error processing: {str(e)}"
|
| 151 |
+
debug_log.append(error_msg)
|
| 152 |
+
return f"Error processing: {str(e)}", "\n".join(debug_log)
|
| 153 |
|
| 154 |
|
| 155 |
# Create Gradio interface
|