Page 1 of 1

Measuring light levels with camera

Posted: Wed Jun 10, 2015 2:26 pm
by jsavage
I am looking for a simple way to measure light levels using a raspberry pi camera. I am not looking for a calibrated answer but either a decimal between 0 and 1 or an integer value. Having read the documentation for the picamera library I was hoping that camera.exposure_speed might provide a value that I could use as a proxy for light intensity but when I tested this I have found it to return the same value (33158) day and night .

The code I am using is as follows: (Loosely based on an example in the documentation)

Code: Select all

import time
import picamera
with picamera.PiCamera() as camera:
    camera.resolution = (1280, 720)
    camera.framerate = 30
    # Wait for the automatic gain control to settle
    time.sleep(2)
    # Now fix the values
    camera.shutter_speed = camera.exposure_speed
    lightValue = camera.exposure_speed

print lightValue
Can anyone suggest an alternative approach using this library?

James

Re: Measuring light levels with camera

Posted: Wed Jun 10, 2015 4:24 pm
by 6by9
You're running at 30fps, so the max exposure time possible is 33ms (1000/30fps). I'd suggest you slow things down to 5fps if you want longer exposure times, and also select the night exposure mode to allow the AGC algorithm to select longer times.
Scene brightness is also affected by analogue gain inside the sensor, and digital gain applied in the image processing pipe. You need to look at those numbers too.

Re: Measuring light levels with camera

Posted: Wed Jun 10, 2015 5:39 pm
by towolf
The binary EXIF field in the jpgs contains the Brightness Value. Isn’t that more of a measurement of scene brightness?

Re: Measuring light levels with camera

Posted: Sat Jun 13, 2015 10:20 pm
by jsavage
Thanks folks. Attempting to use analog_gain revealed that I had installed the library through pip and apt-get and the one in use was the one that was not getting updated. Now that's fixed I am investigating analog_gain. It looks promising but suspect I am going to need to combine it with another value to be able to sense the range of values in one function.

I am going to look into the exif data idea tomorrow as an alternative solution but would prefer to get the answer by accessing the library.

I am sure this must have been done before.

Kind regards
James

Re: Measuring light levels with camera

Posted: Sat Jun 13, 2015 11:01 pm
by mikronauts
Consider using a LDR and MCP3208 to measure the light level. For an example, see:

http://www.mikronauts.com/raspberry-pi/ ... eak-howto/
jsavage wrote:Thanks folks. Attempting to use analog_gain revealed that I had installed the library through pip and apt-get and the one in use was the one that was not getting updated. Now that's fixed I am investigating analog_gain. It looks promising but suspect I am going to need to combine it with another value to be able to sense the range of values in one function.

I am going to look into the exif data idea tomorrow as an alternative solution but would prefer to get the answer by accessing the library.

I am sure this must have been done before.

Kind regards
James

Re: Measuring light levels with camera

Posted: Sun Jun 14, 2015 11:03 am
by pageauc
This is a different approach but I use similar code in my pi-timolo project https://github.com/pageauc/pi-timolo

Sample light meter code

Code: Select all

import picamera
import picamera.array
import numpy as np
with picamera.PiCamera() as camera:
    camera.resolution = (100, 75)
    with picamera.array.PiRGBArray(camera) as stream:
        camera.exposure_mode = 'auto'
        camera.awb_mode = 'auto'
        camera.capture(stream, format='rgb')
        pixAverage = int(np.average(stream.array[...,1]))
print ("Light Meter pixAverage=%i" % pixAverage)
Claude ...

Re: Measuring light levels with camera

Posted: Sun Jun 14, 2015 4:11 pm
by jsavage
mikronauts wrote:Consider using a LDR and MCP3208 to measure the light level.
A agree that LDR is the simplest approach and have done this before just with an LDR, a resistor and a capacitor but the problem I am trying to solve is how to measure ambient light levels with the camera and nothing else. The reason I want to achieve this with software alone is that the camera and pi are already deployed as a security sensor and in use some distance away from me.
James