Skip to content

Commit 647eff8

Browse files
authored
Created image_annotate.py
1 parent 788d95b commit 647eff8

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

computer_vision/image_annotate.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import supervision as sv
2+
from ultralytics import YOLO
3+
from PIL import Image
4+
import numpy as np
5+
import cv2
6+
7+
def image_annotate(image:str) -> Image.Image:
8+
# load the input image
9+
image = cv2.imread(image)
10+
11+
# load pre-trained vision model
12+
model = YOLO("yolo12s.pt")
13+
14+
# run object detection on the image
15+
result = model(image)[0]
16+
17+
# convert YOLO output to a Supervision-compatible detections format
18+
detections = sv.Detections.from_ultralytics(result)
19+
20+
# initialize a box annotator for drawing detection bounding boxes
21+
box_annotator = sv.BoxAnnotator()
22+
23+
# annotate the image with detected objects
24+
annotated_image = box_annotator.annotate(
25+
scene=image.copy(),
26+
detections=detections)
27+
28+
# convert BGR to RGB for correct display
29+
annotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
30+
31+
# convert the annotated NumPy array (RGB) to a PIL Image object
32+
annotated_pil_image = Image.fromarray(annotated_image_rgb)
33+
34+
# return annotated image
35+
return annotated_pil_image
36+
37+
if __name__ == "__main__":
38+
annotate_img = image_annotate("image_file_path")
39+
annotate_img.show()

0 commit comments

Comments
 (0)