Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import io
- import picamera
- import cv2
- import numpy
- import RPi.GPIO as GPIO
- import time
- import json
- import os
- from pushbullet.pushbullet import PushBullet
- from time import sleep
- from gpiozero import MotionSensor
- #api keys to be able to send notification
- apiKey = "o.6ifPy8pW6FaIu97LMGMJhzovspI8JYSQ"
- p = PushBullet(apiKey)
- devices = p.getDevices()
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- GPIO.setup(22, GPIO.IN, GPIO.PUD_UP)
- #Create a memory stream so photos doesn't need to be saved in a file
- stream = io.BytesIO()
- #Declaration for Pir sensor
- pir = MotionSensor(22)
- #for infinite loop of the program
- var = 1
- while var == 1:
- with picamera.PiCamera() as camera:
- #Motion sensor
- input_state = GPIO.input(22)
- if input_state == True:
- time.sleep(2)
- print ('Motion Detected')
- pir.wait_for_motion()
- #start to capture images
- camera.start_preview()
- camera.resolution = (320, 240)
- sleep(1)
- camera.capture(stream, format='jpeg')
- #Convert the picture into a numpy array
- buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
- #Now creates an OpenCV image
- image = cv2.imdecode(buff, 1)
- #Load a cascade file for detecting faces
- face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
- #Convert to grayscale
- gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
- #Look for faces in the image using the loaded cascade file
- faces = face_cascade.detectMultiScale(gray, 1.1, 5)
- print "Found "+str(len(faces))+" face(s)"
- #Draw a rectangle around every found face
- for (x,y,w,h) in faces:
- cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
- #Save the result image
- cv2.imwrite('result.jpg',image)
- #send notification if face detected
- a = str(len(faces))
- if a in ['1','2','3','4','5']:
- p.pushNote(devices[0]["iden"], 'Intruders alert', 'Someone intrudes! Please wait for the captured image!')
- p.pushFile(devices[0]["iden"], "result.jpg", "Intruder's Image", open("/home/pi/Programs/result.jpg", "rb"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement