amirgame197's picture
Update app.py
3cd966d verified
raw
history blame
1.74 kB
import spaces
import gradio as gr
import cv2
#import numpy as np
#from PIL import Image
#from transparent_background import Remover
from backgroundremover import BackgroundRemover
#remover = Remover()
#@spaces.GPU
#def doo(video):
#cap = cv2.VideoCapture(video)
#fps = cap.get(cv2.CAP_PROP_FPS)
#writer = None
#processed_frames = 0
#while cap.isOpened():
# ret, frame = cap.read()
#
# 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)
# 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'
@spaces.GPU
def doo(video):
video = cv2.VideoCapture(video)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480))
while(video.isOpened()):
ret, frame = video.read()
if ret == True:
# Remove the background from the frame
no_bg_frame = remover.remove(frame)
# Write the frame into the file 'output.mp4'
out.write(no_bg_frame)
else:
break
# Release everything when the job is finished
video.release()
out.release()
cv2.destroyAllWindows()
return 'output.mp4'
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
iface.launch()