import cv2
import os
import time
from wxpy import *
"""
树莓派打造智能看门狗
sudo pip3 install opencv-python
sudo pip3 install wechat_sender
"""
bot = Bot()
my_friend = bot.friends().search('监控狗')[0]
def camera(window_name, path_name):
cap = cv2.VideoCapture(0)
classfier = cv2.CascadeClassifier(os.getcwd()+"/haarcascade/haarcascade_frontalface_alt.xml")
color = (0, 255, 0)
num = 0
while cap.isOpened():
ok, frame = cap.read()
if not ok:
break
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faceRects = classfier.detectMultiScale(grey, scaleFactor=1.2, minNeighbors=3, minSize=(32, 32))
if len(faceRects) > 0:
for faceRect in faceRects:
x, y, w, h = faceRect
num = num+1
img_name = "%s/%d.jpg" % (path_name, num)
image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
cv2.imwrite(img_name, image, [int(cv2.IMWRITE_PNG_COMPRESSION), 9])
print("有人来了~~~")
alarm(num)
time.sleep(60)
cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, 'num:%d/1000' % (num), (x + 30, y + 30), font, 1, (255, 0, 255), 4)
c = cv2.waitKey(10)
if c & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
def alarm(num):
print("发送微信通知")
my_friend.send('有人闯进卧室了!')
my_friend.send_image(os.getcwd()+"/dog/"+str(num)+".jpg")
if __name__ == '__main__':
camera("watchdog", os.getcwd()+"/dog")