Update app.py
Browse files
app.py
CHANGED
@@ -2,66 +2,72 @@ import gradio as gr
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
# Load background image
|
23 |
-
bg_image = cv2.imread("background.jpg")
|
24 |
-
|
25 |
-
# Open video file
|
26 |
-
cap = cv2.VideoCapture(video)
|
27 |
-
|
28 |
-
# Get video codec and fps
|
29 |
-
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
|
30 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
35 |
-
|
36 |
-
# Create output video writer
|
37 |
-
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height))
|
38 |
-
|
39 |
-
frames = []
|
40 |
-
|
41 |
-
# Loop through video frames
|
42 |
while True:
|
43 |
ret, frame = cap.read()
|
44 |
if not ret:
|
45 |
break
|
46 |
-
|
47 |
-
|
48 |
-
bg = cv2.imread(bg_image)
|
49 |
-
frames.append(process_frame(frame))
|
50 |
-
|
51 |
-
# Release video file and output video writer
|
52 |
cap.release()
|
53 |
out.release()
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
for frame in frames:
|
58 |
-
out.write(frame)
|
59 |
-
out.release()
|
60 |
-
|
61 |
return "output.mp4"
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
|
5 |
+
def process_frame(frame, bg_image):
|
6 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
7 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_RGB2HSV)
|
8 |
+
lower_green = np.array([45, 100, 50])
|
9 |
+
upper_green = np.array([75, 255, 255])
|
10 |
+
mask = cv2.inRange(hsv, lower_green, upper_green)
|
11 |
+
mask_inv = cv2.bitwise_not(mask)
|
12 |
+
bg = cv2.imread(bg_image)
|
13 |
+
bg = cv2.resize(bg, (frame.shape[1], frame.shape[0]))
|
14 |
+
fg = cv2.bitwise_and(frame, frame, mask=mask_inv)
|
15 |
+
bg = cv2.bitwise_and(bg, bg, mask=mask)
|
16 |
+
result = cv2.add(bg, fg)
|
17 |
+
return result
|
18 |
+
|
19 |
+
def remove_green_screen(input_video, bg_image):
|
20 |
+
cap = cv2.VideoCapture(input_video.name)
|
21 |
+
codec = cv2.VideoWriter_fourcc(*"mp4v")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
23 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
24 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
25 |
+
out = cv2.VideoWriter("output.mp4", codec, fps, (frame_width, frame_height))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
while True:
|
27 |
ret, frame = cap.read()
|
28 |
if not ret:
|
29 |
break
|
30 |
+
result = process_frame(frame, bg_image)
|
31 |
+
out.write(result)
|
|
|
|
|
|
|
|
|
32 |
cap.release()
|
33 |
out.release()
|
34 |
|
35 |
+
def predict(input_video, bg_image):
|
36 |
+
remove_green_screen(input_video, bg_image)
|
|
|
|
|
|
|
|
|
37 |
return "output.mp4"
|
38 |
|
39 |
+
inputs = [
|
40 |
+
gr.inputs.Video(label="Input Video"),
|
41 |
+
gr.inputs.Image(label="Background Image")
|
42 |
+
]
|
43 |
+
|
44 |
+
outputs = gr.outputs.Video(label="Processed Video", type="auto")
|
45 |
+
|
46 |
+
title = "Green Screen Remover"
|
47 |
+
description = "Upload a video and an image to use as the background to remove the green screen."
|
48 |
+
article = "<p style='text-align: center'><a href='https://github.com/gradio-app/examples/blob/master/green_screen_removal.py'>This code</a> was made into an interactive interface using Gradio. See the full tutorial at <a href='https://blog.gradio.app/green-screen-removal-with-opencv/'>this blog post</a> on the Gradio blog.</p>"
|
49 |
+
examples = [
|
50 |
+
[
|
51 |
+
"https://www.youtube.com/watch?v=clD6_yXKo2I",
|
52 |
+
"https://i.imgur.com/lxIhsG6.jpg"
|
53 |
+
],
|
54 |
+
[
|
55 |
+
"https://www.youtube.com/watch?v=6DfZ6UOZi0A",
|
56 |
+
"https://i.imgur.com/6UaTvfo.jpg"
|
57 |
+
],
|
58 |
+
]
|
59 |
+
|
60 |
+
iface = gr.Interface(
|
61 |
+
fn=predict,
|
62 |
+
inputs=inputs,
|
63 |
+
outputs=outputs,
|
64 |
+
title=title,
|
65 |
+
description=description,
|
66 |
+
article=article,
|
67 |
+
examples=examples,
|
68 |
+
analytics_enabled=False,
|
69 |
+
server_port=8000
|
70 |
+
)
|
71 |
+
|
72 |
+
if __name__ == '__main__':
|
73 |
+
iface.launch()
|