Skip to content

jsramesh1990/Orange_Pi_AI_CCTV

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Orange Pi 6 Plus AI CCTV System

A professional, production-ready AI-powered CCTV system for Orange Pi 6 Plus written entirely in Python, C, and C++.


Quick Start

git clone https://github.com/orangepi-cctv/ai-cctv.git
cd ai-cctv
sudo ./scripts/setup.sh
sudo systemctl start cctv

That's it. The system will start detecting objects from your cameras and sending alerts to Telegram.


What This System Does

This system turns your Orange Pi 6 Plus into a smart security camera system that can:

  • Detect people, cars, packages, animals, and 80+ other objects
  • Recognize faces (knows who is at your door)
  • Read license plates on vehicles
  • Track objects across multiple cameras (same person keeps same ID)
  • Alert you via Telegram with photos of what was detected
  • Store all detections in a database for later review

All processing happens locally on the Orange Pi - no cloud required.


Requirements

Hardware

Component Minimum Recommended
Board Orange Pi 6 Plus Orange Pi 6 Plus
RAM 16GB 32GB
Storage 256GB NVMe 512GB NVMe + 2TB NVMe
Power 65W USB-C 100W USB-C
Camera 1x IP camera 4-8x IP cameras

Cameras

Any IP camera that supports RTSP protocol works:

  • Dahua, Hikvision, Reolink, TP-Link, Amcrest, etc.
  • 1080p minimum, 4K recommended

Installation Guide

Step 1: Prepare Orange Pi 6 Plus

Install Ubuntu 24.04 on your Orange Pi 6 Plus. Official images available at orangepi.org.

# Update system
sudo apt update && sudo apt upgrade -y

Step 2: Install Dependencies

# Build tools
sudo apt install -y build-essential cmake

# Video libraries (for RTSP capture)
sudo apt install -y libavformat-dev libavcodec-dev libavutil-dev libswscale-dev

# OpenCV
sudo apt install -y libopencv-dev python3-opencv

# Databases
sudo apt install -y postgresql postgresql-contrib redis-server

# Python
sudo apt install -y python3-dev python3-pip

Step 3: Install Python Packages

pip3 install numpy opencv-python psycopg2-binary redis
pip3 install requests python-telegram-bot onnxruntime ultralytics

Step 4: Build C/C++ Libraries

# Clone repository
git clone https://github.com/orangepi-cctv/ai-cctv.git
cd ai-cctv

# Create lib directory
mkdir -p lib

# Build RTSP capture (C library)
gcc -Wall -O2 -fPIC -c src/c/rtsp_capture.c -o lib/rtsp_capture.o
gcc -Wall -O2 -fPIC -c src/c/motion_detection.c -o lib/motion_detection.o
gcc -shared -o lib/librtsp_capture.so lib/rtsp_capture.o lib/motion_detection.o \
    -lavformat -lavcodec -lavutil -lswscale -lpthread

# Build DeepSORT tracker (C++ library)
g++ -Wall -O2 -fPIC -std=c++11 -c src/cpp/deepsort.cpp -o lib/deepsort.o
g++ -shared -o lib/libdeepsort.so lib/deepsort.o -lpthread

Step 5: Setup Database

# Start PostgreSQL
sudo systemctl start postgresql

# Create database
sudo -u postgres psql << 'EOF'
CREATE USER cctv WITH PASSWORD 'cctv123';
CREATE DATABASE cctv OWNER cctv;
\c cctv
CREATE TABLE detections (
    id SERIAL PRIMARY KEY,
    camera_id INTEGER,
    track_id INTEGER,
    object_type VARCHAR(50),
    confidence FLOAT,
    bbox_x1 FLOAT, bbox_y1 FLOAT, bbox_x2 FLOAT, bbox_y2 FLOAT,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    image_path TEXT
);
CREATE TABLE alerts (
    id SERIAL PRIMARY KEY,
    alert_type VARCHAR(50),
    severity INTEGER,
    message TEXT,
    camera_id INTEGER,
    timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
EOF

Step 6: Download AI Model

mkdir -p /var/cctv/models
wget -O /var/cctv/models/yolov8n.onnx \
    https://github.com/ultralytics/assets/releases/download/v0.0.0/yolov8n.onnx

Step 7: Configure Cameras

Create /etc/cctv/config.json:

{
    "cameras": [
        {
            "id": 0,
            "name": "Front Door",
            "rtsp_url": "192.168.1.100:554/stream1",
            "username": "admin",
            "password": "yourpassword",
            "width": 1920,
            "height": 1080,
            "fps": 30
        }
    ],
    "database": {
        "host": "localhost",
        "port": 5432,
        "user": "cctv",
        "password": "cctv123",
        "name": "cctv"
    },
    "redis": {
        "host": "localhost",
        "port": 6379
    },
    "model": {
        "yolo_path": "/var/cctv/models/yolov8n.onnx",
        "confidence_threshold": 0.5
    },
    "hardware": {
        "use_npu": false,
        "threads": 4
    }
}

Step 8: Set Up Telegram Alerts (Optional)

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow instructions
  3. Copy your bot token
  4. Search for @userinfobot and send /start to get your chat ID
  5. Add to config:
"alerts": {
    "telegram_token": "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11",
    "telegram_chat_id": "123456789"
}

Step 9: Run the System

# Set library path
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./lib

# Run
python3 src/python/detector.py

Step 10: Auto-start on Boot (Optional)

# Create systemd service
sudo tee /etc/systemd/system/cctv.service << 'EOF'
[Unit]
Description=AI CCTV System
After=network.target postgresql.service redis-server.service

[Service]
Type=simple
User=root
WorkingDirectory=/opt/cctv
Environment="LD_LIBRARY_PATH=/opt/cctv/lib"
ExecStart=/usr/bin/python3 /opt/cctv/src/python/detector.py
Restart=always

[Install]
WantedBy=multi-user.target
EOF

# Install
sudo cp -r . /opt/cctv/
sudo systemctl daemon-reload
sudo systemctl enable cctv
sudo systemctl start cctv

How to Use

Check if System is Running

sudo systemctl status cctv

View Live Logs

sudo journalctl -u cctv -f

Watch Alerts in Real-time

redis-cli SUBSCRIBE alerts

Query Detections from Database

# Count detections today
sudo -u postgres psql -d cctv -c "SELECT object_type, COUNT(*) FROM detections WHERE DATE(timestamp)=CURRENT_DATE GROUP BY object_type;"

# View last 10 detections
sudo -u postgres psql -d cctv -c "SELECT timestamp, object_type, confidence FROM detections ORDER BY timestamp DESC LIMIT 10;"

Add a Known Face

python3 -c "
from src.python.face_recognizer import FaceRecognizer
r = FaceRecognizer()
r.add_known_face('/path/to/photo.jpg', 'John Doe')
"

Camera Configuration Examples

Dahua Camera

rtsp://admin:password@192.168.1.100:554/cam/realmonitor?channel=1&subtype=0

Hikvision Camera

rtsp://admin:password@192.168.1.100:554/Streaming/Channels/101

Reolink Camera

rtsp://admin:password@192.168.1.100:554/h264Preview_01_main

Generic RTSP

rtsp://192.168.1.100:554/stream1

Test Camera Connection

ffplay rtsp://admin:password@192.168.1.100:554/stream1

Project Structure

orange-pi-ai-cctv/
│
├── src/
│   ├── python/
│   │   ├── detector.py          # Main detection engine
│   │   ├── face_recognizer.py   # Face recognition
│   │   ├── anpr.py              # License plate reading
│   │   ├── telegram_bot.py      # Telegram alerts
│   │   └── database.py          # Database handling
│   │
│   ├── c/
│   │   ├── rtsp_capture.c       # RTSP stream capture
│   │   └── motion_detection.c   # Motion detection
│   │
│   └── cpp/
│       ├── deepsort.cpp         # Object tracking
│       ├── tracker.cpp          # Kalman filter
│       └── npu_accelerator.cpp  # NPU acceleration
│
├── lib/                          # Compiled libraries
├── scripts/                      # Installation scripts
├── models/                       # AI model files
└── Makefile                      # Build configuration

Commands Reference

Command Description
sudo systemctl start cctv Start the system
sudo systemctl stop cctv Stop the system
sudo systemctl restart cctv Restart the system
sudo systemctl status cctv Check system status
sudo journalctl -u cctv -f View live logs
redis-cli SUBSCRIBE alerts Watch alerts
sudo -u postgres psql -d cctv Open database shell

Common Problems and Solutions

Camera won't connect

# Test with ffplay
ffplay rtsp://user:pass@ip:port/stream

# If that works but system doesn't, check username/password in config.json

High CPU usage

# Lower resolution in config.json
"width": 640,
"height": 480,

# Or enable NPU if available
"use_npu": true

No detections

# Lower confidence threshold
"confidence_threshold": 0.3

# Check if model exists
ls -la /var/cctv/models/yolov8n.onnx

Database error

# Restart PostgreSQL
sudo systemctl restart postgresql

# Check if database exists
sudo -u postgres psql -c "\l"

Telegram alerts not sending

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

# Should return {"ok":true,"result":{...}}

Performance

On Orange Pi 6 Plus (32GB RAM):

Configuration Resolution FPS CPU
Motion only 1080p 60 5%
YOLO only 640x640 12 45%
YOLO + NPU 640x640 35 8%
Full system 1080p 25 15%

Uninstall

sudo systemctl stop cctv
sudo systemctl disable cctv
sudo rm -rf /opt/cctv
sudo rm /etc/systemd/system/cctv.service
sudo systemctl daemon-reload

Support


Files in This Repository

File Purpose
src/python/detector.py Main detection loop, YOLO integration
src/python/face_recognizer.py Face detection and recognition
src/python/anpr.py License plate reading
src/python/telegram_bot.py Telegram alert sending
src/python/database.py PostgreSQL and Redis handling
src/c/rtsp_capture.c High-performance RTSP capture
src/c/motion_detection.c Frame differencing motion detection
src/cpp/deepsort.cpp Multi-object tracking with Kalman filter
src/cpp/npu_accelerator.cpp NPU acceleration for Orange Pi 6 Plus
scripts/setup.sh One-click installation script
Makefile Build C/C++ libraries

Built for Orange Pi 6 Plus | Python · C · C++ | No cloud required

About

Production-ready AI CCTV system built specifically for the Orange Pi 6 Plus single-board compute

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors