Spaces:
Runtime error
Runtime error
amirgame197
commited on
Commit
•
6ce3893
1
Parent(s):
e6ecfd9
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,42 @@
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
-
|
5 |
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
remover = remove()
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
#cap = cv2.VideoCapture(video)
|
17 |
-
#fps = cap.get(cv2.CAP_PROP_FPS)
|
18 |
|
19 |
-
|
20 |
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
-
|
31 |
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
#cap.release()
|
42 |
-
#writer.release()
|
43 |
-
#return 'output.mp4'
|
44 |
-
|
45 |
-
@spaces.GPU
|
46 |
-
def doo(video):
|
47 |
-
video = cv2.VideoCapture(video)
|
48 |
-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
49 |
-
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480))
|
50 |
-
while(video.isOpened()):
|
51 |
-
ret, frame = video.read()
|
52 |
-
if ret == True:
|
53 |
-
# Remove the background from the frame
|
54 |
-
no_bg_frame = remover.remove(frame)
|
55 |
-
|
56 |
-
# Write the frame into the file 'output.mp4'
|
57 |
-
out.write(no_bg_frame)
|
58 |
-
else:
|
59 |
-
break
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
out.release()
|
64 |
-
cv2.destroyAllWindows()
|
65 |
return 'output.mp4'
|
66 |
|
67 |
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
|
|
|
1 |
import spaces
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
+
import numpy as np
|
5 |
|
6 |
+
from PIL import Image
|
7 |
+
from transparent_background import Remover
|
8 |
|
9 |
+
remover = Remover()
|
|
|
10 |
|
11 |
+
@spaces.GPU
|
12 |
+
def doo(video):
|
13 |
+
cap = cv2.VideoCapture(video)
|
14 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
|
15 |
|
16 |
+
writer = None
|
17 |
|
18 |
+
processed_frames = 0
|
19 |
|
20 |
+
while cap.isOpened():
|
21 |
+
ret, frame = cap.read()
|
22 |
+
|
23 |
+
if ret is False:
|
24 |
+
break
|
25 |
|
26 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
27 |
+
img = Image.fromarray(frame).convert('RGB')
|
28 |
|
29 |
+
if writer is None:
|
30 |
+
writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
|
31 |
|
32 |
+
processed_frames += 1
|
33 |
+
print(f"Processing: {processed_frames}")
|
34 |
|
35 |
+
out = remover.process(img, type='green')
|
36 |
+
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
cap.release()
|
39 |
+
writer.release()
|
|
|
|
|
40 |
return 'output.mp4'
|
41 |
|
42 |
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
|