Spaces:
Runtime error
Runtime error
File size: 5,142 Bytes
5b163f1 c34b7e0 7b4534e b643479 c34b7e0 b643479 c34b7e0 69db725 c34b7e0 7b4534e 5b163f1 c34b7e0 7b4534e b643479 c34b7e0 b643479 c34b7e0 7b4534e b643479 c34b7e0 b643479 5b163f1 c34b7e0 b643479 5b163f1 b643479 7b4534e c34b7e0 7b4534e 5b163f1 7b4534e 5b163f1 7b4534e 5b163f1 7b4534e 5b163f1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import uuid
from typing import Tuple, List
import gradio as gr
import numpy as np
import supervision as sv
import torch
from PIL import Image
from tqdm import tqdm
from transformers import pipeline, CLIPModel, CLIPProcessor
MARKDOWN = """
# Auto β‘ ProPainter π§βπ¨
This is a demo for automatic removal of objects from videos using
[Segment Anything Model](https://github.com/facebookresearch/segment-anything),
[MetaCLIP](https://github.com/facebookresearch/MetaCLIP), and
[ProPainter](https://github.com/sczhou/ProPainter) combo.
"""
START_FRAME = 0
END_FRAME = 10
TOTAL = END_FRAME - START_FRAME
MINIMUM_AREA = 0.01
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
SAM_GENERATOR = pipeline(
task="mask-generation",
model="facebook/sam-vit-large",
device=DEVICE)
CLIP_MODEL = CLIPModel.from_pretrained("facebook/metaclip-b32-400m").to(DEVICE)
CLIP_PROCESSOR = CLIPProcessor.from_pretrained("facebook/metaclip-b32-400m")
def run_sam(frame: np.ndarray) -> sv.Detections:
# convert from Numpy BGR to PIL RGB
image = Image.fromarray(frame[:, :, ::-1])
outputs = SAM_GENERATOR(image)
mask = np.array(outputs['masks'])
return sv.Detections(xyxy=sv.mask_to_xyxy(masks=mask), mask=mask)
def run_clip(frame: np.ndarray, text: List[str]) -> np.ndarray:
# convert from Numpy BGR to PIL RGB
image = Image.fromarray(frame[:, :, ::-1])
inputs = CLIP_PROCESSOR(text=text, images=image, return_tensors="pt").to(DEVICE)
outputs = CLIP_MODEL(**inputs)
probs = outputs.logits_per_image.softmax(dim=1)
return probs.detach().cpu().numpy()
def gray_background(image: np.ndarray, mask: np.ndarray, gray_value=128):
gray_color = np.array([gray_value, gray_value, gray_value], dtype=np.uint8)
return np.where(mask[..., None], image, gray_color)
def filter_detections_by_area(frame: np.ndarray, detections: sv.Detections, minimum_area: float) -> sv.Detections:
frame_width, frame_height = frame.shape[1], frame.shape[0]
frame_area = frame_width * frame_height
return detections[detections.area > minimum_area * frame_area]
def filter_detections_by_prompt(frame: np.ndarray, detections: sv.Detections, prompt: str, confidence: float) -> sv.Detections:
text = [f"a picture of {prompt}", "a picture of background"]
filtering_mask = []
for xyxy, mask in zip(detections.xyxy, detections.mask):
crop = gray_background(
image=sv.crop_image(image=frame, xyxy=xyxy),
mask=sv.crop_image(image=mask, xyxy=xyxy))
probs = run_clip(frame=crop, text=text)
filtering_mask.append(probs[0][0] > confidence)
return detections[np.array(filtering_mask)]
def mask_frame(frame: np.ndarray, prompt: str, confidence: float) -> np.ndarray:
detections = run_sam(frame)
detections = filter_detections_by_area(
frame=frame, detections=detections, minimum_area=MINIMUM_AREA)
detections = filter_detections_by_prompt(
frame=frame, detections=detections, prompt=prompt, confidence=confidence)
# converting set of masks to a single mask
mask = np.any(detections.mask, axis=0).astype(np.uint8) * 255
# converting single channel mask to 3 channel mask
return np.repeat(mask[:, :, np.newaxis], 3, axis=2)
def mask_video(source_video: str, prompt: str, confidence: float, name: str) -> str:
video_info = sv.VideoInfo.from_video_path(source_video)
frame_iterator = iter(sv.get_video_frames_generator(
source_path=source_video, start=START_FRAME, end=END_FRAME))
with sv.ImageSink(name, image_name_pattern="{:05d}.png") as image_sink:
with sv.VideoSink(f"{name}.mp4", video_info=video_info) as video_sink:
for _ in tqdm(range(TOTAL), desc="Masking frames"):
frame = next(frame_iterator)
annotated_frame = mask_frame(frame, prompt, confidence)
video_sink.write_frame(annotated_frame)
image_sink.save_image(annotated_frame)
return f"{name}.mp4"
def process(
source_video: str,
prompt: str,
confidence: float,
progress=gr.Progress(track_tqdm=True)
) -> Tuple[str, str]:
name = str(uuid.uuid4())
masked_video = mask_video(source_video, prompt, confidence, name)
return masked_video, masked_video
with gr.Blocks() as demo:
gr.Markdown(MARKDOWN)
with gr.Row():
with gr.Column():
source_video_player = gr.Video(
label="Source video", source="upload", format="mp4")
prompt_text = gr.Textbox(
label="Prompt", value="person")
confidence_slider = gr.Slider(
label="Confidence", minimum=0.5, maximum=1.0, step=0.05, value=0.6)
submit_button = gr.Button("Submit")
with gr.Column():
masked_video_player = gr.Video(label="Masked video")
painted_video_player = gr.Video(label="Painted video")
submit_button.click(
process,
inputs=[source_video_player, prompt_text, confidence_slider],
outputs=[masked_video_player, painted_video_player])
demo.queue().launch(debug=False, show_error=True)
|