Ifeanyi commited on
Commit
8210062
·
verified ·
1 Parent(s): 241d543

Update ImageAnnotator.py

Browse files
Files changed (1) hide show
  1. ImageAnnotator.py +126 -0
ImageAnnotator.py CHANGED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import supervision as sv
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(thickness=6)
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(thickness=6)
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(thickness=6)
44
+ annotated_image_show = corner_annotator.annotate(
45
+ scene=image.copy(),
46
+ detections=detections)
47
+
48
+ elif annotator == "Color":
49
+ color_annotator = sv.ColorAnnotator(opacity=0.7)
50
+ annotated_image_show = color_annotator.annotate(
51
+ scene=image.copy(),
52
+ detections=detections)
53
+
54
+ elif annotator == "Circle":
55
+ circle_annotator = sv.CircleAnnotator(thickness=6)
56
+ annotated_image_show = circle_annotator.annotate(
57
+ scene=image.copy(),
58
+ detections=detections)
59
+
60
+ elif annotator == "Dot":
61
+ dot_annotator = sv.DotAnnotator(radius=20)
62
+ annotated_image_show = dot_annotator.annotate(
63
+ scene=image.copy(),
64
+ detections=detections)
65
+
66
+ elif annotator == "Triangle":
67
+ triangle_annotator = sv.TriangleAnnotator(height=50,base=20)
68
+ annotated_image_show = triangle_annotator.annotate(
69
+ scene=image.copy(),
70
+ detections=detections)
71
+
72
+ elif annotator == "Ellipse":
73
+ ellipse_annotator = sv.EllipseAnnotator(thickness=6)
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(width=80,height=20)
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(radius=90,opacity=0.7)
86
+ annotated_image_show = heatmap_annotator.annotate(
87
+ scene=image.copy(),
88
+ detections=detections)
89
+
90
+ elif annotator == "Label":
91
+ labels = [
92
+ f"{class_id} {confidence:.2f}"
93
+ for class_id, confidence
94
+ in zip(detections.class_id, 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