Update app.py
Browse files
app.py
CHANGED
|
@@ -3,69 +3,25 @@ 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 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 |
-
|
| 71 |
iface.launch()
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
def remove_green_screen(video_file, bg_image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
cap = cv2.VideoCapture(video_file)
|
| 7 |
+
background = cv2.imread(bg_image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
while True:
|
|
|
|
| 9 |
ret, frame = cap.read()
|
|
|
|
|
|
|
| 10 |
if not ret:
|
| 11 |
break
|
|
|
|
|
|
|
| 12 |
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 13 |
+
lower_green = np.array([35, 0, 0])
|
|
|
|
|
|
|
| 14 |
upper_green = np.array([90, 255, 255])
|
|
|
|
|
|
|
| 15 |
mask = cv2.inRange(hsv, lower_green, upper_green)
|
| 16 |
+
mask_inv = cv2.bitwise_not(mask)
|
| 17 |
+
fg = cv2.bitwise_and(frame, frame, mask=mask_inv)
|
| 18 |
+
bg = cv2.bitwise_and(background, background, mask=mask)
|
| 19 |
+
final = cv2.add(fg, bg)
|
| 20 |
+
cv2.imshow("output", final)
|
| 21 |
+
if cv2.waitKey(1) & 0xFF == ord("q"):
|
| 22 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
cap.release()
|
| 24 |
+
cv2.destroyAllWindows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
iface = gr.Interface(fn=remove_green_screen, inputs=["file", "image"], outputs=None, title="Green Screen Remover")
|
| 27 |
iface.launch()
|