amirgame197 commited on
Commit
e50e349
1 Parent(s): c77150e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -1,43 +1,39 @@
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
 
4
  from PIL import Image
5
  from transparent_background import Remover
6
 
7
- remover = Remover(mode='fast') # Custom setting
8
 
9
  def doo(video):
10
- cap = cv2.VideoCapture(video) # Video reader for input
11
  fps = cap.get(cv2.CAP_PROP_FPS)
12
-
13
- processed_frames = [] # List to store processed frames
14
-
15
  while cap.isOpened():
16
- ret, frame = cap.read() # Read video
17
-
18
  if ret is False:
19
  break
20
-
21
- frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
22
  img = Image.fromarray(frame).convert('RGB')
23
-
24
- # Process the frame using the transparent-background model
25
- out = remover.process(img, type='map') # Same as image, except for 'rgba'
26
-
27
- # Convert the processed frame back to a NumPy array
28
- processed_frame = np.array(out)
29
-
30
- # Ensure the processed frame has shape (height, width, 3)
31
- if processed_frame.shape[2] != 3:
32
- raise ValueError("Processed frame does not have 3 channels (RGB)")
33
-
34
- # Append the processed frame to the list
35
- processed_frames.append(processed_frame)
36
-
37
  cap.release()
 
 
38
 
39
- # Return the processed frames (as a list of NumPy arrays) and the original video's frame rate
40
- return processed_frames, fps
41
 
42
- iface = gr.Interface(fn=doo, inputs="video", outputs="video")
43
  iface.launch()
 
1
  import gradio as gr
2
  import cv2
3
  import numpy as np
4
+
5
  from PIL import Image
6
  from transparent_background import Remover
7
 
8
+ remover = Remover(mode='fast') # custom setting
9
 
10
  def doo(video):
11
+ cap = cv2.VideoCapture(video) # video reader for input
12
  fps = cap.get(cv2.CAP_PROP_FPS)
13
+
14
+ writer = None
15
+
16
  while cap.isOpened():
17
+ ret, frame = cap.read() # read video
18
+
19
  if ret is False:
20
  break
21
+
22
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
23
  img = Image.fromarray(frame).convert('RGB')
24
+
25
+ if writer is None:
26
+ writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size) # video writer for output
27
+
28
+ out = remover.process(img, type='map') # same as image, except for 'rgba' which is not for video.
29
+ writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
30
+
 
 
 
 
 
 
 
31
  cap.release()
32
+ writer.release()
33
+ return 'output.mp4' # returning the file path of the processed video
34
 
35
+ def display_video(output_video):
36
+ return output_video
37
 
38
+ iface = gr.Interface(fn=doo, inputs="video", outputs=display_video)
39
  iface.launch()