Skip to main content

Facex: Lightweight, High-Performance Facial Expression Classifier

facex is a Python library for detecting faces and classifying emotions in images lightweight, efficient threading and object pooling for concurrent processing making it suitable for high-performance applications.

Features

  • Face Detection: Uses Haar cascades to detect faces in images.
  • Emotion Classification: Predicts emotions like anger, happiness, sadness, and more.
  • Thread-Safe Pooling: Manages multiple classifiers through a thread-safe object pool.
  • Dedicated Worker Allocation: Assign dedicated classifiers for specific tasks or users.

Installation

Install the library via pip:

pip install facex

Prerequisites

Make sure you have the following:

  • Python 3.7 or higher
  • Required dependencies (installed automatically with pip):
    • numpy
    • opencv-python
    • tensorflow

Additionally, ensure the assets directory contains the following files:

  • haarcascade_frontalface_default.xml
  • model_optimized.tflite

Usage

Example Code

import cv2
import facex

# Initialize the PoolManager
pool_manager = facex.PoolManager(pool_size=3)

# Load a test image
image = cv2.imread("test_image.jpg")

# Predict emotions
predictions = pool_manager.predict(image)

# Display results
for prediction in predictions:
    print(f"Face: {prediction['bbox']}, Emotion: {prediction['emot']}")

# Shutdown the pool manager when done
pool_manager.shutdown()

Key Classes and Methods

facex.EmotionClassifier

A class responsible for detecting faces and classifying emotions in images.

  • __init__(model_path, category=None): Initializes the emotion classifier with the provided TensorFlow Lite model and optional emotion categories.

    • Arguments:
      • model_path (str): Path to the pre-trained TensorFlow Lite model (model_optimized.tflite).
      • category (list, optional): List of emotion categories (default: ['anger', 'disgust', 'fear', 'happy', 'neutral', 'sadness', 'surprised']).
  • detect_faces(input): Detects faces in the input image using OpenCV's Haar Cascade Classifier.

    • Arguments:
      • input (np.ndarray): The input image (e.g., a frame from a video feed).
    • Returns:
      • faces (list of tuples): Coordinates of detected faces in the form (x, y, w, h).
      • procs_input (np.ndarray): Preprocessed grayscale version of the input image.
  • detect_emotion(input): Detects the emotion of a given face using the TensorFlow Lite model.

    • Arguments:
      • input (np.ndarray): The face image (grayscale or resized) to classify.
    • Returns:
      • dict: A dictionary containing the predicted emotion with associated confidence scores.
  • predict(input): Detects faces and predicts the emotions for each detected face in the image.

    • Arguments:
      • input (np.ndarray): The input image (e.g., a frame from a video feed).
    • Returns:
      • list: A list of dictionaries, each containing:
        • 'bbox': Bounding box of the detected face (x, y, w, h).
        • 'emot': Emotion prediction for the face (e.g., {'happy': 0.95, 'sadness': 0.05}).

facex.PoolManager

A class for managing a pool of EmotionClassifier instances, enabling thread-safe predictions.

  • __init__(pool_size=5): Initializes the pool with a given number of EmotionClassifier instances.

    • Arguments:
      • pool_size (int): Number of EmotionClassifier instances in the pool (default: 5).
  • predict(input_data): Retrieves an EmotionClassifier from the pool and uses it to make a prediction on the input data.

    • Arguments:
      • input_data (np.ndarray): The input image data (e.g., a frame from a video feed).
    • Returns:
      • list: A list of dictionaries, each containing:
        • 'bbox': Bounding box of the detected face (x, y, w, h).
        • 'emot': Emotion prediction for the face.
  • get_worker(user_id): Allocates a dedicated EmotionClassifier instance for a specific user.

    • Arguments:
      • user_id (str): The ID of the user requesting the worker.
    • Returns:
      • EmotionClassifier: The dedicated EmotionClassifier instance for the user.
    • Raises:
      • RuntimeError: If the user already has a dedicated worker or if no classifiers are available.
  • release_worker(user_id): Releases the dedicated worker assigned to a user and returns it to the pool.

    • Arguments:
      • user_id (str): The ID of the user releasing the worker.
    • Raises:
      • KeyError: If the user does not have a dedicated worker.
  • shutdown(): Shuts down the pool manager and frees all resources, including threads.

Development

Testing

Run unit tests with pytest:

pip install pytest
pytest

Contribution Guidelines

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m "Add your feature").
  4. Push to the branch (git push origin feature/your-feature).
  5. Submit a pull request.

License

facex is licensed under the MIT License. See the LICENSE file for details.

Support

If you encounter any issues, feel free to open an issue or reach out via email.

Metadata

Release files for facex 1.1.0

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

Source distribution (sdist)

Source distribution for facex 1.1.0
File Size Uploaded Provenance Actions
facex-1.1.0.tar.gz 2.1 MB Without provenance Details for facex-1.1.0.tar.gz

Built distribution (wheel)

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

Total release size: 4.2 MB

Release files / facex-1.1.0.tar.gz

Download URL facex-1.1.0.tar.gz
Size 2.1 MB
Tags Source
SHA-256 checksum
How to use checksums
4a2196556c6b7026ce56f7429f6a9108e539a3e9d0a8e9d922ed129f6d14c911
BLAKE2b-256 checksum
How to use checksums
b09c5b8527d7c641199baead7efbc3748fc86aad5e7bff677a05fdc9811aacd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Release files / facex-1.1.0-py3-none-any.whl

Download URL facex-1.1.0-py3-none-any.whl
Size 2.1 MB
Tags Python 3
SHA-256 checksum
How to use checksums
2e9bddb8fcf563020dac1f50879071b02560d60d8aae124164f20e04660a8c2e
BLAKE2b-256 checksum
How to use checksums
9014705b58c96f121df9715f80455c360e3223347a7a90303bbc1fc72bd78784
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.10.11

Security for facex 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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