preface

Last time we installed most of the system related software, this article mainly introduces the latest version of OpenCV 4.4 on the raspberry PI compilation, installation and use.

New features in OpenCV 4.4

Let’s take a look at the new features of OpenCV 4.4:

  • Support YOLO V4;
  • SIFT patents expired and moved to the main repository;
  • ONNX: Supports Resnet_backbone (Torchvision).
  • Support EfficientDet model;
  • Sample text recognition based on C++ and python;
  • FlowNet2 optical flow;
  • Added support for OpenVINO 2020.3 LTS / 2020.4;

Either one of these is worth a gulp, but there are no directly installable packages for Raspberry PI yet, so we can only compile a version from source code.

OpenCV installation and compilation base library

Start by installing some dependency libraries for compilation:

sudo apt-get -y install build-essential cmake unzip pkg-config
sudo apt-get -y install libjpeg-dev libpng-dev libtiff-dev
sudo apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
sudo apt-get -y install libxvidcore-dev libx264-dev
sudo apt-get -y install libgtk-3-dev
sudo apt-get -y install libcanberra-gtk*
sudo apt-get -y install libatlas-base-dev gfortran
Copy the code

Tip:

Need to install more dependent packages, download always disconnected words, you can hang VPN acceleration.

Install the Python virtual environment

The official raspberry PI image comes with python 2.7.16 and Python 3.7.3. To isolate package conflicts in each environment, specify the required Python version. We’ll start by installing a virtual environment management package, which will particularly support subsequent Tensorflow and Pytorch environment dependencies.

sudo pip3 install -U virtualenv
virtualenv -p python3 ~/my_envs/opencv
source ~/my_envs/opencv/bin/activate
#Install numpy
pip3 install numpy
Copy the code

Tip:

Before compiling OpenCV, ensure that the Numpy package has been installed; otherwise, numpy operations will not be supported.

Compile and install FFmpeg

FFmpeg is an audio and video processing most commonly used open source development package, it is powerful, versatile, a large number of video sites and commercial software, but also many audio and video formats standard encoding/decoding implementation.

sudo apt-get install yasm
sudo apt-get install libsdl2-dev -y
Copy the code

Select ffMPEG 4.3.1 to compile and install:

Wget http://ffmpeg.org/releases/ffmpeg-4.3.1.tar.gz tar XVF - ffmpeg - this tar. Gz ffmpeg - this CD/configure --arch=armel --target-os=linux --enable-gpl --enable-libx264 --enable-nonfree make sudo make installCopy the code

Then complete the configuration of LDConfig

#If not, apply for root permission (sudo su)
echo "/usr/local/lib" >> /etc/ld.so.conf  
ldconfig
Copy the code

Finally, enter ffmpeg in the command line to check whether the command output is displayed. If the command output is displayed, the command output is successful

ffmpeg -version
Copy the code

Tip:

It is important to install FFmpeg before compiling OpenCV and specify WITH_FFMPEG=ON in the compile parameter so that the compiled OpenCV can decode video streams using FFmpeg.

I compiled OpenCV for the first time, only to find the problem when using video decoding, only to uninstall and compile again… Three hours at a time

Compile and install OpenCV 4.4.0

Download the two source packages from github.com/opencv and upload them to the Raspberry PI Downloads directory using WinSCP as described in the previous article:

Opencv - 4.4.0.zip opencv_contrib - 4.4.0. ZipCopy the code

Unzip the files

CD ~/Downloads/ unzip opencv-4.4.0.zip unzip opencv_contrib-4.4.0.zipCopy the code

Changing a directory Name

Mv opencv-4.4.0 opencV mv opencv_contrib-4.4.0 opencv_contribCopy the code

Go to the OpencV directory, create the build folder, specify the build parameters,

Where OPENCV_EXTRA_MODULES_PATH points to contrib source,

WITH_FFMPEG=ON Enables FFmpeg support.

cd opencv mkdir build && cd build cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/Downloads/opencv_contrib/modules \ -D ENABLE_NEON=ON \ -D ENABLE_VFPV3=ON \ -D BUILD_TESTS=OFF \ -D OPENCV_ENABLE_NONFREE=ON \ -D INSTALL_PYTHON_EXAMPLES=OFF \ -D CMAKE_SHARED_LINKER_FLAGS='-latomic'  \ -D WITH_FFMPEG=ON \ -D BUILD_EXAMPLES=OFF .. make -j4Copy the code

Wait patiently for two hours and pay attention to heat dissipation. If the following information is displayed, the system succeeds:

Install compiled OpenCV:

sudo make install
sudo ldconfig
Copy the code

CMAKE_INSTALL_PREFIX=/usr/local

Cpython-37m-arm-linux-gnueabihf.so file is generated in lib/python3.7/ site-Packages /cv2/python3.7. This file is required to compile successfully, and then you need this file on the lib/python3.7/site-packages link in the virtual environment:

Test it in the virtual environment and check whether it is successful:

$ python
>>> import cv2
>>> cv2.__version__
'4.4.0'
Copy the code

Perfect!

Tip:

If you install OpenCV directly under Python3.7, you can skip the step of establishing soft links.

Finally, configure the Opencv. PC file

cd /usr/local/lib
sudo mkdir pkgconfig 
cd pkgconfig
sudo nano opencv.pc
Copy the code

Then add the following information to Opencv. PC. Note that this information needs to correspond to the library path when you install OpencV:

prefix=/usr/local exec_prefix=${prefix} includedir=${prefix}/include libdir=${exec_prefix}/lib Name: Opencv Description: The opencv library Version:4.4.0 Cflags: -i ${includedir}/opencv4 Libs: -L${libdir} -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_core ~Copy the code

Save and exit, then import the file into the environment variable:

export  PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
Copy the code

Opencv. PC is now configured

Pkg-config –cflags –libs opencv

This issue related documents, can be in the public account “deep awakening”, background reply: “RPI03”, get the download link.


Next up

We will compile PyTorch 1.6 and PyTorch 1.7 and run yOLO V5 on them, so stay tuned…

Please scan the code to pay attention and share more