I’m creating a basic Python3 class to use the PiCamera as a light/motion/color sensor.
Thoughts and suggestions?
Sensor Architecture:
- Python3 Class
- Use only R4R installed packages (no cv2)
- Mutex Protected Data Getters and Setters
- PiCamera executes in separate thread (methods are asynchronous where possible)
Methods
Completed:
- light() # return average intensity across entire sensor (0.0 pitch black to 100.0 blinding light)
- right() # return average intensity across right half of sensor
- left() # return average intensity across left half of sensor
- color() # returns estimate of color of central area of sensor
- last_motion() # returns time and direction of last motion {left,right,up,down}
- save_image_to_file()
Still Planned:
- light.max() # return [max_intensity, percent_width, percent_height], value and relative location in image
Maybe horizontal angle would be more directly useful?
Example Usage to talk when someone turns a room light on or off
#!/usr/bin/env python3
import easypicamerasensor
from time import sleep
import espeakng
epcs = easypicamerasensor.EasyPiCameraSensor() # creates 320x240 10FPS sensor
tts= espeakng.Speaker() # set up default text-to-speech object
current_intensity = epcs.light() # get average image light intensity now
think_before_talking_again = 15 # wait 15 seconds before talking again
just_said_something = 0
while True:
try:
last_intensity = current_intensity
current_intensity = epcs.light()
if just_said_something == 0:
if (current_intensity - last_intensity) > 20:
# someone turned a light on
tts.say("Thanks, it was kind of dark over here")
just_said_something = think_before_talking_again
elif (last_intensity - current_intensity > 20:
# someone turned a light off
tts.say("Did you know I'm afraid of the dark?")
just_said_something = think_before_talking_again
sleep(1) # wait 1 second between checks
if just_said_something > 0: just_said_something -= 1
except KeyboardInterrupt:
tts.say("Sure. Exiting stage right.")
sleep(2)
exit(0)