File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 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 ()
You can’t perform that action at this time.
0 commit comments