Forked from FutureSharks/raspberrypi-picamera-motion-detection.py
Created
July 22, 2018 17:28
-
-
Save d3rrick/339f556e33a20da83c9a4ad0b4cc7185 to your computer and use it in GitHub Desktop.
A simple example of using the Raspberry Pi Camera Module and python picamera for motion detection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment