File size: 1,135 Bytes
de2aa9b
ba4d1a9
 
e50e349
e6399c3
ba4d1a9
 
e50e349
ba4d1a9
6ef9294
e50e349
6ef9294
e50e349
 
 
6ef9294
e50e349
 
6ef9294
 
e50e349
 
e6399c3
e50e349
 
 
 
 
 
 
6ef9294
e50e349
 
ce6434e
e50e349
 
de2aa9b
9444948
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
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)
    
    writer = None
    
    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')
    
        if writer is None:
            writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size) # video writer for output
    
        out = remover.process(img, type='map') # same as image, except for 'rgba' which is not for video.
        writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
    
    cap.release()
    writer.release()
    return 'output.mp4' # returning the file path of the processed video

def display_video(output_video):
    return output_video

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