Spaces:
Sleeping
Sleeping
File size: 3,192 Bytes
954551b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | # FaceVision AI π§
A real-time facial expression recognition web app powered by **MediaPipe Face Landmarker** running fully in-browser (WASM), with a **FastAPI** backend for logging emotion data to a **Neon PostgreSQL** database.
---
## Project Structure
```
FACE RECOGNIZATION/
βββ frontend/ # Next.js App Router + Tailwind CSS
β βββ app/
β β βββ hooks/
β β β βββ useFaceTracking.ts # MediaPipe integration & emotion classification
β β β βββ useEmotionLogger.ts # 10s window logging to backend
β β βββ components/
β β β βββ EmotionDisplay.tsx
β β β βββ StatsPanel.tsx
β β βββ FaceVisionApp.tsx # Main client component
β β βββ page.tsx
β β βββ globals.css
β βββ .env.local # NEXT_PUBLIC_BACKEND_URL
β
βββ backend/ # FastAPI + SQLAlchemy
βββ app/
β βββ main.py # FastAPI app, CORS, endpoints
β βββ database.py # SQLAlchemy engine + session
β βββ models.py # EmotionLog ORM model
β βββ schemas.py # Pydantic schemas
βββ requirements.txt
βββ run.py # Start with: python run.py
βββ .env.example # Copy to .env and fill DATABASE_URL
```
---
## Getting Started
### 1. Backend Setup
```bash
cd backend
# Create virtual environment
python -m venv venv
venv\Scripts\activate # Windows
# source venv/bin/activate # macOS/Linux
# Install dependencies
pip install -r requirements.txt
# Configure database
copy .env.example .env
# Edit .env β set DATABASE_URL to your Neon PostgreSQL connection string
# Start the server
python run.py
# β http://localhost:8000
# β Swagger UI: http://localhost:8000/docs
```
### 2. Frontend Setup
```bash
cd frontend
# Install dependencies (already done if you just cloned)
npm install
# Start development server
npm run dev
# β http://localhost:3000
```
---
## Environment Variables
### Frontend (`frontend/.env.local`)
```
NEXT_PUBLIC_BACKEND_URL=http://localhost:8000
```
### Backend (`backend/.env`)
```
DATABASE_URL=postgresql://user:password@your-neon-host.neon.tech/facerecog?sslmode=require
```
---
## How It Works
1. **Camera** β `react-webcam` accesses your device camera at up to 60 FPS.
2. **AI Model** β MediaPipe Face Landmarker WASM model is downloaded from CDN and runs entirely in the browser.
3. **Classification** β 8 key blendshapes are scored to classify: **Happy, Sad, Angry, Surprised, Neutral**.
4. **Smoothing** β A 5-frame moving average prevents rapid flickering.
5. **Logging** β Every 10 seconds, the mode emotion is POSTed to the backend without blocking the video loop.
6. **Database** β FastAPI saves logs to Neon PostgreSQL via SQLAlchemy.
---
## API Endpoints
| Method | Path | Description |
|--------|------|-------------|
| GET | `/` | Health check |
| POST | `/api/logs` | Log emotion entry |
| GET | `/api/logs` | List all logs |
| DELETE | `/api/logs` | Clear all logs |
Interactive docs at: `http://localhost:8000/docs`
|