A FastAPI-based API for detecting and processing license plates in images.
- License plate detection using YOLOv5 (CPU-only)
- Image processing with custom text overlay
- File size validation (max 100MB)
- Rate limiting (10 requests per minute)
- CORS support
- Error handling and logging
- JSON and direct image responses
- Prerequisites
- Installation Steps
- Configuration
- Running the Application
- API Usage
- Troubleshooting
- Development
- Deployment
Before you begin, ensure you have:
- Python 3.8+ installed
- Redis server installed and running
- Git installed
git clone https://github.com/SAMARTHD30/Number-Plate-Detection-API.git
cd Number-Plate-Detection-API# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
.\venv\Scripts\activate
# On Linux/Mac:
source venv/bin/activatepip install -r requirements.txtCreate a .env file in the root directory with:
# Server Configuration
HOST=0.0.0.0
PORT=8000
DEBUG=True
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
REDIS_PASSWORD=
REDIS_URL=redis://localhost:6379/0
# Model Configuration
MODEL_PATH="app/models/best.pt"
# Security
SECRET_KEY=your-secret-key-here
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# Using gunicorn with uvicorn workers
gunicorn app.main:app --workers 4 --worker-class uvicorn.workers.UvicornWorker --bind 0.0.0.0:8000# JSON response
curl -X POST "http://localhost:8000/api/detect" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_image.jpg" \
-F "return_type=json"
# Image response
curl -X POST "http://localhost:8000/api/detect" \
-H "accept: image/jpeg" \
-H "Content-Type: multipart/form-data" \
-F "file=@your_image.jpg" \
-F "return_type=image" \
--output result.jpg- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
# Check Redis status
redis-cli ping
# Should return PONG
# Start Redis service
# Windows: Start Redis service from Services
# Linux: sudo systemctl start redis-server
# Mac: brew services start redis- Ensure
app/models/best.ptexists - Check file permissions
- Verify model file integrity
# Check port usage
# Windows:
netstat -ano | findstr :8000
# Linux/Mac:
lsof -i :8000
# Use different port if needed
uvicorn app.main:app --reload --host 0.0.0.0 --port 8001app/
├── api/
│ ├── routes.py
│ └── __init__.py
├── models/
│ └── best.pt
├── static/
├── main.py
└── __init__.py
- Create new route in
app/api/routes.py - Add request/response models
- Implement business logic
- Add error handling
- Update documentation
# Update system
sudo apt update
sudo apt upgrade -y
# Install required packages
sudo apt install -y python3-pip python3-venv nginx redis-server supervisor
# Install system dependencies for OpenCV and PyTorch
sudo apt install -y libgl1-mesa-glx libglib2.0-0# Create directory
sudo mkdir -p /opt/license-plate-api
cd /opt/license-plate-api
# Clone repository
git clone https://github.com/SAMARTHD30/Number-Plate-Detection-API.git .
# Create virtual environment
python3 -m venv venv
source venv/bin/activate
# Install PyTorch (CPU only)
pip install torch==2.0.1 torchvision==0.15.2 --index-url https://download.pytorch.org/whl/cpu
# Install other dependencies
pip install -r requirements.txt
# Create necessary directories
mkdir -p app/models
# Set up environment variables
cp .env.example .env
# Edit .env with your configuration
nano .env# Ensure the model file is in the correct location
sudo mkdir -p /opt/license-plate-api/app/models
sudo cp path/to/your/best.pt /opt/license-plate-api/app/models/
# Set proper permissions
sudo chown -R www-data:www-data /opt/license-plate-api/app/models
sudo chmod 755 /opt/license-plate-api/app/modelssudo nano /etc/nginx/sites-available/license-plate-api
# Add this configuration:
server {
listen 80;
server_name your_domain.com;
# Increase max body size for image uploads
client_max_body_size 10M;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_connect_timeout 75s;
proxy_read_timeout 300s;
}
}
# Enable the site
sudo ln -s /etc/nginx/sites-available/license-plate-api /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginxsudo nano /etc/supervisor/conf.d/license-plate-api.conf
# Add this configuration:
[program:license-plate-api]
command=/opt/license-plate-api/venv/bin/gunicorn app.main:app -w 4 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 --timeout 300
directory=/opt/license-plate-api
user=www-data
autostart=true
autorestart=true
stderr_logfile=/var/log/license-plate-api.err.log
stdout_logfile=/var/log/license-plate-api.out.log
environment=PYTHONPATH="/opt/license-plate-api"
# Update supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start license-plate-api# Start Redis server
sudo systemctl start redis-server
sudo systemctl enable redis-server
# Verify Redis is working
redis-cli ping
# Should return "PONG"- Set up SSL/HTTPS using Let's Encrypt
- Configure firewall (UFW)
- Set proper file permissions
- Use environment variables for sensitive data
- Set up model access controls
- Configure rate limiting in Redis
- Adjust worker count based on CPU cores
- Configure PyTorch to use available GPU
- Optimize model inference batch size
- Configure proper timeouts for long-running predictions
- Monitor memory usage and adjust as needed
# Monitor GPU usage (if using CUDA)
watch -n 1 nvidia-smi
# Monitor CPU and Memory
htop
# Monitor API requests
tail -f /var/log/nginx/access.log
# Monitor application errors
tail -f /var/log/license-plate-api.err.log- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
MIT License - See LICENSE file for details