amirgame197 commited on
Commit
6ef9294
1 Parent(s): 12783b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -10
app.py CHANGED
@@ -5,19 +5,39 @@ import numpy as np
5
  from PIL import Image
6
  from transparent_background import Remover
7
 
8
- # Load model
9
  remover = Remover(mode='fast') # custom setting
10
 
11
- # Usage for image
12
-
13
-
 
 
 
14
 
15
- def doo(image):
16
- img = Image.fromarray(image).convert('RGB') # read image
17
- out = remover.process(img) # default setting - transparent background
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- out.save('output.png') # save result
20
- return out
 
21
 
22
- iface = gr.Interface(fn=doo, inputs="image", outputs="image")
23
  iface.launch()
 
5
  from PIL import Image
6
  from transparent_background import Remover
7
 
 
8
  remover = Remover(mode='fast') # custom setting
9
 
10
+ #def doo(image):
11
+ #img = Image.fromarray(image).convert('RGB') # read image
12
+ #out = remover.process(img) # default setting - transparent background
13
+
14
+ #out.save('output.png') # save result
15
+ #return out
16
 
17
+ def doo(video):
18
+ cap = cv2.VideoCapture(video) # video reader for input
19
+ fps = cap.get(cv2.CAP_PROP_FPS)
20
+
21
+ writer = None
22
+
23
+ while cap.isOpened():
24
+ ret, frame = cap.read() # read video
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) # video writer for output
34
+
35
+ out = remover.process(img, type='map') # same as image, except for 'rgba' which is not for video.
36
+ writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
37
 
38
+ cap.release()
39
+ writer.release()
40
+ return writer
41
 
42
+ iface = gr.Interface(fn=doo, inputs="video", outputs="video")
43
  iface.launch()