Skip to content

added face emotion recognition in vedio stream #970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions FaceEmotionRecognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Face Emotion Recognition using python

- Model trainig can be found here --> train.ipynb
- You can get the trained model here - [Trained Model](https://drive.google.com/file/d/1-6O9vR9qvU18h7WEToyqMHXO2U_zleri/view?usp=sharing)
- You can take the model and try how this model work through webcam using this code ---> predict-emotion-using-webcam.ipynb

**Dataset Used**:
- fer2013 (collected from kaggle)
245 changes: 245 additions & 0 deletions FaceEmotionRecognition/predict-emotion-using-webcam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting opencv-python\n",
" Downloading https://files.pythonhosted.org/packages/70/a8/e52a82936be6d5696fb06c78450707c26dc13df91bb6bf49583bb9abbaa0/opencv_python-4.5.1.48-cp37-cp37m-win_amd64.whl (34.9MB)\n",
"Requirement already satisfied: numpy>=1.14.5 in d:\\anaconda\\installed_files\\lib\\site-packages (from opencv-python) (1.16.5)\n",
"Installing collected packages: opencv-python\n",
"Successfully installed opencv-python-4.5.1.48\n"
]
}
],
"source": [
"! pip install opencv-python"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import cv2\n",
"#tensorflow packages\n",
"from tensorflow.keras.models import load_model\n",
"from tensorflow.keras.preprocessing import image"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Face Emotion Recognition\n",
"#Here i am using my trained model, that is trained and saved as a h5 file\n",
"faceDetection_model = 'D:\\pavi\\DeepLearningProjects\\Face_Emosion_Recognition\\pretrained_model\\Face_Detection_TrainedModel\\haarcascade_frontalface_default.xml'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Model: \"sequential_1\"\n",
"_________________________________________________________________\n",
"Layer (type) Output Shape Param # \n",
"=================================================================\n",
"conv2d_6 (Conv2D) (None, 48, 48, 64) 1664 \n",
"_________________________________________________________________\n",
"conv2d_7 (Conv2D) (None, 48, 48, 64) 102464 \n",
"_________________________________________________________________\n",
"batch_normalization_3 (Batch (None, 48, 48, 64) 256 \n",
"_________________________________________________________________\n",
"max_pooling2d_3 (MaxPooling2 (None, 24, 24, 64) 0 \n",
"_________________________________________________________________\n",
"conv2d_8 (Conv2D) (None, 24, 24, 128) 73856 \n",
"_________________________________________________________________\n",
"conv2d_9 (Conv2D) (None, 24, 24, 128) 147584 \n",
"_________________________________________________________________\n",
"batch_normalization_4 (Batch (None, 24, 24, 128) 512 \n",
"_________________________________________________________________\n",
"max_pooling2d_4 (MaxPooling2 (None, 12, 12, 128) 0 \n",
"_________________________________________________________________\n",
"conv2d_10 (Conv2D) (None, 12, 12, 256) 295168 \n",
"_________________________________________________________________\n",
"conv2d_11 (Conv2D) (None, 12, 12, 256) 590080 \n",
"_________________________________________________________________\n",
"batch_normalization_5 (Batch (None, 12, 12, 256) 1024 \n",
"_________________________________________________________________\n",
"max_pooling2d_5 (MaxPooling2 (None, 6, 6, 256) 0 \n",
"_________________________________________________________________\n",
"flatten_1 (Flatten) (None, 9216) 0 \n",
"_________________________________________________________________\n",
"dense_3 (Dense) (None, 1024) 9438208 \n",
"_________________________________________________________________\n",
"dropout_2 (Dropout) (None, 1024) 0 \n",
"_________________________________________________________________\n",
"dense_4 (Dense) (None, 1024) 1049600 \n",
"_________________________________________________________________\n",
"dropout_3 (Dropout) (None, 1024) 0 \n",
"_________________________________________________________________\n",
"dense_5 (Dense) (None, 7) 7175 \n",
"=================================================================\n",
"Total params: 11,707,591\n",
"Trainable params: 11,706,695\n",
"Non-trainable params: 896\n",
"_________________________________________________________________\n"
]
}
],
"source": [
"Emotion_Detction_model = 'D:\\pavi\\DeepLearningProjects\\Face_Emosion_Recognition\\pretrained_model\\Face_Emotion_model\\FER_vggnet.h5'\n",
"vggnet = load_model(Emotion_Detction_model)\n",
"vggnet.summary()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"prediction ['Sad']\n",
"Sad\n"
]
},
{
"ename": "TypeError",
"evalue": "only size-1 arrays can be converted to Python scalars",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-13-113054a609db>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[0mfontScale\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0.6\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[0mthickness\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mputText\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mframe\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mlabel\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mlabel_position\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mFONT_HERSHEY_SIMPLEX\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mfontScale\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m255\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mthickness\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLINE_AA\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mputText\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mframe\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;34m'No Face Detection'\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mlabel_position\u001b[0m \u001b[1;33m,\u001b[0m \u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mFONT_HERSHEY_SIMPLEX\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;36m0.6\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m255\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m \u001b[1;33m,\u001b[0m\u001b[0mcv2\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLINE_AA\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mTypeError\u001b[0m: only size-1 arrays can be converted to Python scalars"
]
}
],
"source": [
"#defining the emotion classes for classification\n",
"classes = np.array((\"Angry\", \"Disgust\", \"Fear\", \"Happy\", \"Sad\", \"Surprise\", \"Neutral\"))\n",
"\n",
"#video capturing and classifing\n",
"\n",
"faceCascade = cv2.CascadeClassifier(faceDetection_model)\n",
"video_capture = cv2.VideoCapture(0)\n",
"\n",
"while True:\n",
" ret,frame = video_capture.read()\n",
" \n",
" cv2.imshow('Original Video' , frame)\n",
" \n",
" gray = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)\n",
" \n",
" face = faceCascade.detectMultiScale(gray ,scaleFactor=1.1 , minNeighbors=5,)\n",
" \n",
" #draw rectangle around the face and cut the face only\n",
" for (x,y,w,h) in face:\n",
" \n",
" cv2.rectangle( frame , (x,y) , (x+w , y+h) , (0,255,255) , 2)\n",
" face_img = gray[ y:(y+h) , x:(x+w)]\n",
" x = cv2.resize(face_img, (48,48) , interpolation = cv2.INTER_AREA)\n",
" \n",
" if np.sum([x])!=0:\n",
" #preprocessing\n",
" x = x.astype('float')/255.0 \n",
" x = image.img_to_array(x)\n",
" x = np.expand_dims(x , axis = 0)\n",
" \n",
" \n",
" #face_img = face_img.reshape(48,48)\n",
" \n",
" # prediction\n",
" p = vggnet.predict(x)\n",
" a = np.argmax(p,axis=1)\n",
" print('prediction',classes[a])\n",
" label = str(classes[a][0])\n",
" print(label)\n",
" label_position = (x-10,y-10)\n",
" \n",
" fontScale = 0.6\n",
" thickness = 3\n",
" cv2.putText(frame , label , label_position , cv2.FONT_HERSHEY_SIMPLEX , fontScale , (0,255,0) , thickness , cv2.LINE_AA)\n",
" else:\n",
" cv2.putText(frame , 'No Face Detection' , label_position , cv2.FONT_HERSHEY_SIMPLEX , 0.6 , (0,255,0) , 3 ,cv2.LINE_AA)\n",
" \n",
" #cv2.imshow('croped image' , face_img)\n",
" #display the resulting frame \n",
" \n",
" cv2.imshow('Face Detected Video' , frame)\n",
" \n",
" #break the capturing\n",
" if cv2.waitKey(1) & 0xFF == ord('q'):\n",
" break\n",
" \n",
"video_capture.release()\n",
"cv2.destroyAllWindows()\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'sad'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading