Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple example of using the Raspberry Pi Camera Module and python picamera for motion detection
#!/usr/bin/python
import picamera
import picamera.array
import time
threshold = 10 # How Much pixel changes
sensitivity = 100 # How many pixels change
def takeMotionImage(width, height):
with picamera.PiCamera() as camera:
time.sleep(1)
camera.resolution = (width, height)
with picamera.array.PiRGBArray(camera) as stream:
camera.exposure_mode = 'auto'
camera.awb_mode = 'auto'
camera.capture(stream, format='rgb')
return stream.array
def scanMotion(width, height):
motionFound = False
data1 = takeMotionImage(width, height)
while not motionFound:
data2 = takeMotionImage(width, height)
diffCount = 0L;
for w in range(0, width):
for h in range(0, height):
# get the diff of the pixel. Conversion to int
# is required to avoid unsigned short overflow.
diff = abs(int(data1[h][w][1]) - int(data2[h][w][1]))
if diff > threshold:
diffCount += 1
if diffCount > sensitivity:
break;
if diffCount > sensitivity:
motionFound = True
else:
data2 = data1
return motionFound
def motionDetection():
print "Scanning for Motion threshold=%i sensitivity=%i... % (threshold, sensitivity)
while True:
if scanMotion(224, 160):
print "Motion detected"
if __name__ == '__main__':
try:
motionDetection()
finally:
print "Exiting Program"
@theawsomeboss1102

This comment has been minimized.

Copy link

theawsomeboss1102 commented May 17, 2018

I sound like a total noob but where are the captured pictured stored and how do I run this script on my raspberry pi

@Esser420

This comment has been minimized.

Copy link

Esser420 commented Jun 28, 2018

@theawsomeboss1102 this script just detects motion and prints to the console if it does indeed detect something. It does not save video or pictura of motion happening.

I suggest learning python and read the PiCamera docs if you need to tinker with this script.

@bluejakester

This comment has been minimized.

Copy link

bluejakester commented Jul 20, 2018

Thank you for writing and sharing this! I've been trying unsuccessfully to modify "motion" and other programs to take a night vision image with "raspistill" and my parms. With your code the motion detect is there and working, making it simple to add my specific image capture requirements.

@itsmysurport

This comment has been minimized.

Copy link

itsmysurport commented Dec 9, 2018

Code Error Founded

You left out double quotation marks in 42 line. print "Scanning for Motion threshold=%i sensitivity=%i... % (threshold, sensitivity)

print "Scanning for Motion threshold=%i sensitivity=%i..." % (threshold, sensitivity)

@cooc1501

This comment has been minimized.

Copy link

cooc1501 commented Jan 5, 2019

getting a syntax error at line 25, any idea what's causing that?

@mrcolo

This comment has been minimized.

Copy link

mrcolo commented Feb 7, 2019

same syntax error, line 25..

@khalidbourhaba

This comment has been minimized.

Copy link

khalidbourhaba commented Apr 2, 2019

If you are having error on line 25, try to run the script with python 2.7..

@pabe40

This comment has been minimized.

Copy link

pabe40 commented Apr 8, 2019

 Line38       data2 = data1 . Shouldn't be data1 = data2 to get a new offset and not always comparing against the initial image in data1?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.