1
+ import cv2
2
+ import numpy as np
3
+ from trainModel import *
4
+
5
+ cascade_path = 'cascades/haarcascade_frontalface_default.xml'
6
+ face_cascade = cv2 .CascadeClassifier (cascade_path )
7
+
8
+ # Finds face and return the original image and cropped face
9
+ def detect_face (image ):
10
+ gray = cv2 .cvtColor (image , cv2 .COLOR_BGR2GRAY )
11
+ faces = face_cascade .detectMultiScale (gray , 1.3 , 5 )
12
+
13
+ if faces is ():
14
+ return image
15
+
16
+ for (x , y , w , h ) in faces :
17
+ cv2 .rectangle (image , (x ,y ), (x + w , y + h ), (0 ,255 ,0 ), 2 )
18
+ cropped_face = image [y :y + h , x :x + w ]
19
+ cropped_face = cv2 .resize (cropped_face , (200 ,200 ))
20
+
21
+ return cropped_face
22
+
23
+ capture = cv2 .VideoCapture (0 )
24
+
25
+ while True :
26
+ response , frame = capture .read ()
27
+ found_face = detect_face (frame )
28
+
29
+ try :
30
+ gray_face = cv2 .cvtColor (found_face , cv2 .COLOR_BGR2GRAY )
31
+
32
+ # predict using trained model
33
+ label , score = classifier .predict (gray_face )
34
+ if score < 500 :
35
+ confidence_score = int (100 * (1 - score / 400 ))
36
+ message = 'I am {} confident ' .format (str (confidence_score ))
37
+
38
+ cv2 .putText (frame , message , (50 ,50 ), cv2 .FONT_ITALIC , 1 , (200 ,0 ,200 ), 2 )
39
+
40
+ if score > 75 :
41
+ message = 'Sorry! You are not who you say'
42
+ cv2 .putText (frame , message , (150 ,50 ), cv2 .FONT_ITALIC , 1 , (200 ,200 ,0 ), 2 )
43
+ cv2 .imshow ('I rekognize You' , image )
44
+ else :
45
+ message = 'You you are finally here'
46
+ cv2 .putText (frame , message , (150 ,50 ), cv2 .FONT_ITALIC , 1 , (0 ,200 ,200 ), 2 )
47
+ cv2 .imshow ('I rekognize You' , image )
48
+
49
+ except :
50
+ message = 'No face found!'
51
+ cv2 .putText (frame , message , (150 ,150 ), cv2 .FONT_ITALIC , 1 , (0 ,200 ,200 ), 2 )
52
+ cv2 .imshow ('I rekognize You' , frame )
53
+ pass
54
+
55
+ if cv2 .waitKey (0 ):
56
+ break
57
+
58
+ capture .release ()
59
+ cv2 .destroyAllWindows ()
60
+
61
+
0 commit comments