forked from Splitter/face-recognition-to-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognizer.py
79 lines (62 loc) · 2.79 KB
/
recognizer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
FACE RECOGNITION TO MQTT - main face recognition script
This uses the pi camera and compares detected faces from camera stream against stored user face data.
If a match is detected it fires off an MQTT message with the name of the person detected
Usage:
1. Can either call it directly(python recognizer.py) or import it and call recognizeToMQTT()
"""
import face_recognition
import picamera
import numpy
import os
import pickle
import paho.mqtt.client as mqtt
#--- MQTT SETTINGS ---#
mqttHost = 'localhost' #required
mqttPort = 1883 #required
mqttUser = None #optional
mqttPassword = None #optional
mqttClientId = "face_recognition_to_mqtt" #each client needs a unique ID
mqttTopicPrefix = "cmnd/faceToMqtt/user/" #mqtt message will be this plus detected users name appended to end
mqttPayload = "detected"
def recognizeToMQTT():
# Initialize Raspberry Pi camera.
camera = picamera.PiCamera()
camera.resolution = (320, 240)
newImg = numpy.empty((240, 320, 3), dtype=numpy.uint8)
#mqtt initialization
mqttClient = mqtt.Client(mqttClientId)
#add password/username if set
if mqttUser and not mqttPassword:
mqttClient.username_pw_set(mqttUser)
elif mqttUser and mqttPassword:
mqttClient.username_pw_set(mqttUser, mqttPassword)
#connect to mqtt server
mqttClient.connect(mqttHost, mqttPort)
#variables for captured frame data
faceLocations = []
faceEncodings = []
#retrieve stored vectors/encodings of user images
data = pickle.loads(open(os.getcwd()+os.path.sep+"encodings.pickle", "rb").read())
print("Beginning to watch for known faces...\n")
while True:
#capture a single frame/image
camera.capture(newImg, format="rgb")
#generate data for capture
faceLocations = face_recognition.face_locations(newImg)
faceEncodings = face_recognition.face_encodings(newImg, faceLocations)
#if a face is in the captured image loop over detected face data and compare to stored face data
for faceEncoding in faceEncodings:
#check for match
matchIdxs = face_recognition.compare_faces(data["encodings"], faceEncoding)
for index in matchIdxs:
if matchIdxs[index] == True: #match found so send mqtt message
name = str(data["names"][index])
print("User detected {}!".format(name))
mqttClient.publish( mqttTopicPrefix + name )
break
if __name__ == "__main__":
print("\n\n\nFACE RECOGNITION TO MQTT - main face recognition")
print("* captures frames from picamera and compares to see if a known user/person is captured")
print("* fires off an mqtt message if a known user/person is detected \n\n\n")
recognizeToMQTT()