Facial expressions show inner human emotions. They help us identify whether a person is angry, sad, happy, or normal. Medical researchers also use facial emotions to detect and understand a person's mental health.
Artificial intelligence can play a big role in identifying a person’s emotions. With the help of convolutional neural networks, we can identify a person's emotions based on his images or live videos.
Facial Expression Recognition is a Python library that can be used to detect a person's emotions with less effort and fewer lines of code. It was developed with deep neural networks using Tensorflow and Keras libraries implemented in Python. The dataset used is from the Kaggle competition challenge in Representation Learning: Facial Expression Recognition Challenge.
We can use pip to install the library in the local system. Just run the command below and see your library being installed.
pip install per
Dependencies:
from fer import FERimport matplotlib.pyplot as plt img = plt.imread("img.jpg")detector = FER(mtcnn=True)print(detector.detect_emotions(img))plt.imshow(img)
Save using 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 application code for real-time prediction
from fer import FERimport matplotlib.pyplot as pltimport streamlit as stfrom 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)
Use Emotion _ web.py Save the Python file.
streamlit run FILENAME.py
Copy the URL and paste it into your browser to see the web application running .
The above is the detailed content of How to easily detect facial emotions with 10 lines of code?. For more information, please follow other related articles on the PHP Chinese website!