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?
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:
Add a check to ensure the frame isn’t empty before processing.
Create an output directory if it doesn’t already exist.
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.