6faf505f4b
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
99 lines
2.8 KiB
Bash
99 lines
2.8 KiB
Bash
#!/bin/bash
|
|
# PiCam First-Boot Setup Script
|
|
# Run this on the Raspberry Pi after first boot to configure the IMX219 camera
|
|
# and install the streaming server.
|
|
|
|
set -e
|
|
|
|
LOGFILE="/var/log/picam-setup.log"
|
|
PROJECT_DIR="/home/pi/piCam"
|
|
REPO_URL="" # Leave empty if copying files manually to the SD card
|
|
|
|
exec > >(tee -a "$LOGFILE")
|
|
exec 2>&1
|
|
|
|
echo "========================================"
|
|
echo "PiCam First-Boot Setup Starting"
|
|
echo "Date: $(date)"
|
|
echo "========================================"
|
|
|
|
# Update system
|
|
echo "[1/6] Updating package lists..."
|
|
sudo apt-get update
|
|
|
|
echo "[2/6] Installing system dependencies..."
|
|
sudo apt-get install -y \
|
|
python3-pip \
|
|
python3-venv \
|
|
python3-picamera2 \
|
|
libcamera-dev \
|
|
libcap-dev \
|
|
git \
|
|
i2c-tools \
|
|
v4l-utils
|
|
|
|
# Ensure camera interface is enabled (legacy method, though Bookworm uses libcamera)
|
|
echo "[3/6] Ensuring camera interface is enabled..."
|
|
if command -v raspi-config &> /dev/null; then
|
|
sudo raspi-config nonint do_camera 0 || true
|
|
fi
|
|
|
|
# Create project directory
|
|
echo "[4/6] Setting up project directory..."
|
|
mkdir -p "$PROJECT_DIR"
|
|
|
|
# If files are already present (e.g., copied to SD card), use them.
|
|
# Otherwise, clone or create a placeholder.
|
|
if [ ! -f "$PROJECT_DIR/stream_picamera2.py" ]; then
|
|
echo "Stream scripts not found in $PROJECT_DIR."
|
|
echo "Please copy the project files manually:"
|
|
echo " scp -r /path/to/piCam/* pi@picam.local:/home/pi/piCam/"
|
|
fi
|
|
|
|
# Setup Python virtual environment
|
|
echo "[5/6] Creating Python virtual environment..."
|
|
python3 -m venv "$PROJECT_DIR/venv"
|
|
source "$PROJECT_DIR/venv/bin/activate"
|
|
|
|
# Install Python dependencies
|
|
echo "[6/6] Installing Python packages..."
|
|
pip install --upgrade pip
|
|
pip install Flask opencv-python-headless
|
|
|
|
# Install systemd service for auto-start
|
|
echo "Installing 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=pi
|
|
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
|
|
|
|
echo "========================================"
|
|
echo "Setup Complete!"
|
|
echo "========================================"
|
|
echo "The streaming server will start automatically on boot."
|
|
echo "To start it now, run: sudo systemctl start picam-stream"
|
|
echo "To check status: sudo systemctl status picam-stream"
|
|
echo "To view logs: sudo journalctl -u picam-stream -f"
|
|
echo ""
|
|
echo "Open in your browser: http://$(hostname -I | awk '{print $1}'):5000"
|
|
|
|
# Mark setup as complete so it doesn't run again
|
|
touch /home/pi/.picam-setup-done
|