Getting actual bounding box, for object detection with raspberry pi

Im trying to know the position of detection object in the frame. I need help to understand the format of HailoBbox.

My result:

Frame count: 186
Detection 0: Label = Spantile, BBox = <hailo.HailoBBox(140733461937616)>, Confidence = 0.97
Total detections: 1

Frame count: 187
Detection 0: Label = Spantile, BBox = <hailo.HailoBBox(140733461935024)>, Confidence = 0.97
Total detections: 1

Frame count: 188
Detection 0: Label = Spantile, BBox = <hailo.HailoBBox(140733461937584)>, Confidence = 0.97
Total detections: 1

Frame count: 189
Detection 0: Label = Spantile, BBox = <hailo.HailoBBox(140733461935152)>, Confidence = 0.97
Total detections: 1

FPS: 30.10, Droprate: 0.00, Avg FPS: 30.25

My detection code:

def app_callback(pad, info, user_data):
    # Get the GstBuffer from the probe info
    buffer = info.get_buffer()
    # Check if the buffer is valid
    if buffer is None:
        return Gst.PadProbeReturn.OK
        
    # Using the user_data to count the number of frames
    user_data.increment()
    string_to_print = f"Frame count: {user_data.get_count()}\n"
    
    # Get the caps from the pad
    format, width, height = get_caps_from_pad(pad)

    # Get the video frame from the buffer if needed
    frame = None
    if user_data.use_frame and format is not None and width is not None and height is not None:
        frame = get_numpy_from_buffer(buffer, format, width, height)

    # Get the detections from the buffer
    roi = hailo.get_roi_from_buffer(buffer)
    detections = roi.get_objects_typed(hailo.HAILO_DETECTION)
    
    # Parse the detections and output detailed info
    detection_count = 0
    for detection in detections:
        label = detection.get_label()
        bbox = detection.get_bbox()  # (x_min, y_min, x_max, y_max)
        confidence = detection.get_confidence()
        string_to_print += f"Detection {detection_count}: Label = {label}, BBox = {bbox}, Confidence = {confidence:.2f}\n"
        detection_count += 1
    
    if detection_count > 0:
        string_to_print += f"Total detections: {detection_count}\n"
    
    # If frame is used, annotate and convert it
    if user_data.use_frame and frame is not None:
        cv2.putText(frame, f"Detections: {detection_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        user_data.set_frame(frame)

    print(string_to_print)
    return Gst.PadProbeReturn.OK

Hey @nasirmohidin93,

Welcome to the Hailo Community!

To check how to work with the HailoBBox and how to use it, you can refer to the following example code:

These examples will show you how to use and display the detections you get using the HailoBBox.

Please let me know if you have any other questions.

Best regards,

1 Like

Thanks it is very helpful!

Hi @nasirmohidin93
I have seen the examples, but unfortunately they are beyond my knowledge.
Can you show me your Python script with which you got the x_min, y_min, x_max, y_max of the bounding box.
Thank you

Hello, can someone please help me here. I have seen the examples given. However, I am not able to determine the coordinates of the bounding boxes. - A Python script would be helpful.

Hi,

Maybe this helps, here is example how to get car box :

...
    for detection in detections:
        label = detection.get_label()
        bbox = detection.get_bbox()
        confidence = detection.get_confidence()
        if label == "car" and confidence >= 0.5:
            x1_norm = bbox.xmin()
            y1_norm = bbox.ymin()
            x2_norm = bbox.xmax()
            y2_norm = bbox.ymax()
            x1 = int(x1_norm * width)
            y1 = int(y1_norm * height)
            x2 = int(x2_norm * width)
            y2 = int(y2_norm * height)
            car_detection = (x1, y1, x2 - x1, y2 - y1)
...
4 Likes

Thank you very much!
Otto