0

I am trying to lower the time to capture a still image from the Pi camera (I have a V2 and HQ). To this end, I have found my way to the PiCamera 1.13 docs, specifically chapter 16 dealing with the MMAL object. I tried following the example code given between 16.1.1 and 16.1.7, which seems to be a basic set-up and capture. I reduced the code to the minimum I thought would work; there is a lot of input and output confirmation I removed. Here is the code:

from picamera import mmal, mmalobj as mo
import io

def image_callback(port, buf):
    output.write(buf.data)
    return bool(buf.flags & mmal.MMAL_BUFFER_HEADER_FLAG_FRAME_END)

camera = mo.MMALCamera()
preview = mo.MMALRenderer()

camera.outputs[2].format = mmal.MMAL_ENCODING_RGB24
camera.outputs[2].framesize = (640, 480)
camera.outputs[2].commit()
camera.outputs[2]
camera.outputs[2].enable()
camera.outputs[2].disable()

output = io.open('image.data', 'wb')

camera.outputs[2].enable(image_callback)
output.tell()

camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = True
camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = False
output.tell()
camera.outputs[2].disable()
output.close()

This code runs without error from a file, cam_test.py, but produces the file image.data of size 0 bytes. However, when the code is run interactively from the Python console, as the examples do, the desired file is created properly only when the code is entered in parts. That is, I can paste the code up to the first output.tell(), then copy & paste the remaining code (the image capture) into the console and have success. This tells me I have the code to create the image file; why does it not work from a Python file? I have checked Python versions in both cases is the same (3.7.3) and use a Python virtual environment. Thanks in advance for any input.

Edit: The second part of the code is the last 5 lines

camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = True
camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = False
output.tell()
camera.outputs[2].disable()
output.close()

I had tried a 1 second delay between the two parts (in the .py file); still image.data file of 0 bytes.

  • Could it be a timing issue? It will take a few seconds to post the second part of the code and wonder if that’s needed - maybe adding a sleep would help? – Andyroo Jun 16 at 5:48
0

Well, it appears it is a timing issue. I modified the code to include a loop to capture an image and finish when the size of image.data is greater than zero. Obviously slower than the first version but it appears we can't rush the pi camera. I would prefer not to import two io/os type modules so will look into that, and also whether both forms of CAPTURE are required in the loop.

while (os.path.getsize('image.data') == 0):
    camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = True
    camera.outputs[2].params[mmal.MMAL_PARAMETER_CAPTURE] = False
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.