amirgame197 commited on
Commit
6bb347f
·
verified ·
1 Parent(s): 762bc47

Update app.py

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