Skip to content

How to get the coordinates of the bounding box in YOLO object detection? #388

Closed

Description

@milind-soni

❔Question

I need to get the bounding box coordinates generated in an image using the object detection. How do I achieve that

Activity

  1. github-actions commented on Jul 13, 2020

    @github-actions
    Contributor

    Hello @milind-soni, thank you for your interest in our work! Please visit our Custom Training Tutorial to get started, and see our Jupyter Notebook Open In Colab, Docker Image, and Google Cloud Quickstart Guide for example environments.

    If this is a bug report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

    If this is a custom model or data training question, please note that Ultralytics does not provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as:

    • Cloud-based AI systems operating on hundreds of HD video streams in realtime.
    • Edge AI integrated into custom iOS and Android apps for realtime 30 FPS video inference.
    • Custom data training, hyperparameter evolution, and model exportation to any destination.

    For more information please visit https://www.ultralytics.com.

  2. glenn-jocher commented on Jul 13, 2020

    @glenn-jocher
    Member

    @milind-soni detection results are available here:

    yolov5/detect.py

    Lines 92 to 102 in ea34f84

    # Write results
    for *xyxy, conf, cls in det:
    if save_txt: # Write to file
    xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
    with open(txt_path + '.txt', 'a') as f:
    f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
    if save_img or view_img: # Add bbox to image
    label = '%s %.2f' % (names[int(cls)], conf)
    plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

  3. ankamdeepika commented on Jul 18, 2020

    @ankamdeepika

    @glenn-jocher could you help me crop the detected objects using the bounding box coordinates

  4. fahimnawaz7 commented on Dec 24, 2020

    @fahimnawaz7

    I too have the same issue

  5. glenn-jocher commented on Dec 24, 2020

    @glenn-jocher
    Member

    @fahimnawaz7 you can get the coordinates within detect.py, or you can use YOLOv5 from PyTorch Hub. See https://pytorch.org/hub/ultralytics_yolov5/

    import cv2
    import torch
    from PIL import Image
    
    # Model
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).autoshape()  # for PIL/cv2/np inputs and NMS
    
    # Images
    for f in ['zidane.jpg', 'bus.jpg']:  # download 2 images
        print(f'Downloading {f}...')
        torch.hub.download_url_to_file('https://github.com/ultralytics/yolov5/releases/download/v1.0/' + f, f)
    img1 = Image.open('zidane.jpg')  # PIL image
    img2 = cv2.imread('bus.jpg')[:, :, ::-1]  # OpenCV image (BGR to RGB)
    imgs = [img1, img2]  # batched list of images
    
    # Inference
    results = model(imgs, size=640)  # includes NMS
    
    # Results
    results.print()  # print results to screen
    results.show()  # display results
    results.save()  # save as results1.jpg, results2.jpg... etc.
    
    # Data
    print('\n', results.xyxy[0])  # print img1 predictions
    #          x1 (pixels)  y1 (pixels)  x2 (pixels)  y2 (pixels)   confidence        class
    # tensor([[7.47613e+02, 4.01168e+01, 1.14978e+03, 7.12016e+02, 8.71210e-01, 0.00000e+00],
    #         [1.17464e+02, 1.96875e+02, 1.00145e+03, 7.11802e+02, 8.08795e-01, 0.00000e+00],
    #         [4.23969e+02, 4.30401e+02, 5.16833e+02, 7.20000e+02, 7.77376e-01, 2.70000e+01],
    #         [9.81310e+02, 3.10712e+02, 1.03111e+03, 4.19273e+02, 2.86850e-01, 2.70000e+01]])
  6. AmelNozieres commented on Feb 16, 2021

    @AmelNozieres

    Hello, is it good practice to modify the standard detect.py? what about if there is a new version? could we upgrade our code without any damage. How about we call detect.py with --save.txt, we parse the bounding boxes and we crop the image after that?
    Also is there a function as torch.hub.load to call with my weights?

  7. glenn-jocher commented on Feb 16, 2021

    @glenn-jocher
    Member

    @AmelNozieres yes YOLOv5 Hub models can be created from custom weights. See PyTorch Hub Tutorial for details:

    Tutorials

  8. fcakyon commented on Feb 20, 2021

    @fcakyon
    Member

    @glenn-jocher is It possible to load custom weights in an environment that doesnt have internet access via yolov5 hub workflow? All examples I have come across require online access to the related github repo.

  9. glenn-jocher commented on Feb 20, 2021

    @glenn-jocher
    Member

    @fcakyon yes, if the hub cache and models are stored locally stored internet access is not needed.

  10. glenn-jocher commented on Feb 24, 2021

    @glenn-jocher
    Member
  11. glenn-jocher commented on Feb 24, 2021

    @glenn-jocher
    Member

    @mycuriosity123 its assumed that users have at least a working knowledge of python here. To produce bounding box coordinates you simply copy and paste the code at the link I provided you:

    import torch
    
    # Model
    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
    
    # Images
    dir = 'https://github.com/ultralytics/yolov5/raw/master/data/images/'
    imgs = [dir + f for f in ('zidane.jpg', 'bus.jpg')]  # batched list of images
    
    # Inference
    results = model(imgs)
    
    # Results
    results.print()  
    results.save()  # or .show()
    
    # Data
    print(results.xyxy[0])  # print img1 predictions (pixels)
    #                   x1           y1           x2           y2   confidence        class
    # tensor([[7.50637e+02, 4.37279e+01, 1.15887e+03, 7.08682e+02, 8.18137e-01, 0.00000e+00],
    #         [9.33597e+01, 2.07387e+02, 1.04737e+03, 7.10224e+02, 5.78011e-01, 0.00000e+00],
    #         [4.24503e+02, 4.29092e+02, 5.16300e+02, 7.16425e+02, 5.68713e-01, 2.70000e+01]])
  12. glenn-jocher commented on Feb 24, 2021

    @glenn-jocher
    Member

    @mycuriosity123 all the question you are asking are explained in the torch hub tutorial. I strongly recommend you start there.

    Tutorials

  13. mycuriosity123 commented on Feb 25, 2021

    @mycuriosity123

    ❔Question

    I need to get the bounding box coordinates generated in an image using the object detection. How do I achieve that

    have you achieved bounding box co ordinates?

  14. 40 remaining items

  15. ChanHaoHao commented on Mar 6, 2023

    @ChanHaoHao

    Maybe it will be easier to use --save-txt to get the coordinates of the boxes of each figure in YOLO form.

  16. mathhauk commented on Mar 23, 2023

    @mathhauk

    Is it possible to get coordinates for the bounding boxes when the object detection is used on a video? Can't seem to find this anywhere.

  17. glenn-jocher commented on Nov 15, 2023

    @glenn-jocher
    Member

    @mathhauk 👋 Hello! Yes, it's absolutely possible to obtain bounding box coordinates from YOLOv5 object detection in a video. In fact, this can be easily achieved using YOLOv5 by saving detection results to a text file using the --save-txt argument in the inference script. Each text file will contain the bounding box coordinates of the detected objects in YOLO format (x_center, y_center, width, height).

    Afterwards, these coordinates can be utilized to calculate pixel locations using OpenCV or to convert to absolute coordinates based on the size of the image or frame from the video. If you have any other questions or need further assistance, feel free to ask!

  18. HankSteel77 commented on Dec 11, 2023

    @HankSteel77

    Hi @glenn-jocher, I wanted to ask you if there is any possibility to extract detected image as nparray to carry out further processing, for example ocr. I saw that results.crop(save=False) returns 'Im', what seems to be image array containging information about detected object limited by the boundingbox.

  19. glenn-jocher commented on Dec 11, 2023

    @glenn-jocher
    Member

    @HankSteel77 yes, it is indeed possible to extract the detected image as an nparray for further processing such as OCR. You can achieve this by using the results.crop() method, which returns an image array containing the detected object limited by the bounding box. This can then be used for downstream tasks such as OCR. If you have any more questions or need additional assistance, feel free to ask!

  20. HankSteel77 commented on Dec 11, 2023

    @HankSteel77

    @glenn-jocher Thanks for fast answer! I wanted to ask if there is build in functionality that allows to extract data as array like results.ims[0] or something that extends functionality of results.crop. I searched the common.py file but i had trouble to understand how to simply obtain information only about 'Im' array. In a nutshell, i just wanted to know if there is build in function that allows to receive information about 'Im' in "air', without saving the and parsing data. I would be grateful if you told me how to do it.

  21. glenn-jocher commented on Dec 12, 2023

    @glenn-jocher
    Member

    @HankSteel77 🎉 Yes, there is a straightforward built-in functionality in YOLOv5 that enables you to obtain the "Im" array without having to save and parse the data. You can access the "Im" array directly using the results.render() method. This method returns a list of cropped image arrays (not saved) from the results, making it simple and efficient to obtain the desired "Im" information. If you have any more questions or need further assistance, feel free to ask!

  22. Manaschintawar commented on Mar 28, 2024

    @Manaschintawar

    i want to extract the class id from the bounding box but
    results = model.predict(frame)
    a = results[0].boxes.xyxy
    px = pd.DataFrame(a).astype("float")

    for index, row in px.iterrows():
        print(px) this is the code and when i am printing the value i dont get the class id in the last row of the extract data now for class id what should i do? plz help 😣!!
    
  23. glenn-jocher commented on Mar 28, 2024

    @glenn-jocher
    Member

    @Manaschintawar hello! To extract the class ID along with the bounding box details, you can access the results[0].pandas().xyxy attribute, which provides a Pandas DataFrame including class IDs. Here's how you can modify your code:

    results = model.predict(frame)
    df = results[0].pandas().xyxy[0]  # results in a DataFrame
    
    for index, row in df.iterrows():
        print(row['class'])  # This prints the class ID

    This will give you the class ID for each detected object in your frame. If you have any more questions, feel free to ask!

  24. Manaschintawar commented on Apr 8, 2024

    @Manaschintawar
  25. glenn-jocher commented on Apr 9, 2024

    @glenn-jocher
    Member

    @Manaschintawar hey there! YOLOv8 employs similar syntax for working with results as YOLOv5. For extracting class IDs and bounding boxes, you can use the results.pandas().xyxy[0] attribute after making predictions. Here's a quick example:

    results = model.predict(frame)
    df = results.pandas().xyxy[0]  # Obtain a DataFrame with detection details
    
    for index, row in df.iterrows():
        print(row['class'])  # Print out the class ID

    If YOLOv8 has brought any specific changes or additional features, I recommend checking out the official documentation or the updates section of the GitHub repo. Let me know if there's anything else you need! 🌟

  26. Muhnajh commented on May 10, 2024

    @Muhnajh

    @milind-soni detection results are available here:

    yolov5/detect.py

    Lines 92 to 102 in ea34f84

    # Write results
    for *xyxy, conf, cls in det:
    if save_txt: # Write to file
    xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
    with open(txt_path + '.txt', 'a') as f:
    f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format
    if save_img or view_img: # Add bbox to image
    label = '%s %.2f' % (names[int(cls)], conf)
    plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3)

    hai sir, can you give code for appear the coordinate with bounding box after Run evaluation image,
    which part I must added from the code roboflow generate for Yolov5 which run at google colab

  27. tejasri19 commented on Aug 15, 2024

    @tejasri19
    results = model("path to image")
    boxes = []
    scores = []
    for box in results[0].boxes:
        cords = box.xyxy[0].tolist()
        x1, y1, x2, y2 = [round(x) for x in cords]
        score = box.conf[0].item()  # Assuming the confidence score is available in box.conf
        cls = results[0].names[box.cls[0].item()]
        boxes.append([x1, y1, x2, y2, score, cls])
        scores.append(score)
    
    print("Boxes:", boxes)
    print("Scores:", scores
    ```)
    
    This is working.
    
  28. glenn-jocher commented on Aug 15, 2024

    @glenn-jocher
    Member

    Hello @tejasri19,

    Great to hear that you have a working solution! If you want to display the coordinates of the bounding boxes on the evaluation image, you can modify your code to include drawing the bounding boxes on the image. Here's an example of how you can achieve this using OpenCV:

    import cv2
    import matplotlib.pyplot as plt
    
    # Load the image
    image_path = "path to image"
    image = cv2.imread(image_path)
    
    # Perform inference
    results = model(image_path)
    
    # Extract bounding box coordinates and class names
    boxes = []
    scores = []
    for box in results[0].boxes:
        cords = box.xyxy[0].tolist()
        x1, y1, x2, y2 = [round(x) for x in cords]
        score = box.conf[0].item()  # Assuming the confidence score is available in box.conf
        cls = results[0].names[box.cls[0].item()]
        boxes.append([x1, y1, x2, y2, score, cls])
        scores.append(score)
    
        # Draw the bounding box on the image
        cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(image, f'{cls} {score:.2f}', (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
    
    # Display the image with bounding boxes
    plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
    plt.axis('off')
    plt.show()
    
    print("Boxes:", boxes)
    print("Scores:", scores)

    This code will draw the bounding boxes and class labels on the image and display it using matplotlib. If you encounter any issues or have further questions, feel free to ask! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions