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