This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRecognizer.py
150 lines (117 loc) · 5.43 KB
/
Recognizer.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import cv2, os, sys, pickle
import numpy as np
from PIL import Image
from configs import *
testImage = sys.argv[1]
faceCascade = cv2.CascadeClassifier(cascadePath)
profileCascade = cv2.CascadeClassifier(cascadePath2)
recognizer = cv2.createLBPHFaceRecognizer()
#can also use createEigenFaceRecognizer() or createFisherFaceRecognizer() or createLBPHFaceRecognizer()
#Read http://docs.opencv.org/2.4/modules/contrib/doc/facerec/facerec_tutorial.html to understand ML behind
def predictFacesFromPhoto(image):
#TODO: Similarly, recognize left and right faces
img_gray = Image.open(image).convert('L')
img = np.array(img_gray, 'uint8')
face = faceCascade.detectMultiScale(img, scaleFactor, minNeighbors, cascadeFlags, minSize)
for (x, y, w, h) in face:
name_predicted, confidence = recognizer.predict(cv2.resize(img[y: y + h, x: x + w], face_resolution))
print("It is predicted as "+str(name_predicted)+" with confidence"+str(confidence))
#cv2.imshow("Scanned Face", img[y: y+h, x: x+w])
#cv2.waitKey(1000)
####################
def predictFacesFromWebcam(label2name_map):
#http://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#videocapture-get
video_capture = cv2.VideoCapture(cameraSource)
frame_width = video_capture.get(3)
frame_height = video_capture.get(4)
while True:
#Get a frame & Convert to grayscale
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
##Phase 1: FACE DETECTION
#to detect frontal face, left&right side faces
faces = faceCascade.detectMultiScale(gray, scaleFactor, minNeighbors, cascadeFlags, minSize)
sidefaces_left = profileCascade.detectMultiScale(gray, scaleFactor, minNeighbors, cascadeFlags, minSize)
sidefaces_right = profileCascade.detectMultiScale(np.fliplr(gray), scaleFactor, minNeighbors, cascadeFlags, minSize)
##Phase 2: FACE RECOGNITION
#Predict all detected faces to the list
#The below commented lines checks if faces overlap to avoid recognizing & drawing rectangle around same face twice
#I commented them since they seem to be unnecessary processing overhead, and works cool without them..
for (x, y, w, h) in faces:
#overlaps = False
#for(xi, yi, wi, hi) in sidefaces_left:
# if(x>=xi+wi or xi>=x+w or y>=yi+hi or yi>=y+h): #http://www.geeksforgeeks.org/find-two-rectangles-overlap/
# overlaps = False
# else:
# overlaps = True
# break
#if overlaps==True:
# print("Overlapping left and frontal face")
# continue
#overlaps = False
#for(Xi, yi, wi, hi) in sidefaces_right:
# xi = frame_width-(Xi+wi)
# if(x>=xi+wi or xi>=x+w or y>=yi+hi or yi>=y+h): #http://www.geeksforgeeks.org/find-two-rectangles-overlap/
# overlaps = False
# else:
# overlaps = True
# break
#if overlaps==True:
# print("Overlapping right and frontal face")
# continue
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
name_predicted, confidence = recognizer.predict(cv2.resize(gray[y: y + h, x: x + w], face_resolution))
if(name_predicted!=0 and confidence<confidence_threshold):
print("It is predicted as "+label2name_map[name_predicted]+" with confidence "+str(confidence))
cv2.putText(frame, label2name_map[name_predicted]+":"+str(confidence), (x+w,y+h), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))
for (x, y, w, h) in sidefaces_left:
#overlaps = False
#for(xi, yi, wi, hi) in faces:
# if(x>=xi+wi or xi>=x+w or y>=yi+hi or yi>=y+h): #http://www.geeksforgeeks.org/find-two-rectangles-overlap/
# overlaps = False
# else:
# overlaps = True
# break
#if overlaps==True:
# print("Conflicting front face with left face")
# continue
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
name_predicted, confidence = recognizer.predict(cv2.resize(gray[y: y + h, x: x + w], face_resolution))
if(name_predicted!=0 and confidence<confidence_threshold):
print("LSF is predicted as "+label2name_map[name_predicted]+" with confidence "+str(confidence))
cv2.putText(frame, label2name_map[name_predicted]+":"+str(confidence), (x+w,y+h), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))
for (X, y, w, h) in sidefaces_right:
x = int(frame_width-(X+w))
#overlaps = False
#for(xi, yi, wi, hi) in faces:
# if(x>=xi+wi or xi>=x+w or y>=yi+hi or yi>=y+h): #http://www.geeksforgeeks.org/find-two-rectangles-overlap/
# overlaps = False
# else:
# overlaps = True
# break
#if overlaps==True:
# print("Conflicting front face with right face")
# continue
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
name_predicted, confidence = recognizer.predict(cv2.resize(gray[y: y + h, x: x+w], face_resolution))
if(name_predicted!=0 and confidence<confidence_threshold):
print("RSF is predicted as "+label2name_map[name_predicted]+" with confidence "+str(confidence))
cv2.putText(frame, label2name_map[name_predicted]+":"+str(confidence), (x+w,y+h), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,0,255))
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
print("\nQuitting")
break
video_capture.release()
cv2.destroyAllWindows()
####_MAIN_####
if os.path.isfile(outfile): recognizer.load(outfile)
else:
print "Train your images first"
exit()
with open(label_name_map_file, 'rb') as handle:
label_name_map = pickle.load(handle)
print("Press 'q' to quit\n\n\n")
if(testImage == "webcam"):
predictFacesFromWebcam(label_name_map)
else:
predictFacesFromPhoto(testImage)