amirgame197
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -2,6 +2,7 @@ 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
|
@@ -14,6 +15,12 @@ def doo(video):
|
|
14 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
15 |
|
16 |
writer = None
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
while cap.isOpened():
|
19 |
ret, frame = cap.read()
|
@@ -25,14 +32,45 @@ def doo(video):
|
|
25 |
img = Image.fromarray(frame).convert('RGB')
|
26 |
|
27 |
if writer is None:
|
28 |
-
writer = cv2.VideoWriter('
|
29 |
|
30 |
out = remover.process(img, type='green')
|
31 |
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
cap.release()
|
34 |
-
writer
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
|
38 |
-
iface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
import cv2
|
4 |
import numpy as np
|
5 |
+
import os
|
6 |
|
7 |
from PIL import Image
|
8 |
from transparent_background import Remover
|
|
|
15 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
16 |
|
17 |
writer = None
|
18 |
+
frame_count = 0
|
19 |
+
max_frames = 400 # Maximum number of frames to process at a time
|
20 |
+
segment_count = 0 # Count of video segments processed
|
21 |
+
|
22 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) # Total number of frames in the video
|
23 |
+
processed_frames = 0 # Number of frames processed so far
|
24 |
|
25 |
while cap.isOpened():
|
26 |
ret, frame = cap.read()
|
|
|
32 |
img = Image.fromarray(frame).convert('RGB')
|
33 |
|
34 |
if writer is None:
|
35 |
+
writer = cv2.VideoWriter(f'output_{segment_count}.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
|
36 |
|
37 |
out = remover.process(img, type='green')
|
38 |
writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
|
39 |
+
|
40 |
+
frame_count += 1
|
41 |
+
processed_frames += 1
|
42 |
+
gr.Interface.progress(processed_frames / total_frames) # Update the progress bar
|
43 |
+
|
44 |
+
if frame_count >= max_frames:
|
45 |
+
cap.release()
|
46 |
+
writer.release()
|
47 |
+
|
48 |
+
# Process the next segment
|
49 |
+
cap = cv2.VideoCapture(video)
|
50 |
+
for _ in range(frame_count):
|
51 |
+
cap.read() # Skip frames that have already been processed
|
52 |
+
writer = None
|
53 |
+
frame_count = 0
|
54 |
+
segment_count += 1
|
55 |
|
56 |
cap.release()
|
57 |
+
if writer is not None:
|
58 |
+
writer.release()
|
59 |
+
|
60 |
+
# Concatenate all the video segments
|
61 |
+
final_output = cv2.VideoWriter('final_output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
|
62 |
+
for i in range(segment_count):
|
63 |
+
cap = cv2.VideoCapture(f'output_{i}.mp4')
|
64 |
+
while cap.isOpened():
|
65 |
+
ret, frame = cap.read()
|
66 |
+
if ret is False:
|
67 |
+
break
|
68 |
+
final_output.write(frame)
|
69 |
+
cap.release()
|
70 |
+
os.remove(f'output_{i}.mp4') # Delete the segment file
|
71 |
+
final_output.release()
|
72 |
+
|
73 |
+
return 'final_output.mp4'
|
74 |
|
75 |
iface = gr.Interface(fn=doo, inputs="video", outputs="video")
|
76 |
+
iface.launch()
|