raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
852 stars 180 forks source link

[HOW-TO] pass arrays through functions *whilst* displaying real time footage #961

Open gabbylancs opened 7 months ago

gabbylancs commented 7 months ago

Hi,

I'm trying to pre-process and detect features/circles etc on a raspberry pi camera using the open-cv functions. Currently I'm trying to do it in the pre-callback interrupt:

I've got in the call function:

with MappedArray(request, "main") as m: "code"

in the code I've tried passing arr = picam2.capture_array to normal opencv functions (e.g gaussian blur) but it freezes the footage I tried passing m.array directly but this doesnt return anything

I'm getting the idea that if I want to manipulate the image like this I cant use a certain mode with the callback or something? Can you advise please, I've read the documentation but its not super clear?

davidplowman commented 7 months ago

The relevant part of the manual is section 8.2.1. As it says there, from within the callback you can't use anything that has to wait for the camera (such as capture_request() or capture_metadata() methods).

It also points you at some examples showing how to use the MappedArray, and warns against doing lots of processing (object detection is often quite expensive!) within the callback. Note that the array that is available through the MappedArray is stored in camera specific memory, so to alter these images you have to be sure that you're overwriting the pixels in the original buffer.

Can you say anything more specific about what you've done that doesn't work? A (very short but otherwise working) example is often helpful.