Rraspberry Pi 4B python3 install opencv

 

1, of course, can be used to install or vi nano, as used herein, vim (command line)

sudo apt-get install vim

2, apt edit the configuration file (command line)

sudo vim /etc/apt/sources.list

Press the insert key to enter edit mode, delete (or comment) original content or backup files, and then changed to, Paste as Ctrl + Shift + V, save as Ctrl + C and then enter "  : WQ  "

deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi

deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ buster main contrib non-free rpi

Then enter (command line)

sudo came /etc/apt/sources.list.d/raspi.list

Change the contents of the file

deb http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui

deb-src http://mirror.tuna.tsinghua.edu.cn/raspberrypi/ buster main ui

3, update apt (command line)

sudo apt-get update

sudo apt-get upgrade

(In my firmware version will prevail) system has been built Python2.7 and Python3.7, because the system relies Python2.7 file, in order to maintain system stability, so use Python3.7, some tutorial uses the virtual space, if necessary free to Baidu tutorial

PIP change to the source (the system has built-PIP3, if not installed Python or PIP find yourself installation tutorial)

Input (command line)

sudo came /etc/pip.conf

Change the contents of the file (as used herein, Ali cloud sources, Tsinghua source can be used, such as watercress source source address)

[global]

index-url = http://mirrors.aliyun.com/pypi/simple/

[install]

trusted-host=mirrors.aliyun.com

5, TF card to expand the space, if not do this operation, the back will be problems, such as stuck

Input (command line)

sudo raspi-config

Then select "  Advanced Options  " and select "  Expand Filesystem  " and then restart the Raspberry Pi

6, began to prepare for the OpenCV installation, configuration required libraries (one line)

 

1 sudo apt-get install build-essential git cmake pkg-config -y

2 sudo apt-get install libjpeg8-dev -y

3 sudo apt-get install libtiff5-dev -y

4 sudo apt-get install libjasper-dev -y

5 sudo apt-get install libpng12-dev -y

6 sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y

7 sudo apt-get install libgtk2.0-dev -y

8 sudo apt-get install libatlas-base-dev gfortran -y

7, install numpy

sudo pip3 install numpy                                       

 

Since the default has been installed, remember the installation path / usr / lib / to python3 / dist-packages (1.16.2) , in the back cmake need to modify the corresponding numpy / core / include path i.e. dist-packages of the path

8, download opencv3.4.3 and opencv_contrib3.4.3 (you can choose other versions, but both must be the same version number)
Act One:

cd /home/pi/Downloads
 
wget https://github.com/Itseez/opencv/archive/3.4.3.zip
wget https://github.com/Itseez/opencv_contrib/archive/3.4.3.zip

The following are two ways you can windows or mac download easy to use u disk to copy to the party should raspberry path (/ home / pi / Downloads or build your own path, be noted that the follow-up will be used, remember to modify)
Act II:
opencv3.4.3 downloaded directly from the official website opencv Releases, opencv Releases link
opencv_contrib3.4.3 downloaded from GitHub, opencv_contrib GitHub link
Act III:
OpenCV download Baidu cloud Inn
opencv_contrib Baidu cloud download post
contains multiple opencv and corresponding contrib version, you need to select the relevant documents and extract the installation
-extracting file

cd /home/pi/Downloads
unzip opencv-3.4.3.zip
unzip opencv_contrib-3.4.3.zip

 

9, create and build a path set parameters cmake

cd /home/pi/Downloads/opencv-3.4.0

mkdir build

cd build

 

10, note that the path you set cmake parameters corresponding to the relevant documents and the path python3 numpy installed here attach parameters used for this :( this step carefully, I turned to see the car ... cmake installed after the completion of the content, if there is package installation fails)

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D OPENCV_EXTRA_MODULES_PATH=/home/pi/Downloads/opencv_contrib-3.4.3/modules -D BUILD_EXAMPLES=ON -D WITH_LIBV4L=ON PYTHON3_EXECUTABLE=/usr/bin/python3.7 PYTHON_INCLUDE_DIR=/usr/include/python3.7 PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include ..

Please note the following:

  1. OPENCV_EXTRA_MODULES_PATH = / home / pi / Downloads / opencv_contrib-3.4.3 / modules decompression path your opencv-contrib
  2. PYTHON3_EXECUTABLE = / usr / bin / python3.7 PYTHON_INCLUDE_DIR = / usr / include / python3.7 PYTHON_LIBRARY = / usr / lib / arm-linux-gnueabihf / libpython3.7m.so, wherein python version corresponds to the version of your python3, most libpython3.7m.so file location is just great confirmation
  3. PYTHON3_NUMPY_INCLUDE_DIRS = / usr / lib / python3 / dist-packages / numpy / core include .., numpy include the correct folder path /, this is a Raspberry Pi comes python3 version 4B, if you install, make sure corresponds to the correct path
    wait for the configuration is complete, and if successful will be prompted

build files have been written to : /home/pi/Downloads/opencv-3.4.3/build

If this fails, check the parameters and cmake opencv and opencv_contrib lossless file, but corresponding to (the same two version numbers)

11, compiled
under /home/pi/Downloads/opencv-3.4.3/build path:

 make -j4

This process is relatively long, takes about a couple of hours (aside to do their own thing, come back to complete)
then you can install

sudo make install

After completion of the test installation was successful

sudo python3

import CV2

cv2.__version__

 

Guess you like

Origin www.cnblogs.com/SkystarX/p/12286019.html