Skip to main content

FER

Facial expression recognition.

image

PyPI version Build Status Downloads

Open In Colab

DOI

INSTALLATION

Currently FER only supports Python 3.6 onwards. It can be installed through pip:

$ pip install fer

This implementation requires OpenCV>=3.2 and Tensorflow>=1.7.0 installed in the system, with bindings for Python3.

They can be installed through pip (if pip version >= 9.0.1):

$ pip install tensorflow>=1.7 opencv-contrib-python==3.3.0.9

or compiled directly from sources (OpenCV3, Tensorflow).

Note that a tensorflow-gpu version can be used instead if a GPU device is available on the system, which will speedup the results. It can be installed with pip:

$ pip install tensorflow-gpu\>=1.7.0

To extract videos that includes sound, ffmpeg and moviepy packages must be installed with pip:

$ pip install ffmpeg moviepy 

USAGE

The following example illustrates the ease of use of this package:

from fer import FER
import cv2

img = cv2.imread("justin.jpg")
detector = FER()
detector.detect_emotions(img)

Sample output:

[{'box': [277, 90, 48, 63], 'emotions': {'angry': 0.02, 'disgust': 0.0, 'fear': 0.05, 'happy': 0.16, 'neutral': 0.09, 'sad': 0.27, 'surprise': 0.41}]

Pretty print it with import pprint; pprint.pprint(result).

Just want the top emotion? Try:

emotion, score = detector.top_emotion(img) # 'happy', 0.99

MTCNN Facial Recognition

Faces by default are detected using OpenCV's Haar Cascade classifier. To use the more accurate MTCNN network, add the parameter:

detector = FER(mtcnn=True)

Video

For recognizing facial expressions in video, the Video class splits video into frames. It can use a local Keras model (default) or Peltarion API for the backend:

from fer import Video
from fer import FER

video_filename = "tests/woman2.mp4"
video = Video(video_filename)

# Analyze video, displaying the output
detector = FER(mtcnn=True)
raw_data = video.analyze(detector, display=True)
df = video.to_pandas(raw_data)

The detector returns a list of JSON objects. Each JSON object contains two keys: 'box' and 'emotions':

  • The bounding box is formatted as [x, y, width, height] under the key 'box'.
  • The emotions are formatted into a JSON object with the keys 'anger', 'disgust', 'fear', 'happy', 'sad', surprise', and 'neutral'.

Other good examples of usage can be found in the files demo.py located in the root of this repository.

To run the examples, install click for command line with pip install click and enter python demo.py [image|video|webcam] --help.

TF-SERVING

Support running with online TF Serving docker image.

To use: Run docker-compose up and initialize FER with FER(..., tfserving=True).

MODEL

FER bundles a Keras model.

The model is a convolutional neural network with weights saved to HDF5 file in the data folder relative to the module's path. It can be overriden by injecting it into the FER() constructor during instantiation with the emotion_model parameter.

LICENSE

MIT License.

CREDIT

This code includes methods and package structure copied or derived from Iván de Paz Centeno's implementation of MTCNN and Octavio Arriaga's facial expression recognition repo.

REFERENCE

FER 2013 dataset curated by Pierre Luc Carrier and Aaron Courville, described in:

"Challenges in Representation Learning: A report on three machine learning contests," by Ian J. Goodfellow, Dumitru Erhan, Pierre Luc Carrier, Aaron Courville, Mehdi Mirza, Ben Hamner, Will Cukierski, Yichuan Tang, David Thaler, Dong-Hyun Lee, Yingbo Zhou, Chetan Ramaiah, Fangxiang Feng, Ruifan Li, Xiaojie Wang, Dimitris Athanasakis, John Shawe-Taylor, Maxim Milakov, John Park, Radu Ionescu, Marius Popescu, Cristian Grozea, James Bergstra, Jingjing Xie, Lukasz Romaszko, Bing Xu, Zhang Chuang, and Yoshua Bengio, arXiv:1307.0414.

Release files for fer 25.10.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fer 25.10.3
File Size Uploaded
fer-25.10.3.tar.gz 891.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fer 25.10.3
File Interpreter ABI Platform
fer-25.10.3-py3-none-any.whl Python 3 none any Details

Total release size: 1.8 MB

Release files / fer-25.10.3.tar.gz

Download URL fer-25.10.3.tar.gz
Size 891.9 kB
Tags Source
SHA-256 checksum
How to use checksums
37f263b38e830899fb5237cad74eb4b0558adeb67f44d881a40b8573d9716d7b
BLAKE2b-256 checksum
How to use checksums
1db885416b1499791b57c5059761f106c2e9ff7e968e5e7dfae21c20e33be50d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.18

Release files / fer-25.10.3-py3-none-any.whl

Download URL fer-25.10.3-py3-none-any.whl
Size 891.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
84e05d6a2bf800ac1c4566da16e1862fc9e893ea88fe91907d32d2f5a24a3671
BLAKE2b-256 checksum
How to use checksums
a8614772e396b4c286b3c21d49a564c1b9afaa2e00db89e746639fa30ba6bfa4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.18

Release history Release notifications | RSS feed

This release

25.10.3 This release

2 release files

22.5.0

2 release files

22.3.0

2 release files

22.2.0

2 release files

22.1.1

2 release files

22.1.0

2 release files

22.0.1

2 release files

21.0.6

1 release file

21.0.5

2 release files

21.0.4

2 release files

21.0.3

2 release files

20.1.3

1 release file

20.1.2

2 release files

20.1.1

2 release files

20.1.0

1 release file

20.0.0

1 release file

19.0.7

2 release files

19.0.6

2 release files

19.0.4

2 release files

19.0.3

2 release files

19.0.2

2 release files

19.0.1

2 release files

19.0.0

2 release files

0.1.3

1 release file

0.1.2

2 release files

0.1.0

2 release files

0.0.8

1 release file

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page