Frame is empty when I try to save image

Hello, I’m getting stuck on something simple.
I just want to save an image when a certain class is detected.
Here is the code:

    for detection in detections:
        label = detection.get_label()
        bbox = detection.get_bbox()
        confidence = detection.get_confidence()
        output_path = os.path.join('output_frames', f"frame_{user_data.get_count()}.jpg")
        if label == "person":
            string_to_print += f"Detection: {label} {confidence:.2f}\n"
            detection_count += 1
            cv2.imwrite(output_path, frame)
            time.sleep(3)

I’m just adding it to the detection example but I’m getting an assertion that the frame is empty.
Is this because I’m not handling empty frames (are there any?) or is it that I’m trying to save the frame in the wrong part of the code?

Hey @sjnicholls414 ,

The issue you’re encountering is likely due to how the frame is processed in the callback function.
Here’s a suggested approach that might help:

  1. Add a check to ensure the frame isn’t empty before processing.
  2. Create an output directory if it doesn’t already exist.
  3. Convert the frame from RGB to BGR before saving it with OpenCV.

Here’s how you might implement these changes:

    person_detected = False
    for detection in detections:
        label = detection.get_label()
        confidence = detection.get_confidence()
        if label == "person":
            string_to_print += f"Detection: {label} {confidence:.2f}\n"
            detection_count += 1
            person_detected = True

    if person_detected and frame is not None:
        output_dir = 'output_frames'
        os.makedirs(output_dir, exist_ok=True)
        output_path = os.path.join(output_dir, f"frame_{user_data.get_count()}.jpg")
        cv2.imwrite(output_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
        print(f"Saved frame to {output_path}")

    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)
        cv2.putText(frame, f"{user_data.new_function()} {user_data.new_variable}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
        user_data.set_frame(frame)

This modification ensures that we only process the frame if it’s available, creates an output directory for the saved frames, and converts the frame to the correct color space for OpenCV.

Hope this helps fix the issue
Regards

Thank you for the reply, I’ll try this tonight. Just curious, would this save the low res frame or the full res?