mylamour / blog

Your internal mediocrity is the moment when you lost the faith of being excellent. Just do it.
https://fz.cool
61 stars 14 forks source link

树莓派红外视频监控及推送 #42

Open mylamour opened 5 years ago

mylamour commented 5 years ago

树莓派型号: zero w, 接了个红外摄像头,用人脸识别,然后推送到了微信。最开始打算的是darknettiny yolo去识别视频流。但是darknet在zero w只能segmention fault。无语。最后选择纯python套路 。还挺好玩的。

采用的库主要为picamera, face_recognition, wxpy

逻辑较为简单,实现起来也不难,问题主要在raspberry zero w 上编译dlib, 安装软件等。所以有个更好的方法是直接下载whl文件,face_recognition的官方安装方式太慢了。只编译dlib都用了一天,而且还要下载model,所以建议下载好model,然后手动安装。 wget https://www.piwheels.org/simple/face-recognition-models/face_recognition_models-0.3.0-py2.py3-none-any.whl

#coding:utf-8
from wxpy import *
from PIL import Image
import face_recognition
import picamera
import numpy as np

bot = Bot(cache_path=True)

bot.self.send("准备启动树莓派机器人")

KNOWNFACE="i.png"

camera = picamera.PiCamera()
camera.resolution = (320, 240)
output = np.empty((240, 320, 3), dtype=np.uint8)

print("Loading known face image(s)")
known_image = face_recognition.load_image_file(KNOWNFACE)
known_face_encoding = face_recognition.face_encodings(known_image)[0]

# Initialize some variables
face_locations = []
face_encodings = []

while True:
    print("Capturing image.")
    camera.capture(output, format="rgb")

    face_locations = face_recognition.face_locations(output)
    print("Found {} faces in image.".format(len(face_locations)))
    face_encodings = face_recognition.face_encodings(output, face_locations)

    for face_encoding in face_encodings:
        match = face_recognition.compare_faces([known_face_encoding], face_encoding)
        name = "<Unknown Person>"

        if match[0]:
            name = KNOWNFACE.split('/')[-1].split('.')[0]
            bot.self.send('{}正在进行访问'.format(name))
            break
        print("I see someone named {}!".format(name))
        im = Image.fromarray(output)
        im.save('stranger.png')
        bot.self.send('发现陌生人,将为您发送照片')
        bot.self.send_image('stranger.png')

让我们来看下面的视频, 一共117秒。夜里搞的测试。红外的效果还不错

output

其他

wxpy还有其他很多好玩的用法,可以其官方文档。

Resources