Spaces:
Paused
Paused
File size: 2,378 Bytes
d97f5ef 02af6f7 d97f5ef 02af6f7 d97f5ef 18e9287 02af6f7 d97f5ef 62861c6 02af6f7 62861c6 d97f5ef 18e9287 d97f5ef 02af6f7 d97f5ef 02af6f7 171e3cd d97f5ef 62861c6 d97f5ef 18e9287 d97f5ef 02af6f7 d97f5ef 02af6f7 d97f5ef 02af6f7 d97f5ef 62861c6 d97f5ef 18e9287 | 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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import spaces
import gradio as gr
import cv2
import numpy as np
import time
import random
from PIL import Image
from transparent_background import Remover
import subprocess
import os
@spaces.GPU()
def doo(video, mode):
if(mode == 'Fast'):
remover = Remover(mode='fast')
else:
remover = Remover()
cap = cv2.VideoCapture(video)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
tmpname = random.randint(111111111, 999999999)
tmp_video = f"/tmp/{tmpname}_video.mp4"
tmp_audio = f"/tmp/{tmpname}_audio.aac"
output_video = f"/tmp/{tmpname}_output.mp4"
writer = cv2.VideoWriter(tmp_video, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
processed_frames = 0
start_time = time.time()
while cap.isOpened():
ret, frame = cap.read()
if ret is False:
break
if time.time() - start_time >= 55:
print("GPU Timeout is coming")
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = Image.fromarray(frame).convert('RGB')
processed_frames += 1
print(f"Processing: {processed_frames}")
out = remover.process(img, type='green')
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_RGB2BGR))
cap.release()
writer.release()
# ์ค๋์ค ์ถ์ถ
subprocess.call(['ffmpeg', '-i', video, '-vn', '-acodec', 'copy', tmp_audio])
# ์ฒ๋ฆฌ๋ ๋น๋์ค์ ์ค๋์ค ๊ฒฐํฉ
subprocess.call(['ffmpeg', '-i', tmp_video, '-i', tmp_audio, '-c', 'copy', output_video])
# ์์ ํ์ผ ์ญ์
os.remove(tmp_video)
os.remove(tmp_audio)
return output_video
# Gradio ์ธํฐํ์ด์ค ์ ์
css = """footer { visibility: hidden; }"""
b_interface = gr.Interface(
fn=doo,
inputs=[
gr.Video(label="Video"),
gr.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",
css=css,
title="Video Background Removal",
description="Select a video and mode to remove the background."
)
# Gradio ์ฑ ์คํ
if __name__ == "__main__":
b_interface.launch() |