Spaces:
Sleeping
Sleeping
File size: 5,167 Bytes
1aa2334 fadf3f5 1aa2334 7bbf985 | 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | ---
title: Plate Detector
emoji: ๐
colorFrom: gray
colorTo: gray
sdk: gradio
sdk_version: 6.14.0
python_version: '3.10'
app_file: app.py
pinned: false
license: mit
---
# ๐ Vehicle Intelligence System
Advanced license plate detection, OCR, and natural language database querying system with security hardening.
## ๐ฏ Features
### Detection Pipeline
- ๐ฅ **YOLO Plate Detection** - Real-time license plate localization
- ๐ค **PaddleOCR** - High-accuracy OCR for plate text extraction
- ๐ **Vehicle Classification** - Classify vehicle types (car, truck, bike, etc.)
- ๐ก๏ธ **Confidence Scoring** - All detections include confidence metrics
### Database & Analytics
- ๐ **Supabase PostgreSQL** - Robust cloud database with automatic backups
- ๐ **NLP-to-SQL** - Ask questions in natural language, AI converts to SQL
- ๐ก๏ธ **SQL Validation** - Prevents injection attacks and dangerous queries
- โก **Optimized Indexes** - Fast queries with proper database indexing
- ๐ **Real-time Analytics** - Dashboard with state distribution, hourly traffic, suspicious vehicles
### Security Features
- โ
**SQL Injection Prevention** - Whitelist/blacklist keyword validation
- ๐ **Read-Only Queries** - Only SELECT queries allowed
- ๐ **Query History** - Track all database queries for audit
- ๐ **State Extraction** - Automatically extract state codes from license plates
## ๐ Modular Architecture
```
plate-detector/
โโโ app.py # Main Gradio UI with 3 tabs
โโโ detector.py # YOLO, OCR, vehicle classification
โโโ database.py # SQL validation, LLM, database operations
โโโ requirements.txt # Python dependencies
โโโ .env # Environment variables (Supabase, HF token)
```
### Files Explained
**app.py** - Simplified UI layer
- Detection tab: Upload images, detect plates
- Database Query tab: NLP-to-SQL search with example queries
- Analytics tab: Real-time dashboard with metrics
**detector.py** - Computer Vision Module
- License plate detection using YOLO
- OCR using PaddleOCR
- Vehicle classification (10 types)
- State code extraction (TN, KA, KL, etc.)
- Image preprocessing and augmentation
**database.py** - Data & Security Layer
- SQL validation (blocks DELETE, UPDATE, INSERT, DROP)
- SQLCoder LLM integration for NLP-to-SQL
- Database initialization with improved schema
- Automatic index creation for performance
- Query history tracking
- Analytics functions (suspicious vehicles, hourly traffic, etc.)
## ๐ Deployment
### Environment Variables
Create `.env` file with:
```
DATABASE_URL=postgresql://user:password@host:5432/postgres
HF_TOKEN=hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```
### Run Locally
```bash
pip install -r requirements.txt
python app.py
```
Visit: http://localhost:7860
### Deploy on Hugging Face Spaces
1. Push code to HF Spaces repository
2. Add secrets in Settings:
- `DATABASE_URL` - Supabase connection string
- `HF_TOKEN` - Hugging Face API token
## ๐ก๏ธ Security Measures
### SQL Validation
```python
ALLOWED_KEYWORDS = ["SELECT", "FROM", "WHERE", "COUNT", ...]
BLOCKED_KEYWORDS = ["DROP", "DELETE", "UPDATE", "INSERT", ...]
# Only SELECT queries allowed
# Blocks all dangerous operations
```
### Database Schema
```sql
CREATE TABLE vehicle_logs (
id BIGSERIAL PRIMARY KEY,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
plate TEXT,
state TEXT,
vehicle_type TEXT,
vehicle_conf FLOAT,
camera_id TEXT,
location TEXT,
image_url TEXT,
date DATE,
hour INTEGER,
day TEXT
);
```
### Indexes for Performance
```sql
CREATE INDEX idx_plate ON vehicle_logs(plate);
CREATE INDEX idx_state ON vehicle_logs(state);
CREATE INDEX idx_vehicle_type ON vehicle_logs(vehicle_type);
CREATE INDEX idx_timestamp ON vehicle_logs(timestamp);
CREATE INDEX idx_date ON vehicle_logs(date);
```
## ๐ฏ Example Queries
- "How many cars today?"
- "Show all trucks"
- "Count bikes"
- "Show TN vehicles"
- "Show vehicles after 6pm"
- "List latest 10 detections"
- "Count vehicles by type"
- "Show high confidence detections"
- "List all unique plates"
- "Show suspicious vehicles"
## ๐ Analytics Dashboard
- **Vehicles by State** - Distribution of detections across states
- **Traffic by Hour** - Hourly traffic patterns
- **Top Plates** - Most frequently detected license plates
- **Suspicious Vehicles** - High-frequency detections (>20 times)
## ๐ฎ Future Improvements
- [ ] Vector search for similar plates
- [ ] Async database queries for better performance
- [ ] Image storage and retrieval
- [ ] Camera tracking and geolocation
- [ ] Result caching with Redis
- [ ] FastAPI backend separation
- [ ] React dashboard frontend
## ๐ฆ Dependencies
- ultralytics - YOLO detection
- paddleocr - OCR
- transformers - Vehicle classification
- sqlalchemy - Database ORM
- huggingface-hub - LLM inference
- gradio - Web UI
- opencv - Image processing
## ๐ License
MIT License - See LICENSE file
## ๐ค Contributing
Contributions welcome! Please submit pull requests or issues.
---
**Live Demo:** https://huggingface.co/spaces/BARATH0070/plate-detector
|