Metadata-Version: 2.5
Name: erml
Version: 1.0.1
Summary: Facial emotion recognition SDK — drop-in library for emotion detection from images
Project-URL: Homepage, https://github.com/sid-lakhani/erml
Project-URL: Repository, https://github.com/sid-lakhani/erml
Author: ERML Contributors
License: MIT License
        
        Copyright (c) 2026 ERML Contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: computer-vision,emotion,face,machine-learning,recognition
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.8.1
Requires-Dist: numpy>=1.23
Requires-Dist: onnxruntime>=1.16
Requires-Dist: opencv-python-headless>=4.6
Requires-Dist: pillow>=9.0
Requires-Dist: pydantic>=2.0
Provides-Extra: dev
Requires-Dist: black>=23.0; extra == 'dev'
Requires-Dist: flake8>=6.0; extra == 'dev'
Requires-Dist: onnxruntime>=1.16; extra == 'dev'
Requires-Dist: pydantic>=2.0; extra == 'dev'
Requires-Dist: pytest>=7.0; extra == 'dev'
Provides-Extra: export
Requires-Dist: tensorflow>=2.10; extra == 'export'
Requires-Dist: tf2onnx>=1.16; extra == 'export'
Provides-Extra: train
Requires-Dist: scipy>=1.10; extra == 'train'
Requires-Dist: tensorflow>=2.10; extra == 'train'
Description-Content-Type: text/markdown

# ERML — Emotion Recognition ML

A pip-installable Python SDK for facial emotion recognition. Drop it into your own project, pass it a frame, get back structured emotion data — no camera or display logic included.

```python
from erml import EmotionDetector, FacePrediction, format_results

detector = EmotionDetector()
results = detector.analyze("photo.jpg")

# Object access — full IDE autocomplete
print(results[0].emotion, results[0].confidence)

# Or export to plain dict
print(format_results(results))
```

```
Face 1  [x=85 y=67 w=259 h=259]
  Emotion    : Neutral
  Confidence : 42.9%
  All scores :
    neutral    42.9%  ████████
    sad        18.2%  ███
    fear       15.4%  ███
    angry      10.2%  ██
    happy       7.5%  █
    surprise    5.2%  █
    disgust     0.1%
```

## Install

```bash
pip install erml
```

Or from source:

```bash
git clone https://github.com/sid-lakhani/erml
cd erml
uv venv --python 3.12
source .venv/bin/activate.fish   # or: source .venv/bin/activate (bash/zsh)
uv pip install -r requirements.txt -r requirements-dev.txt
uv pip install -e .
```

> Requires Python 3.8–3.12. TensorFlow does not yet support Python 3.13+.
> To train, additionally install: `uv pip install -r requirements-train.txt`

## Usage

### Basic

```python
from erml import EmotionDetector

detector = EmotionDetector()
results = detector.analyze("photo.jpg")
```

### Input types

```python
import cv2
import numpy as np
from PIL import Image

# File path
results = detector.analyze("photo.jpg")

# OpenCV BGR array
frame = cv2.imread("photo.jpg")
results = detector.analyze(frame)

# PIL Image
pil_img = Image.open("photo.jpg")
results = detector.analyze(pil_img)
```

### Output

`analyze()` returns a list of dicts — one per detected face:

```python
[
  FacePrediction(
    emotion="happy",        # top predicted emotion
    confidence=0.87,        # float 0–1
    all={                   # scores for all 7 emotions
      "angry": 0.01,
      "disgust": 0.00,
      "fear": 0.02,
      "happy": 0.87,
      "sad": 0.03,
      "surprise": 0.05,
      "neutral": 0.02
    },
    bbox=BoundingBox(x=120, y=80, w=64, h=64)
  )
]
```

Access fields directly with full IDE autocomplete:
```python
result = results[0]
print(result.emotion)        # "happy"
print(result.bbox.x)         # 120
```

Or export to a plain dictionary (e.g. for JSON logging):
```python
raw = results[0].model_dump()
raw_list = [r.model_dump() for r in results]
```

Returns `[]` if no face is detected. Never raises on empty input.

### Human-readable output

```python
from erml import EmotionDetector, format_results

detector = EmotionDetector()
print(format_results(detector.analyze("photo.jpg")))
```

## Emotions

`angry` · `disgust` · `fear` · `happy` · `sad` · `surprise` · `neutral`

Trained on [FER-2013](https://www.kaggle.com/datasets/msambare/fer2013).

## Training

To retrain from scratch, download FER-2013 and place it at `dataset/` organized by emotion subfolder, then:

```bash
python training/train.py
```

Trains for up to 20 epochs with early stopping, saves best weights to `erml/assets/erml_v1.h5`.

## Quick inference script

```bash
python examples/test_inference.py photo.jpg
```

## Webcam demo

```bash
python examples/webcam_demo.py
python examples/webcam_demo.py --camera 1  # alternate camera index
```

Press **Q** to quit. Draws bounding boxes and emotion labels in real time.

## Tests

```bash
pytest tests/ -v
```

All tests run fully headless — no camera, no display, no trained model required.

## Project structure

```
erml/
├── erml/
│   ├── __init__.py       # public API: EmotionDetector, FacePrediction, format_results
│   ├── constants.py      # EMOTION_LABELS and shared constants
│   ├── detector.py       # EmotionDetector class (ONNX & YuNet runtime)
│   ├── download.py       # atomic auto-downloader for ONNX weights
│   ├── preprocess.py     # face ROI preprocessing
│   ├── schemas.py        # Pydantic output models (FacePrediction, BoundingBox)
│   ├── utils.py          # format_results helper
│   └── assets/           # ONNX weights cached here automatically
├── training/
│   ├── model.py          # TF/Keras CNN architecture definition
│   └── train.py          # training script (requires: pip install erml[train])
├── tests/
├── scripts/
│   └── export_onnx.py    # utility to export trained Keras models to ONNX
├── examples/
└── pyproject.toml
```

## Versioning

| Version | Status | Notes |
|---------|--------|-------|
| v0.1.0 | current | FER-2013, basic CNN, ~52% val accuracy |
| v0.2.0 | planned | Improved architecture, confidence calibration |
| v1.0.0 | planned | Stable public API, full docs |

## License

MIT
