Search code examples
object-detectionyolov8

How to get coordinates of YOLOv8 object detection model?


I have a YOLOv8 object detection model trained on custom. It takes image as input and annotates the different objects my question is How do I get coordinates of different objects? I want these coordinate data to further crop the images.

If the object detected is a person I want coordinates of that same for cat and dog.

Thanks for help in advance.

My code looks something like this

infer = YOLO(r'path_to_trained_model.pt')
image_path = r'image'
results = infer(image_path)

class_name = "Person"  
confidence_threshold = 0.5 

for r in results:
    r.boxes.xyxy ## I was also getting this error 'list' object has no attribute 'xyxy'

I know I am missing quite a bit of data here since I have no idea how it works.


Solution

  • You can refer to the predicted box coordinates like this:

    results = model(image_path)
    boxes = results[0].boxes
    for box in boxes:
        print(box.xyxy)
    

    More information is here: https://docs.ultralytics.com/modes/predict/#working-with-results

    box.xyxy # box coordinates (tensor)
    box.cls.item() # class id
    box.conf.item() # confidence value