forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_annotate.py
More file actions
39 lines (29 loc) · 1.1 KB
/
image_annotate.py
File metadata and controls
39 lines (29 loc) · 1.1 KB
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
import supervision as sv
from ultralytics import YOLO
from PIL import Image
import numpy as np
import cv2
def image_annotate(image:str) -> Image.Image:
# load the input image
image = cv2.imread(image)
# load pre-trained vision model
model = YOLO("yolo12s.pt")
# 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)
# initialize a box annotator for drawing detection bounding boxes
box_annotator = sv.BoxAnnotator()
# annotate the image with detected objects
annotated_image = box_annotator.annotate(
scene=image.copy(),
detections=detections)
# convert BGR to RGB for correct display
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
# convert the annotated NumPy array (RGB) to a PIL Image object
annotated_pil_image = Image.fromarray(annotated_image_rgb)
# return annotated image
return annotated_pil_image
if __name__ == "__main__":
annotate_img = image_annotate("/content/sample_data/Furry.png")
annotate_img.show()