File size: 8,699 Bytes
75be6c3 1015457 41975a2 094752c 1015457 094752c 1015457 377e9f4 1015457 094752c 1015457 75be6c3 e317241 094752c 75be6c3 094752c 8a7385d e317241 377e9f4 8a7385d 377e9f4 094752c 8a7385d e317241 75be6c3 e317241 094752c 75be6c3 e317241 094752c 1015457 094752c 8a7385d 094752c 1015457 75be6c3 1015457 75be6c3 6057bcc 75be6c3 41975a2 75be6c3 8a7385d 75be6c3 1015457 094752c 1015457 094752c 1015457 e317241 1015457 377e9f4 1015457 8a7385d 1015457 8a7385d 1015457 8a7385d 1015457 094752c 1015457 8a7385d 1015457 094752c |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
import os
from typing import TypeVar
from tqdm import tqdm
import gradio as gr
import numpy as np
import supervision as sv
from PIL import Image
from rfdetr import RFDETRBase, RFDETRLarge
from rfdetr.detr import RFDETR
from rfdetr.util.coco_classes import COCO_CLASSES
from utils.image import calculate_resolution_wh
from utils.video import create_directory, generate_unique_name
ImageType = TypeVar("ImageType", Image.Image, np.ndarray)
MARKDOWN = """
# RF-DETR 🔥
[`[code]`](https://github.com/roboflow/rf-detr)
[`[blog]`](https://blog.roboflow.com/rf-detr)
[`[notebook]`](https://colab.research.google.com/github/roboflow-ai/notebooks/blob/main/notebooks/how-to-finetune-rf-detr-on-detection-dataset.ipynb)
RF-DETR is a real-time, transformer-based object detection model architecture developed
by [Roboflow](https://roboflow.com/) and released under the Apache 2.0 license.
"""
IMAGE_PROCESSING_EXAMPLES = [
['https://media.roboflow.com/supervision/image-examples/people-walking.png', 0.3, 728, "large"],
['https://media.roboflow.com/supervision/image-examples/vehicles.png', 0.3, 728, "large"],
['https://media.roboflow.com/notebooks/examples/dog-2.jpeg', 0.5, 560, "base"],
]
VIDEO_PROCESSING_EXAMPLES = [
["videos/people-walking.mp4", 0.3, 728, "large"],
["videos/vehicles.mp4", 0.3, 728, "large"],
]
COLOR = sv.ColorPalette.from_hex([
"#ffff00", "#ff9b00", "#ff8080", "#ff66b2", "#ff66ff", "#b266ff",
"#9999ff", "#3399ff", "#66ffff", "#33ff99", "#66ff66", "#99ff00"
])
MAX_VIDEO_LENGTH_SECONDS = 5
VIDEO_SCALE_FACTOR = 0.5
VIDEO_TARGET_DIRECTORY = "tmp"
create_directory(directory_path=VIDEO_TARGET_DIRECTORY)
def detect_and_annotate(
model: RFDETR,
image: ImageType,
confidence: float
) -> ImageType:
detections = model.predict(image, threshold=confidence)
resolution_wh = calculate_resolution_wh(image)
text_scale = sv.calculate_optimal_text_scale(resolution_wh=resolution_wh) - 0.2
thickness = sv.calculate_optimal_line_thickness(resolution_wh=resolution_wh)
bbox_annotator = sv.BoxAnnotator(color=COLOR, thickness=thickness)
label_annotator = sv.LabelAnnotator(
color=COLOR,
text_color=sv.Color.BLACK,
text_scale=text_scale
)
labels = [
f"{COCO_CLASSES[class_id]} {confidence:.2f}"
for class_id, confidence
in zip(detections.class_id, detections.confidence)
]
annotated_image = image.copy()
annotated_image = bbox_annotator.annotate(annotated_image, detections)
annotated_image = label_annotator.annotate(annotated_image, detections, labels)
return annotated_image
def load_model(resolution: int, checkpoint: str) -> RFDETR:
if checkpoint == "base":
return RFDETRBase(resolution=resolution)
elif checkpoint == "large":
return RFDETRLarge(resolution=resolution)
raise TypeError("Checkpoint must be a base or large.")
def image_processing_inference(
input_image: Image.Image,
confidence: float,
resolution: int,
checkpoint: str
):
model = load_model(resolution=resolution, checkpoint=checkpoint)
return detect_and_annotate(model=model, image=input_image, confidence=confidence)
def video_processing_inference(
input_video: str,
confidence: float,
resolution: int,
checkpoint: str,
progress=gr.Progress(track_tqdm=True)
):
model = load_model(resolution=resolution, checkpoint=checkpoint)
name = generate_unique_name()
output_video = os.path.join(VIDEO_TARGET_DIRECTORY, f"{name}.mp4")
video_info = sv.VideoInfo.from_video_path(input_video)
video_info.width = int(video_info.width * VIDEO_SCALE_FACTOR)
video_info.height = int(video_info.height * VIDEO_SCALE_FACTOR)
total = min(video_info.total_frames, video_info.fps * MAX_VIDEO_LENGTH_SECONDS)
frames_generator = sv.get_video_frames_generator(input_video, end=total)
with sv.VideoSink(output_video, video_info=video_info) as sink:
for frame in tqdm(frames_generator, total=total):
annotated_frame = detect_and_annotate(
model=model,
image=frame,
confidence=confidence
)
annotated_frame = sv.scale_image(annotated_frame, VIDEO_SCALE_FACTOR)
sink.write_frame(annotated_frame)
return output_video
with gr.Blocks() as demo:
gr.Markdown(MARKDOWN)
with gr.Tab("Image"):
with gr.Row():
image_processing_input_image = gr.Image(
label="Upload image",
image_mode='RGB',
type='pil',
height=600
)
image_processing_output_image = gr.Image(
label="Output image",
image_mode='RGB',
type='pil',
height=600
)
with gr.Row():
with gr.Column():
image_processing_confidence_slider = gr.Slider(
label="Confidence",
minimum=0.0,
maximum=1.0,
step=0.05,
value=0.5,
)
image_processing_resolution_slider = gr.Slider(
label="Inference resolution",
minimum=560,
maximum=1120,
step=56,
value=728,
)
image_processing_checkpoint_dropdown = gr.Dropdown(
label="Checkpoint",
choices=["base", "large"],
value="base"
)
with gr.Column():
image_processing_submit_button = gr.Button("Submit", value="primary")
gr.Examples(
fn=image_processing_inference,
examples=IMAGE_PROCESSING_EXAMPLES,
inputs=[
image_processing_input_image,
image_processing_confidence_slider,
image_processing_resolution_slider,
image_processing_checkpoint_dropdown
],
outputs=image_processing_output_image,
cache_examples=True,
run_on_click=True
)
image_processing_submit_button.click(
image_processing_inference,
inputs=[
image_processing_input_image,
image_processing_confidence_slider,
image_processing_resolution_slider,
image_processing_checkpoint_dropdown
],
outputs=image_processing_output_image,
)
with gr.Tab("Video"):
with gr.Row():
video_processing_input_video = gr.Video(
label='Upload video',
height=600
)
video_processing_output_video = gr.Video(
label='Output video',
height=600
)
with gr.Row():
with gr.Column():
video_processing_confidence_slider = gr.Slider(
label="Confidence",
minimum=0.0,
maximum=1.0,
step=0.05,
value=0.5,
)
video_processing_resolution_slider = gr.Slider(
label="Inference resolution",
minimum=560,
maximum=1120,
step=56,
value=728,
)
video_processing_checkpoint_dropdown = gr.Dropdown(
label="Checkpoint",
choices=["base", "large"],
value="base"
)
with gr.Column():
video_processing_submit_button = gr.Button("Submit", value="primary")
gr.Examples(
fn=video_processing_inference,
examples=VIDEO_PROCESSING_EXAMPLES,
inputs=[
video_processing_input_video,
video_processing_confidence_slider,
video_processing_resolution_slider,
video_processing_checkpoint_dropdown
],
outputs=video_processing_output_video,
run_on_click=True
)
video_processing_submit_button.click(
video_processing_inference,
inputs=[
video_processing_input_video,
video_processing_confidence_slider,
video_processing_resolution_slider,
video_processing_checkpoint_dropdown
],
outputs=video_processing_output_video
)
demo.launch(debug=False, show_error=True)
|