2

I have posed this question on the Picamera Github issues forum but it has not been answered in nearly a month. Hoping for better luck here.

I am running a Pi Camera v2.1 in an industrial measurement environment and control over exposure speed is critical. Pi 2 B is running Raspbian stretch and using Python 3.5.3 with the picamera 1.13 package.

The problem is that after setting camera.shutter_speed to a specific exposure time value (in us), each image retrieved from the camera is darker and darker, and camera.exposure_speed drops by about 75-76 us each frame, until the minimum exposure speed of 9 us is reached. If camera.shutter_speed is written before every frame is retrieved, then camera.exposure_speed drops only once, by 76 us. See comments in code below.

Setting breakpoints in the Picamera module code shows that the exposure_speed value read from its MMAL structure is in fact changing.

I am using the following code to allocate the camera and lock its exposure system.

    from picamera.array import PiArrayOutput
    from picamera import PiCamera
    from time import sleep

    camera = PiCamera(sensor_mode=2)
    camera.resolution = camera.MAX_RESOLUTION
    camera.framerate = 5
    camera.awb_mode = 'off'
    camera.iso = 60      # Set ISO to force unity gains, then lock...
    camera.exposure_mode = 'off'      # ... locks gains
    camera.shutter_speed = 1000
    sleep(2)             # Ensure gains are locked and report correctly

and this code to retrieve images (using the full Bayer array data).

    bayer_capture = PiArrayOutput(camera, (2480, 4128))
    stream = camera.capture_continuous(bayer_capture, format='jpeg', bayer=True, quality=1, thumbnail=None)

    for f in stream:
        # If shutter speed is forced after every image, the exposure_speed drop
        # occurs only for the first frame
        #camera.shutter_speed = 1000
        print("exp: {}".format(camera.exposure_speed))

The output of this simple program is as follows.

exp: 936
exp: 860
exp: 784
exp: 709
exp: 633
exp: 558
exp: 482
exp: 406
exp: 331
exp: 255
exp: 180
exp: 104
exp: 28
exp: 9
exp: 9

Note that the camera.exposure_speed is reducing by 75 - 76 us for each image.

Other background:

With a Pi Camera v1.3 this code works fine: the image does not dim, camera.exposure_speed holds constant and equals camera.shutter_speed. With Pi Camera v2.1 before about February 2018, it also worked fine. Around early Feb 2018 I did a Raspbian update, but looking through changelogs neither picamera nor raspberrypi-firmware was updated around then. I do not use rpi-update.

Is there something obvious I am missing? Is anyone else having this problem?

UPDATE 4/12/18:

Substantially simpler code example that exhibits the same problem is below. The problem seems to be triggered by a write to either camera.shutter_speed or camera.exposure_mode. When both of those assignments are commented out in the code below, the camera maintains a controlled exposure time as long as the light entering the sensor is held constant (since the camera is not in locked exposure mode). When either or both assignments are uncommented, camera.exposure_speed drops on each frame.

from picamera import PiCamera
from time import sleep
import io

with PiCamera(sensor_mode=2) as camera:
    camera.exposure_mode = 'off'    # Lock gains and disable auto exposure
    camera.shutter_speed = 1000
    sleep(1)                        # Let shutter speed take effect

    stream = camera.capture_continuous(io.BytesIO(), format='jpeg')

    for f in stream:
        print("exp: {}".format(camera.exposure_speed))
  • What version of picamera did you use with the ver 1.3 Pi Camera? – Seamus Apr 12 '18 at 3:53
  • @Seamus, all work has been done with picamera 1.13, on the same physical RPi board and just switching the camera module. Two v2.1 camera modules have the same issue (I originally thought it was a hardware problem in the camera module). Have not tried using a different RPi board yet. – mklein9 Apr 12 '18 at 16:56
  • 1
    Feels like facing a haystack to find a needle. I'll offer this: If you'll send me some code, I'll run it on my RPi 3B+, and report the results back to you. At least that'll answer the RPi board question. – Seamus Apr 12 '18 at 17:49
  • @Seamus, thank you... the code in my question update of 4/12/18 is a fully self-contained Python program that shows the issue. Expected result is output showing a constant exposure value slightly below 1000. – mklein9 Apr 12 '18 at 20:03
  • I scrounged up an RPi B+ (not 2 B+ as is the other board) that was running Raspbian Jessie, have v2.1 camera modules on each, and tried swapping flash cards. The card with Raspbian Jessie, updated with all updates as of today, works on both RPis; Stretch doesn't. Now a longer effort to determine if an update was corrupted along the way or if PiCamera 1.13 is incompatible with Stretch's current state. – mklein9 Apr 13 '18 at 20:42
3

Adding to the answer of @modilion, the magic numbers I found are

9 or 2363*n-9 

where

n = { 1, 2, 3, ... }

I found it by measurements. Starting from shutter_speed=100000 all the way down to somewhere around 60000 and I found the pattern.

2

(This is all wrong. See below)

I have been attempting the same feat. I'm using PiCamera 1.13, Raspberry Pi 3 B+, everything updated and running Stretch. I've been able to get reliable locked camera exposure and gain with the following code:

from picamera import PiCamera
from time import sleep
import io

with PiCamera(sensor_mode=2) as camera:
    camera.shutter_speed = 1000     # Assign shutter speed to non-zero first
    camera.exposure_mode = 'off'    # Lock gains and disable auto exposure
    sleep(1)                        # Let shutter speed take effect

    stream = camera.capture_continuous(io.BytesIO(), format='jpeg')
    f.next() #dump the first frame.
    for f in stream:
        print("exp: {}".format(camera.exposure_speed))
        print("shut: {}".format(camera.shutter_speed))

The trouble I see is that the shutter speed and the exposure speed do not match, but at least everything remains stable.

[CORRECTIONS]

I've been doing more work to figure this out. It seems that setting shutter_speed to specific values will allow for a stable exposure_speed. But the specific stable speeds are not obvious.

Stable shutter_speed list: [9,2354,4717,7080,9443...]

Any shutter_speed setting below 2354 will slowly settle to 9. Any setting below 4717 will slowly settle to 2354. I'm going to call this answer a complete fail.

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.