File size: 6,034 Bytes
773b38b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Docker Setup for Crowd Detection API

This guide will help you dockerize and run the Crowd Detection API using Docker and Docker Compose.

## πŸ“ File Structure

Make sure your project directory has the following structure:

```
crowd-detection-api/
β”œβ”€β”€ main.py                 # Your main FastAPI application
β”œβ”€β”€ start_backend.py        # Startup script
β”œβ”€β”€ requirements.txt        # Python dependencies
β”œβ”€β”€ Dockerfile             # Docker image definition
β”œβ”€β”€ docker-compose.yml     # Docker Compose configuration
β”œβ”€β”€ .dockerignore          # Files to exclude from Docker build
β”œβ”€β”€ build-and-run.sh       # Build and run script
└── uploads/               # Directory for uploaded files (created automatically)
└── models/                # Directory for AI models (created automatically)
└── logs/                  # Directory for logs (created automatically)
```

## πŸš€ Quick Start

### Option 1: Using the Build Script (Recommended)

1. **Make the script executable:**
   ```bash
   chmod +x build-and-run.sh
   ```

2. **Run the interactive script:**
   ```bash
   ./build-and-run.sh
   ```

3. **Or run directly with commands:**
   ```bash
   ./build-and-run.sh run      # Build and run with docker-compose
   ./build-and-run.sh simple   # Build and run simple container
   ./build-and-run.sh test     # Test API endpoints
   ./build-and-run.sh logs     # Show container logs
   ./build-and-run.sh stop     # Stop services
   ```

### Option 2: Manual Docker Commands

1. **Build the Docker image:**
   ```bash
   docker build -t crowd-detection-api:latest .
   ```

2. **Run the container:**
   ```bash
   docker run -d \
     --name crowd-detection-backend \
     -p 8000:8000 \
     -v $(pwd)/uploads:/app/uploads \
     -v $(pwd)/models:/app/models \
     -v $(pwd)/logs:/app/logs \
     --restart unless-stopped \
     crowd-detection-api:latest
   ```

### Option 3: Using Docker Compose

1. **Start the services:**
   ```bash
   docker-compose up -d
   ```

2. **Stop the services:**
   ```bash
   docker-compose down
   ```

## πŸ” Accessing the API

Once the container is running, you can access:

- **API Base URL:** http://localhost:8000
- **API Documentation:** http://localhost:8000/docs
- **Health Check:** http://localhost:8000/health
- **Interactive API:** http://localhost:8000/redoc

## πŸ“Š Testing the API

### Health Check
```bash
curl http://localhost:8000/health
```

### Get Zones with Heatmap
```bash
curl http://localhost:8000/zones/heatmap
```

### WebSocket Connection (Alerts)
```javascript
const ws = new WebSocket('ws://localhost:8000/ws/alerts');
ws.onmessage = (event) => {
    console.log('Alert:', JSON.parse(event.data));
};
```

## πŸ› οΈ Development Mode

For development with auto-reload:

```bash
docker run -it --rm \
  -p 8000:8000 \
  -v $(pwd):/app \
  -w /app \
  python:3.9-slim \
  bash -c "pip install -r requirements.txt && python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload"
```

## πŸ“‹ Container Management

### View running containers:
```bash
docker ps
```

### View container logs:
```bash
docker logs crowd-detection-backend -f
```

### Execute commands in container:
```bash
docker exec -it crowd-detection-backend bash
```

### Stop and remove container:
```bash
docker stop crowd-detection-backend
docker rm crowd-detection-backend
```

## πŸ”§ Configuration

### Environment Variables

You can configure the container using environment variables:

```bash
docker run -d \
  --name crowd-detection-backend \
  -p 8000:8000 \
  -e PYTHONUNBUFFERED=1 \
  -e ENVIRONMENT=production \
  crowd-detection-api:latest
```

### Volume Mounts

The container uses the following volumes:
- `./uploads:/app/uploads` - For uploaded video/image files
- `./models:/app/models` - For AI model cache
- `./logs:/app/logs` - For application logs

## 🚨 Troubleshooting

### Container won't start:
1. Check if port 8000 is available:
   ```bash
   netstat -tulpn | grep 8000
   ```

2. Check container logs:
   ```bash
   docker logs crowd-detection-backend
   ```

### API not responding:
1. Check if container is healthy:
   ```bash
   docker ps
   ```

2. Test from inside container:
   ```bash
   docker exec -it crowd-detection-backend curl http://localhost:8000/health
   ```

### Model download issues:
The container automatically downloads YOLOv8 models on first run. If this fails:

1. Check internet connectivity in container
2. Pre-download models manually:
   ```bash
   docker exec -it crowd-detection-backend python -c "from ultralytics import YOLO; YOLO('yolov8s.pt')"
   ```

## πŸ”’ Security Considerations

- The container runs as a non-root user (`appuser`)
- Only necessary system packages are installed
- Resource limits are configured in docker-compose.yml
- Health checks are enabled for monitoring

## πŸ“ˆ Performance Tuning

### Resource Limits

Adjust in `docker-compose.yml`:
```yaml
deploy:
  resources:
    limits:
      cpus: '4.0'      # Increase for better performance
      memory: 8G       # Increase for large models
    reservations:
      cpus: '2.0'
      memory: 4G
```

### GPU Support

For GPU acceleration, add to docker-compose.yml:
```yaml
deploy:
  resources:
    reservations:
      devices:
        - driver: nvidia
          count: 1
          capabilities: [gpu]
```

## πŸ”„ Updates and Maintenance

### Update the application:
1. Stop the container:
   ```bash
   ./build-and-run.sh stop
   ```

2. Rebuild and restart:
   ```bash
   ./build-and-run.sh run
   ```

### Clean up Docker resources:
```bash
# Remove unused images
docker image prune

# Remove unused volumes
docker volume prune

# Remove unused networks
docker network prune
```

## πŸ“ž Support

If you encounter issues:
1. Check the container logs
2. Verify all required files are present
3. Ensure Docker has sufficient resources allocated
4. Check network connectivity for model downloads

The API will automatically start when the container starts and includes health checks for monitoring.