Advertisement
Guest User

Untitled

a guest
Jan 21st, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. import io
  2. import picamera
  3. import cv2
  4. import numpy
  5. import RPi.GPIO as GPIO
  6. import time
  7. import json
  8. import os
  9. from pushbullet.pushbullet import PushBullet
  10. from time import sleep
  11. from gpiozero import MotionSensor
  12.  
  13. #api keys to be able to send notification
  14. apiKey = "o.6ifPy8pW6FaIu97LMGMJhzovspI8JYSQ"
  15.  
  16. p = PushBullet(apiKey)
  17.  
  18. devices = p.getDevices()
  19.  
  20.  
  21.  
  22. GPIO.setmode(GPIO.BCM)
  23. GPIO.setwarnings(False)
  24. GPIO.setup(22, GPIO.IN, GPIO.PUD_UP)
  25.  
  26. #Create a memory stream so photos doesn't need to be saved in a file
  27. stream = io.BytesIO()
  28. #Declaration for Pir sensor
  29. pir = MotionSensor(22)
  30. #for infinite loop of the program
  31. var = 1
  32.  
  33. while var == 1:
  34. with picamera.PiCamera() as camera:
  35. #Motion sensor
  36. input_state = GPIO.input(22)
  37. if input_state == True:
  38.  
  39. time.sleep(2)
  40. print ('Motion Detected')
  41. pir.wait_for_motion()
  42. #start to capture images
  43. camera.start_preview()
  44. camera.resolution = (320, 240)
  45. sleep(1)
  46. camera.capture(stream, format='jpeg')
  47.  
  48. #Convert the picture into a numpy array
  49. buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
  50.  
  51. #Now creates an OpenCV image
  52. image = cv2.imdecode(buff, 1)
  53.  
  54. #Load a cascade file for detecting faces
  55. face_cascade = cv2.CascadeClassifier('/usr/share/opencv/haarcascades/haarcascade_frontalface_alt.xml')
  56.  
  57. #Convert to grayscale
  58. gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
  59.  
  60. #Look for faces in the image using the loaded cascade file
  61. faces = face_cascade.detectMultiScale(gray, 1.1, 5)
  62. print "Found "+str(len(faces))+" face(s)"
  63.  
  64. #Draw a rectangle around every found face
  65. for (x,y,w,h) in faces:
  66. cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
  67.  
  68. #Save the result image
  69. cv2.imwrite('result.jpg',image)
  70.  
  71.  
  72. #send notification if face detected
  73. a = str(len(faces))
  74. if a in ['1','2','3','4','5']:
  75.  
  76.  
  77. p.pushNote(devices[0]["iden"], 'Intruders alert', 'Someone intrudes! Please wait for the captured image!')
  78. 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