Story
Live video streaming server using RPi
There are many ways to stream video to browsers, and each method has its benefits and disadvantages. The method that works well with the streaming feature of Flask is to stream a sequence of independent JPEG pictures. This is called Motion JPEG . This method has low latency, but quality is not the best, since JPEG compression is not very efficient for motion video.
1. Connect the camera module and enable it
First of all, with the Pi switched off, you’ll need to connect the Camera Module to the Raspberry Pi’s camera port.
- Start up the Pi.
- Open the Raspberry Pi Configuration Tool from the main menu or by typing sudo raspi-config in terminal(ctrl+alt+t).
- Enable Camera and I2C
2. How to install OpenCV (prefer to use less than 3)
3. What is Flask
Flask is a micro framework for Python.Flask depends on two external libraries:
Jinja2 template engine and the Werkzeug WSGI toolkit. To install Flask use pip:
pip install Flask
4. Program
If you are using separate directory for flask app then make sure python file and templates directory in same folder, HTML file should be in templates directory.
Make an empty file (test.py):
# import required modules
from flask import Flask, render_template, Response
import picamera
import cv2
import socket
import io
app = Flask(__name__)
vc = cv2.VideoCapture(0)
@app.route('/')
def index():
"""Video streaming ."""
return render_template('index.html')
def gen():
"""Video streaming generator function."""
while True:
rval, frame = vc.read()
cv2.imwrite('pic.jpg', frame)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + open('pic.jpg', 'rb').read() + b'\r\n')
@app.route('/video_feed')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(gen(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)
Make another empty html file (index.html ):
<html>
<head>
<title>Video Streaming </title>
</head>
<body>
<h1> Live Video Streaming </h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
The application has two routes. The / route serves the main page, which is defined in the index.html
The /video_feed route returns the streaming response. Because this stream returns the images that are to be displayed in the web page, the URL to this route is in the src attribute of the image tag. The browser will automatically keep the image element updated by displaying the stream of JPEG images in it.
You can also modify script to take snapshot from live video streaming.
Note
If you are not able to see the video streaming then install these. For streaming video ffpeg and uv4l1.
1. How to install uv4l ( User space Video4Linux)
To install Uv4l on Raspbian Wheezy add the following line to the file/etc/apt/sources.list :
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main
On Raspbian Jessie add the following line to the file /etc/apt/sources.list :
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ jessie main
sudo apt-get update
sudo apt-get install uv4l uv4l-raspicam
If you want the driver to be loaded at boot, also install this optional package:
sudo apt-get install uv4l-raspicam-extras
Apart from the driver for the Raspberry Pi Camera Board, the following Streaming Server front-end and drivers can be optionally installed:
sudo apt-get install uv4l-server uv4l-uvc uv4l-xscreen uv4l-mjpegstream uv4l-dummy uv4l-raspidisp
If you are getting error in installing uv4l-server like required installer libssl1.0.0 is not present so to install that we have to add line to /etc/apt/sources.list
deb http://ftp.de.debian.org/debian jessie main
sudo apt-get update
The WebRTC extension for the Streaming Server is also available with two alternative packages depending on the Raspberry Pi model in use.
If you have a Raspberry Pi 1, Zero or Zero W (Wireless), type:
sudo apt-get install uv4l-webrtc-armv6
For Raspberry Pi 2 or 3) type:
sudo apt-get install uv4l-webrtc
To restart the server:
sudo service uv4l_raspicam restart
It works on port 8080.
2. To install ffmpeg:
Add the following line to the file /etc/apt/sources.list:
deb http://www.deb-multimedia.org jessie main non-free
Update the list of available packages:
sudo apt-get update
Install ffmpeg by command:
sudo apt-get install ffmpeg
5. Output
After modifying script: