User_data.use_frame Returning False, Unable to Retrieve Frame (Hailo RPI Pipeline Issue)

Hi all,

I’m working on a license plate recognition pipeline using the Hailo AI platform on a Raspberry Pi. My goal is to detect license plates in video frames, crop them, and perform OCR to read the plate numbers. I trained my own YOLOv8n model for license plate detection, converted it to a .hef file, and I am using that in my pipeline. While the bounding boxes for license plates are correctly positioned when the code runs, I encounter an issue with extracting the frame for OCR processing.

The issue is that the user_data.use_frame variable keeps returning False, even though I set it to True, which prevents me from retrieving and saving the video frames where the license plates are detected. This issue happens even though the pipeline is detecting the plates correctly and displaying bounding boxes.

Here’s a snippet of the error messages I’m seeing:

Main user_data id: 140736275771792
Main user_data.use_frame: True
...
user_data id: 140736275771792
user_data.use_frame: False
Format: RGB, Width: 640, Height: 640
Frame not retrieved due to missing format or dimensions.
Detection: 0.75
Frame is None, cannot extract plate image.
Frame count: 1

user_data id: 140736275771792
user_data.use_frame: False
Format: RGB, Width: 640, Height: 640
Frame not retrieved due to missing format or dimensions.
Detection: 0.75
Frame is None, cannot extract plate image.
Frame count: 2
...

In my code, user_data.use_frame is explicitly set to True in the callback class initialization:

class user_app_callback_class(app_callback_class):
    def __init__(self):
        super().__init__()
        self.use_frame = True

Despite this, it continues to return False during processing, which prevents the frames from being captured. Here’s a relevant section of my callback function:

def app_callback(pad, info, user_data):
    print(f"user_data id: {id(user_data)}")
    print(f"user_data.use_frame: {user_data.use_frame}")
    
    buffer = info.get_buffer()
    if buffer is None:
        return Gst.PadProbeReturn.OK
    
    user_data.increment()
    format, width, height = get_caps_from_pad(pad)
    print(f"Format: {format}, Width: {width}, Height: {height}")
    print(f"user_data.use_frame: {user_data.use_frame}")
    
    frame = None
    if user_data.use_frame and format and width and height:
        frame = get_numpy_from_buffer(buffer, format, width, height)
        if frame is None:
            print("Failed to get frame from buffer.")
        else:
            print(f"Frame shape: {frame.shape}")
    else:
        print("Frame not retrieved due to missing format or dimensions.")
    
    # Crop the license plate region from the frame
    if frame is not None:
        plate_image = frame[y_min:y_max, x_min:x_max]
        # Save the license plate image
        output_dir = "/path/to/save"
        os.makedirs(output_dir, exist_ok=True)
        filename = os.path.join(output_dir, f"plate_{user_data.get_count()}_{detection_count}.png")
        cv2.imwrite(filename, plate_image)
    else:
        print("Frame is None, cannot extract plate image.")
    
    if user_data.use_frame and frame is not None:
        # Draw detection count on the frame
        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)

I’m currently processing a video file as the input source, and the terminal shows the bounding boxes correctly during runtime.

I would appreciate any guidance on why user_data.use_frame might be returning False even though it is initialized as True. If there’s a more efficient way to achieve my goal of license plate detection and OCR, I am open to trying alternative approaches or recommendations.

Thank you for your help!

Hey there, took me hours to figure the same thing out, but turns out you need to add the argument --use-frame when running the code from terminal

1 Like

Thanks, I tried your suggestion and it worked. Appreciate the help.