Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions computer_vision/image_annotate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import supervision as sv
from ultralytics import YOLO
from PIL import Image
import numpy as np

Check failure on line 4 in computer_vision/image_annotate.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

computer_vision/image_annotate.py:4:17: F401 `numpy` imported but unused
import cv2

Check failure on line 5 in computer_vision/image_annotate.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

computer_vision/image_annotate.py:1:1: I001 Import block is un-sorted or un-formatted

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)

Check failure on line 27 in computer_vision/image_annotate.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

computer_vision/image_annotate.py:27:1: W293 Blank line contains whitespace
# 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()
Loading