Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions emotion_detection _classification/Emotion_detector_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# importing the module
import cv2
import random
import numpy as np

import mtcnn


from tensorflow.keras.models import model_from_json

detector = mtcnn.MTCNN()



json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()

loaded_model = model_from_json(loaded_model_json)

print(loaded_model.__class__)

loaded_model.load_weights("model.h5")
print("Loaded model from disk")
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

emotions = ['anger','fear','happy','neutral','sad']

name = 'examples/smile.jpeg'

img = cv2.imread(name)


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)



height = img.shape[0]
width = img.shape[1]


faces = detector.detect_faces(img)

if (len(faces)>0):

for f in faces:
x,y,w,h = f['box']
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)

roi_gray = gray[y:y+h, x:x+w]

roi_gray = cv2.resize(roi_gray, (224, 224))

face = cv2.cvtColor(roi_gray,cv2.COLOR_GRAY2RGB)

# converting image data into proper shape for tensorflow model

face = np.expand_dims(face, axis=0)

emote = loaded_model.predict(face)[0] ## check face image with model

emote = np.argmax(emote)


cv2.putText(img,emotions[emote],(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2,)

cv2.imshow('img',img)


name = name.split('.')[0]

cv2.imwrite('output-'+name+'.png',img)

cv2.waitKey(0)
cv2.destroyAllWindows()








Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# importing the module
import cv2
import random
import numpy as np

import mtcnn


from tensorflow.keras.models import model_from_json


cap = cv2.VideoCapture(0)

detector = mtcnn.MTCNN()



json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()

loaded_model = model_from_json(loaded_model_json)

print(loaded_model.__class__)

loaded_model.load_weights("model.h5")
print("Loaded model from disk")
loaded_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])


emotions = ['anger','fear','happy','neutral','sad']

while 1:

ret, img = cap.read()


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)



height = img.shape[0]
width = img.shape[1]


faces = detector.detect_faces(img)

if (len(faces)>0):

for f in faces:
x,y,w,h = f['box']
cv2.rectangle(img,(x,y),(x+w,y+h),(,0,0),2)

roi_gray = gray[y:y+h, x:x+w]

roi_gray = cv2.resize(roi_gray, (224, 224))

face = cv2.cvtColor(roi_gray,cv2.COLOR_GRAY2RGB)


# converting image data into proper shape for tensorflow model

face = np.expand_dims(face, axis=0)

emote = loaded_model.predict(face)[0] ## check face image with model

emote = np.argmax(emote)


cv2.putText(img,emotions[emote],(x,y-10),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),2,)

cv2.imshow('img',img)


k = cv2.waitKey(30) & 0xff
if k == ord('x'):
break

cv2.waitKey(0)
cv2.destroyAllWindows()








8 changes: 8 additions & 0 deletions emotion_detection _classification/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Face detection and Emotion classification using MTCNN and EfficientNet B0

Dataset used FER 2013 https://www.kaggle.com/msambare/fer2013

Folders for Surprise and Disgust were removed from both Test and Train folders before training

Document link: https://docs.google.com/document/d/1RxdJks12F8r_bM4kFxoAcud1aGchy5qwwGFv2JLhZXg/edit?usp=sharing

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added emotion_detection _classification/model.h5
Binary file not shown.
1 change: 1 addition & 0 deletions emotion_detection _classification/model.json

Large diffs are not rendered by default.

729 changes: 729 additions & 0 deletions emotion_detection _classification/training.ipynb

Large diffs are not rendered by default.