Hi there, this is the first post of my new blog. In this brief tutorial I will demonstrate install UV4L and OpenCV for Raspberry Pi. I strongly suggest that you should read the guide from top to bottom first before getting into the process. So, lets begin!
Before we begin, if you’ve tried another way of installing OpenCV I do not guarantee error free results. There might be old links to deleted libraries, directories etc. so a fresh install of Raspbian is recommended. If you believe you won’t get any errors though, you can skip this part.
A list of what we should do:
- Download a fresh Raspbian image from Raspberry Pi official site download section.
- Write the image to the SD card.
- Boot up for the first time. If you are able to see config panel with blue background, continue.
- Choose “Expand Filesystem” option to enlarge partition size.
- Choose “Enable Boot to Desktop/Scratch” and select “Desktop Log in as user ……” option to boot graphical desktop.
- From “Internationalisation Options”, change your keyboard layout to your language specific keyboard.
- Choose “Enable Camera” and select “Enable”. Although camera support might be enabled as default, lets do this just to be sure.
- (Optional) Overclock the Raspberry Pi to fasten up compile process. Default clock frequency + 100MHz won’t hurt I believe but any more than that you might need heatsinks or advanced cooling options. Since I have Raspberry Pi 2, I’m going to choose “Pi2” option here.
- Finally, select “Finish” with right arrow key and reboot the Pi.
That is all for installing Raspbian OS. Moving on to UV4L driver install process.
Beginning the install of UV4L driver for Raspicam. This part won’t take too long. Just copy/paste given commands or edit necessary files.
- Copy/Paste and run command given below, this command adds UV4L repository key.
curl http://www.linux-projects.org/listing/uv4l_repo/lrkey.asc | sudo apt-key add -
- Run the given commands below.
sudo nano /etc/apt/sources.list
deb http://www.linux-projects.org/listing/uv4l_repo/raspbian/ wheezy main
- Run the two lines given below, now this section might take a few minutes. Doing classic apt-get update and installing UV4L driver.
sudo apt-get update
sudo apt-get install uv4l uv4l-raspicam
- (Optional) The next line is for making driver to run at boot with a specific configuration set. If you don’t want to start driver manually each time you boot Raspberry Pi, I highly suggest this part. With this package we can restart driver at any time, too.
sudo apt-get install uv4l-raspicam-extras
- Now we will do a system update. Run the given command.
sudo rpi-update
- (Optional) Well, this part is actually not optional if you don’t want a huge preview screen popping up everytime you run your OpenCV program. This part only works if you’ve done part 4. First, lets open UV4L configuration file to make the change.
sudo nano /etc/uv4l/uv4l-raspicam.conf
- (Optional) Finally, if you are considering doing streams or accessing your camera by network you should run commands given here to install extra packages.
sudo apt-get install uv4l-server
sudo apt-get install uv4l-uvc
sudo apt-get install uv4l-xscreen
sudo apt-get install uv4l-mjpegstream
sudo apt-get install uv4l-webrtc
- And thats it, lets make a final check here. If you are able to capture a frame with line below, you can proceed to next part.
dd if=/dev/video0 of=snapshot.jpeg bs=11M count=1
The last and the longest part. Nothing much to do here but since we are compiling OpenCV from source, it will take a LONG time. From 2-3 hours (Pi 2) to 10-13 hours. Lets begin!
- Firstly let do a quick check for updates.
- sudo apt-get update
- sudo apt-get upgrade
- Now, copy and execute given command exactly as it is. This is the first block of dependencies.
- sudo apt-get -y install build-essential cmake cmake-curses-gui pkg-config libpng12-0 libpng12-dev libpng++-dev libpng3 libpnglite-dev zlib1g-dbg zlib1g zlib1g-dev pngtools libtiff4-dev libtiff4 libtiffxx0c2 libtiff-tools libeigen3-dev
- Moving on to the second block of dependencies.
- sudo apt-get -y install libjpeg8 libjpeg8-dev libjpeg8-dbg libjpeg-progs ffmpeg libavcodec-dev libavcodec53 libavformat53 libavformat-dev libgstreamer0.10-0-dbg libgstreamer0.10-0 libgstreamer0.10-dev libxine1-ffmpeg libxine-dev libxine1-bin libunicap2 libunicap2-dev swig libv4l-0 libv4l-dev python-numpy libpython2.6 python-dev python2.6-dev libgtk2.0-dev
- Lets download OpenCV source code.
- wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.11/opencv-2.4.11.zip/download -O opencv-2.4.11.zip
- Execute commands below one by one to set environment.
- unzip opencv-2.4.11.zip
- cd opencv-2.4.11
- mkdir release
- cd release
- ccmake ../
- Now, we are at the most important part. I will share an example build configuration but please read each one carefully otherwise you might have problems later. I’ve enabled NEON support here because NEON registers can be used in increasing OpenCV performance but I’ve yet to try it. Also if you want to have text fields, buttons and fancy stuff like that within your OpenCV program, install QT before doing this part and enable QT support in build configuration. Also, you can disable examples, documentation etc. to gain time while compiling. It’s your choice. Press ‘c’ key to configurate, move around with arrow keys and to change options press enter. When you finish, press ‘c’ again to save configuration and press ‘g’ to generate Makefile. (Do not try to copy/paste this part, read each line and check if it is the same.)
ANT_EXECUTABLE ANT_EXECUTABLE-NOTFOUND BUILD_DOCS ON BUILD_EXAMPLES OFF BUILD_JASPER ON BUILD_JPEG ON BUILD_OPENEXR ON BUILD_PACKAGE ON BUILD_PERF_TESTS ON BUILD_PNG ON BUILD_SHARED_LIBS ON BUILD_TBB OFF BUILD_TESTS ON BUILD_TIFF ON BUILD_WITH_DEBUG_INFO ON BUILD_ZLIB ON BUILD_opencv_apps ON BUILD_opencv_calib3d ON BUILD_opencv_contrib ON BUILD_opencv_core ON BUILD_opencv_features2d ON BUILD_opencv_flann ON BUILD_opencv_gpu ON BUILD_opencv_highgui ON BUILD_opencv_imgproc ON BUILD_opencv_legacy ON BUILD_opencv_ml ON BUILD_opencv_nonfree ON BUILD_opencv_objdetect ON BUILD_opencv_ocl ON BUILD_opencv_photo ON BUILD_opencv_python ON BUILD_opencv_stitching ON BUILD_opencv_superres ON BUILD_opencv_ts ON BUILD_opencv_video ON BUILD_opencv_videostab ON BUILD_opencv_world OFF CLAMDBLAS_INCLUDE_DIR CLAMDBLAS_INCLUDE_DIR-NOTFOUND CLAMDBLAS_ROOT_DIR CLAMDBLAS_ROOT_DIR-NOTFOUND CLAMDFFT_INCLUDE_DIR CLAMDFFT_INCLUDE_DIR-NOTFOUND CLAMDFFT_ROOT_DIR CLAMDFFT_ROOT_DIR-NOTFOUND CMAKE_BUILD_TYPE Release CMAKE_CONFIGURATION_TYPES Debug;Release CMAKE_INSTALL_PREFIX /usr/local CMAKE_VERBOSE OFF CUDA_BUILD_CUBIN OFF CUDA_BUILD_EMULATION OFF CUDA_HOST_COMPILER /usr/bin/gcc CUDA_SDK_ROOT_DIR CUDA_SDK_ROOT_DIR-NOTFOUND CUDA_SEPARABLE_COMPILATION OFF CUDA_TOOLKIT_ROOT_DIR CUDA_TOOLKIT_ROOT_DIR-NOTFOUND CUDA_VERBOSE_BUILD OFF EIGEN_INCLUDE_PATH /usr/include/eigen3 ENABLE_NEON ON ENABLE_NOISY_WARNINGS OFF ENABLE_OMIT_FRAME_POINTER ON ENABLE_PRECOMPILED_HEADERS ON ENABLE_PROFILING OFF ENABLE_SOLUTION_FOLDERS OFF ENABLE_VFPV3 OFF EXECUTABLE_OUTPUT_PATH /home/pi/opencv-2.4.11/release/bin GIGEAPI_INCLUDE_PATH GIGEAPI_INCLUDE_PATH-NOTFOUND GIGEAPI_LIBRARIES GIGEAPI_LIBRARIES-NOTFOUND INSTALL_CREATE_DISTRIB OFF INSTALL_C_EXAMPLES OFF INSTALL_PYTHON_EXAMPLES OFF INSTALL_TO_MANGLED_PATHS OFF OPENCV_CONFIG_FILE_INCLUDE_DIR /home/pi/opencv/opencv-2.4.11/release OPENCV_EXTRA_MODULES_PATH OPENCV_WARNINGS_ARE_ERRORS OFF OPENEXR_INCLUDE_PATH OPENEXR_INCLUDE_PATH-NOTFOUND PVAPI_INCLUDE_PATH PVAPI_INCLUDE_PATH-NOTFOUND PYTHON_NUMPY_INCLUDE_DIR /usr/lib/pymodules/python2.7/numpy/core/include PYTHON_PACKAGES_PATH lib/python2.7/dist-packages SPHINX_BUILD SPHINX_BUILD-NOTFOUND WITH_1394 OFF WITH_CUBLAS OFF WITH_CUDA OFF WITH_CUFFT OFF WITH_EIGEN ON WITH_FFMPEG ON WITH_GIGEAPI OFF WITH_GSTREAMER ON WITH_GTK ON WITH_JASPER ON WITH_JPEG ON WITH_LIBV4L ON WITH_NVCUVID OFF WITH_OPENCL ON WITH_OPENCLAMDBLAS ON WITH_OPENCLAMDFFT ON WITH_OPENEXR ON WITH_OPENGL ON WITH_OPENMP OFF WITH_OPENNI OFF WITH_PNG ON WITH_PVAPI ON WITH_QT OFF WITH_TBB OFF WITH_TIFF ON WITH_UNICAP OFF WITH_V4L ON WITH_XIMEA OFF WITH_XINE OFF
Now the boring part comes, first command takes a lot of time as I’ve said. Second line doesn’t take long. If you have any kind of difficulty while compiling with first line (such as electricity blackouts, OS freeze or crash etc.); just open up the terminal again, go to the opencv-2.4.11/release/ and run make command again. It’ll continue from where it left off.make
sudo make install
After you’ve executed both commands above, you might get an error like “Error while loading shared libraries. Cannot open shared object file. No such file or directory” while running an OpenCV C++ program. If you get this error, just run the line below.
sudo ldconfig -v
If you didn’t have any problem until this point, congratulations! Now, you have a SD Card with Raspbian + UV4L Cam Driver + OpenCV configured in it. I strongly recommend backing up your SD Card as a precaution at this point. (Be careful, since we’ve expanded file partition, image size of SD Card will be as large as SD Card capacity!)
Have fun!
Sources: