summaryrefslogtreecommitdiff
path: root/Biz/Kidcam/README.md
blob: fa69c3cbc5e47cae82d51db02072b6bcd6972d82 (plain)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
# Kidcam - Motion-Activated Family Video Streaming

Kidcam is an intelligent video monitoring system that detects people, sends Telegram notifications, and provides RTSP streaming. Designed for Jetson Nano and similar ARM-based systems.

## Quick Start

```bash
# Clone repository
git clone git@simatime.com:omni.git
cd omni

# Deploy (on Jetson Nano running Ubuntu 20.04)
sudo Biz/Kidcam/deploy.sh

# Configure Telegram credentials
sudo nano /etc/kidcam/config.env

# Start service
sudo systemctl start kidcam

# Check status
sudo systemctl status kidcam
sudo journalctl -u kidcam -f
```

## Hardware Requirements

### Minimum
- **Device**: NVIDIA Jetson Nano (4GB recommended)
- **OS**: Ubuntu 20.04 LTS (ARM64)
- **Camera**: USB webcam or CSI camera module
- **Storage**: 16GB+ microSD card
- **Network**: WiFi or Ethernet connection

### Recommended
- 32GB+ microSD card for logging and model caching
- 5V 4A power supply for Jetson Nano
- Camera with 1080p resolution

## Installation

### Automated Deployment

The deployment script handles everything:

```bash
sudo Biz/Kidcam/deploy.sh
```

This script:
1. Verifies system is Ubuntu 20.04 on aarch64
2. Creates `kidcam` system user
3. Installs system dependencies (Python, OpenCV, GStreamer)
4. Sets up Python virtual environment at `/opt/kidcam/venv`
5. Installs Python packages (ultralytics, telegram bot, opencv)
6. Creates directories: `/opt/kidcam` and `/etc/kidcam`
7. Installs and enables systemd service
8. Creates example configuration file

### Manual Installation

If you prefer manual setup:

```bash
# 1. Install system dependencies
sudo apt-get update
sudo apt-get install -y python3 python3-venv python3-opencv \
    gstreamer1.0-tools v4l-utils

# 2. Create user and directories
sudo useradd --system --no-create-home kidcam
sudo usermod -aG video kidcam
sudo mkdir -p /opt/kidcam /etc/kidcam

# 3. Copy project files
sudo rsync -av ./ /opt/kidcam/

# 4. Setup Python environment
sudo python3 -m venv /opt/kidcam/venv
sudo /opt/kidcam/venv/bin/pip install ultralytics python-telegram-bot opencv-python

# 5. Install systemd service
sudo cp Biz/Kidcam/kidcam.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable kidcam
```

## Configuration

Edit `/etc/kidcam/config.env`:

```bash
# Camera Configuration
CAMERA_DEVICE=/dev/video0        # USB camera (use /dev/video1 for CSI)
STREAM_PORT=8554                 # RTSP streaming port

# Detection Parameters
DETECTION_CONFIDENCE=0.5         # Confidence threshold (0.0-1.0)
COOLDOWN_MINUTES=5               # Minutes before re-triggering

# Telegram Bot Setup
TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
TELEGRAM_CHAT_ID=123456789

# Python Path
PYTHONPATH=/opt/kidcam
```

### Getting Telegram Credentials

1. **Create Bot**:
   - Message [@BotFather](https://t.me/BotFather) on Telegram
   - Send `/newbot` and follow instructions
   - Copy the bot token

2. **Get Chat ID**:
   - Start a chat with your new bot
   - Send any message
   - Visit: `https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates`
   - Find `"chat":{"id":123456789}` in the JSON response

### Camera Device Selection

```bash
# List available cameras
v4l2-ctl --list-devices

# Test camera
ffmpeg -f v4l2 -i /dev/video0 -frames 1 test.jpg
```

Common devices:
- `/dev/video0` - USB webcam
- `/dev/video1` - CSI camera (Jetson)

## Service Management

```bash
# Start service
sudo systemctl start kidcam

# Stop service
sudo systemctl stop kidcam

# Restart service
sudo systemctl restart kidcam

# View status
sudo systemctl status kidcam

# Enable auto-start on boot
sudo systemctl enable kidcam

# Disable auto-start
sudo systemctl disable kidcam

# View logs (live)
sudo journalctl -u kidcam -f

# View last 100 lines
sudo journalctl -u kidcam -n 100

# View logs since boot
sudo journalctl -u kidcam -b
```

## Testing Individual Modules

### Test Camera Access

```bash
# Check camera permissions
ls -l /dev/video0
groups kidcam  # Should include 'video' group

# Capture test image
v4l2-ctl --device=/dev/video0 --stream-mmap --stream-count=1 \
         --stream-to=test.jpg
```

### Test Person Detection

```bash
cd /opt/kidcam
source venv/bin/activate
python3 -c "
import Biz.Kidcam.Detector as Detector
detector = Detector.PersonDetector('/dev/video0', 0.5)
print('Detector initialized')
"
```

### Test Telegram Notifications

```bash
cd /opt/kidcam
source venv/bin/activate
python3 -c "
import os
import Biz.Kidcam.Notifier as Notifier
notifier = Notifier.TelegramNotifier(
    os.getenv('TELEGRAM_BOT_TOKEN'),
    os.getenv('TELEGRAM_CHAT_ID')
)
import asyncio
asyncio.run(notifier.send_notification('Test message'))
"
```

### Test RTSP Streaming

```bash
cd /opt/kidcam
source venv/bin/activate
python3 -c "
import Biz.Kidcam.Streamer as Streamer
streamer = Streamer.VideoStreamer('/dev/video0', 8554)
print('Streamer initialized')
"
```

View stream with VLC:
```bash
vlc rtsp://localhost:8554/stream
```

## Troubleshooting

### Service Won't Start

```bash
# Check detailed error logs
sudo journalctl -u kidcam -n 50 --no-pager

# Verify configuration
sudo cat /etc/kidcam/config.env

# Test manual start
cd /opt/kidcam
sudo -u kidcam venv/bin/python3 -m Biz.Kidcam.Core
```

### Camera Not Detected

```bash
# List all video devices
v4l2-ctl --list-devices

# Check permissions
ls -l /dev/video*
groups kidcam  # Must include 'video'

# Verify camera works
ffplay /dev/video0
```

### Import Errors

```bash
# Verify PYTHONPATH
grep PYTHONPATH /etc/kidcam/config.env

# Check Python path in virtual environment
cd /opt/kidcam
source venv/bin/activate
python3 -c "import sys; print('\n'.join(sys.path))"

# Reinstall packages
sudo -u kidcam venv/bin/pip install --upgrade ultralytics python-telegram-bot
```

### Telegram Not Working

```bash
# Test bot token
curl "https://api.telegram.org/bot<YOUR_TOKEN>/getMe"

# Check network connectivity
ping api.telegram.org

# Verify environment variables are loaded
sudo systemctl show kidcam -p Environment
```

### High CPU/Memory Usage

```bash
# Check resource usage
top -u kidcam

# Increase cooldown period
sudo nano /etc/kidcam/config.env
# Set COOLDOWN_MINUTES=10 or higher

# Lower detection confidence
# Set DETECTION_CONFIDENCE=0.7  (fewer detections)
```

### RTSP Stream Issues

```bash
# Check if port is open
sudo netstat -tulpn | grep 8554

# Test with ffplay
ffplay rtsp://localhost:8554/stream

# Check GStreamer plugins
gst-inspect-1.0 | grep rtsp
```

## Architecture

### State Machine

Kidcam operates in three states:

1. **IDLE**: Monitoring for person detection
2. **ACTIVE**: Person detected, streaming and sending notifications
3. **COOLDOWN**: Waiting before re-triggering

### Components

- **Core.py**: Main orchestration service
- **Detector.py**: YOLO-based person detection
- **Streamer.py**: GStreamer RTSP video streaming
- **Notifier.py**: Telegram bot integration

### Data Flow

```
Camera → Detector → [Person?] → Core → Streamer + Notifier
                                   ↓
                              State Machine
```

## Performance Tuning

### Jetson Nano Optimization

```bash
# Max performance mode
sudo nvpmodel -m 0
sudo jetson_clocks

# Check power mode
sudo nvpmodel -q
```

### Reduce Resource Usage

Edit `/etc/kidcam/config.env`:
```bash
# Lower resolution in Streamer.py configuration
# Increase detection interval
COOLDOWN_MINUTES=15

# Higher confidence = fewer false positives
DETECTION_CONFIDENCE=0.7
```

## Development

### Local Testing (Non-Jetson)

```bash
# Build with bild
bild Biz/Kidcam/Core.py

# Run directly
python3 -m Biz.Kidcam.Core

# Run with custom config
CAMERA_DEVICE=/dev/video0 python3 -m Biz.Kidcam.Core
```

### Linting and Type Checking

```bash
# Lint
lint Biz/Kidcam/*.py

# Type check
typecheck.sh Biz/Kidcam/Core.py
```

## Security Considerations

- Service runs as unprivileged `kidcam` user
- Config file has restricted permissions (640)
- Network access limited to Telegram API and RTSP
- systemd hardening applied (NoNewPrivileges, ProtectSystem, etc.)

## License

See repository root for license information.

## Support

For issues or questions:
1. Check logs: `sudo journalctl -u kidcam -f`
2. Review this troubleshooting guide
3. File issue in repository