jsavage
Posts: 22
Joined: Wed Aug 21, 2013 8:11 am

Measuring light levels with camera

Wed Jun 10, 2015 2:26 pm

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

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7301
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Measuring light levels with camera

Wed Jun 10, 2015 4:24 pm

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.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

towolf
Posts: 421
Joined: Fri Jan 18, 2013 2:11 pm

Re: Measuring light levels with camera

Wed Jun 10, 2015 5:39 pm

The binary EXIF field in the jpgs contains the Brightness Value. Isn’t that more of a measurement of scene brightness?

jsavage
Posts: 22
Joined: Wed Aug 21, 2013 8:11 am

Re: Measuring light levels with camera

Sat Jun 13, 2015 10:20 pm

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

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Measuring light levels with camera

Sat Jun 13, 2015 11:01 pm

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
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

pageauc
Posts: 224
Joined: Fri Jan 04, 2013 10:52 pm

Re: Measuring light levels with camera

Sun Jun 14, 2015 11:03 am

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 ...
GitHub - https://github.com/pageauc
YouTube - https://www.youtube.com/user/pageaucp

jsavage
Posts: 22
Joined: Wed Aug 21, 2013 8:11 am

Re: Measuring light levels with camera

Sun Jun 14, 2015 4:11 pm

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

Return to “Camera board”