Visual information: looking at surroundings

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 =
	motionDetectedNow =
	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][1] - buffer2[x,y][1])
				pixelSum += buffer1[x,y][1]
				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 =

		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 and sensitivity. Basically 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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: