forked from EbenKouao/MMM-Face-Recognition-SMAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecSer.py
119 lines (104 loc) · 4 KB
/
recSer.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# server
import socket
import pickle
import sys
import face_recognition
import picamera
import numpy as np
import os
import time
from PIL import Image
import json
fileUrl = './modules/HPS-FaceID/'
def encodeKnown():
# encode known faces
known_people = [] # names
temp_image = [] # image object
known_face_encodings = [] # encoded objects
for file in os.listdir("./public/faces"):
try:
known_people.append(file.replace(".png",""))
file = os.path.join("./public/faces/", file)
temp_image = face_recognition.load_image_file(file)
known_face_encodings.append(face_recognition.face_encodings(temp_image)[0].tolist())
except Exception as e:
pass
encodedJson = {name: encodedImg for name, encodedImg in zip(known_people, known_face_encodings)}
with open('./faces.json', 'w') as f:
json.dump(encodedJson, f, indent=2)
def rec(face_encodings):
# load known faces
known_people = [] # names
known_face_encodings = [] # encoded objects
with open(fileUrl + 'faces.json', 'r') as f:
data = json.load(f)
for iterator in data:
known_people.append(iterator)
known_face_encodings.append(data[iterator])
face_id = "Guest"
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
print(matches)
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
face_id = known_people[best_match_index]
break
print("Person Detected: {}!".format(face_id))
with open(fileUrl + 'faceID.json', 'r') as f:
data = json.load(f)
data['user'] = face_id
with open(fileUrl + 'faceID.json', 'w') as f:
json.dump(data, f, indent=2)
return face_id
def saveFace(face_encodings, output):
with open(fileUrl + 'faceID.json', 'r') as f:
data = json.load(f)
newUser = 'User' + str(data['count'])
data['count'] += 1
data['user'] = newUser
with open(fileUrl + 'faceID.json', 'w') as f:
json.dump(data, f, indent=2)
img = Image.fromarray(output, mode="RGB")
img.save(fileUrl + 'public/faces/'+newUser+'.png')
with open(fileUrl + 'faces.json', 'r') as f:
data = json.load(f)
data[newUser] = face_encodings[0].tolist()
with open(fileUrl + 'faces.json', 'w') as f:
json.dump(data, f, indent=2)
return newUser
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == 'encode':
encodeKnown()
exit()
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
print('listening...')
while True:
conn, addr = s.accept()
print ('Connected by', addr)
while True:
chunk = conn.recv(32768)
img = face_recognition.load_image_file(fileUrl + 'tmp.png')
face_locations = face_recognition.face_locations(img)
if len(face_locations) > 0:
print('{} faces detected'.format(len(face_locations)))
imgEncodings = face_recognition.face_encodings(img)
command = pickle.loads(chunk)
print(command)
if command == 'add':
newUser = saveFace(imgEncodings, img)
conn.sendall(bytes(newUser, encoding='utf-8'))
elif command == 'rec':
knownUser = rec(imgEncodings)
conn.sendall(bytes(knownUser, encoding='utf-8'))
break
else:
print('no faces detected')
conn.sendall(bytes('retake', encoding='utf-8'))
conn.close()