-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface1.py
80 lines (63 loc) · 2.63 KB
/
face1.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
#Firstly run face_train.py then run this
import pickle
import cv2
import smtplib
import numpy as np
face_cascade=cv2.CascadeClassifier("cascades/data/haarcascade_frontalface_default.xml")
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainner.yml")
labels={"person_name":1}
with open("labels.pickle","rb") as f:
og_labels=pickle.load(f)
labels={v:k for k,v in og_labels.items()}
cap=cv2.VideoCapture(0)
minW = 0.1*cap.get(3)
minH = 0.1*cap.get(4)
count=0
Num=0
flag=0
while(True):
ret,frame =cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces=face_cascade.detectMultiScale(gray,scaleFactor=1.5,minNeighbors=5,minSize=(int(minW),int(minH)))
for (x,y,w,h) in faces:
print(x,y,w,h)
Num=Num+1
roi_gray=gray[y:y+h, x:x+w]
roi_color=frame[y:y+h, x:x+w]
cv2.rectangle(frame,(x,y),(x+h,y+h),(255,0,0),2)
id_, conf = recognizer.predict(roi_gray)
front=cv2.FONT_HERSHEY_SIMPLEX
color=(255,255,255)
stroke=2
print(conf)
# Check if confidence is less them 100 ==> "0" is perfect match
if conf>=45 and conf<=80: #confidence(conf) depends on the dataset you taken
print(id_) #Note:-the value of confidence usually depends on your data, but the lower the better.
#you should adjust the threshold
print(labels[id_])
name=labels[id_]
confidence = " {0}%".format(round(100 - conf))
cv2.putText(frame,name,(x,y),front,1,color,stroke,cv2.LINE_AA)
cv2.putText(frame, str(confidence), (x+5,y+h-5), front, 1,color, 1)
else:
cv2.putText(frame,"unknown",(x,y),front,1,color,stroke,cv2.LINE_AA)
count=count+1
if count>20:
print("unknown face is recognised many times")
flag=1
import mail #please run any of these mail or sms by commenting other one
#if you run mail and sms at a time then it will going to hang... because smtp will take time to send mail same as sms
#import sms
break
cv2.imshow("frame",frame)
if cv2.waitKey(20)&0xFF==ord("q"):
break
elif Num>50:
break
elif flag==1:
img_item="my_img.png"
cv2.imwrite(img_item,roi_gray)
break
cap.release()
cv2.destroyAllWindows()