-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_classification.py
More file actions
65 lines (34 loc) · 1.43 KB
/
expression_classification.py
File metadata and controls
65 lines (34 loc) · 1.43 KB
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
import cv2
import numpy as np
from tensorflow.keras.models import model_from_json
#### Loading tensorflow model
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model.h5")
print("Loaded model from disk")
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
#####
cap = cv2.VideoCapture(0)
### Loading HAAR Cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
smile = 0
while True:
_,img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) ## finding faces in image
for (x,y,w,h) in faces: ## We are assuming only one face is present in the image
cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_gray = cv2.resize(roi_gray, (100, 100))
face = roi_gray[:,:,np.newaxis] ## converting image data into proper shape for tensorflow model
face = np.expand_dims(face, axis=0)
smile = loaded_model.predict(face)[0,0] ## check face image with model
if smile >= 0.8:
cv2.putText(img,'Smiling :)',(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2,)
cv2.imshow('game',img)
if cv2.waitKey(1) & 0xFF == ord('x'):
break
cap.release()
cv2.destroyAllWindows()