Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,62 @@
|
|
|
|
|
| 1 |
import cv2
|
| 2 |
import numpy as np
|
| 3 |
-
import gradio as gr
|
| 4 |
from tqdm import tqdm
|
| 5 |
|
| 6 |
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
frames = []
|
| 25 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
for i in tqdm(range(frame_count)):
|
| 27 |
-
|
| 28 |
-
if not
|
| 29 |
-
|
| 30 |
frames.append(process_frame(frame))
|
| 31 |
-
|
| 32 |
cap.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
]
|
| 43 |
-
|
| 44 |
-
)
|
| 45 |
|
| 46 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
from tqdm import tqdm
|
| 5 |
|
| 6 |
|
| 7 |
+
def process_frame(frame):
|
| 8 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 9 |
+
lower_green = np.array([36, 25, 25])
|
| 10 |
+
upper_green = np.array([86, 255, 255])
|
| 11 |
+
mask = cv2.inRange(hsv, lower_green, upper_green)
|
| 12 |
+
mask_inv = cv2.bitwise_not(mask)
|
| 13 |
+
bg = cv2.imread(bg_image)
|
| 14 |
+
bg = cv2.resize(bg, (frame.shape[1], frame.shape[0]))
|
| 15 |
+
bg_masked = cv2.bitwise_and(bg, bg, mask=mask)
|
| 16 |
+
fg_masked = cv2.bitwise_and(frame, frame, mask=mask_inv)
|
| 17 |
+
return cv2.add(bg_masked, fg_masked)
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
def remove_green_screen(video, bg_image):
|
| 21 |
+
cap = cv2.VideoCapture(video.name)
|
| 22 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
|
|
|
| 23 |
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 24 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 25 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 26 |
+
out = cv2.VideoWriter('out.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 27 |
+
frames = []
|
| 28 |
for i in tqdm(range(frame_count)):
|
| 29 |
+
ret, frame = cap.read()
|
| 30 |
+
if not ret:
|
| 31 |
+
break
|
| 32 |
frames.append(process_frame(frame))
|
|
|
|
| 33 |
cap.release()
|
| 34 |
+
for frame in tqdm(frames):
|
| 35 |
+
out.write(frame)
|
| 36 |
+
out.release()
|
| 37 |
+
return 'out.mp4'
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def predict_video(video, bg_image):
|
| 41 |
+
result_file = remove_green_screen(video, bg_image)
|
| 42 |
+
with open(result_file, 'rb') as f:
|
| 43 |
+
result = f.read()
|
| 44 |
+
return result
|
| 45 |
+
|
| 46 |
|
| 47 |
+
inputs = [
|
| 48 |
+
gr.inputs.Video(label="Video"),
|
| 49 |
+
gr.inputs.Image(label="Background Image")
|
| 50 |
+
]
|
| 51 |
|
| 52 |
+
outputs = gr.outputs.Video(label="Processed Video", type="mp4", source="file")
|
| 53 |
|
| 54 |
+
title = "Green Screen Video Remover"
|
| 55 |
+
description = "Remove the green background from a video and replace it with another image."
|
| 56 |
+
article = "<p style='text-align: center'><a href='https://huggingface.co/blog/how-to-build-a-web-app-for-a-transformer-in-pytorch'>How to Build a Web App for a Transformer in PyTorch</a></p>"
|
| 57 |
+
examples = [
|
| 58 |
+
["./input.mp4", "./bg.png"],
|
| 59 |
+
["./input2.mp4", "./bg2.png"]
|
| 60 |
+
]
|
|
|
|
| 61 |
|
| 62 |
+
gr.Interface(predict_video, inputs, outputs, title=title, description=description, article=article, examples=examples, live=True).launch()
|