Spaces:
Sleeping
Sleeping
File size: 4,417 Bytes
565e3f9 345eb49 565e3f9 345eb49 565e3f9 |
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 |
import supervision as sv
from ultralytics import YOLO
from PIL import Image
import gradio as gr
import numpy as np
import cv2
import urllib
# load pre-trained vision model
model = YOLO("yolo12s.pt")
def image_annotate(image:str, annotator:str) -> Image.Image:
"""
Args:
image: the path to the image file
annotator: the type of annotator to use
Returns:
annotated image
"""
# load the input image
image = cv2.imread(image)
# run object detection on the image
result = model(image)[0]
# convert YOLO output to a Supervision-compatible detections format
detections = sv.Detections.from_ultralytics(result)
# select annotator
if annotator == "Box":
box_annotator = sv.BoxAnnotator()
annotated_image_show = box_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Roundbox":
round_box_annotator = sv.RoundBoxAnnotator()
annotated_image_show = round_box_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Boxcorner":
corner_annotator = sv.BoxCornerAnnotator()
annotated_image_show = corner_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Color":
color_annotator = sv.ColorAnnotator()
annotated_image_show = color_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Circle":
circle_annotator = sv.CircleAnnotator()
annotated_image_show = circle_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Dot":
dot_annotator = sv.DotAnnotator()
annotated_image_show = dot_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Triangle":
triangle_annotator = sv.TriangleAnnotator()
annotated_image_show = triangle_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Ellipse":
ellipse_annotator = sv.EllipseAnnotator()
annotated_image_show = ellipse_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Percentage":
percentage_bar_annotator = sv.PercentageBarAnnotator()
annotated_image_show = percentage_bar_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Heatmap":
heatmap_annotator = sv.HeatMapAnnotator()
annotated_image_show = heatmap_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Label":
labels = [
f"{class_name} {confidence:.2f}"
for class_name, confidence
in zip(detections.class_name, detections.confidence)
]
rich_label_annotator = sv.RichLabelAnnotator(
text_color=sv.Color.BLACK,
text_padding=10,
text_position=sv.Position.CENTER)
annotated_image_show = rich_label_annotator.annotate(
scene=image.copy(),
detections=detections,
labels=labels )
elif annotator == "Blur":
blur_annotator = sv.BlurAnnotator()
annotated_image_show = blur_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Pixelate":
pixelate_annotator = sv.PixelateAnnotator()
annotated_image_show = pixelate_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Backgroundcolor":
background_overlay_annotator = sv.BackgroundOverlayAnnotator()
annotated_image_show = background_overlay_annotator.annotate(
scene=image.copy(),
detections=detections)
# return annotated image
return annotated_image_show
app = gr.Interface(
fn = image_annotate,
title="Object Detection",
inputs = [gr.Image(type="filepath",label="Image"),gr.Radio(label="Select Annotator",
choices=["Box","Roundbox","Boxcorner","Color","Circle","Dot","Triangle",
"Ellipse","Percentage","Heatmap","Label","Blur",
"Pixelate","Backgroundcolor"],
value = "Box")],
outputs = gr.Image(label = "Annotated Image"),
examples = [["cars.jpg"],
["colorful-backgrounds-for-laptops.jpg"],
["final_animals-homeschooling_credit-alamy.jpg"]]
)
if __name__ == "__main__":
app.launch(mcp_server = True) |