In this post I am setting up a network available RTSP video camera on a Raspberry Pi 4 Model B with the latest Debian Buster installed, you will also want another ‘remote’ computer with VLC installed on the same network. I have used a camera module connected via a ribbon cable to the Pi’s video port. The performance is more like what you would see from a security camera rather than some fancy dedicated device. I will be making the configuration with a bash shell using SSH rather than the GUI.
Installation
If you haven’t already, enable the camera on the Pi with sudo raspi-config and go to Interface Options > Camera. To reduce the load on the computer I also set the Pi to boot to the Console in System Options > Boot / Auto Login and selecting Console, you should also check that at least 256MB of memory is allocated to the GPU in Performance Options > GPU Memory, when complete reboot to set the updated configuration.
Install some prerequisites, you may have some or all of these installed already:
1 2 |
$ sudo apt update $ sudo apt install curl git build-essential software-properties-common cmake |
Next add the Video For Linux (V4L) Repository, this adds the drivers for the video on the hardware side
1 |
$ curl https://www.linux-projects.org/listing/uv4l_repo/lpkey.asc | sudo apt-key add - |
and add the following line to your apt sources with $ sudo nano /etc/apt/sources.list
1 |
deb https://www.linux-projects.org/listing/uv4l_repo/raspbian/stretch stretch main |
We can now install the V4L packages:
1 2 |
$ sudo apt update $ sudo apt install uv4l uv4l-raspicam liblog4cpp5-dev libv4l-dev uv4l-server uv4l-raspicam-extras |
We now need to install the RTSP server from Git, this server connects to the V4L drivers above:
1 2 3 4 5 6 |
$ cd ~ $ git clone https://github.com/mpromonet/v4l2rtspserver.git $ cd v4l2rtspserver $ cmake . $ sudo make install $ sudo reboot |
Configuration
To setup V4L, edit the configuration file $ sudo nano /etc/uv4l/uv4l-raspicam.conf and update the driver options and uncomment the h264 options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
################################## # raspicam driver options ################################## encoding = h264 #720p width = 1280 height = 720 framerate = 12 ### h264 options: profile = high level = 4.2 bitrate = 8000000 intra-refresh-mode = dummy intra-period = #arg inline-headers = yes sps-timing = no quantisation-parameter #arg |
Restart the service once you have saved the file:
1 |
$ sudo service uv4l_raspicam restart |
Useful screen Resolutions; designation, ratio: width x height:
- 1080p, 16:9: 1920 x 1080
- 720p, 16:9: 1280 x 720
- 720, 4:3: 1296 x 792
- Standard Definition, 4:3: 640 x 480
I’ve chosen 720p 16:9 widescreen, this gives a good image quality without stressing the Pi 4B. The framerate is a compromise between quality and usability, its unlikely you would want to go above 25fps.
Connecting
If you don’t know it already, find the IP address of your Pi with $ ip a. Look for the address in eth0 or wlan0 depending on if you are using ethernet or wireless. In this case I will be using 192.168.2.33 in the examples.
Start the RTSP server with:
1 |
$ v4l2rtspserver -U user:password -F12 -W 1280 -H 720 -P 8555 /dev/video0 |
Replace user:password with a suitable username and password. I have assumed that the camera is on /dev/video0, you can test this with:
1 |
$ v4l2-ctl -d /dev/video0 -D |
and look for a line such as Driver version: 5.10.17, if this is 0.0.0 then try a different device, /dev/video1, /dev/video2 etc.
On a successful load it will tell you the connection:
1 2 |
2021-07-01 10:07:39,251 [NOTICE] - /home/pi/v4l2rtspserver/inc/V4l2RTSPServer.h:81 Play this stream using the URL "rtsp://192.168.2.33:8555/unicast" |
Now on your ‘remote’ computer open VLC and choose Media > Open Network Stream and place the given stream address into the network URL, you will then be asked for the username and password. For me it took less than five seconds for an image to appear, there is a fairly high latency on the image shown, the picture lags by about a second from reality.
Using OpenCV
You can access the stream using Python scripts and OpenCV on your ‘remote’ computer for AI/Machine Learning. For me this is a Debian Linux box with FFmpeg as OpenCV uses this to access the stream.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/bin/env python import os import cv2 # if the script doesn't work, uncomment the following, OpenCV may be # defaulting to TCP when we are wanting to use UDP. #os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;udp" # replace user:password with your username and password rtspUrl = "rtsp://user:password@192.168.2.33:8555/unicast" cap = cv2.VideoCapture(rtspUrl) #Check if camera was opened correctly if not (cap.isOpened()): print("Could not open video device") #Set the resolution cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720) while(True): ret, frame = cap.read() cv2.imshow('VIDEO', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() |
You can check OpenCV is working with FFmpeg with:
1 |
$ python -c "import cv2; print(cv2.getBuildInformation())" |
and looking in the output for FFMPEG saying YES:
1 2 3 4 5 6 7 8 9 10 |
Video I/O: DC1394: NO FFMPEG: YES avcodec: YES (58.109.100) avformat: YES (58.61.100) avutil: YES (56.60.100) swscale: YES (5.8.100) avresample: NO GStreamer: NO v4l/v4l2: YES (linux/videodev2.h) |
Links and Sources
- V4L Installation: https://www.linux-projects.org/uv4l/installation/
- RTSP Server https://github.com/mpromonet/v4l2rtspserver
sir
thank you for the course above. i followed your steps but got trouble on cmake.
cmake not found but when i check my etc/apt/ CMake was there.
im struggling to get the cmake working when installing. im using aspberry Pi 4 Model B with the latest Debian Buster installed. any advice?
Thank you
Hello,
I thought cmake was in the build-essential package, I just checked and it is not. Try installing it with:
$ sudo apt install cmake
I’ll update the page.
Thanks