Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,67 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 25 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
frames = []
|
| 28 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 55 |
-
|
| 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(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import cv2
|
| 3 |
import numpy as np
|
| 4 |
+
|
| 5 |
+
def remove_green_screen(video):
|
| 6 |
+
def process_frame(frame):
|
| 7 |
+
# Convert to HSV color space
|
| 8 |
+
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
| 9 |
+
|
| 10 |
+
# Define the range of green color
|
| 11 |
+
lower_green = np.array([25, 52, 72])
|
| 12 |
+
upper_green = np.array([102, 255, 255])
|
| 13 |
+
|
| 14 |
+
# Threshold the HSV image to get only green colors
|
| 15 |
+
mask = cv2.inRange(hsv, lower_green, upper_green)
|
| 16 |
+
|
| 17 |
+
# Bitwise-AND mask and original image
|
| 18 |
+
res = cv2.bitwise_and(frame, frame, mask=mask)
|
| 19 |
+
|
| 20 |
+
return res
|
| 21 |
+
|
| 22 |
+
# Load background image
|
| 23 |
+
bg_image = cv2.imread("background.jpg")
|
| 24 |
+
|
| 25 |
+
# Open video file
|
| 26 |
+
cap = cv2.VideoCapture(video)
|
| 27 |
+
|
| 28 |
+
# Get video codec and fps
|
| 29 |
+
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
|
| 30 |
+
fps = int(cap.get(cv2.CAP_PROP_FPS))
|
| 31 |
+
|
| 32 |
+
# Get video dimensions
|
| 33 |
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 34 |
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 35 |
+
|
| 36 |
+
# Create output video writer
|
| 37 |
+
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height))
|
| 38 |
+
|
| 39 |
frames = []
|
| 40 |
+
|
| 41 |
+
# Loop through video frames
|
| 42 |
+
while True:
|
| 43 |
ret, frame = cap.read()
|
| 44 |
if not ret:
|
| 45 |
break
|
| 46 |
+
|
| 47 |
+
# Process frame
|
| 48 |
+
bg = cv2.imread(bg_image)
|
| 49 |
frames.append(process_frame(frame))
|
| 50 |
+
|
| 51 |
+
# Release video file and output video writer
|
| 52 |
cap.release()
|
|
|
|
|
|
|
| 53 |
out.release()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# Write output video
|
| 56 |
+
out = cv2.VideoWriter("output.mp4", fourcc, fps, (width, height))
|
| 57 |
+
for frame in frames:
|
| 58 |
+
out.write(frame)
|
| 59 |
+
out.release()
|
| 60 |
|
| 61 |
+
return "output.mp4"
|
|
|
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
|
| 64 |
+
input_video = gr.inputs.Video(label="Input Video")
|
| 65 |
+
outputs = gr.outputs.Video(label="Processed Video", type="mp4")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
gr.Interface(remove_green_screen, inputs=input_video, outputs=outputs).launch()
|