Detect Face Emotions With 10 Lines Of Code
Face Emotion Recognition (FER), A Libray To Detect Emotions
Facial Emotions Show the inside of a human heart. They help us to identify whether the person is angry, sad, happy, or normal. Medical Researcher also uses facial emotions to detect and understand the mental health of a person.
Artificial Intelligence Can play a big role in identifying a person's emotions. with the help of a convolutional neural network, it is possible to identify a person's emotions based on his image or a real-time video.
Facial Expression Recognition is a python library that can be used to detect a person’s emotion with less effort and fewer lines of code. It is developed with a deep neural network using Tensorflow and Keras libraries implemented in python. Dataset used in it is from Kaggle competition Challenges in Representation Learning: Facial Expression Recognition Challenge.
Installation
We Can Use PIP to install the library in our local systems. Just run the below command and you will see your library is installing.
pip install perThe Installation Will Take Some Time. The Library is around 500 MB in size with all its supported packages.
Dependencies: 1. OpenCV 3.2+ 2. Tensorflow 1.7+ 3. Python 3.6+
Predicting Emotions On an Image
from fer import FER
import matplotlib.pyplot as plt
img = plt.imread("img.jpg")
detector = FER(mtcnn=True)
print(detector.detect_emotions(img))
plt.imshow(img)Save It With emotion.py and simply run it using python emotion.py.
Output — [OrderedDict([(‘box’, (160, 36, 99, 89)), (’emotions’, {‘angry’: 0.0, ‘disgust’: 0.0, ‘fear’: 0.0, ‘happy’: 1.0, ‘sad’: 0.0, ‘surprise’: 0.0, ‘neutral’: 0.0})])]

Web App Code For Realtime Predictions
from fer import FER
import matplotlib.pyplot as plt
import streamlit as st
from PIL import Image, ImageOpsst.write('''
# Emotion Detector
''')st.write("A Image Classification Web App That Detects the Emotions Based On An Image")file = st.file_uploader("Please Upload an image of Person With Face", type=['jpg','png'])if file is None:
st.text("Please upload an image file")
else:
image = Image.open(file)
detector = FER(mtcnn=True)
result = detector.detect_emotions(image)
st.write(result)
st.image(image, use_column_width=True)Save The Python File With Emotion_web.py.
Run it using
streamlit run FILENAME.py
Copy The URL and Paste it into your browser to see the web app in action.
To Learn More About Streamlit and How To Create awesome web apps using it check out Streamlit 101: An in-depth introduction an article by Shail Deliwala
