-
Notifications
You must be signed in to change notification settings - Fork 0
/
facedetection.py
34 lines (29 loc) · 1.16 KB
/
facedetection.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
import numpy as np
import tensorflow as tf
import cv2
from PIL import Image
import keras
model = keras.models.load_model('model.h5')
ex = {1:'angry',2:'disgust',3:'fear',4:'happy',5:'netural',6:'sad',7:'surprise'}
cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
while True:
ret,frame = cap.read()
frame = cv2.resize(frame,(600,600))
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
face = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in face:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0))
img = gray[x:x+w,y:y+h]
img = cv2.resize(img,(48,48))
img_data = tf.keras.preprocessing.image.img_to_array(img)
img_data = np.expand_dims(img_data,0)
predict = model.predict(img_data)
maxindex = int(np.argmax(predict))
print(ex[maxindex+1])
cv2.putText(frame, str(ex[maxindex+1]), (x+20, y-60), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA)
cv2.imshow('frame',frame)
if cv2.waitKey(1) == 0xFF == ord('q'):
break
cap.release()
cap.destroyAllWindows()