Spaces:
Runtime error
Runtime error
Add mask generation to video processing pipeline
Browse files
app.py
CHANGED
@@ -1,23 +1,39 @@
|
|
|
|
1 |
import time
|
2 |
import uuid
|
3 |
from typing import Tuple
|
4 |
|
5 |
import gradio as gr
|
6 |
import supervision as sv
|
|
|
7 |
from tqdm import tqdm
|
|
|
|
|
8 |
|
9 |
START_FRAME = 0
|
10 |
END_FRAME = 10
|
11 |
TOTAL = END_FRAME - START_FRAME
|
12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
21 |
video_info = sv.VideoInfo.from_video_path(source_video)
|
22 |
frame_iterator = iter(sv.get_video_frames_generator(
|
23 |
source_path=source_video, start=START_FRAME, end=END_FRAME))
|
@@ -25,10 +41,22 @@ def process(
|
|
25 |
with sv.VideoSink(f"{name}.mp4", video_info=video_info) as sink:
|
26 |
for _ in tqdm(range(TOTAL), desc="Masking frames"):
|
27 |
frame = next(frame_iterator)
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
|
34 |
with gr.Blocks() as demo:
|
|
|
1 |
+
import torch
|
2 |
import time
|
3 |
import uuid
|
4 |
from typing import Tuple
|
5 |
|
6 |
import gradio as gr
|
7 |
import supervision as sv
|
8 |
+
import numpy as np
|
9 |
from tqdm import tqdm
|
10 |
+
from transformers import pipeline
|
11 |
+
from PIL import Image
|
12 |
|
13 |
START_FRAME = 0
|
14 |
END_FRAME = 10
|
15 |
TOTAL = END_FRAME - START_FRAME
|
16 |
|
17 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
18 |
+
SAM_GENERATOR = pipeline(
|
19 |
+
task="mask-generation",
|
20 |
+
model="facebook/sam-vit-base",
|
21 |
+
device=DEVICE)
|
22 |
+
MASK_ANNOTATOR = sv.MaskAnnotator(
|
23 |
+
color=sv.Color.red(),
|
24 |
+
color_lookup=sv.ColorLookup.INDEX)
|
25 |
|
26 |
+
|
27 |
+
def run_sam(frame: np.ndarray) -> sv.Detections:
|
28 |
+
# convert from Numpy BGR to PIL RGB
|
29 |
+
image = Image.fromarray(frame[:, :, ::-1])
|
30 |
+
|
31 |
+
outputs = SAM_GENERATOR(image)
|
32 |
+
mask = np.array(outputs['masks'])
|
33 |
+
return sv.Detections(xyxy=sv.mask_to_xyxy(masks=mask), mask=mask)
|
34 |
+
|
35 |
+
|
36 |
+
def mask_video(source_video: str, prompt: str, confidence: float, name: str) -> str:
|
37 |
video_info = sv.VideoInfo.from_video_path(source_video)
|
38 |
frame_iterator = iter(sv.get_video_frames_generator(
|
39 |
source_path=source_video, start=START_FRAME, end=END_FRAME))
|
|
|
41 |
with sv.VideoSink(f"{name}.mp4", video_info=video_info) as sink:
|
42 |
for _ in tqdm(range(TOTAL), desc="Masking frames"):
|
43 |
frame = next(frame_iterator)
|
44 |
+
detections = run_sam(frame)
|
45 |
+
annotated_frame = MASK_ANNOTATOR.annotate(
|
46 |
+
scene=frame.copy(), detections=detections)
|
47 |
+
sink.write_frame(annotated_frame)
|
48 |
+
return f"{name}.mp4"
|
49 |
+
|
50 |
|
51 |
+
def process(
|
52 |
+
source_video: str,
|
53 |
+
prompt: str,
|
54 |
+
confidence: float,
|
55 |
+
progress=gr.Progress(track_tqdm=True)
|
56 |
+
) -> Tuple[str, str]:
|
57 |
+
name = str(uuid.uuid4())
|
58 |
+
masked_video = mask_video(source_video, prompt, confidence, name)
|
59 |
+
return masked_video, masked_video
|
60 |
|
61 |
|
62 |
with gr.Blocks() as demo:
|