Home > Technology peripherals > AI > body text

How to easily detect facial emotions with 10 lines of code?

PHPz
Release: 2024-01-08 16:42:04
forward
1186 people have browsed it

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.

How to easily detect facial emotions with 10 lines of code?

Installation

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
Copy after login

Dependencies:

  • OpenCV 3.2
  • Tensorflow 1.7
  • Python 3.6

Predicting emotions on images

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)
Copy after login

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})])]
Copy after login

How to easily detect facial emotions with 10 lines of code?

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)
Copy after login

Use Emotion _ web.py Save the Python file.

Run

streamlit run FILENAME.py
Copy after login

How to easily detect facial emotions with 10 lines of code?

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!