From 647eff87aa316c7dfd971ef2d0f7267fdbe37e03 Mon Sep 17 00:00:00 2001 From: Ifeanyi Idiaye <72707830+Ifeanyi55@users.noreply.github.com> Date: Thu, 9 Oct 2025 19:22:13 +0100 Subject: [PATCH] Created image_annotate.py --- computer_vision/image_annotate.py | 39 +++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 computer_vision/image_annotate.py diff --git a/computer_vision/image_annotate.py b/computer_vision/image_annotate.py new file mode 100644 index 000000000000..7c82e25f17e9 --- /dev/null +++ b/computer_vision/image_annotate.py @@ -0,0 +1,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("image_file_path") + annotate_img.show()