Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3869

AI Camera - IMX500 • Re: Recording bounding boxes on top of the video

$
0
0
Thanks, the pre_callback was a bit laggy but I got the post_callback working. This records a 10 second video and shows the bounding boxes on top:

Code:

# Camera setuppicam2 = Picamera2(imx500.camera_num)config = picam2.create_video_configuration(    main={"size": (1440, 1080), "format": "RGB888"},    controls={"FrameRate": fps_value})imx500.show_network_fw_progress_bar()picam2.start(config)last_results = Nonepicam2.post_callback = draw_detections# Start recording with a unique filenameoutput_folder = '/media/ssd/videos'timestamp = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')h264_filename = os.path.join(output_folder, f'motion_{timestamp}.h264')encoder = H264Encoder(bitrate=10000000)picam2.start_encoder(encoder, h264_filename)  # Use start_encoderstart = time.time()while time.time() - start < 10:    metadata = picam2.capture_metadata()    last_results = parse_detections(metadata)    picam2.stop_encoder()encoder = Nonemp4_filename = h264_filename.replace('.h264', '.mp4')# Convert to MP4 using ffmpegsubprocess.run(    ['ffmpeg', '-i', h264_filename, '-c:v', 'copy', mp4_filename],    check=True,    stdout=subprocess.DEVNULL,    stderr=subprocess.DEVNULL)print(f"Video saved as {mp4_filename}")
However, when I adapted the above code to only record video when a person is detected, the actual recording works great, but the bounding boxes are no longer recorded on top. Not sure why

Code:

# Camera setuppicam2 = Picamera2(imx500.camera_num)config = picam2.create_video_configuration(    main={"size": (1440, 1080), "format": "RGB888"},    controls={"FrameRate": fps_value})picam2.post_callback = draw_detectionsimx500.show_network_fw_progress_bar()picam2.start(config)last_results = Noneencoder = H264Encoder(bitrate=10000000)# Initialize variablesperson_detected = Falselast_person_time = 0  # Time when the last person was detectedno_person_time_threshold = 5  # 5 seconds of no person detected to stop recordingrecording_started = False# Modify the while loop to handle dynamic recording based on person detectionwhile True:    metadata = picam2.capture_metadata()    detections = parse_detections(metadata)        # Check if a person is detected in the current frame    person_detected = any(detection.category == 0 and detection.conf > threshold_value  # Assuming category '0' is "person"                          for detection in detections)    current_time = time.time()  # Current time to check detection duration    if person_detected:        if not recording_started:            # Start recording if not already started            timestamp = datetime.now().strftime('%d-%m-%Y_%H-%M-%S')            h264_filename = os.path.join(output_folder, f'motion_{timestamp}.h264')                        picam2.start_encoder(encoder, h264_filename)  # Start recording            recording_started = True            start_time = current_time  # Record the start time            print(f"Recording started at {timestamp}")                        # Update the time when the person was last detected        last_person_time = current_time    else:        # Check if no person has been detected for 5 continuous seconds        if recording_started and current_time - last_person_time >= no_person_time_threshold:            picam2.stop_encoder()  # Stop recording            encoder = None            mp4_filename = h264_filename.replace('.h264', '.mp4')            subprocess.run(['ffmpeg', '-i', h264_filename, '-c:v', 'copy', mp4_filename], check=True)            print(f"Recording stopped. Video saved as {mp4_filename}")            recording_started = False            last_person_time = 0  # Reset the last person detection time

Statistics: Posted by joestables1 — Fri Nov 15, 2024 6:07 pm



Viewing all articles
Browse latest Browse all 3869

Trending Articles