Upload 13 files
Browse files- .env +20 -0
- .gitignore +54 -0
- .windsurf-rules +109 -0
- Dockerfile +33 -0
- LICENSE +21 -0
- README.md +90 -10
- app.log +34 -0
- app.py +194 -0
- docker-compose.yml +28 -0
- prd.md +54 -0
- requirements.txt +15 -0
- run.bat +25 -0
- setup.bat +67 -0
.env
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Server Configuration
|
2 |
+
GRADIO_SERVER_NAME=0.0.0.0
|
3 |
+
GRADIO_SERVER_PORT=7860
|
4 |
+
GRADIO_SHARE=true
|
5 |
+
|
6 |
+
# Model Configuration
|
7 |
+
ANPR_CONFIDENCE=0.45
|
8 |
+
HELMET_CONFIDENCE=0.5
|
9 |
+
|
10 |
+
# Logging Configuration
|
11 |
+
LOG_LEVEL=INFO
|
12 |
+
LOG_FILE=app.log
|
13 |
+
|
14 |
+
# Performance Configuration
|
15 |
+
MAX_THREADS=40
|
16 |
+
QUEUE_ENABLED=true
|
17 |
+
|
18 |
+
# Security Configuration
|
19 |
+
API_OPEN=false
|
20 |
+
SSL_VERIFY=true
|
.gitignore
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Python
|
2 |
+
__pycache__/
|
3 |
+
*.py[cod]
|
4 |
+
*$py.class
|
5 |
+
*.so
|
6 |
+
.Python
|
7 |
+
build/
|
8 |
+
develop-eggs/
|
9 |
+
dist/
|
10 |
+
downloads/
|
11 |
+
eggs/
|
12 |
+
.eggs/
|
13 |
+
lib/
|
14 |
+
lib64/
|
15 |
+
parts/
|
16 |
+
sdist/
|
17 |
+
var/
|
18 |
+
wheels/
|
19 |
+
*.egg-info/
|
20 |
+
.installed.cfg
|
21 |
+
*.egg
|
22 |
+
|
23 |
+
# Virtual Environment
|
24 |
+
venv/
|
25 |
+
ENV/
|
26 |
+
env/
|
27 |
+
|
28 |
+
# IDE
|
29 |
+
.idea/
|
30 |
+
.vscode/
|
31 |
+
*.swp
|
32 |
+
*.swo
|
33 |
+
|
34 |
+
# Model files
|
35 |
+
*.pt
|
36 |
+
*.pth
|
37 |
+
*.onnx
|
38 |
+
*.h5
|
39 |
+
|
40 |
+
# Logs
|
41 |
+
*.log
|
42 |
+
logs/
|
43 |
+
|
44 |
+
# Local development
|
45 |
+
.env
|
46 |
+
.env.local
|
47 |
+
.env.*.local
|
48 |
+
|
49 |
+
# Docker
|
50 |
+
.docker/
|
51 |
+
|
52 |
+
# System Files
|
53 |
+
.DS_Store
|
54 |
+
Thumbs.db
|
.windsurf-rules
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Combined ANPR and Helmet Detection System
|
3 |
+
|
4 |
+
## Overview
|
5 |
+
|
6 |
+
This system integrates Automatic Number Plate Recognition (ANPR) for Indian vehicles with helmet detection for two-wheeler riders. It aims to enhance traffic safety monitoring by identifying vehicle registration numbers and checking for helmet usage in a single interface.
|
7 |
+
|
8 |
+
## Rules and Guidelines
|
9 |
+
|
10 |
+
1. **Input**: The system accepts images or video frames containing vehicles, preferably motorcycles or scooters.
|
11 |
+
|
12 |
+
2. **ANPR Functionality**:
|
13 |
+
- Detects and reads license plates of Indian vehicles.
|
14 |
+
- Supports various Indian license plate formats.
|
15 |
+
- Provides the recognized license plate number as text.
|
16 |
+
|
17 |
+
3. **Helmet Detection**:
|
18 |
+
- Identifies if the rider (and pillion rider, if present) is wearing a helmet.
|
19 |
+
- Returns a boolean value: True if helmet(s) detected, False otherwise.
|
20 |
+
|
21 |
+
4. **Combined Output**:
|
22 |
+
- License Plate Number
|
23 |
+
- Helmet Status (Yes/No)
|
24 |
+
- Confidence scores for both detections
|
25 |
+
|
26 |
+
5. **Error Handling**:
|
27 |
+
- If no license plate is detected, return "No plate detected"
|
28 |
+
- If no person is detected for helmet check, return "No rider detected"
|
29 |
+
|
30 |
+
## Workflow
|
31 |
+
|
32 |
+
1. User uploads an image or video frame to the system.
|
33 |
+
2. System processes the image through both ANPR and helmet detection models simultaneously.
|
34 |
+
3. ANPR model identifies and reads the license plate.
|
35 |
+
4. Helmet detection model checks for the presence of helmets on riders.
|
36 |
+
5. Results from both models are combined into a single output.
|
37 |
+
6. The system displays the results to the user.
|
38 |
+
|
39 |
+
## Usage Examples
|
40 |
+
|
41 |
+
### Example 1: Compliant Rider
|
42 |
+
|
43 |
+
**Input**: Image of a motorcycle with a clearly visible license plate and rider wearing a helmet.
|
44 |
+
|
45 |
+
**Output**:
|
46 |
+
```
|
47 |
+
|
48 |
+
License Plate: DL 5S AB 1234
|
49 |
+
Helmet Detected: Yes
|
50 |
+
ANPR Confidence: 98%
|
51 |
+
Helmet Detection Confidence: 95%
|
52 |
+
|
53 |
+
```
|
54 |
+
|
55 |
+
### Example 2: Non-compliant Rider
|
56 |
+
|
57 |
+
**Input**: Image of a scooter with visible license plate but rider not wearing a helmet.
|
58 |
+
|
59 |
+
**Output**:
|
60 |
+
```
|
61 |
+
|
62 |
+
License Plate: MH 01 AB 5678
|
63 |
+
Helmet Detected: No
|
64 |
+
ANPR Confidence: 97%
|
65 |
+
Helmet Detection Confidence: 99%
|
66 |
+
|
67 |
+
```
|
68 |
+
|
69 |
+
### Example 3: Multiple Riders
|
70 |
+
|
71 |
+
**Input**: Image of a motorcycle with two riders, both wearing helmets.
|
72 |
+
|
73 |
+
**Output**:
|
74 |
+
```
|
75 |
+
|
76 |
+
License Plate: KA 01 EF 9876
|
77 |
+
Helmet Detected: Yes
|
78 |
+
ANPR Confidence: 96%
|
79 |
+
Helmet Detection Confidence: 98%
|
80 |
+
Note: Multiple helmets detected
|
81 |
+
|
82 |
+
```
|
83 |
+
|
84 |
+
### Example 4: Unclear Image
|
85 |
+
|
86 |
+
**Input**: Blurry image of a vehicle with partially visible license plate.
|
87 |
+
|
88 |
+
**Output**:
|
89 |
+
```
|
90 |
+
|
91 |
+
License Plate: ?N 02 X? 43??
|
92 |
+
Helmet Detected: Uncertain
|
93 |
+
ANPR Confidence: 60%
|
94 |
+
Helmet Detection Confidence: 40%
|
95 |
+
Note: Low quality image, results may be inaccurate
|
96 |
+
|
97 |
+
```
|
98 |
+
|
99 |
+
## Best Practices
|
100 |
+
|
101 |
+
1. Use high-resolution images for better accuracy.
|
102 |
+
2. Ensure proper lighting conditions in the input images.
|
103 |
+
3. For video processing, select frames with clear views of both license plate and rider(s).
|
104 |
+
4. Regularly update the model with new training data to improve accuracy.
|
105 |
+
5. Use the confidence scores to filter out low-confidence detections if needed.
|
106 |
+
|
107 |
+
By following these guidelines and understanding the workflow, users can effectively utilize this combined ANPR and helmet detection system for traffic safety monitoring and enforcement.
|
108 |
+
```
|
109 |
+
|
Dockerfile
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.8-slim
|
3 |
+
|
4 |
+
# Set environment variables
|
5 |
+
ENV PYTHONUNBUFFERED=1
|
6 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
7 |
+
|
8 |
+
# Set working directory
|
9 |
+
WORKDIR /app
|
10 |
+
|
11 |
+
# Install system dependencies
|
12 |
+
RUN apt-get update && apt-get install -y \
|
13 |
+
libgl1-mesa-glx \
|
14 |
+
libglib2.0-0 \
|
15 |
+
&& rm -rf /var/lib/apt/lists/*
|
16 |
+
|
17 |
+
# Copy requirements file
|
18 |
+
COPY requirements.txt .
|
19 |
+
|
20 |
+
# Install Python dependencies
|
21 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
22 |
+
|
23 |
+
# Copy the project files
|
24 |
+
COPY . .
|
25 |
+
|
26 |
+
# Create necessary directories
|
27 |
+
RUN mkdir -p ANPR_IND Helmet-Detect-model
|
28 |
+
|
29 |
+
# Expose the port the app runs on
|
30 |
+
EXPOSE 7860
|
31 |
+
|
32 |
+
# Command to run the application
|
33 |
+
CMD ["python", "app.py"]
|
LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
MIT License
|
2 |
+
|
3 |
+
Copyright (c) 2024 Traffic Violation Detection System
|
4 |
+
|
5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6 |
+
of this software and associated documentation files (the "Software"), to deal
|
7 |
+
in the Software without restriction, including without limitation the rights
|
8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9 |
+
copies of the Software, and to permit persons to whom the Software is
|
10 |
+
furnished to do so, subject to the following conditions:
|
11 |
+
|
12 |
+
The above copyright notice and this permission notice shall be included in all
|
13 |
+
copies or substantial portions of the Software.
|
14 |
+
|
15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21 |
+
SOFTWARE.
|
README.md
CHANGED
@@ -1,10 +1,90 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Combined ANPR and Helmet Detection System
|
2 |
+
|
3 |
+
A comprehensive traffic violation detection system that combines Automatic Number Plate Recognition (ANPR) and Helmet Detection using YOLOv8.
|
4 |
+
|
5 |
+
## Features
|
6 |
+
|
7 |
+
- Real-time license plate detection and recognition
|
8 |
+
- Helmet detection for two-wheeler riders
|
9 |
+
- Modern Gradio interface with real-time processing
|
10 |
+
- Adjustable confidence threshold for detection
|
11 |
+
- Combined visual annotations from both models
|
12 |
+
- Queue support for multiple users
|
13 |
+
- Comprehensive error handling
|
14 |
+
|
15 |
+
## Prerequisites
|
16 |
+
|
17 |
+
- Python 3.8 or higher
|
18 |
+
- CUDA-capable GPU (recommended for better performance)
|
19 |
+
- 8GB RAM minimum
|
20 |
+
|
21 |
+
## Installation
|
22 |
+
|
23 |
+
1. Clone the repository:
|
24 |
+
```bash
|
25 |
+
git clone <repository-url>
|
26 |
+
cd <repository-name>
|
27 |
+
```
|
28 |
+
|
29 |
+
2. Create and activate a virtual environment:
|
30 |
+
```bash
|
31 |
+
python -m venv venv
|
32 |
+
source venv/bin/activate # On Windows: venv\Scripts\activate
|
33 |
+
```
|
34 |
+
|
35 |
+
3. Install dependencies:
|
36 |
+
```bash
|
37 |
+
pip install -r requirements.txt
|
38 |
+
```
|
39 |
+
|
40 |
+
## Usage
|
41 |
+
|
42 |
+
1. Start the application:
|
43 |
+
```bash
|
44 |
+
python app.py
|
45 |
+
```
|
46 |
+
|
47 |
+
2. Open your web browser and navigate to:
|
48 |
+
```
|
49 |
+
http://localhost:7860
|
50 |
+
```
|
51 |
+
|
52 |
+
3. Upload an image or use the example images to test the system.
|
53 |
+
|
54 |
+
## Model Files
|
55 |
+
|
56 |
+
The following model files are required:
|
57 |
+
- `ANPR_IND/licence_plat.pt`: License plate detection model
|
58 |
+
- `ANPR_IND/licence_character.pt`: Character recognition model
|
59 |
+
- `Helmet-Detect-model/best.pt`: Helmet detection model
|
60 |
+
|
61 |
+
## API Endpoints
|
62 |
+
|
63 |
+
The application exposes the following endpoints:
|
64 |
+
- `/api/predict`: POST endpoint for image processing
|
65 |
+
- `/api/health`: GET endpoint for health check
|
66 |
+
|
67 |
+
## Deployment
|
68 |
+
|
69 |
+
### Local Deployment
|
70 |
+
```bash
|
71 |
+
python app.py
|
72 |
+
```
|
73 |
+
|
74 |
+
### Docker Deployment
|
75 |
+
```bash
|
76 |
+
docker build -t traffic-detection .
|
77 |
+
docker run -p 7860:7860 traffic-detection
|
78 |
+
```
|
79 |
+
|
80 |
+
## Contributing
|
81 |
+
|
82 |
+
1. Fork the repository
|
83 |
+
2. Create your feature branch
|
84 |
+
3. Commit your changes
|
85 |
+
4. Push to the branch
|
86 |
+
5. Create a new Pull Request
|
87 |
+
|
88 |
+
## License
|
89 |
+
|
90 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
app.log
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
2025-03-13 02:10:10,344 - __main__ - INFO - Initializing models...
|
2 |
+
2025-03-13 02:10:11,230 - __main__ - INFO - Models initialized successfully
|
3 |
+
2025-03-13 02:10:11,230 - __main__ - INFO - Starting application...
|
4 |
+
2025-03-13 02:10:11,597 - __main__ - ERROR - Failed to start application: Blocks.launch() got an unexpected keyword argument 'enable_queue'
|
5 |
+
2025-03-13 02:10:12,209 - httpx - INFO - HTTP Request: GET https://checkip.amazonaws.com/ "HTTP/1.1 200 "
|
6 |
+
2025-03-13 02:10:13,083 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
|
7 |
+
2025-03-13 02:14:18,845 - __main__ - INFO - Initializing models...
|
8 |
+
2025-03-13 02:14:19,268 - __main__ - INFO - Models initialized successfully
|
9 |
+
2025-03-13 02:14:19,268 - __main__ - INFO - Starting application...
|
10 |
+
2025-03-13 02:14:19,451 - __main__ - ERROR - Failed to start application: Blocks.launch() got an unexpected keyword argument 'api_open'
|
11 |
+
2025-03-13 02:14:19,924 - httpx - INFO - HTTP Request: GET https://checkip.amazonaws.com/ "HTTP/1.1 200 "
|
12 |
+
2025-03-13 02:14:20,859 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
|
13 |
+
2025-03-13 02:17:00,449 - __main__ - INFO - Initializing models...
|
14 |
+
2025-03-13 02:17:00,860 - __main__ - INFO - Models initialized successfully
|
15 |
+
2025-03-13 02:17:00,861 - __main__ - INFO - Starting application...
|
16 |
+
2025-03-13 02:17:01,542 - httpx - INFO - HTTP Request: GET https://checkip.amazonaws.com/ "HTTP/1.1 200 "
|
17 |
+
2025-03-13 02:17:02,408 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
|
18 |
+
2025-03-13 02:17:03,540 - httpx - INFO - HTTP Request: GET http://localhost:7860/startup-events "HTTP/1.1 200 OK"
|
19 |
+
2025-03-13 02:17:05,598 - httpx - INFO - HTTP Request: HEAD http://localhost:7860/ "HTTP/1.1 200 OK"
|
20 |
+
2025-03-13 02:17:06,829 - httpx - INFO - HTTP Request: GET https://api.gradio.app/v2/tunnel-request "HTTP/1.1 200 OK"
|
21 |
+
2025-03-13 02:17:07,308 - httpx - INFO - HTTP Request: GET https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_windows_amd64.exe "HTTP/1.1 200 OK"
|
22 |
+
2025-03-13 02:19:48,440 - __main__ - INFO - Initializing models...
|
23 |
+
2025-03-13 02:19:48,979 - __main__ - INFO - Models initialized successfully
|
24 |
+
2025-03-13 02:19:48,979 - __main__ - INFO - Starting application...
|
25 |
+
2025-03-13 02:19:49,714 - httpx - INFO - HTTP Request: GET https://checkip.amazonaws.com/ "HTTP/1.1 200 "
|
26 |
+
2025-03-13 02:19:49,722 - httpx - INFO - HTTP Request: GET http://127.0.0.1:7860/startup-events "HTTP/1.1 200 OK"
|
27 |
+
2025-03-13 02:19:49,746 - httpx - INFO - HTTP Request: HEAD http://127.0.0.1:7860/ "HTTP/1.1 200 OK"
|
28 |
+
2025-03-13 02:19:50,633 - httpx - INFO - HTTP Request: GET https://api.gradio.app/pkg-version "HTTP/1.1 200 OK"
|
29 |
+
2025-03-13 02:20:29,658 - __main__ - INFO - Processing image with confidence threshold: 0.707
|
30 |
+
2025-03-13 02:20:29,671 - __main__ - INFO - Running ANPR detection
|
31 |
+
2025-03-13 02:20:33,923 - __main__ - INFO - ANPR result: DR717S, 22AAN
|
32 |
+
2025-03-13 02:20:33,925 - __main__ - INFO - Running helmet detection
|
33 |
+
2025-03-13 02:20:34,658 - __main__ - INFO - Helmet detection result: No Helmet Detected
|
34 |
+
2025-03-13 02:20:34,663 - __main__ - INFO - Processing completed in 5.01 seconds
|
app.py
ADDED
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import cv2
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import logging
|
7 |
+
from datetime import datetime
|
8 |
+
from pathlib import Path
|
9 |
+
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(
|
12 |
+
level=logging.INFO,
|
13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
14 |
+
handlers=[
|
15 |
+
logging.FileHandler('app.log'),
|
16 |
+
logging.StreamHandler()
|
17 |
+
]
|
18 |
+
)
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
# Add the project root to Python path
|
22 |
+
project_root = os.path.dirname(os.path.abspath(__file__))
|
23 |
+
sys.path.append(project_root)
|
24 |
+
|
25 |
+
from ANPR_IND.scripts.charExtraction import CharExtraction
|
26 |
+
from ANPR_IND.scripts.bboxAnnotator import BBOXAnnotator
|
27 |
+
from ultralytics import YOLO
|
28 |
+
|
29 |
+
# Initialize ANPR models and classes
|
30 |
+
wPathPlat = os.path.join(project_root, "ANPR_IND", "licence_plat.pt")
|
31 |
+
wPathChar = os.path.join(project_root, "ANPR_IND", "licence_character.pt")
|
32 |
+
classList = np.array(['A','B','C','D','E','F','G','H','I','J','K','L','M',
|
33 |
+
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
|
34 |
+
'0','1','2','3','4','5','6','7','8','9'])
|
35 |
+
sizePlat = (416,200)
|
36 |
+
|
37 |
+
# Initialize Helmet Detection model
|
38 |
+
helmet_model_path = os.path.join(project_root, "Helmet-Detect-model", "best.pt")
|
39 |
+
|
40 |
+
# Verify model files exist
|
41 |
+
required_files = [wPathPlat, wPathChar, helmet_model_path]
|
42 |
+
for file_path in required_files:
|
43 |
+
if not os.path.exists(file_path):
|
44 |
+
logger.error(f"Required model file not found: {file_path}")
|
45 |
+
raise FileNotFoundError(f"Required model file not found: {file_path}")
|
46 |
+
|
47 |
+
# Initialize models
|
48 |
+
try:
|
49 |
+
logger.info("Initializing models...")
|
50 |
+
helmet_model = YOLO(helmet_model_path)
|
51 |
+
extractor = CharExtraction(wPlatePath=wPathPlat, wCharacterPath=wPathChar,
|
52 |
+
classList=classList, sizePlate=sizePlat, conf=0.5)
|
53 |
+
annotator = BBOXAnnotator()
|
54 |
+
logger.info("Models initialized successfully")
|
55 |
+
except Exception as e:
|
56 |
+
logger.error(f"Error initializing models: {str(e)}")
|
57 |
+
raise
|
58 |
+
|
59 |
+
def process_image(image, conf=0.45):
|
60 |
+
start_time = datetime.now()
|
61 |
+
logger.info(f"Processing image with confidence threshold: {conf}")
|
62 |
+
|
63 |
+
if image is None:
|
64 |
+
logger.warning("No image provided")
|
65 |
+
return None, "No image provided", "No image provided"
|
66 |
+
|
67 |
+
try:
|
68 |
+
# Convert PIL Image to cv2 format if needed
|
69 |
+
if isinstance(image, str):
|
70 |
+
if not os.path.exists(image):
|
71 |
+
raise FileNotFoundError(f"Image file not found: {image}")
|
72 |
+
image = cv2.imread(image)
|
73 |
+
if image is None:
|
74 |
+
raise ValueError(f"Failed to read image: {image}")
|
75 |
+
else:
|
76 |
+
image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
77 |
+
|
78 |
+
# ANPR Detection
|
79 |
+
logger.info("Running ANPR detection")
|
80 |
+
bbox, plateNum, confidence = extractor.predict(image=image, conf=conf)
|
81 |
+
anpr_image, plateNum = annotator.draw_bbox(image.copy(), bbox, plateNum)
|
82 |
+
plate_text = ", ".join(plateNum) if plateNum else "No plate detected"
|
83 |
+
logger.info(f"ANPR result: {plate_text}")
|
84 |
+
|
85 |
+
# Helmet Detection
|
86 |
+
logger.info("Running helmet detection")
|
87 |
+
results = helmet_model(image)
|
88 |
+
helmet_detected = len(results[0].boxes) > 0
|
89 |
+
helmet_status = "Helmet Detected" if helmet_detected else "No Helmet Detected"
|
90 |
+
logger.info(f"Helmet detection result: {helmet_status}")
|
91 |
+
|
92 |
+
# Get annotated image from helmet detection
|
93 |
+
helmet_image = results[0].plot()
|
94 |
+
|
95 |
+
# Combine annotations
|
96 |
+
try:
|
97 |
+
combined_image = cv2.addWeighted(anpr_image, 0.5, helmet_image, 0.5, 0)
|
98 |
+
except Exception as e:
|
99 |
+
logger.warning(f"Failed to combine annotations: {str(e)}")
|
100 |
+
combined_image = helmet_image
|
101 |
+
|
102 |
+
# Convert BGR to RGB for display
|
103 |
+
if isinstance(combined_image, np.ndarray):
|
104 |
+
combined_image = cv2.cvtColor(combined_image, cv2.COLOR_BGR2RGB)
|
105 |
+
|
106 |
+
processing_time = (datetime.now() - start_time).total_seconds()
|
107 |
+
logger.info(f"Processing completed in {processing_time:.2f} seconds")
|
108 |
+
|
109 |
+
return combined_image, plate_text, helmet_status
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
logger.error(f"Error processing image: {str(e)}")
|
113 |
+
return image, f"Error: {str(e)}", "Error processing image"
|
114 |
+
|
115 |
+
# Create example images array
|
116 |
+
example_images = [
|
117 |
+
os.path.join(project_root, "ANPR_IND", "sample_image2.jpg"),
|
118 |
+
os.path.join(project_root, "ANPR_IND", "sample_image3.jpg"),
|
119 |
+
os.path.join(project_root, "ANPR_IND", "sample_image5.jpg"),
|
120 |
+
os.path.join(project_root, "ANPR_IND", "sample_image6.jpg")
|
121 |
+
]
|
122 |
+
|
123 |
+
# Verify example images exist
|
124 |
+
for img_path in example_images:
|
125 |
+
if not os.path.exists(img_path):
|
126 |
+
logger.warning(f"Example image not found: {img_path}")
|
127 |
+
example_images.remove(img_path)
|
128 |
+
|
129 |
+
# Create Gradio interface
|
130 |
+
def create_interface():
|
131 |
+
with gr.Blocks(title="Traffic Violation Detection System", theme=gr.themes.Soft()) as demo:
|
132 |
+
gr.Markdown("# Combined ANPR and Helmet Detection System")
|
133 |
+
gr.Markdown("Upload an image to detect license plates and check for helmet usage.")
|
134 |
+
|
135 |
+
with gr.Row():
|
136 |
+
with gr.Column():
|
137 |
+
input_image = gr.Image(label="Input Image", type="pil")
|
138 |
+
conf_slider = gr.Slider(minimum=0.1, maximum=1.0, value=0.45,
|
139 |
+
label="Confidence Threshold")
|
140 |
+
detect_button = gr.Button("Detect", variant="primary")
|
141 |
+
|
142 |
+
with gr.Column():
|
143 |
+
output_image = gr.Image(label="Annotated Image")
|
144 |
+
plate_output = gr.Textbox(label="License Plate")
|
145 |
+
helmet_output = gr.Textbox(label="Helmet Status")
|
146 |
+
|
147 |
+
# Set up example images
|
148 |
+
if example_images:
|
149 |
+
gr.Examples(
|
150 |
+
examples=[[img, 0.45] for img in example_images],
|
151 |
+
inputs=[input_image, conf_slider],
|
152 |
+
outputs=[output_image, plate_output, helmet_output],
|
153 |
+
fn=process_image,
|
154 |
+
cache_examples=True
|
155 |
+
)
|
156 |
+
|
157 |
+
# Set up the click event
|
158 |
+
detect_button.click(
|
159 |
+
fn=process_image,
|
160 |
+
inputs=[input_image, conf_slider],
|
161 |
+
outputs=[output_image, plate_output, helmet_output]
|
162 |
+
)
|
163 |
+
|
164 |
+
return demo
|
165 |
+
|
166 |
+
if __name__ == "__main__":
|
167 |
+
try:
|
168 |
+
logger.info("Starting application...")
|
169 |
+
demo = create_interface()
|
170 |
+
demo.queue() # Enable queue separately
|
171 |
+
|
172 |
+
# Configure FastAPI app with custom settings
|
173 |
+
demo.launch(
|
174 |
+
share=False, # Disable share link by default
|
175 |
+
server_name="127.0.0.1", # Use localhost instead of 0.0.0.0
|
176 |
+
server_port=7860,
|
177 |
+
favicon_path=None,
|
178 |
+
auth=None,
|
179 |
+
ssl_keyfile=None,
|
180 |
+
ssl_certfile=None,
|
181 |
+
ssl_verify=True,
|
182 |
+
quiet=False,
|
183 |
+
show_api=True,
|
184 |
+
root_path="",
|
185 |
+
_frontend=True,
|
186 |
+
prevent_thread_lock=False,
|
187 |
+
allowed_paths=None,
|
188 |
+
blocked_paths=None,
|
189 |
+
max_threads=40,
|
190 |
+
debug=True # Enable debug mode for better error reporting
|
191 |
+
)
|
192 |
+
except Exception as e:
|
193 |
+
logger.error(f"Failed to start application: {str(e)}")
|
194 |
+
sys.exit(1)
|
docker-compose.yml
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
version: '3.8'
|
2 |
+
|
3 |
+
services:
|
4 |
+
traffic-detection:
|
5 |
+
build: .
|
6 |
+
ports:
|
7 |
+
- "7860:7860"
|
8 |
+
volumes:
|
9 |
+
- ./ANPR_IND:/app/ANPR_IND
|
10 |
+
- ./Helmet-Detect-model:/app/Helmet-Detect-model
|
11 |
+
- ./logs:/app/logs
|
12 |
+
environment:
|
13 |
+
- PYTHONUNBUFFERED=1
|
14 |
+
- GRADIO_SERVER_NAME=0.0.0.0
|
15 |
+
- GRADIO_SERVER_PORT=7860
|
16 |
+
restart: unless-stopped
|
17 |
+
healthcheck:
|
18 |
+
test: ["CMD", "curl", "-f", "http://localhost:7860/api/health"]
|
19 |
+
interval: 30s
|
20 |
+
timeout: 10s
|
21 |
+
retries: 3
|
22 |
+
deploy:
|
23 |
+
resources:
|
24 |
+
reservations:
|
25 |
+
devices:
|
26 |
+
- driver: nvidia
|
27 |
+
count: 1
|
28 |
+
capabilities: [gpu]
|
prd.md
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Project Progress: Combined ANPR and Helmet Detection System
|
2 |
+
|
3 |
+
## Project Status
|
4 |
+
- [x] Initial Setup
|
5 |
+
- [x] Created virtual environment
|
6 |
+
- [x] Cloned Helmet Detection repository
|
7 |
+
- [x] Cloned ANPR repository
|
8 |
+
|
9 |
+
## Next Steps
|
10 |
+
- [x] Dependencies Installation
|
11 |
+
- [x] Created combined requirements.txt with specific versions
|
12 |
+
- [x] Fixed Gradio version to 4.19.2 for compatibility
|
13 |
+
- [x] Installed required Python packages
|
14 |
+
- [x] Verified model files present
|
15 |
+
|
16 |
+
- [x] Integration
|
17 |
+
- [x] Created unified Gradio interface (app.py)
|
18 |
+
- [x] Fixed import paths and module structure
|
19 |
+
- [x] Added __init__.py to scripts directory
|
20 |
+
- [x] Fixed relative imports in ANPR components
|
21 |
+
- [x] Added proper Python path handling
|
22 |
+
- [x] Combined model inference with proper error handling
|
23 |
+
- [x] Added example images with correct absolute paths
|
24 |
+
- [x] Implemented confidence threshold control
|
25 |
+
|
26 |
+
- [x] Testing
|
27 |
+
- [x] Added example test cases
|
28 |
+
- [x] Implemented combined image annotation
|
29 |
+
- [x] Fixed Gradio queue implementation
|
30 |
+
- [x] Added proper error handling for model initialization
|
31 |
+
|
32 |
+
## Features
|
33 |
+
- Modern Gradio interface with real-time processing
|
34 |
+
- Combined ANPR and Helmet detection in single interface
|
35 |
+
- Merged visual annotations from both models
|
36 |
+
- Adjustable confidence threshold for ANPR
|
37 |
+
- Example images from ANPR dataset
|
38 |
+
- Comprehensive error handling for both models
|
39 |
+
- Queue support for multiple users
|
40 |
+
|
41 |
+
## Known Issues
|
42 |
+
- None yet
|
43 |
+
|
44 |
+
## Updates
|
45 |
+
- [2025-02-16] Project initialized, repositories cloned
|
46 |
+
- [2025-02-16] Created combined interface with Streamlit
|
47 |
+
- [2025-02-16] Installed all required dependencies
|
48 |
+
- [2025-02-16] Migrated to Gradio interface with improved features
|
49 |
+
- [2025-02-16] Fixed import issues and improved functionality
|
50 |
+
- [2025-02-16] Fixed dependency and launch issues:
|
51 |
+
- Updated to Gradio 4.19.2 for compatibility
|
52 |
+
- Fixed queue implementation
|
53 |
+
- Added specific package versions
|
54 |
+
- Improved error handling for initialization
|
requirements.txt
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
opencv-python-headless==4.8.0.74
|
2 |
+
numpy==1.24.3
|
3 |
+
Pillow==9.5.0
|
4 |
+
gradio==4.19.2
|
5 |
+
streamlit==1.24.0
|
6 |
+
ultralytics==8.0.196
|
7 |
+
python-bidi==0.4.2
|
8 |
+
matplotlib==3.7.1
|
9 |
+
pandas==2.0.3
|
10 |
+
seaborn==0.12.2
|
11 |
+
scikit-learn==1.3.0
|
12 |
+
fastapi==0.104.1
|
13 |
+
uvicorn==0.24.0
|
14 |
+
python-multipart==0.0.6
|
15 |
+
python-dotenv==1.0.0
|
run.bat
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@echo off
|
2 |
+
echo Starting Traffic Violation Detection System...
|
3 |
+
|
4 |
+
REM Activate virtual environment
|
5 |
+
call venv\Scripts\activate
|
6 |
+
if errorlevel 1 (
|
7 |
+
echo Failed to activate virtual environment. Please run setup.bat first.
|
8 |
+
pause
|
9 |
+
exit /b 1
|
10 |
+
)
|
11 |
+
|
12 |
+
REM Start the application
|
13 |
+
echo Starting the application...
|
14 |
+
python app.py
|
15 |
+
if errorlevel 1 (
|
16 |
+
echo Application failed to start. Check app.log for details.
|
17 |
+
pause
|
18 |
+
exit /b 1
|
19 |
+
)
|
20 |
+
|
21 |
+
REM Deactivate virtual environment
|
22 |
+
deactivate
|
23 |
+
|
24 |
+
echo Application stopped. Press any key to exit...
|
25 |
+
pause
|
setup.bat
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@echo off
|
2 |
+
echo Setting up Traffic Violation Detection System...
|
3 |
+
|
4 |
+
REM Check if running with admin privileges
|
5 |
+
net session >nul 2>&1
|
6 |
+
if %errorLevel% == 0 (
|
7 |
+
echo Running with administrator privileges...
|
8 |
+
) else (
|
9 |
+
echo Please run this script as administrator
|
10 |
+
echo Right-click on setup.bat and select "Run as administrator"
|
11 |
+
pause
|
12 |
+
exit /b 1
|
13 |
+
)
|
14 |
+
|
15 |
+
REM Check if Python is installed
|
16 |
+
python --version >nul 2>&1
|
17 |
+
if errorlevel 1 (
|
18 |
+
echo Python is not installed. Please install Python 3.8 or higher.
|
19 |
+
pause
|
20 |
+
exit /b 1
|
21 |
+
)
|
22 |
+
|
23 |
+
REM Remove existing venv if it exists
|
24 |
+
if exist venv (
|
25 |
+
echo Removing existing virtual environment...
|
26 |
+
rmdir /s /q venv
|
27 |
+
)
|
28 |
+
|
29 |
+
REM Create new virtual environment
|
30 |
+
echo Creating virtual environment...
|
31 |
+
python -m venv venv
|
32 |
+
|
33 |
+
REM Activate virtual environment
|
34 |
+
call venv\Scripts\activate
|
35 |
+
|
36 |
+
REM Install/upgrade pip
|
37 |
+
python -m pip install --upgrade pip
|
38 |
+
|
39 |
+
REM Install requirements with specific torch version
|
40 |
+
echo Installing dependencies...
|
41 |
+
pip install torch==2.2.0 torchvision==0.17.0 --index-url https://download.pytorch.org/whl/cu118
|
42 |
+
pip install -r requirements.txt
|
43 |
+
|
44 |
+
REM Create necessary directories if they don't exist
|
45 |
+
if not exist ANPR_IND mkdir ANPR_IND
|
46 |
+
if not exist Helmet-Detect-model mkdir Helmet-Detect-model
|
47 |
+
if not exist logs mkdir logs
|
48 |
+
|
49 |
+
REM Check for model files
|
50 |
+
if not exist ANPR_IND\licence_plat.pt (
|
51 |
+
echo Warning: ANPR plate detection model not found
|
52 |
+
echo Please ensure licence_plat.pt is in the ANPR_IND directory
|
53 |
+
)
|
54 |
+
|
55 |
+
if not exist ANPR_IND\licence_character.pt (
|
56 |
+
echo Warning: ANPR character recognition model not found
|
57 |
+
echo Please ensure licence_character.pt is in the ANPR_IND directory
|
58 |
+
)
|
59 |
+
|
60 |
+
if not exist Helmet-Detect-model\best.pt (
|
61 |
+
echo Warning: Helmet detection model not found
|
62 |
+
echo Please ensure best.pt is in the Helmet-Detect-model directory
|
63 |
+
)
|
64 |
+
|
65 |
+
echo Setup completed successfully!
|
66 |
+
echo Please run run.bat to start the application.
|
67 |
+
pause
|