Add PiCam network streamer with auto-setup for Pi 5
Initial commit of the MJPEG streaming server for Raspberry Pi. Supports Pi Camera Module (via picamera2), USB webcams (via OpenCV), and auto-detection of camera type. Includes: - `stream_picamera2.py` — optimized Pi CSI camera streamer - `stream_usb.py` — generic USB camera streamer - `stream.py` — auto-detects camera and launches correct streamer - `image-setup/` — SD card imaging scripts and Pi 5 headless setup guide for first-boot automation with systemd service - `requirements.txt` — Python dependencies
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
#!/bin/bash
|
||||
# PiCam Live Setup — Run this directly on your Raspberry Pi 5 after first boot.
|
||||
# This automates installation of dependencies and enables auto-start streaming.
|
||||
|
||||
set -e
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_DIR="$HOME/piCam"
|
||||
LOGFILE="/var/log/picam-setup.log"
|
||||
|
||||
mkdir -p "$(dirname "$LOGFILE")"
|
||||
exec > >(tee -a "$LOGFILE")
|
||||
exec 2>&1
|
||||
|
||||
echo "============================================"
|
||||
echo " PiCam Setup Script"
|
||||
echo " Running on: $(hostname)"
|
||||
echo " Date: $(date)"
|
||||
echo "============================================"
|
||||
|
||||
# 1. System update
|
||||
echo "[1/7] Updating package lists..."
|
||||
sudo apt-get update
|
||||
|
||||
# 2. Install system packages
|
||||
echo "[2/7] Installing system packages..."
|
||||
sudo apt-get install -y \
|
||||
python3-pip \
|
||||
python3-venv \
|
||||
python3-picamera2 \
|
||||
libcamera-dev \
|
||||
libcap-dev \
|
||||
git \
|
||||
i2c-tools \
|
||||
v4l-utils
|
||||
|
||||
# 3. Detect camera
|
||||
echo "[3/7] Detecting camera..."
|
||||
if command -v libcamera-hello >/dev/null 2>&1; then
|
||||
echo "libcamera devices:"
|
||||
libcamera-hello --list-cameras || true
|
||||
else
|
||||
echo "libcamera-hello not found; relying on picamera2 detection."
|
||||
fi
|
||||
|
||||
# 4. Copy project files
|
||||
echo "[4/7] Installing project files..."
|
||||
mkdir -p "$PROJECT_DIR"
|
||||
|
||||
if [ -d "$SCRIPT_DIR/../" ] && [ -f "$SCRIPT_DIR/../stream_picamera2.py" ]; then
|
||||
# Running from cloned repo
|
||||
cp "$SCRIPT_DIR/../stream_picamera2.py" "$PROJECT_DIR/"
|
||||
cp "$SCRIPT_DIR/../stream_usb.py" "$PROJECT_DIR/"
|
||||
cp "$SCRIPT_DIR/../stream.py" "$PROJECT_DIR/"
|
||||
cp "$SCRIPT_DIR/../requirements.txt" "$PROJECT_DIR/"
|
||||
else
|
||||
echo "WARNING: Could not find project Python files."
|
||||
echo "Please copy them manually to $PROJECT_DIR"
|
||||
fi
|
||||
|
||||
# 5. Python environment
|
||||
echo "[5/7] Creating Python virtual environment..."
|
||||
python3 -m venv "$PROJECT_DIR/venv"
|
||||
source "$PROJECT_DIR/venv/bin/activate"
|
||||
|
||||
pip install --upgrade pip
|
||||
pip install Flask opencv-python-headless
|
||||
|
||||
# 6. Create systemd service
|
||||
echo "[6/7] Creating systemd service..."
|
||||
cat <<EOF | sudo tee /etc/systemd/system/picam-stream.service
|
||||
[Unit]
|
||||
Description=PiCam MJPEG Streamer
|
||||
After=network-online.target
|
||||
Wants=network-online.target
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=$USER
|
||||
WorkingDirectory=$PROJECT_DIR
|
||||
Environment="PATH=$PROJECT_DIR/venv/bin"
|
||||
ExecStart=$PROJECT_DIR/venv/bin/python $PROJECT_DIR/stream_picamera2.py --host 0.0.0.0 --port 5000
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
EOF
|
||||
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable picam-stream.service
|
||||
|
||||
# 7. Start service
|
||||
echo "[7/7] Starting streamer..."
|
||||
sudo systemctl start picam-stream.service
|
||||
|
||||
echo "============================================"
|
||||
echo " Setup Complete!"
|
||||
echo "============================================"
|
||||
echo ""
|
||||
echo "Streamer is running as a systemd service."
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " Status: sudo systemctl status picam-stream"
|
||||
echo " Logs: sudo journalctl -u picam-stream -f"
|
||||
echo " Stop: sudo systemctl stop picam-stream"
|
||||
echo " Start: sudo systemctl start picam-stream"
|
||||
echo ""
|
||||
IP=$(hostname -I | awk '{print $1}')
|
||||
echo "Open in browser: http://$IP:5000"
|
||||
Reference in New Issue
Block a user