Spaces:
Sleeping
Sleeping
File size: 3,771 Bytes
8210062 43bacca 8210062 |
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 |
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:
"""
Annotates objects detected in an 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(thickness=6)
annotated_image_show = box_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Roundbox":
round_box_annotator = sv.RoundBoxAnnotator(thickness=6)
annotated_image_show = round_box_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Boxcorner":
corner_annotator = sv.BoxCornerAnnotator(thickness=6)
annotated_image_show = corner_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Color":
color_annotator = sv.ColorAnnotator(opacity=0.7)
annotated_image_show = color_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Circle":
circle_annotator = sv.CircleAnnotator(thickness=6)
annotated_image_show = circle_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Dot":
dot_annotator = sv.DotAnnotator(radius=20)
annotated_image_show = dot_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Triangle":
triangle_annotator = sv.TriangleAnnotator(height=50,base=20)
annotated_image_show = triangle_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Ellipse":
ellipse_annotator = sv.EllipseAnnotator(thickness=6)
annotated_image_show = ellipse_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Percentage":
percentage_bar_annotator = sv.PercentageBarAnnotator(width=80,height=20)
annotated_image_show = percentage_bar_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Heatmap":
heatmap_annotator = sv.HeatMapAnnotator(radius=90,opacity=0.7)
annotated_image_show = heatmap_annotator.annotate(
scene=image.copy(),
detections=detections)
elif annotator == "Label":
labels = [
f"{class_id} {confidence:.2f}"
for class_id, confidence
in zip(detections.class_id, 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
|