Update app.py
Browse files
app.py
CHANGED
@@ -1,39 +1,46 @@
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
|
|
|
|
4 |
|
5 |
def remove_green_screen(video_file, bg_image):
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
while True:
|
10 |
-
ret, frame = cap.read()
|
11 |
-
if not ret:
|
12 |
-
break
|
13 |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
cap.release()
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
gr.Interface(fn=remove_green_screen, inputs=[input_video, input_bg], outputs=output_video, title="Green Screen Remover").launch()
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
import gradio as gr
|
4 |
+
from tqdm import tqdm
|
5 |
+
|
6 |
|
7 |
def remove_green_screen(video_file, bg_image):
|
8 |
+
|
9 |
+
def process_frame(frame):
|
10 |
+
# remove green screen
|
|
|
|
|
|
|
|
|
11 |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
12 |
+
mask = cv2.inRange(hsv, (36, 25, 25), (86, 255, 255))
|
13 |
+
mask = cv2.merge((mask, mask, mask))
|
14 |
+
masked = cv2.bitwise_and(frame, mask)
|
15 |
+
|
16 |
+
# add background
|
17 |
+
bg = cv2.imread(bg_image)
|
18 |
+
bg = cv2.resize(bg, (frame.shape[1], frame.shape[0]))
|
19 |
+
masked[np.where((masked == [0, 0, 0]).all(axis=2))] = bg[np.where((masked == [0, 0, 0]).all(axis=2))]
|
20 |
+
|
21 |
+
return masked
|
22 |
+
|
23 |
+
cap = cv2.VideoCapture(video_file)
|
24 |
+
frames = []
|
25 |
+
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
26 |
+
for i in tqdm(range(frame_count)):
|
27 |
+
success, frame = cap.read()
|
28 |
+
if not success:
|
29 |
+
continue
|
30 |
+
frames.append(process_frame(frame))
|
31 |
+
|
32 |
cap.release()
|
33 |
+
|
34 |
+
return np.array(frames)
|
35 |
+
|
36 |
+
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=remove_green_screen,
|
39 |
+
inputs=[
|
40 |
+
gr.inputs.Video(type="mp4", label="Input video"),
|
41 |
+
gr.inputs.Image(label="Background image")
|
42 |
+
],
|
43 |
+
outputs=gr.outputs.Video(label="Processed video")
|
44 |
+
)
|
45 |
+
|
46 |
+
iface.launch()
|
|
|
|