Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,71 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
<div style="width:100%;height:0;padding-bottom:50%;position:relative;">
|
| 7 |
-
<iframe style="border:none;position:absolute;top:0;left:0;width:100%;height:100%;min-height:360px;border:none;overflow:hidden !important;" src="//openspeedtest.com/speedtest"></iframe>
|
| 8 |
-
</div>
|
| 9 |
-
</div>
|
| 10 |
-
Provided by <a href="https://openspeedtest.com">OpenSpeedtest.com</a>
|
| 11 |
-
</div>''')
|
| 12 |
-
|
| 13 |
-
iface = gr.Interface(
|
| 14 |
-
fn=lambda x: x,
|
| 15 |
-
inputs=None,
|
| 16 |
-
outputs=iframe,
|
| 17 |
-
capture_session=False)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
iface.launch()
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
+
def remove_green_screen(video_file, bg_image):
|
| 6 |
+
# Load the background image
|
| 7 |
+
bg = cv2.imread(bg_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Create a video capture object to read from the video file
|
| 10 |
+
cap = cv2.VideoCapture(video_file)
|
| 11 |
+
|
| 12 |
+
# Get the video dimensions
|
| 13 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 14 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 15 |
+
|
| 16 |
+
# Create a video writer object to write the output to a new video file
|
| 17 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
| 18 |
+
out = cv2.VideoWriter("output.mp4", fourcc, 30, (width, height))
|
| 19 |
+
|
| 20 |
+
# Loop over each frame in the video
|
| 21 |
+
while True:
|
| 22 |
+
# Read a frame from the video
|
| 23 |
+
ret, frame = cap.read()
|
| 24 |
+
|
| 25 |
+
# If we have reached the end of the video, break out of the loop
|
| 26 |
+
if not ret:
|
| 27 |
+
break
|
| 28 |
+
|
| 29 |
+
# Convert the frame to HSV color space
|
| 30 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 31 |
+
|
| 32 |
+
# Define the range of green color in HSV
|
| 33 |
+
lower_green = np.array([40, 50, 50])
|
| 34 |
+
upper_green = np.array([90, 255, 255])
|
| 35 |
+
|
| 36 |
+
# Create a mask for the green color range
|
| 37 |
+
mask = cv2.inRange(hsv, lower_green, upper_green)
|
| 38 |
+
|
| 39 |
+
# Invert the mask
|
| 40 |
+
mask = cv2.bitwise_not(mask)
|
| 41 |
+
|
| 42 |
+
# Apply the mask to the frame to remove the green screen
|
| 43 |
+
fg = cv2.bitwise_and(frame, frame, mask=mask)
|
| 44 |
+
|
| 45 |
+
# Apply the mask to the background image
|
| 46 |
+
bg_mask = cv2.bitwise_not(mask)
|
| 47 |
+
bg_mask = cv2.cvtColor(bg_mask, cv2.COLOR_GRAY2BGR)
|
| 48 |
+
bg_fg = cv2.bitwise_and(bg, bg_mask)
|
| 49 |
+
|
| 50 |
+
# Combine the foreground and background images
|
| 51 |
+
output = cv2.add(fg, bg_fg)
|
| 52 |
+
|
| 53 |
+
# Write the output frame to the video file
|
| 54 |
+
out.write(output)
|
| 55 |
+
|
| 56 |
+
# Release the video capture and video writer objects
|
| 57 |
+
cap.release()
|
| 58 |
+
out.release()
|
| 59 |
+
|
| 60 |
+
return "output.mp4"
|
| 61 |
+
|
| 62 |
+
# Define the input and output components for the app
|
| 63 |
+
video_file = gr.inputs.Video(label="Input Video")
|
| 64 |
+
bg_image = gr.inputs.Image(label="Background Image")
|
| 65 |
+
output_file = gr.outputs.Video(label="Output Video")
|
| 66 |
+
|
| 67 |
+
# Create the app interface
|
| 68 |
+
iface = gr.Interface(fn=remove_green_screen, inputs=[video_file, bg_image], outputs=output_file)
|
| 69 |
+
|
| 70 |
+
# Launch the app
|
| 71 |
iface.launch()
|