om-app commited on
Commit
589c5cb
·
1 Parent(s): 2911157

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -32
app.py CHANGED
@@ -1,39 +1,46 @@
1
  import cv2
2
  import numpy as np
3
  import gradio as gr
 
 
4
 
5
  def remove_green_screen(video_file, bg_image):
6
- cap = cv2.VideoCapture(video_file)
7
- bg = cv2.imread(bg_image)
8
- img_array = []
9
- while True:
10
- ret, frame = cap.read()
11
- if not ret:
12
- break
13
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
14
- lower_green = np.array([30, 60, 60])
15
- upper_green = np.array([90, 255, 255])
16
- mask = cv2.inRange(hsv, lower_green, upper_green)
17
- mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, np.ones((3,3),np.uint8))
18
- mask = cv2.dilate(mask, np.ones((3,3),np.uint8), iterations=5)
19
- mask = cv2.blur(mask, (30,30))
20
- mask = np.stack([mask, mask, mask], axis=2)
21
- frame = np.where(mask == np.array([255, 255, 255]), bg, frame)
22
- img_array.append(frame)
 
 
 
 
 
 
 
 
 
 
 
23
  cap.release()
24
- output_file = "output.mp4"
25
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
26
- fps = 30.0
27
- height, width, layers = img_array[0].shape
28
- size = (width,height)
29
- out = cv2.VideoWriter(output_file, fourcc, fps, size)
30
- for i in range(len(img_array)):
31
- out.write(img_array[i])
32
- out.release()
33
- return output_file
34
-
35
- input_video = gr.inputs.Video(type="mp4")
36
- input_bg = gr.inputs.Image(label="Background Image")
37
- output_video = gr.outputs.Video(type="mp4")
38
-
39
- gr.Interface(fn=remove_green_screen, inputs=[input_video, input_bg], outputs=output_video, title="Green Screen Remover").launch()
 
1
  import cv2
2
  import numpy as np
3
  import gradio as gr
4
+ from tqdm import tqdm
5
+
6
 
7
  def remove_green_screen(video_file, bg_image):
8
+
9
+ def process_frame(frame):
10
+ # remove green screen
 
 
 
 
11
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
12
+ mask = cv2.inRange(hsv, (36, 25, 25), (86, 255, 255))
13
+ mask = cv2.merge((mask, mask, mask))
14
+ masked = cv2.bitwise_and(frame, mask)
15
+
16
+ # add background
17
+ bg = cv2.imread(bg_image)
18
+ bg = cv2.resize(bg, (frame.shape[1], frame.shape[0]))
19
+ masked[np.where((masked == [0, 0, 0]).all(axis=2))] = bg[np.where((masked == [0, 0, 0]).all(axis=2))]
20
+
21
+ return masked
22
+
23
+ cap = cv2.VideoCapture(video_file)
24
+ frames = []
25
+ frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
26
+ for i in tqdm(range(frame_count)):
27
+ success, frame = cap.read()
28
+ if not success:
29
+ continue
30
+ frames.append(process_frame(frame))
31
+
32
  cap.release()
33
+
34
+ return np.array(frames)
35
+
36
+
37
+ iface = gr.Interface(
38
+ fn=remove_green_screen,
39
+ inputs=[
40
+ gr.inputs.Video(type="mp4", label="Input video"),
41
+ gr.inputs.Image(label="Background image")
42
+ ],
43
+ outputs=gr.outputs.Video(label="Processed video")
44
+ )
45
+
46
+ iface.launch()