# AI Face Recognition Server

A Flask-based standalone server for face recognition, designed for integration with Odoo Point of Sale (POS).

## Features
- **Real-time Recognition**: Processes images and returns matches with metadata (ID, Name, Probability).
- **Face Training**: API endpoint to register new faces from base64 images.
- **Production Ready**: Supports Gunicorn for stability and concurrency.
- **Optimized for POS**: Default 60% match threshold handles varied lighting conditions well.

## Prerequisites
- Python 3.10+
- System libraries for `dlib` (cmake, g++, etc.)
- Virtual environment (recommended)

## Installation

1. **Activate the Virtual Environment**:
   ```bash
   source .venv/bin/activate
   ```

2. **Install Dependencies**:
   ```bash
   pip install -r requirements.txt
   ```

## Running the Server

### Production (Recommended)
Run using Gunicorn for better performance and stability:
```bash
.venv/bin/gunicorn -w 1 -b 0.0.0.0:5000 app:app --timeout 120
```

### Development
```bash
python app.py
```

## API Endpoints

### 1. Recognize Face
- **Endpoint**: `POST /recognize`
- **Body**: `{ "image": "base64_string_without_header" }`
- **Response**: List of matches with `id`, `name`, and `probability`.

### 2. Train Face
- **Endpoint**: `POST /train`
- **Body**: `{ "partner_id": 123, "name": "John Doe", "images": ["base64_1", "base64_2"] }`
- **Description**: Saves images to the `faces/` directory and updates the in-memory model.

## Matching Logic
- **Tolerance**: `0.4` (Distance)
- **Probability**: `1.0 - distance` (Matches >= 60% are returned)
- **Diagnostics**: Server logs the exact distance and probability to the terminal for every request.

## Directory Structure
- `app.py`: Main Flask application.
- `faces/`: Directory where trained face images are stored.
- `requirements.txt`: Python dependencies.
