Tough to get exposure correct

I’m using the QRCode sample, modified slightly, to read a large QR Code. The problem I’m having is how difficult it is to get the exposure right so that the code can be read. Ideally, I want to use a properly tuned auto exposure, because I’m sure that the lighting conditions will change in my lab over the course of a day. The sun hits the windows for part of the day. I tried turning off autoexposure and setting things manually, but it didn’t work the way I thought it should.

Here is my code. You can see in the images that the QR Code is overexposed and the histogram shows it. Using manual exposure settings is only good if the environment is tightly controlled. I need a better autoexposure.

import sensor, image, time

sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing((240, 240))      # look at center 240x240 pixels of the VGA resolution.
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)           # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)      
sensor.set_auto_exposure(True)
#sensor.set_brightness(0)              # range 0-31
#sensor.set_saturation(0)              # range -3 to 3
#sensor.set_gainceiling(2)             # 2,4,8,16,32,64,128
#sensor.set_contrast(2)                # range -3 to 3
clock = time.clock()

while(True):
    clock.tick()
    img = sensor.snapshot()
    for code in img.find_qrcodes():
        img.draw_rectangle(code.rect(), color = 127)
        print(code)


Find QR Code.jpg

Hi, I’m having the same problem.
The code I currently have for changing the exposure is as follows:

Variables:

ExpStep = 200
CurExp = 1000
MinBrt = 20
MaxBrt = 35
MinExp = 10
MaxExp = 60000

KP_High = 10
KP_Low = 100

And in the loop:

clock.tick()
sensor.set_auto_exposure(False, CurExp)

sensor.skip_frames(4)
img = sensor.snapshot()
curStats = img.get_statistics()
CurBrt = curStats.l_mean()

if(CurBrt > MaxBrt):
    ErrHigh = CurBrt - MaxBrt
    ExpStep = KP_High * ErrHigh
    CurExp = max(CurExp - ExpStep, MinExp)
elif(CurBrt < MinBrt):
    ErrLow = MinBrt - CurBrt
    ExpStep = KP_Low * ErrLow
    CurExp = min(CurExp + ExpStep, MaxExp)

It kind of works but it’s quite unreliable. Any suggestions?

Thanks,

Ana

Hi, I made the camera driver better recently in the latest firmware about to come out. What boards do you have? I can upload the latest firmware for them here.