raspberrypi / picamera2

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

[HOW-TO] Reduce delay after switching sensor modes. #1062

Closed h-huss closed 4 months ago

h-huss commented 4 months ago

Describe what it is that you want to accomplish I want to switch from a 120fps sensor mode to a still configuration with as litte delay as possible. (RPI 5 / HQ Camera / picamera2 0.3.18)

Describe alternatives you've considered

After the mode switch, the first image takes 500-700ms, after that, the following ones take 50 ms. This is possibly related to the modeswitch itself, due to the eventloop its hard to identify the time-consuming operation.

Is this a limitation of the camera stack itself?

import time
from picamera2 import Picamera2

picam = Picamera2()
cfgHighspeed = picam.create_video_configuration(
    {"size": (1024, 768)},
    controls={
        "FrameRate": 120,
        "ExposureTime": 20000,
        "AnalogueGain": 2.0,
        "ColourGains": (2.26, 2.48),
    },
)

cfgStill = picam.create_still_configuration(
    {"size": (2028, 1620)},
    controls={
        "ExposureTime": 20000,
        "AnalogueGain": 2.0,
        "ColourGains": (2.26, 2.48),
    },
)

picam.start()
for i in range(1, 40):
    picam.capture_metadata()

picam.switch_mode(cfgStill)

for i in range(1, 5):
    start = time.time()
    picam.capture_metadata()
    print(time.time() - start)
    # First request: 500 ms, following ones 50ms
davidplowman commented 4 months ago

Yes, unfortunately this is inherent in the camera stack, and is the same whether you're using Python or C++. Programming and starting a camera is a relatively slow process because there are lots of I2C register writes down in the kernel. It's quite dependent on the camera too, as some early frames may be invalid and get dropped by the system.

h-huss commented 4 months ago

Thanks, just as I feared / expected :)

sandyol55 commented 4 months ago

If you could use a 2028x1080 still format, rather than the full 2028x1520, there is an experimental (Pi5 only) HQ camera driver that supports 120 fps at that resolution - so there'd be no need to switch configurations for the still capture.

See https://forums.raspberrypi.com/viewtopic.php?t=371216#p2227660

(The faster 10-bit modes at 2028x1520 and 2028x1080 currently aren't working, but the 12-bit modes are, and the 12-bit 2028x1080 mode supports 122 fps).

P.S. Realise your code snippets are examples, but just to note that the FrameRate and ExposureTime are in conflict. A 120fps framerate allows only a little under 8333 microseconds for exposure. Not sure which takes precedence when both are manually set.(??)

h-huss commented 4 months ago

Thank you, that looks very promising!

I only need the central quarter of the image (however, the final still capture should have no binning), so these new modes are exciting. (ScalerCrop doesn't seem to help to increase fps)

A 120fps framerate allows only a little under 8333 microseconds for exposure. Not sure which takes precedence when both are manually set.(??)

I had noticed it too, but it worked fine for the tests, so I didn't bother changing it