Spaces:
Runtime error
Runtime error
fix frames
Browse files
app.py
CHANGED
|
@@ -68,15 +68,51 @@ class ChaplinGradio:
|
|
| 68 |
if frame is None:
|
| 69 |
return "No video input detected"
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
|
| 82 |
# Create Gradio interface
|
|
|
|
| 68 |
if frame is None:
|
| 69 |
return "No video input detected"
|
| 70 |
|
| 71 |
+
try:
|
| 72 |
+
# Create temp directory if it doesn't exist
|
| 73 |
+
os.makedirs("temp", exist_ok=True)
|
| 74 |
+
|
| 75 |
+
# Generate temporary video file path
|
| 76 |
+
temp_video = f"temp/frame_{time.time_ns()}.mp4"
|
| 77 |
+
|
| 78 |
+
# Compress and save frame as video
|
| 79 |
+
frame_height, frame_width = frame.shape[:2]
|
| 80 |
+
out = cv2.VideoWriter(
|
| 81 |
+
temp_video,
|
| 82 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
| 83 |
+
self.fps,
|
| 84 |
+
(frame_width, frame_height),
|
| 85 |
+
False # isColor
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
# Convert frame to grayscale if it's not already
|
| 89 |
+
if len(frame.shape) == 3:
|
| 90 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
|
| 91 |
+
|
| 92 |
+
# Write frame to video
|
| 93 |
+
out.write(frame)
|
| 94 |
+
out.release()
|
| 95 |
+
|
| 96 |
+
# Process the video file using the pipeline
|
| 97 |
+
try:
|
| 98 |
+
predicted_text = self.vsr_model(temp_video)
|
| 99 |
+
|
| 100 |
+
# Clean up temp file
|
| 101 |
+
os.remove(temp_video)
|
| 102 |
+
|
| 103 |
+
return predicted_text
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error during inference: {str(e)}")
|
| 107 |
+
return f"Error processing frame: {str(e)}"
|
| 108 |
+
|
| 109 |
+
except Exception as e:
|
| 110 |
+
print(f"Error saving frame: {str(e)}")
|
| 111 |
+
return f"Error saving frame: {str(e)}"
|
| 112 |
+
finally:
|
| 113 |
+
# Make sure we always try to clean up
|
| 114 |
+
if 'temp_video' in locals() and os.path.exists(temp_video):
|
| 115 |
+
os.remove(temp_video)
|
| 116 |
|
| 117 |
|
| 118 |
# Create Gradio interface
|