File size: 1,900 Bytes
7de892f
de2aa9b
3cd966d
6ce3893
caa1229
e50e349
6ce3893
 
ba4d1a9
27202f7
5066517
a3504d6
 
632ab5a
a3504d6
6ce3893
 
63b8881
6ce3893
63b8881
6ce3893
dded11c
a3504d6
e50e349
6ce3893
 
 
 
 
dded11c
27202f7
 
 
 
 
6ce3893
 
63b8881
6ce3893
 
d4ae210
6ce3893
 
 
 
3cd966d
6ce3893
 
5be44ed
ce6434e
8cd5202
927e8af
9552fb3
8cd5202
927e8af
 
 
a3504d6
 
632ab5a
6015db0
a3504d6
63b8881
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
import spaces
import gradio as gr
import cv2
import numpy as np
import time

from PIL import Image
from transparent_background import Remover

@spaces.GPU()
def doo(video, mode):
    if(mode == 'Normal'):
        remover = Remover()
    else:
        remover = Remover(mode='fast')
    cap = cv2.VideoCapture(video)
    fps = cap.get(cv2.CAP_PROP_FPS)
    
    writer = None
    
    processed_frames = 0
    start_time = time.time()
    print('Starting on mode: ' + mode)
    
    while cap.isOpened():
        ret, frame = cap.read()
    
        if ret is False:
            break

        if time.time() - start_time >= 55:
            print("GPU Timeout is coming")
            cap.release()
            writer.release()
            return 'output.mp4'
        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)

        processed_frames += 1
        print(f"Processing: {processed_frames}")
        out = remover.process(img, type='green')
        writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
    
    cap.release()
    writer.release()
    return 'output.mp4'

title = "🎞️Video Background Removal tool🎥"

description = r"""Please note that if your video file is long (or having high amount of frames) the result may be shorter than input video because of the GPU timeout.<br>
In this case consider trying Fast mode."""

examples = [['./input.mp4'],]

iface = gr.Interface(
    fn=doo, 
    inputs=["video", gr.components.Radio(['Normal', 'Fast'], label='Select mode', value='Normal', info= 'Normal is more accurate, but takes longer. | Fast has lower accuracy so the process will be faster.')], 
    outputs="video", examples=examples, title=title, description=description
)
iface.launch()