Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install mxnet for raspberry pie 4B

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to install mxnet in raspberry pie 4B. The article is rich and analyzed and described from a professional point of view. I hope you can get something after reading this article.

Preface

Raspberry pie 4B has up to 4G memory, and CPU,GPU has a higher improvement than 3, so it is suitable to run some AI recognition tasks. However, installing a working environment on a raspberry pie requires some preparation.

System preparation

Generally speaking, we use burning software to burn the raspbian system to our cards, but in order for more users to use it, we generally use 8G cards as images, while we generally use 16G or 32G cards, so we need to expand. Raspbian has provided us with tools, directly use raspi-config, select the Expand Filesystem in the Advanced Options, you can use all the TF card space.

Installation preparation

First of all, we use pyenv to manage the python version, and the default python is 2.7. Then, we use pipenv to manage the python package. There will be some preparatory work here.

Install the necessary dependencies

Pyenv installs python from the source code after downloading the gz package from the official website. Some dev dependencies are required in the compilation process, otherwise the compilation will fail, or there may be fewer features (such as bzip2,SQLite3, etc.)

Sudo apt updatesudo apt install-y libffi-dev libbz2-dev libsqlite3-dev libatlas-base-dev libgfortran3

The first three of them are used by Python when compiling, and libatlas-base-dev is a necessary package when numpy starts. If you don't install it, import numpy will have the following error

Original error was: libf77blas.so.3: cannot open shared object file: No such file or directory

Libgfortran3 is the library that mxnet needs.

Note that when pip install mxnet installs mxnet, the dependency declaration in mxnet requires no more than 1.15.2 numpy, so this version of numpy will be installed, but in fact this version is incompatible and will appear

ValueError: numpy.ufunc size changed, may indicate binary incompatibility. Expected 124 from C header, got 112 from PyObject

Such an error report. At this point, execute pip install-U numpy to install the latest version of numpy (1.17.2 in my case), and there will be no problem.

Install PyQt5

We are going to use PyQt5 to do GUI, so to install, Raspbian comes with PyQt5, do not install.

However, because we use virtualenv, in the virtual environment, it is impossible to directly use the PyQt5 under the system path, the solution is to make a soft link to the virtual environment.

Ln-s / usr/lib/python3/dist-packages/PyQt5/ ~ / .virtualenvs/cv/lib/python3.7/site-packages/ln-s / usr/lib/python3/dist-packages/sipconfig_nd7.py ~ / .virtualenvs/cv/lib/python3.7/site-packages/ln-s / usr/lib/python3/dist-packages/sipconfig.py ~ / .virtualenvs/cv/lib/python3.7/site-packages/ln-s / usr/lib/python3/dist-packages/sip.py ~ / .virtualenvs/cv/lib/python3.7/site-packages/ln-s / usr/lib/python3/dist-packages/sip.cpython-37m-arm-linux-gnueabihf.so ~ / .virtualenvs/cv/lib/python3.7/site-packages/

Then execute from PyQt5.QtWidgets import QApplication in the python of the virtual environment cv to see if the import is working.

Install python3-opencv

Many of the operations are based on opencv, so we also need to install python3-opencv. Although opencv-python-armv71 can be found in pip for raspberry pie ARMv7 chip package, but the actual installation shows that can not find any version, no solution, direct source installation!

Install the virtualenv environment

First of all, when compiling, you need to set up the python information and install numpy (required to compile the python version of opencv). Here we use virtualenv to build the corresponding environment.

Pip3 install virtualenv virtualenvwrapper

Then edit the ~ / .bashrc file and add

# virtualenv and virtualenvwrapperexport WORKON_HOME=$HOME/.virtualenvsexport VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3source / usr/local/bin/virtualenvwrapper.sh

Then you can source ~ / .bashrc, or you can just exit the connection and re-enter.

Note that the pip3 here means python 3 version of pip, using pip directly will install version 2.7. as python 2 will come to an end on December 31, 2019, there will be no more maintenance, so we all use python 3 as the use version, which is currently 3.7.

You can then execute mkvirtualenv cv-py python3, and then a new virtual environment, cv, is automatically generated. After execution, you will enter the cv environment by default. After logging in later, if you want to enter the cv environment, you can enter it through workon cv.

Then execute pip install numpy to install the latest version of numpy.

Note that you only need to use pip to call the python 3 version, because when our virtual environment was created, we specified to use version 3.7 python.

Compile OpenCV pre-compilation preparation

Download the latest version of the OpenCV source code, which includes opencv code and contrib third-party contribution code (including many algorithms that are not in the core).

You can download the corresponding version (usually the latest version) at https://github.com/opencv/opencv/releases and https://github.com/opencv/opencv_contrib/releases respectively. Note that when downloading, because the file name is a simple version number plus suffix, please download to a different subdirectory.

After decompressing, you need to use cmake to generate Makefile, so install sudo apt install cmake first.

Since we will use all four CPU cores in our later compilation, considering that some raspberry pies have only 1G of memory, which may be insufficient, while the default swap partition is only 100MB, you can enlarge the swap partition in order to prevent memory overflow in the middle of the compilation. Now the new version of Debian doesn't actually swap partitions, but uses swap files. The exchange file can be enlarged by changing the CONF_SWAPSIZE=100 into CONF_SWAPSIZE=2048 through sudo vi / etc/dphys-swapfile. And then execute

Sudo / etc/init.d/dphys-swapfile stopsudo / etc/init.d/dphys-swapfile start

To take effect. Remember to repeat the above after the compilation is complete, changing the swap file size back to 100MB, as this is generally sufficient.

Generate Makefile

Then go to the opencv directory and execute the following command

Mkdir buildcd buildcmake-D CMAKE_BUILD_TYPE=RELEASE\-D CMAKE_INSTALL_PREFIX=/usr/local\-D OPENCV_EXTRA_MODULES_PATH=../../contrib/opencv_contrib-4.1.1/modules/\-D ENABLE_NEON=ON-D ENABLE_VFPV3=ON\-D BUILD_TESTS=OFF-D OPENCV_ENABLE_NONFREE=ON\-D INSTALL_PYTHON_EXAMPLES=OFF-D BUILD_EXAMPLES=OFF.

The path to OPENCV_EXTRA_MODULES_PATH, which points to the modules directory of contrib. Please set it according to your own directory structure. Note that since version 4.1.1 has a bug, you need to modify the modules/python3/CMakeFiles/opencv_python3.dir/link.txt file in the build directory by changing the beginning of / usr/bin/c++-fPIC-fsigned-char-W-Wall to / usr/bin/c++-fPIC-latomic-fsigned-char-W-Wall. If you want the OpenCV version of python 2.7 to work with import, you need to modify the modules/python2/CMakeFiles/opencv_python2.dir/link.txt file in the same way

Compile and install OpenCV

After normal execution, a series of Makefile files are generated, and then you can compile and install OpenCV by doing the following.

Make-j4sudo make installsudo ldconfig

After this installation, the system automatically installs the current default system version (usually python 2.7), so you also need to install the python3 package manually. My approach here is to assume that the previous version of the sudo make install installation is 2.7, so the installation is shown here to version 3.7.

Sudo mkdir-p / usr/local/lib/python3.7/dist-packages/cv2/python-3.7sudo ln-s / usr/local/lib/python3.7/site-packages/cv2/python-3.7/cv2.cpython-37m-arm-linux-gnueabihf.so / usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.so

In this way, you can detect whether the installation is successful using the following command

Python2-c "import cv2" python3-c "import cv2"

If there is no error message, the installation is successful.

Next, we will install it into the previous cv environment. Execute the following command: ln-s / usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.so ~ / .virtualenvs/cv/lib/python3.7/site-packages/cv2.so

You can then use the workon cv command to switch to virtualenv and execute python-c "import cv2". If no error is reported, the installation is successful.

This is how to install mxnet in the raspberry pie 4B shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report