-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
93 lines (74 loc) · 3.13 KB
/
camera.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
import cv2
import pickle
from imutils.video import WebcamVideoStream
import face_recognition
import MySQLdb
db = MySQLdb.connect("localhost","root","","mtoto" )
cursor = db.cursor()
class VideoCamera(object):
def __init__(self):
self.stream = WebcamVideoStream(src=0).start()
with open("model/trained_knn_model.clf", 'rb') as f:
self.knn_clf = pickle.load(f)
def __del__(self):
self.stream.stop()
def predict(self, frame, knn_clf, distance_threshold=0.4):
# Find face locations
X_face_locations = face_recognition.face_locations(frame)
if len(X_face_locations) == 0:
return []
# Find encodings for faces in the test iamge
faces_encodings = face_recognition.face_encodings(frame, known_face_locations=X_face_locations)
# Use the KNN model to find the best matches for the test face
closest_distances = knn_clf.kneighbors(faces_encodings, n_neighbors=1)
are_matches = [closest_distances[0][i][0] <= distance_threshold for i in range(len(X_face_locations))]
for i in range(len(X_face_locations)):
print("closest_distances")
print(closest_distances[0][i][0])
# Predict classes and remove classifications that aren't within the threshold
return [(pred, loc) if rec else ("12", loc) for pred, loc, rec in
zip(knn_clf.predict(faces_encodings), X_face_locations, are_matches)]
def get_frame(self):
image = self.stream.read()
# f = open("trainStatus.txt")
# for i in f:
# isTrained = int(i)
# if isTrained: # change updated model file
# # load again
# with open("model/trained_knn_model.clf", 'rb') as f:
# self.knn_clf = pickle.load(f)
# file = open("trainStatus.txt", "w")
# file.write("0")
# file.close()
predictions = self.predict(image, self.knn_clf)
name = ''
for name, (top, right, bottom, left) in predictions:
startX = int(left)
startY = int(top)
endX = int(right)
endY = int(bottom)
# try:
cursor.execute("SELECT * FROM enfantb WHERE idEnfant = "+name)
data = cursor.fetchall()
nom = ''
for row in data:
idE = row[0]
nom = row[1]
age = row[2]
sexe = row[3]
pere=row[4]
adresse=row[5]
telP=row[6]
#print("id : ", idE, "Nom : ",nom)
#except:
# print('Unable to fectch data')
#db.close()
#print("Voici le nom "+nom)
cv2.rectangle(image, (startX, startY), (endX, endY), (255, 181, 51), 1)
cv2.putText(image, (nom+" "+pere+" "+adresse+" "+telP), (endX - 70, endY - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (255, 255, 255), 1)
#print(name)
ret, jpeg = cv2.imencode('.jpg', image)
data = []
data.append(jpeg.tobytes())
data.append(name)
return data