A Flask-based web streaming solution for Raspberry Pi cameras using PiCamera2
Project description
PiCamera2 Web Streamer
A Flask-based web streaming solution for Raspberry Pi cameras using PiCamera2. Stream your Raspberry Pi camera feed securely over HTTPS with minimal latency.
Features
- Real-time MJPEG streaming over HTTPS
- Adaptive frame rate based on client connections
- Clean shutdown handling
- Mobile-responsive web interface
- Thread-safe implementation
- Configurable camera parameters
- Resource-efficient with multiple client support
Prerequisites
- Raspberry Pi (tested on Raspberry Pi 4)
- Raspberry Pi Camera Module
- Python 3.7+
- picamera2
- OpenCV
- Flask
Installation
Via pip
pip install picamera2-webstream
Quick Installation
For a quick automated installation:
git clone https://github.com/yourusername/picamera2-webstream.git
cd picamera2-webstream
./install.sh
The installation script will:
- Install all required system dependencies
- Enable the camera interface
- Set up a Python virtual environment
- Install Python package dependencies
- Generate SSL certificates
- Add your user to the video group
- Verify camera detection
After installation completes:
- Log out and log back in (required for video group access)
- Activate the virtual environment:
source venv/bin/activate
- Run the example:
python examples/basic_stream.py
- Open
https://your-pi-ip
in your browser
To uninstall:
./uninstall.sh
Usage
- Basic usage:
from picamera2_webstream import VideoStream
from flask import Flask
app = Flask(__name__)
stream = VideoStream()
stream.start()
if __name__ == '__main__':
app.run(host='0.0.0.0', port=443, ssl_context=('cert.pem', 'key.pem'))
- Access the stream:
- Open your browser and navigate to
https://your-pi-ip
- Accept the self-signed certificate warning
- View your camera stream!
Camera Configuration
Automatic Configuration
To find the optimal settings for your camera, run the diagnostic tool:
python examples/camera_diagnostics.py
This will:
- Detect all available cameras
- Show detailed camera capabilities
- Test different resolutions and formats
- Measure actual achievable framerates
- Suggest optimal configuration settings
Manual Configuration
You can customize various parameters when initializing the VideoStream:
stream = VideoStream(
resolution=(1280, 720), # Width x Height
framerate=30, # Target framerate
format="MJPEG", # Video format
brightness=0.0, # -1.0 to 1.0
contrast=1.0, # 0.0 to 2.0
saturation=1.0 # 0.0 to 2.0
)
Common camera settings:
- Resolution: Common values include (1920, 1080), (1280, 720), (640, 480)
- Format: Usually "MJPEG" for web streaming
- Framerate: Higher values (30+) for smooth video, lower values (15-) for reduced bandwidth
To see all available settings for your camera:
# List all video devices
v4l2-ctl --list-devices
# Show device capabilities (replace X with your device number)
v4l2-ctl -d /dev/videoX --all
# List supported formats
v4l2-ctl -d /dev/videoX --list-formats-ext
For USB cameras, you might also want to check:
# Show detailed USB device information
lsusb -v | grep -A 10 "Video"
Performance Considerations
- Higher resolutions and framerates require more CPU and bandwidth
- MJPEG format provides good quality with reasonable bandwidth usage
- If streaming over the internet, consider lower resolutions and framerates
- Monitor CPU usage and network bandwidth to find optimal settings
Development
If you want to modify the code:
- Create a development environment:
# Clone and enter the repository
git clone https://github.com/yourusername/picamera2-webstream.git
cd picamera2-webstream
# Create and activate virtual environment
python3 -m venv venv
source venv/bin/activate
# Install in editable mode
pip install -e .
- Run tests (once implemented):
pip install pytest
pytest
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Thanks to the picamera2 team for their excellent camera interface
- The Flask team for their lightweight web framework
Troubleshooting
Common issues and solutions:
-
Camera not detected:
- Ensure the camera is properly connected
- Check if the camera interface is enabled in
raspi-config
- Verify with
libcamera-hello
command
-
ImportError for picamera2:
- Make sure system packages are installed:
sudo apt install python3-libcamera python3-picamera2
- Ensure you're using the virtual environment
- Make sure system packages are installed:
-
SSL Certificate issues:
- Regenerate certificates if they've expired
- Ensure certificates are in the same directory as the script
-
Permission denied errors:
- Ensure your user is in the video group:
sudo usermod -a -G video $USER
- Logout and login again for group changes to take effect
- Ensure your user is in the video group:
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file picamera2_webstream-0.1.2.tar.gz
.
File metadata
- Download URL: picamera2_webstream-0.1.2.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3ca47ccd893be3cb7c5266b01c9af9739fc509257a4e96329a3eeb29dd8c5875 |
|
MD5 | 259a53d191463b29c15c54d91203105e |
|
BLAKE2b-256 | f7b48214b548005cf9981fdf4adb29fa8d457ae251d5c8270934093d573e0135 |
File details
Details for the file picamera2_webstream-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: picamera2_webstream-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32d2997d363ab152d7e6d87aa19e4bee471fd0bca2bb6bf8a9835237fab50044 |
|
MD5 | de6612336453705b2ebfbd3521fc5162 |
|
BLAKE2b-256 | f056e3cbed4174eeb3ef7d50065bbab91c70bf23f63a3a134caba086cedb5954 |