Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,27 +4,26 @@ import cv2
|
|
| 4 |
import numpy as np
|
| 5 |
import time
|
| 6 |
import random
|
| 7 |
-
|
| 8 |
from PIL import Image
|
| 9 |
from transparent_background import Remover
|
| 10 |
|
| 11 |
@spaces.GPU()
|
| 12 |
-
def doo(video, mode):
|
| 13 |
-
if
|
| 14 |
remover = Remover(mode='fast')
|
| 15 |
else:
|
| 16 |
remover = Remover()
|
|
|
|
| 17 |
cap = cv2.VideoCapture(video)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
writer = None
|
| 21 |
tmpname = random.randint(111111111, 999999999)
|
| 22 |
processed_frames = 0
|
| 23 |
start_time = time.time()
|
| 24 |
-
|
| 25 |
while cap.isOpened():
|
| 26 |
ret, frame = cap.read()
|
| 27 |
-
|
| 28 |
if ret is False:
|
| 29 |
break
|
| 30 |
|
|
@@ -33,31 +32,33 @@ def doo(video, mode):
|
|
| 33 |
# cap.release()
|
| 34 |
# writer.release()
|
| 35 |
# return str(tmpname) + '.mp4'
|
| 36 |
-
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 37 |
img = Image.fromarray(frame).convert('RGB')
|
| 38 |
-
|
| 39 |
if writer is None:
|
| 40 |
-
writer = cv2.VideoWriter(str(tmpname) + '.mp4', cv2.VideoWriter_fourcc(*'mp4v'),
|
| 41 |
|
| 42 |
processed_frames += 1
|
| 43 |
-
print(f"Processing
|
|
|
|
| 44 |
out = remover.process(img, type='green')
|
| 45 |
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
| 46 |
-
|
| 47 |
cap.release()
|
| 48 |
writer.release()
|
| 49 |
return str(tmpname) + '.mp4'
|
| 50 |
|
| 51 |
-
title = "🎞️Video Background Removal
|
| 52 |
-
|
| 53 |
-
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>
|
| 54 |
-
In this case consider trying Fast mode."""
|
| 55 |
|
| 56 |
-
examples = [['./input.mp4']
|
| 57 |
|
| 58 |
iface = gr.Interface(
|
| 59 |
-
fn=doo,
|
| 60 |
-
inputs=["video", gr.components.Radio(['Normal', 'Fast'], label='Select mode', value='Normal', info=
|
| 61 |
-
outputs="video",
|
|
|
|
|
|
|
|
|
|
| 62 |
)
|
| 63 |
-
iface.launch()
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import time
|
| 6 |
import random
|
|
|
|
| 7 |
from PIL import Image
|
| 8 |
from transparent_background import Remover
|
| 9 |
|
| 10 |
@spaces.GPU()
|
| 11 |
+
def doo(video, mode, progress=gr.Progress()):
|
| 12 |
+
if mode == 'Fast':
|
| 13 |
remover = Remover(mode='fast')
|
| 14 |
else:
|
| 15 |
remover = Remover()
|
| 16 |
+
|
| 17 |
cap = cv2.VideoCapture(video)
|
| 18 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # Get total frames
|
|
|
|
| 19 |
writer = None
|
| 20 |
tmpname = random.randint(111111111, 999999999)
|
| 21 |
processed_frames = 0
|
| 22 |
start_time = time.time()
|
| 23 |
+
|
| 24 |
while cap.isOpened():
|
| 25 |
ret, frame = cap.read()
|
| 26 |
+
|
| 27 |
if ret is False:
|
| 28 |
break
|
| 29 |
|
|
|
|
| 32 |
# cap.release()
|
| 33 |
# writer.release()
|
| 34 |
# return str(tmpname) + '.mp4'
|
| 35 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
| 36 |
img = Image.fromarray(frame).convert('RGB')
|
| 37 |
+
|
| 38 |
if writer is None:
|
| 39 |
+
writer = cv2.VideoWriter(str(tmpname) + '.mp4', cv2.VideoWriter_fourcc(*'mp4v'), cap.get(cv2.CAP_PROP_FPS), img.size)
|
| 40 |
|
| 41 |
processed_frames += 1
|
| 42 |
+
print(f"Processing frame {processed_frames}")
|
| 43 |
+
progress(processed_frames / total_frames, desc=f"Processing frame {processed_frames}/{total_frames}")
|
| 44 |
out = remover.process(img, type='green')
|
| 45 |
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
| 46 |
+
|
| 47 |
cap.release()
|
| 48 |
writer.release()
|
| 49 |
return str(tmpname) + '.mp4'
|
| 50 |
|
| 51 |
+
title = "🎞️ Video Background Removal Tool 🎥"
|
| 52 |
+
description = """Please note that if your video file is long (or has a high number of frames), the result may be shorter than the input video due to GPU timeout. In this case, consider trying Fast mode."""
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
examples = [['./input.mp4']]
|
| 55 |
|
| 56 |
iface = gr.Interface(
|
| 57 |
+
fn=doo,
|
| 58 |
+
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.')],
|
| 59 |
+
outputs="video",
|
| 60 |
+
examples=examples,
|
| 61 |
+
title=title,
|
| 62 |
+
description=description
|
| 63 |
)
|
| 64 |
+
iface.launch()
|