NihalGazi commited on
Commit
83956cb
·
verified ·
1 Parent(s): eb80591

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -12
app.py CHANGED
@@ -18,9 +18,9 @@ def extract_frames(video_path):
18
  cap = cv2.VideoCapture(video_path)
19
  frames = []
20
  while True:
21
- ret, frame = cap.read() # ret is True if a frame is read.
22
  if not ret:
23
- break # Stop if no more frames.
24
  frames.append(frame)
25
  cap.release()
26
  return frames
@@ -32,10 +32,10 @@ def apply_style_propagation(frames, style_image_path):
32
  """
33
  Applies the style from the provided image onto each video frame.
34
  Logic:
35
- - Resize the style image to match the video dimensions.
36
- - Use the style image as the starting point.
37
  - For each subsequent frame, compute optical flow between consecutive frames.
38
- - Warp the previous styled frame so that the style follows the motion.
39
  """
40
  # Load and resize the style image.
41
  style_image = cv2.imread(style_image_path)
@@ -83,7 +83,7 @@ def save_video(frames, output_path, fps=30):
83
  Combines frames into a video and saves it.
84
  Logic:
85
  - Create a VideoWriter with the specified FPS and frame size.
86
- - Write each frame to the video file.
87
  """
88
  h, w, _ = frames[0].shape
89
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' codec for MP4.
@@ -106,12 +106,24 @@ def process_video(video_file, style_image_file, fps=30):
106
  Returns:
107
  - Path to the generated styled video.
108
  """
109
- # Get file paths from the uploaded file objects.
110
- # If not a string, assume it's a dictionary with a "name" key.
111
  video_path = video_file if isinstance(video_file, str) else video_file["name"]
112
- style_image_path = style_image_file if isinstance(style_image_file, str) else style_image_file["name"]
113
 
114
- # Extract frames from the video.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  frames = extract_frames(video_path)
116
  if not frames:
117
  return "Error: No frames extracted from the video."
@@ -145,7 +157,7 @@ iface = gr.Interface(
145
  )
146
 
147
  # -----------------------------
148
- # Launch the Gradio App.
149
  # -----------------------------
150
  if __name__ == "__main__":
151
- iface.launch()
 
18
  cap = cv2.VideoCapture(video_path)
19
  frames = []
20
  while True:
21
+ ret, frame = cap.read() # ret is True if a frame is successfully read.
22
  if not ret:
23
+ break # Exit loop when no more frames.
24
  frames.append(frame)
25
  cap.release()
26
  return frames
 
32
  """
33
  Applies the style from the provided image onto each video frame.
34
  Logic:
35
+ - Load and resize the style image to match video dimensions.
36
+ - Use the style image as the first styled frame.
37
  - For each subsequent frame, compute optical flow between consecutive frames.
38
+ - Warp the previous styled frame using the flow so that the style follows the motion.
39
  """
40
  # Load and resize the style image.
41
  style_image = cv2.imread(style_image_path)
 
83
  Combines frames into a video and saves it.
84
  Logic:
85
  - Create a VideoWriter with the specified FPS and frame size.
86
+ - Write each frame sequentially to the video file.
87
  """
88
  h, w, _ = frames[0].shape
89
  fourcc = cv2.VideoWriter_fourcc(*'mp4v') # Use 'mp4v' codec for MP4.
 
106
  Returns:
107
  - Path to the generated styled video.
108
  """
109
+ # For the video file, we expect a file path.
 
110
  video_path = video_file if isinstance(video_file, str) else video_file["name"]
 
111
 
112
+ # For the style image, Gradio might return a numpy array.
113
+ if isinstance(style_image_file, str):
114
+ style_image_path = style_image_file
115
+ elif isinstance(style_image_file, dict) and "name" in style_image_file:
116
+ style_image_path = style_image_file["name"]
117
+ elif isinstance(style_image_file, np.ndarray):
118
+ # If the image is a numpy array, save it to a temporary file.
119
+ tmp_path = os.path.join(tempfile.gettempdir(), "temp_style_image.jpeg")
120
+ # Gradio images are usually in RGB; OpenCV uses BGR.
121
+ cv2.imwrite(tmp_path, cv2.cvtColor(style_image_file, cv2.COLOR_RGB2BGR))
122
+ style_image_path = tmp_path
123
+ else:
124
+ return "Error: Unsupported style image file format."
125
+
126
+ # Extract frames from the input video.
127
  frames = extract_frames(video_path)
128
  if not frames:
129
  return "Error: No frames extracted from the video."
 
157
  )
158
 
159
  # -----------------------------
160
+ # Launch the Gradio App with public sharing enabled.
161
  # -----------------------------
162
  if __name__ == "__main__":
163
+ iface.launch(share=True)