willwade commited on
Commit
13f27c7
·
1 Parent(s): 88a03bc

fix frames as video

Browse files
Files changed (1) hide show
  1. app.py +49 -36
app.py CHANGED
@@ -19,6 +19,10 @@ class ChaplinGradio:
19
  self.frame_interval = 1 / self.fps
20
  self.frame_compression = 25
21
  self.last_frame_time = time.time()
 
 
 
 
22
 
23
  def download_models(self):
24
  """Download required model files from HuggingFace"""
@@ -57,7 +61,7 @@ class ChaplinGradio:
57
  print("Model loaded successfully!")
58
 
59
  def process_frame(self, frame):
60
- """Process a single frame with rate limiting and compression"""
61
  current_time = time.time()
62
 
63
  if current_time - self.last_frame_time < self.frame_interval:
@@ -69,50 +73,59 @@ class ChaplinGradio:
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
 
19
  self.frame_interval = 1 / self.fps
20
  self.frame_compression = 25
21
  self.last_frame_time = time.time()
22
+
23
+ # Frame buffer
24
+ self.frame_buffer = []
25
+ self.buffer_size = 16 # Number of frames to accumulate before processing
26
 
27
  def download_models(self):
28
  """Download required model files from HuggingFace"""
 
61
  print("Model loaded successfully!")
62
 
63
  def process_frame(self, frame):
64
+ """Process frames with buffering"""
65
  current_time = time.time()
66
 
67
  if current_time - self.last_frame_time < self.frame_interval:
 
73
  return "No video input detected"
74
 
75
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  # Convert frame to grayscale if it's not already
77
  if len(frame.shape) == 3:
78
  frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
 
 
 
 
79
 
80
+ # Add frame to buffer
81
+ self.frame_buffer.append(frame)
82
+
83
+ # Only process when we have enough frames
84
+ if len(self.frame_buffer) >= self.buffer_size:
85
+ # Create temp directory if it doesn't exist
86
+ os.makedirs("temp", exist_ok=True)
87
+
88
+ # Generate temporary video file path
89
+ temp_video = f"temp/frames_{time.time_ns()}.mp4"
90
+
91
+ # Get frame dimensions from first frame
92
+ frame_height, frame_width = self.frame_buffer[0].shape[:2]
93
 
94
+ # Create video writer
95
+ out = cv2.VideoWriter(
96
+ temp_video,
97
+ cv2.VideoWriter_fourcc(*'mp4v'),
98
+ self.fps,
99
+ (frame_width, frame_height),
100
+ False # isColor
101
+ )
102
 
103
+ # Write all frames to video
104
+ for f in self.frame_buffer:
105
+ out.write(f)
106
+ out.release()
107
 
108
+ # Clear buffer
109
+ self.frame_buffer = []
110
+
111
+ try:
112
+ # Process the video file using the pipeline
113
+ predicted_text = self.vsr_model(temp_video)
114
+ return predicted_text
115
+
116
+ except Exception as e:
117
+ print(f"Error during inference: {str(e)}")
118
+ return f"Error processing frames: {str(e)}"
119
+ finally:
120
+ # Clean up temp file
121
+ if os.path.exists(temp_video):
122
+ os.remove(temp_video)
123
+
124
+ return "Collecting frames..." # Return status while collecting frames
125
 
126
  except Exception as e:
127
+ print(f"Error processing: {str(e)}")
128
+ return f"Error processing: {str(e)}"
 
 
 
 
129
 
130
 
131
  # Create Gradio interface