|
import gradio as gr |
|
import cv2 |
|
import numpy as np |
|
|
|
def remove_green_screen(video): |
|
def process_frame(frame): |
|
|
|
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) |
|
|
|
|
|
lower_green = np.array([25, 52, 72]) |
|
upper_green = np.array([102, 255, 255]) |
|
|
|
|
|
mask = cv2.inRange(hsv, lower_green, upper_green) |
|
|
|
|
|
res = cv2.bitwise_and(frame, frame, mask=mask) |
|
|
|
return res |
|
|
|
|
|
bg_image = cv2.imread("background.jpg") |
|
|
|
|
|
cap = cv2.VideoCapture(video) |
|
|
|
|
|
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) |
|
fps = int(cap.get(cv2.CAP_PROP_FPS)) |
|
|
|
|
|
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
|
|
|
|
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height)) |
|
|
|
frames = [] |
|
|
|
|
|
while True: |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
bg = cv2.imread(bg_image) |
|
frames.append(process_frame(frame)) |
|
|
|
|
|
cap.release() |
|
out.release() |
|
|
|
|
|
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height)) |
|
for frame in frames: |
|
out.write(frame) |
|
out.release() |
|
|
|
return "output.mp4" |
|
|
|
|
|
input_video = gr.inputs.Video(label="Input Video") |
|
outputs = gr.outputs.Video(label="Processed Video", type="mp4") |
|
|
|
gr.Interface(remove_green_screen, inputs=input_video, outputs=outputs).launch() |
|
|