File size: 1,776 Bytes
69a9435
0094cc8
 
b250141
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69a9435
 
b250141
 
 
 
69a9435
b250141
 
 
69a9435
 
 
b250141
 
 
589c5cb
b250141
 
0094cc8
69a9435
 
b250141
 
 
 
 
589c5cb
b250141
589c5cb
 
b250141
 
589c5cb
b250141
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import gradio as gr
import cv2
import numpy as np

def remove_green_screen(video):
    def process_frame(frame):
        # Convert to HSV color space
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        # Define the range of green color
        lower_green = np.array([25, 52, 72])
        upper_green = np.array([102, 255, 255])

        # Threshold the HSV image to get only green colors
        mask = cv2.inRange(hsv, lower_green, upper_green)

        # Bitwise-AND mask and original image
        res = cv2.bitwise_and(frame, frame, mask=mask)

        return res

    # Load background image
    bg_image = cv2.imread("background.jpg")

    # Open video file
    cap = cv2.VideoCapture(video)

    # Get video codec and fps
    fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
    fps = int(cap.get(cv2.CAP_PROP_FPS))

    # Get video dimensions
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    # Create output video writer
    out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height))

    frames = []

    # Loop through video frames
    while True:
        ret, frame = cap.read()
        if not ret:
            break

        # Process frame
        bg = cv2.imread(bg_image)
        frames.append(process_frame(frame))

    # Release video file and output video writer
    cap.release()
    out.release()

    # Write output video
    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()