This project demonstrates how to stream video from a Raspberry Pi's camera or USB camera using OpenCV and Flask. The live video feed is captured using OpenCV and streamed over a local network to any device connected to the same network. The video is served through a simple web interface, making it accessible via a web browser.
- Live video streaming from the Raspberry Pi's camera.
- Web interface accessible from any device connected to the same network.
- Lightweight and easy to deploy on any Raspberry Pi.
- Raspberry Pi (tested on Raspberry Pi 4/5, but should work on earlier models)
- Raspberry Pi Camera or USB camera
- Python 3
- Flask and OpenCV
- Flask (
2.3.2) - OpenCV (
4.8.0.74)
You can find these in the requirements.txt file for easy installation.
Make sure your Raspberry Pi is set up and that the camera is enabled.
-
Update your system:
sudo apt update && sudo apt upgrade -y⚠️ Warning: The below step is not required or even possible in rpi5 with bookworm. -
Enable the Raspberry Pi camera (if using the Raspberry Pi Camera Module):
sudo raspi-config
Go to Interfacing Options > Camera and enable it. Then reboot the Pi:
sudo reboot
Clone the repository (or create the necessary files if not using version control):
git clone https://github.com/CanonEllis/rpi_video.git
cd <project-folder>First, make sure you have pip installed. Then install the dependencies listed in the requirements.txt file:
pip3 install -r requirements.txtThis command installs Flask and OpenCV, which are required for the application.
To start the Flask application that streams video:
-
Run the Flask app:
python3 app.py
This will start the Flask server on the Raspberry Pi, listening on port
5000. -
On any device connected to the same local network, open a browser and navigate to the Raspberry Pi's IP address:
http://<raspberry-pi-ip>:5000For example:
http://192.168.1.100:5000You will now see the live video stream from the Raspberry Pi camera.
├── app.py # Main Python script that runs the Flask server and streams video.
├── requirements.txt # Python dependencies needed for the project.
└── README.md # Documentation for the project.
from flask import Flask, Response
import cv2
app = Flask(__name__)
# OpenCV capture from the Raspberry Pi camera (or USB camera)
camera = cv2.VideoCapture(0) # 0 for default camera, adjust if needed
# Function to generate frames from the camera
def generate_frames():
while True:
success, frame = camera.read() # Read a frame from the camera
if not success:
break
else:
# Encode the frame as JPEG
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# Use yield to stream the frame in a multipart format
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
# Route to serve the video stream
@app.route('/video_feed')
def video_feed():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
# Route to serve the HTML page that displays the video stream
@app.route('/')
def index():
return '''
<html>
<head>
<title>Live Video Stream</title>
</head>
<body>
<h1>Live Video Feed</h1>
<img src="/video_feed" style="width:640px; height:480px;">
</body>
</html>
'''
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)This file lists all the dependencies required to run the application:
Flask==2.3.2
opencv-python==4.8.0.74
- Make sure the Raspberry Pi and the device you are accessing it from (e.g., a laptop or phone) are on the same local network (Wi-Fi or Ethernet).
- Open a browser and go to
http://<raspberry-pi-ip>:5000. - The video feed should be visible.
- Camera Not Found: Ensure that the camera is enabled via
raspi-config, and that it is correctly connected. - Wrong IP Address: Use
hostname -Ion the Raspberry Pi to find its IP address on the network. - Camera Capture Issues: If using a USB camera, you may need to adjust the
cv2.VideoCapture()index (0,1, etc.) depending on which camera is being used.
This project is licensed under the MIT License.