File size: 1,289 Bytes
de2aa9b
ba4d1a9
 
e6399c3
ba4d1a9
 
ce6434e
ba4d1a9
6ef9294
ce6434e
6ef9294
ce6434e
 
 
6ef9294
ce6434e
 
6ef9294
 
ce6434e
e6399c3
 
ce6434e
e6399c3
 
ce6434e
e6399c3
 
ce6434e
69a2ec7
 
 
 
6e756ce
 
ce6434e
6ef9294
ce6434e
6e756ce
ce6434e
de2aa9b
6ef9294
ce6434e
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
import gradio as gr
import cv2
import numpy as np
from PIL import Image
from transparent_background import Remover

remover = Remover(mode='fast')  # Custom setting

def doo(video):
    cap = cv2.VideoCapture(video)  # Video reader for input
    fps = cap.get(cv2.CAP_PROP_FPS)

    processed_frames = []  # List to store processed frames

    while cap.isOpened():
        ret, frame = cap.read()  # Read video

        if ret is False:
            break

        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        img = Image.fromarray(frame).convert('RGB')

        # Process the frame using the transparent-background model
        out = remover.process(img, type='map')  # Same as image, except for 'rgba'

        # Convert the processed frame back to a NumPy array
        processed_frame = np.array(out)

        # Ensure the processed frame has shape (height, width, 3)
        if processed_frame.shape[2] != 3:
            raise ValueError("Processed frame does not have 3 channels (RGB)")

        # Append the processed frame to the list
        processed_frames.append(processed_frame)

    cap.release()

    # Return the list of processed frames (as NumPy arrays)
    return processed_frames

iface = gr.Interface(fn=doo, inputs="video", outputs="video")
iface.launch()