I think it might be that, because the encoder runs in a background thread, you're drawing on the image buffer after the encoder has taken it.
I would try putting the drawing code into Picamera2's pre_callback. You can see how to do that in the simpler imx500_object_detection_demo.py example. Note that, in the pre_callback, you won't have access to the detection results for that frame, only for a previous frame - so there'll be a slight "lag" in the box position when people are moving.
In case the slight lag is a problem, it is possible to feed the encoder frames "by hand", in your case after you've run detection and drawn boxes onto that image. The other approach is easier, but if you want to try it, the basic procedure for driving the encoder like this is shown below.
I would try putting the drawing code into Picamera2's pre_callback. You can see how to do that in the simpler imx500_object_detection_demo.py example. Note that, in the pre_callback, you won't have access to the detection results for that frame, only for a previous frame - so there'll be a slight "lag" in the box position when people are moving.
In case the slight lag is a problem, it is possible to feed the encoder frames "by hand", in your case after you've run detection and drawn boxes onto that image. The other approach is easier, but if you want to try it, the basic procedure for driving the encoder like this is shown below.
Code:
import timefrom picamera2 import Picamera2from picamera2.encoders import H264Encoderfrom picamera2.outputs import FileOutputpicam2 = Picamera2()config = picam2.create_video_configuration({'format': 'YUV420', 'size': (640, 360)})picam2.start(config)encoder = H264Encoder()encoder.framerate = 30encoder.size = config["main"]["size"]encoder.format = config["main"]["format"]encoder.bitrate = 5000000encoder.output = FileOutput("test.h264")encoder.start()start = time.time()while time.time() - start < 10: with picam2.captured_request() as request: encoder.encode('main', request)encoder.stop()picam2.stop()
Statistics: Posted by therealdavidp — Thu Nov 14, 2024 10:36 am