amirgame197 commited on
Commit
6ce3893
1 Parent(s): e6ecfd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -50
app.py CHANGED
@@ -1,67 +1,42 @@
1
  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
8
 
9
- from backgroundremover from bg import remove
10
- remover = remove()
11
 
12
- #remover = Remover()
13
-
14
- #@spaces.GPU
15
- #def doo(video):
16
- #cap = cv2.VideoCapture(video)
17
- #fps = cap.get(cv2.CAP_PROP_FPS)
18
 
19
- #writer = None
20
 
21
- #processed_frames = 0
22
 
23
- #while cap.isOpened():
24
- # ret, frame = cap.read()
25
- #
26
- # if ret is False:
27
- # break
28
 
29
- # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
30
- # img = Image.fromarray(frame).convert('RGB')
31
 
32
- # if writer is None:
33
- # writer = cv2.VideoWriter('output.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
34
 
35
- # processed_frames += 1
36
- # print(f"Processing: {processed_frames}")
37
 
38
- # out = remover.process(img, type='green')
39
- # writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
40
-
41
- #cap.release()
42
- #writer.release()
43
- #return 'output.mp4'
44
-
45
- @spaces.GPU
46
- def doo(video):
47
- video = cv2.VideoCapture(video)
48
- fourcc = cv2.VideoWriter_fourcc(*'mp4v')
49
- out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640, 480))
50
- while(video.isOpened()):
51
- ret, frame = video.read()
52
- if ret == True:
53
- # Remove the background from the frame
54
- no_bg_frame = remover.remove(frame)
55
-
56
- # Write the frame into the file 'output.mp4'
57
- out.write(no_bg_frame)
58
- else:
59
- break
60
 
61
- # Release everything when the job is finished
62
- video.release()
63
- out.release()
64
- cv2.destroyAllWindows()
65
  return 'output.mp4'
66
 
67
  iface = gr.Interface(fn=doo, inputs="video", outputs="video")
 
1
  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
8
 
9
+ remover = Remover()
 
10
 
11
+ @spaces.GPU
12
+ def doo(video):
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:
24
+ break
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")