amirgame197 commited on
Commit
5be44ed
1 Parent(s): 5530b7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -45
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,12 +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
- 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,47 +25,15 @@ def doo(video):
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
-
43
- # Print progress information
44
- print(f"Processing: {processed_frames}/{total_frames} frames")
45
-
46
- if frame_count >= max_frames:
47
- cap.release()
48
- writer.release()
49
-
50
- # Process the next segment
51
- cap = cv2.VideoCapture(video)
52
- for _ in range(frame_count):
53
- cap.read() # Skip frames that have already been processed
54
- writer = None
55
- frame_count = 0
56
- segment_count += 1
57
 
58
  cap.release()
59
- if writer is not None:
60
- writer.release()
61
-
62
- # Concatenate all the video segments
63
- final_output = cv2.VideoWriter('final_output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
64
- for i in range(segment_count):
65
- cap = cv2.VideoCapture(f'output_{i}.mp4')
66
- while cap.isOpened():
67
- ret, frame = cap.read()
68
- if ret is False:
69
- break
70
- final_output.write(frame)
71
- cap.release()
72
- os.remove(f'output_{i}.mp4') # Delete the segment file
73
- final_output.release()
74
-
75
- return 'final_output.mp4'
76
 
77
  iface = gr.Interface(fn=doo, inputs="video", outputs="video")
78
- 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
+ print(f"Processing: {processed_frames}/{total_frames} frames")
31
  out = remover.process(img, type='green')
32
  writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  cap.release()
35
+ writer.release()
36
+ return 'output.mp4'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  iface = gr.Interface(fn=doo, inputs="video", outputs="video")
39
+ iface.launch()