-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecognition.py
72 lines (56 loc) Β· 2.27 KB
/
recognition.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
import numpy as np
import imutils
import pickle
import time
import cv2
embeddingModel="Attendence-System-Face-Recognition/openface.nn4.small2.v1.t7"
embeddingFile='Attendence-System-Face-Recognition/output/embeddings.pickle'
recognizerFile='Attendence-System-Face-Recognition/output/recognizer.pickle'
labelEncFile='Attendence-System-Face-Recognition/output/le.pickle'
conf=0.3
print("Loading face detector")
prototxt="Attendence-System-Face-Recognition/model/deploy.prototxt.txt"
model="Attendence-System-Face-Recognition/model/res10_300x300_ssd_iter_140000.caffemodel"
detector=cv2.dnn.readNetFromCaffe(prototxt,model)
print("Loading Face Recognizer")
embedder=cv2.dnn.readNetFromTorch(embeddingModel)
recognizer=pickle.loads(open(recognizerFile,"rb").read())
le=pickle.loads(open(labelEncFile,"rb").read())
box=[]
print("Starting video stream...")
cam=cv2.VideoCapture(0)
time.sleep(2.0)
while True:
_,frame=cam.read()
frame=imutils.resize(frame,width=600)
(h,w)=frame.shape[:2]
imageBlob=cv2.dnn.blobFromImage(cv2.resize(frame,(300,300)),1.0,(300,300),(104.0,177.0,123.0),swapRB=False)
detector.setInput(imageBlob)
detections=detector.forward()
for i in range(0,detections.shape[2]):
confidence=detections[0,0,i,2]
if confidence>conf:
box=detections[0,0,i,3:7]*np.array([w,h,w,h])
(startX,startY,endX,endY)=box.astype("int")
face=frame[startY:endY, startX:endX]
(fH,fW)=face.shape[:2]
if fW < 20 or fH < 20:
continue
faceBlob = cv2.dnn.blobFromImage(face, 1.0 / 255, (96, 96), (0, 0, 0), swapRB=True, crop=False)
embedder.setInput(faceBlob)
vec = embedder.forward()
preds=recognizer.predict_proba(vec)[0]
j=np.argmax(preds)
proba=preds[j]
name=le.classes_[j]
text="{}:{:.2f}%".format(name,proba*100)
y=startY-10 if startY-10>10 else startY+10
cv2.rectangle(frame,(startX,startY),(endX,endY),(0,0,255),2)
cv2.putText(frame,text,(startX,y),
cv2.FONT_HERSHEY_SIMPLEX,0.45,(0,255,0),2)
cv2.imshow("Frame",frame)
key=cv2.waitKey(1) & 0xFF
if key==27:
break
cam.release()
cv2.destroyAllWindows()