EmotiVid API
A modular FastAPI backend for video behavior and emotion analysis.
Overview
EmotiVid API is a powerful backend service that analyzes videos to detect emotions, facial expressions, body language, eye contact, and transcribe speech. It provides comprehensive analysis of the content using computer vision, machine learning, and natural language processing techniques.
Features
- Video Management: Upload, list, and retrieve video metadata
- Emotion Analysis: Detect emotions in video frames using deep learning models
- Eye Contact Analysis: Measure eye contact consistency and engagement
- Body Language Analysis: Analyze posture, gestures, and non-verbal cues
- Speech-to-Text: Transcribe speech in videos using Whisper
- AI Analysis: Process results using OpenAI and Groq for deeper insights
- Background Processing: Process videos asynchronously with status updates
- Annotated Video Generation: Generate videos with behavior annotations
- User Authentication: Secure API with JWT authentication
- API Key Authentication: Alternative authentication method for direct integrations
Project Structure
behavior_backend/
βββ app/ # Application code
β βββ api/ # API endpoints
β β βββ routes/ # Route definitions
β βββ core/ # Core application code
β β βββ config.py # Configuration management
β β βββ exceptions.py # Custom exceptions
β βββ db/ # Database related code
β β βββ base.py # Database setup
β β βββ models.py # SQLAlchemy models
β β βββ repositories/ # Database access layer
β βββ models/ # Pydantic models for API
β βββ services/ # Business logic
β β βββ video_service.py # Video management service
β β βββ processing/ # Processing services
β β βββ video_processor.py # Main video processing pipeline
β β βββ emotion_analyzer.py # Facial emotion analysis
β β βββ eye_contact_analyzer.py # Eye contact detection
β β βββ body_language_analyzer.py # Body language analysis
β β βββ speech_service.py # Speech transcription
β β βββ ai_analysis.py # AI-powered insights
β βββ utils/ # Utility functions
βββ static/ # Static files
β βββ uploads/ # Upload directory
β βββ results/ # Results directory
βββ annotated_videos/ # Processed videos with annotations
βββ temp_face_frames/ # Temporary storage for processed frames
βββ logs/ # Application logs
βββ tests/ # Test directory
βββ .env # Environment variables
βββ main.py # Application entry point
βββ requirements.txt # Production dependencies
βββ requirements-dev.txt # Development dependencies
βββ run.sh # Production server script
βββ start_server.sh # Development server script
Prerequisites
- Python 3.9+
- FFmpeg (for video processing)
- GPU support (optional, for faster processing)
Installation
Option 1: Using Python Virtual Environment
Create and activate a virtual environment:
# Windows python -m venv venv venv\Scripts\activate # Linux/Mac python -m venv venv source venv/bin/activateInstall dependencies:
# For production pip install -r requirements.txt # For development pip install -r requirements-dev.txtCreate a
.envfile with the following variables:DATABASE_URL=sqlite:///app.db OPENAI_API_KEY=your_openai_api_key GROQ_API_KEY=your_groq_api_key SECRET_KEY=your_secret_key
Running the Application
Development Server
For development with hot-reload and debug features:
# Windows
venv\Scripts\activate
uvicorn main:app --reload
# Linux/Mac
source venv/bin/activate
./start_server.sh
This will start the development server with hot-reload enabled and make the API available at http://localhost:8000.
Production Server
For production deployment:
# Windows
venv\Scripts\activate
uvicorn main:app --workers 4 --host 0.0.0.0 --port 8000
# Linux/Mac
source venv/bin/activate
./run.sh
API Documentation
API documentation is available at:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Key Endpoints
POST /api/v1/videos/upload: Upload a videoGET /api/v1/videos: List all videosGET /api/v1/videos/{video_id}: Get video metadataPOST /api/v1/processing/analyze/{video_id}: Process a videoGET /api/v1/processing/status/{video_id}: Get processing statusGET /api/v1/processing/results/{video_id}: Get processing resultsPOST /api/v1/auth/login: User loginPOST /api/v1/auth/register: User registrationGET /api/v1/users/me: Get current userPOST /api/v1/videos/upload-and-process-direct: Upload and process a video with API key authentication
API Key Authentication
Some endpoints support API key authentication for direct integration with other systems. To use these endpoints:
- Set the
API_KEYenvironment variable or let it auto-generate - Include the API key in the
X-API-Keyheader with your requests - Endpoints that support API key authentication are documented in the API docs
Example:
curl -X POST "http://localhost:8000/api/v1/videos/upload-and-process-direct" \
-H "X-API-Key: your-api-key" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@video.mp4" \
-F "frame_rate=35" \
-F "backend=mediapipe"
Testing
Run tests with pytest:
pytest
Development Tools
The project includes several development tools:
- Black: Code formatting
- isort: Import sorting
- flake8: Code linting
- mypy: Type checking
- pytest: Testing framework
Environment Variables
| Variable | Description | Default |
|---|---|---|
| DATABASE_URL | SQLite or PostgreSQL connection string | sqlite:///app.db |
| SECRET_KEY | JWT secret key | None |
| API_KEY | API key for direct endpoints | Auto-generated |
| OPENAI_API_KEY | OpenAI API key for analysis | None |
| GROQ_API_KEY | Groq API key for analysis | None |
| CORS_ORIGINS | Allowed CORS origins | ["http://localhost:3000"] |
License
This project is licensed under the MIT License.