Spaces:
Runtime error
Runtime error
amirgame197
commited on
Commit
•
63b8881
1
Parent(s):
613494a
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import spaces
|
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
-
import gc # For manual garbage collection
|
6 |
|
7 |
from PIL import Image
|
8 |
from transparent_background import Remover
|
@@ -14,11 +13,11 @@ def doo(video):
|
|
14 |
cap = cv2.VideoCapture(video)
|
15 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
16 |
|
|
|
|
|
17 |
processed_frames = 0
|
18 |
-
|
19 |
-
|
20 |
while cap.isOpened():
|
21 |
-
writer = None
|
22 |
ret, frame = cap.read()
|
23 |
|
24 |
if ret is False:
|
@@ -26,27 +25,19 @@ def doo(video):
|
|
26 |
|
27 |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
28 |
img = Image.fromarray(frame).convert('RGB')
|
29 |
-
|
30 |
if writer is None:
|
31 |
writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
|
32 |
|
33 |
processed_frames += 1
|
34 |
print(f"Processing: {processed_frames}")
|
|
|
35 |
out = remover.process(img, type='green')
|
36 |
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
37 |
-
|
38 |
-
# Release GPU memory after processing a certain number of frames
|
39 |
-
if processed_frames % 100 == 0:
|
40 |
-
processed_frames = 0
|
41 |
-
del out # Delete the variable holding the processed frame
|
42 |
-
del writer
|
43 |
-
gc.collect() # Perform manual garbage collection to release GPU memory
|
44 |
-
print("Memory reset")
|
45 |
|
46 |
cap.release()
|
47 |
-
|
48 |
return 'output.mp4'
|
49 |
|
50 |
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
|
51 |
-
iface.launch()
|
52 |
-
#
|
|
|
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
|
|
|
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:
|
|
|
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")
|
43 |
+
iface.launch()
|
|