-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemotion.py
38 lines (24 loc) · 982 Bytes
/
emotion.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
import cv2
# You sould install the deep face library
# pip install deepface
# this code was tested in Python 3.8
from deepface import DeepFace
# You can download the file 'haarcascade_frontalface_default.xml'
# from cv2 Git hub
face_cascade = cv2.CascadeClassifier(r'C:\Users\akash\Desktop\Emotion_Detection\haarcascade_frontalface_default - Copy.xml')
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
result = DeepFace.analyze(img_path = frame , actions=['emotion'], enforce_detection=False )
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.1,4)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),3)
emotion = result["dominant_emotion"]
txt = str(emotion)
cv2.putText(frame,txt,(50,50),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
cv2.destroyAllWindows()