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 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
import gradio as gr
|
| 5 |
+
import numpy as np
|
| 6 |
+
import cv2
|
| 7 |
+
import urllib
|
| 8 |
+
|
| 9 |
+
# load pre-trained vision model
|
| 10 |
+
model = YOLO("yolo12s.pt")
|
| 11 |
+
|
| 12 |
+
def image_annotate(image:str, annotator:str) -> Image.Image:
|
| 13 |
+
"""
|
| 14 |
+
Args:
|
| 15 |
+
image: the path to the image file
|
| 16 |
+
annotator: the type of annotator to use
|
| 17 |
+
Returns:
|
| 18 |
+
annotated image
|
| 19 |
+
"""
|
| 20 |
+
# load the input image
|
| 21 |
+
image = cv2.imread(image)
|
| 22 |
+
|
| 23 |
+
# run object detection on the image
|
| 24 |
+
result = model(image)[0]
|
| 25 |
+
|
| 26 |
+
# convert YOLO output to a Supervision-compatible detections format
|
| 27 |
+
detections = sv.Detections.from_ultralytics(result)
|
| 28 |
+
|
| 29 |
+
# select annotator
|
| 30 |
+
if annotator == "Box":
|
| 31 |
+
box_annotator = sv.BoxAnnotator()
|
| 32 |
+
annotated_image_show = box_annotator.annotate(
|
| 33 |
+
scene=image.copy(),
|
| 34 |
+
detections=detections)
|
| 35 |
+
|
| 36 |
+
elif annotator == "Roundbox":
|
| 37 |
+
round_box_annotator = sv.RoundBoxAnnotator()
|
| 38 |
+
annotated_image_show = round_box_annotator.annotate(
|
| 39 |
+
scene=image.copy(),
|
| 40 |
+
detections=detections)
|
| 41 |
+
|
| 42 |
+
elif annotator == "Boxcorner":
|
| 43 |
+
corner_annotator = sv.BoxCornerAnnotator()
|
| 44 |
+
annotated_image_show = corner_annotator.annotate(
|
| 45 |
+
scene=image.copy(),
|
| 46 |
+
detections=detections)
|
| 47 |
+
|
| 48 |
+
elif annotator == "Color":
|
| 49 |
+
color_annotator = sv.ColorAnnotator()
|
| 50 |
+
annotated_image_show = color_annotator.annotate(
|
| 51 |
+
scene=image.copy(),
|
| 52 |
+
detections=detections)
|
| 53 |
+
|
| 54 |
+
elif annotator == "Circle":
|
| 55 |
+
circle_annotator = sv.CircleAnnotator()
|
| 56 |
+
annotated_image_show = circle_annotator.annotate(
|
| 57 |
+
scene=image.copy(),
|
| 58 |
+
detections=detections)
|
| 59 |
+
|
| 60 |
+
elif annotator == "Dot":
|
| 61 |
+
dot_annotator = sv.DotAnnotator()
|
| 62 |
+
annotated_image_show = dot_annotator.annotate(
|
| 63 |
+
scene=image.copy(),
|
| 64 |
+
detections=detections)
|
| 65 |
+
|
| 66 |
+
elif annotator == "Triangle":
|
| 67 |
+
triangle_annotator = sv.TriangleAnnotator()
|
| 68 |
+
annotated_image_show = triangle_annotator.annotate(
|
| 69 |
+
scene=image.copy(),
|
| 70 |
+
detections=detections)
|
| 71 |
+
|
| 72 |
+
elif annotator == "Ellipse":
|
| 73 |
+
ellipse_annotator = sv.EllipseAnnotator()
|
| 74 |
+
annotated_image_show = ellipse_annotator.annotate(
|
| 75 |
+
scene=image.copy(),
|
| 76 |
+
detections=detections)
|
| 77 |
+
|
| 78 |
+
elif annotator == "Percentage":
|
| 79 |
+
percentage_bar_annotator = sv.PercentageBarAnnotator()
|
| 80 |
+
annotated_image_show = percentage_bar_annotator.annotate(
|
| 81 |
+
scene=image.copy(),
|
| 82 |
+
detections=detections)
|
| 83 |
+
|
| 84 |
+
elif annotator == "Heatmap":
|
| 85 |
+
heatmap_annotator = sv.HeatMapAnnotator()
|
| 86 |
+
annotated_image_show = heatmap_annotator.annotate(
|
| 87 |
+
scene=image.copy(),
|
| 88 |
+
detections=detections)
|
| 89 |
+
|
| 90 |
+
elif annotator == "Label":
|
| 91 |
+
labels = [
|
| 92 |
+
f"{class_name} {confidence:.2f}"
|
| 93 |
+
for class_name, confidence
|
| 94 |
+
in zip(detections.class_name, detections.confidence)
|
| 95 |
+
]
|
| 96 |
+
|
| 97 |
+
rich_label_annotator = sv.RichLabelAnnotator(
|
| 98 |
+
text_color=sv.Color.BLACK,
|
| 99 |
+
text_padding=10,
|
| 100 |
+
text_position=sv.Position.CENTER)
|
| 101 |
+
|
| 102 |
+
annotated_image_show = rich_label_annotator.annotate(
|
| 103 |
+
scene=image.copy(),
|
| 104 |
+
detections=detections,
|
| 105 |
+
labels=labels )
|
| 106 |
+
|
| 107 |
+
elif annotator == "Blur":
|
| 108 |
+
blur_annotator = sv.BlurAnnotator()
|
| 109 |
+
annotated_image_show = blur_annotator.annotate(
|
| 110 |
+
scene=image.copy(),
|
| 111 |
+
detections=detections)
|
| 112 |
+
|
| 113 |
+
elif annotator == "Pixelate":
|
| 114 |
+
pixelate_annotator = sv.PixelateAnnotator()
|
| 115 |
+
annotated_image_show = pixelate_annotator.annotate(
|
| 116 |
+
scene=image.copy(),
|
| 117 |
+
detections=detections)
|
| 118 |
+
|
| 119 |
+
elif annotator == "Backgroundcolor":
|
| 120 |
+
background_overlay_annotator = sv.BackgroundOverlayAnnotator()
|
| 121 |
+
annotated_image_show = background_overlay_annotator.annotate(
|
| 122 |
+
scene=image.copy(),
|
| 123 |
+
detections=detections)
|
| 124 |
+
|
| 125 |
+
# return annotated image
|
| 126 |
+
return annotated_image_show
|
| 127 |
+
|
| 128 |
+
app = gr.Interface(
|
| 129 |
+
fn = image_annotate,
|
| 130 |
+
title="Object Detection",
|
| 131 |
+
inputs = [gr.Image(type="filepath",label="Image"),gr.Radio(label="Select Annotator",
|
| 132 |
+
choices=["Box","Roundbox","Boxcorner","Color","Circle","Dot","Triangle",
|
| 133 |
+
"Ellipse","Percentage","Heatmap","Label","Blur",
|
| 134 |
+
"Pixelate","Backgroundcolor"],
|
| 135 |
+
value = "Box")],
|
| 136 |
+
outputs = gr.Image(label = "Annotated Image"),
|
| 137 |
+
examples = [["cars.jpg"],
|
| 138 |
+
["colorful-backgrounds-for-laptops.jpg"],
|
| 139 |
+
["final_animals-homeschooling_credit-alamy.jpg"]]
|
| 140 |
+
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
+
app.launch(mcp_server = True)
|