There are several aspects to looking at the surroundings: motion detection, light detection, face recognition. Motion will activate a lot of the processes (like face recognition) and the light detection is useful for being smart about turning on and off lights.
Here is the basic code:
def lookAtSurroundings(threadName): print "Started listening on thread %s" % threadName motionDetectedLast = datetime.datetime.now() motionDetectedNow = datetime.datetime.now() sensitivity = 60 threshold = 80 # Get first image image1, buffer1 = captureTestImage() # Reset last capture time lastCapture = time.time() while (1): # Get comparison image image2, buffer2 = captureTestImage() pixelSum = 0 numCountedPixels = 0 motionHasBeenDetected = False # Count changed pixels changedPixels = 0 for x in xrange(0, 100): # Scan one line of image then check sensitivity for movement for y in xrange(0, 75): # Just check green channel as it's the highest quality channel pixdiff = abs(buffer1[x,y] - buffer2[x,y]) pixelSum += buffer1[x,y] numCountedPixels += 1 if pixdiff > threshold: changedPixels += 1 # Save image and Exit before full image scan complete if changedPixels > sensitivity: lastCapture = time.time() print "Motion detected!" motionDetectedLast = datetime.datetime.now() faceRecognition() break continue image1 = image2 buffer1 = buffer2
It is pretty self explantory. A lot of this was actually done by
brownmose and others at the RPi forum. What is important is the setting of the
threshold sets how much a single pixel has to change to be a “changed pixel” and the
sensitivity sets how many “changed pixels” you need to set it off. Also of note – the
pixelSum is basically how much “light” there is which can be used to detect which lights are on/off or daytime/nighttime. These variables are things you will have to fiddle with to get correct.
I’ll talk about the facial recognition in another post and also the implementation of the “seeing” thread with the “listening” thread…because of the CPU power of the little RPi this has to be done carefully.