-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeTest.py
145 lines (110 loc) · 4.11 KB
/
CodeTest.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
import os
import tensorflow as tf
import cv2
import numpy as np
import face_recognition
# username = input('Enter your name?\n')
username = "Minhaz"
model = tf.keras.models.load_model("CSE499A_Model.h5")
camera_id = 0
faceDetectionPath = "haarcascade_frontalface_alt2.xml"
camera_id = camera_id
font_scale = 1.5
font = cv2.FONT_HERSHEY_PLAIN
averageEmotion = []
classNames = ["Angry", "Disgust", "Fear",
"Happy", "Neutral", "Sad", "Surprised"]
pred = None
webcamName = None
webcamEmotion = None
path = "ImageData"
averageFace = []
checkIfFound = []
images = []
fileNames = []
myList = os.listdir(path)
x = 1
y = 1
w = 1
h = 1
for cl in myList:
curImg = cv2.imread(f'{path}/{cl}')
images.append(curImg)
fileNames.append(os.path.splitext(cl)[0])
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
encodeListKnown = findEncodings(images)
cap = cv2.VideoCapture(camera_id)
# Check if the webcam is open correctly
if not cap.isOpened():
raise IOError("Can't open Webcam")
while True:
face_roi = None
ret, frame = cap.read()
face_detect = cv2.CascadeClassifier(
cv2.data.haarcascades + faceDetectionPath)
gray_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_detect.detectMultiScale(gray_img, 1.1, 5)
for x, y, w, h in faces:
x = x
y = y
w = w
h = h
roi_gray_img = gray_img[y:y+h, x:x+w]
roi_color = frame[y:y+h, x:x+w]
facess = face_detect.detectMultiScale(roi_gray_img)
if len(facess) == 0:
print("Face not detected")
else:
for (ex, ey, ew, eh) in facess:
face_roi = roi_color[ey: ey+eh, ex:ex + ew]
if face_roi is not None:
final_img = cv2.resize(face_roi, (224, 224))
final_img = np.expand_dims(final_img, axis=0) # need 4th dimension
final_img = final_img/255 # normalizing
prediction = model.predict(final_img)
pred = np.argmax(prediction[0])
averageEmotion.append(classNames[pred])
imgS = cv2.resize(frame, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
matchIndex = np.argmin(faceDis)
if faceDis[matchIndex] > 0.50:
checkIfFound.append(0)
webcamName = "unknown"
if matches[matchIndex]:
name = fileNames[matchIndex]
averageFace.append(name)
checkIfFound.append(1)
webcamName = name
webcamEmotion = classNames[pred]
cv2.rectangle(frame, (x, y), (x+w, y+h), (255,255,0),5)
cv2.rectangle(frame, (0,0), (150,50),(255,255,0),cv2.FILLED)
cv2.putText(frame, webcamName, (10,30),
cv2.FONT_HERSHEY_SIMPLEX, 0.8,(0,0,0), 2)
cv2.rectangle(frame, (0,60), (150,110),(255,0,0),cv2.FILLED)
cv2.putText(frame, webcamEmotion, (10,90),
cv2.FONT_HERSHEY_SIMPLEX, 0.8,(255,255,255), 2)
cv2.imshow("Face emotion recognition", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
if max(averageFace,key=averageFace.count,default=0).lower()==username.lower() and max(checkIfFound,key=checkIfFound.count,default=0)==1:
print("Verification successful")
print("Welcome " + name)
print("Emotion Detected:")
print(max(averageEmotion, key=averageEmotion.count, default=0))
elif max(averageFace,key=averageFace.count,default=0).lower()!=username.lower() and max(checkIfFound,key=checkIfFound.count,default=0)==1:
print("Name not found")
else:
print("Coud not verify!")
break
cap.release()
cv2.destroyAllWindows()