Object recognition using hailopython

I would like to write an object recognition code by referring to the address python postprocess writing method below.

https://github.com/hailo-ai/tappas/blob/master/docs/write_your_own_application/write-your-own-python-postprocess.rst

Module code

import hailo
from gsthailo import VideoFrame
from gi.repository import Gst
import numpy as np

def run(video_frame: VideoFrame):
    try:
        # Get the ROI from the VideoFrame
        roi = video_frame.roi

        # Retrieve tensors from the ROI
        tensors = roi.get_tensors()

        # Debugging: Print available tensor names
        print("Available tensors:")
        for tensor in tensors:
            print(f"Tensor name: {tensor.name()}")

        # Locate the specific tensor
        output_tensor = None
        for tensor in tensors:
            if tensor.name() == "yolov8n/yolov8_nms_postprocess":
                output_tensor = tensor
                break

        if not output_tensor:
            print("Output tensor 'yolov8s/yolov8_nms_postprocess' not found!")
            return Gst.FlowReturn.OK

        # Convert tensor to a numpy array
        tensor_array = np.array(output_tensor, copy=False)

        # Ensure the tensor is not empty
        if tensor_array.size == 0:
            print(f"Tensor '{output_tensor.name()}' is empty. Check the input data and model configuration.")
            print(f"Tensor shape: {output_tensor.height()}x{output_tensor.width()}x{output_tensor.features()}")
            return Gst.FlowReturn.OK

        # Process each detection in the tensor
        for detection in tensor_array:
            if len(detection) < 6:
                print(f"Invalid detection data: {detection}")
                continue

            # Unpack detection data
            xmin, ymin, xmax, ymax, confidence, class_id = detection[:6]

            # Filter detections by confidence threshold
            if confidence < 0.5:  # Example confidence threshold
                continue

            # Create a bounding box
            bbox = hailo.HailoBBox(xmin=float(xmin), ymin=float(ymin),
                                   width=float(xmax - xmin), height=float(ymax - ymin))

            # Map class_id to a label (example mapping, replace with your own)
            label_map = {0: "person", 1: "bicycle", 2: "car"}  # Example class labels
            label = label_map.get(int(class_id), "unknown")

            # Create a detection object
            detection_obj = hailo.HailoDetection(bbox=bbox, label=label, confidence=float(confidence))

            # Add detection to the ROI
            roi.add_object(detection_obj)

    except Exception as e:
        print(f"Error during object detection: {e}")

    return Gst.FlowReturn.OK

Execution Code

import gi
gi.require_version('Gst', '1.0')
from gi.repository import Gst

Gst.init(None)

def main():
    
    device = "/dev/video0"
    hef_path = "yolov8n.hef" 
    python_module = "test.py"

    pipeline_str = f"""
    v4l2src device={device} ! video/x-raw, width=640, height=480, format=YUY2 ! queue !
    videoconvert ! videoflip video-direction=horiz ! video/x-raw, format=RGB, width=640, height=480 ! queue !
    videoscale ! video/x-raw, format=RGB, width=640, height=640 ! queue !
    hailonet hef-path={hef_path} batch-size=1 ! video/x-raw, format=RGB, width=640, height=640 ! queue !
    hailopython module={python_module} ! hailooverlay font-thickness=2 line-thickness=1 ! videoconvert ! autovideosink sync=false
    """

    print(f"Pipeline: {pipeline_str}")

    pipeline = Gst.parse_launch(pipeline_str)

    pipeline.set_state(Gst.State.PLAYING)

    bus = pipeline.get_bus()
    message = bus.timed_pop_filtered(
        Gst.CLOCK_TIME_NONE,
        Gst.MessageType.EOS | Gst.MessageType.ERROR
    )

    if message.type == Gst.MessageType.ERROR:
        err, debug = message.parse_error()
        print(f"Error: {err}, {debug}")
    elif message.type == Gst.MessageType.EOS:
        print("End of stream")

    pipeline.set_state(Gst.State.NULL)

if __name__ == "__main__":
    main()

The part where the model is read is successful, but the tensor is said to be empty.

Available tensors:
Tensor name: yolov8n/yolov8_nms_postprocess
Tensor 'yolov8n/yolov8_nms_postprocess' is empty. Check the input data and model configuration.
Tensor shape: 2x100x0

Is there a way to solve this

Even if it’s not this way, is there a way to create object recognition using the module code of hailopython