Raspberry Pi-python3.11 encountered problems during the opencv installation process

Follow this article to carry out the detailed process of installing opencv-python on Raspberry Pi 4b.

Method 1: Compile and install, which will encounter many problems. I have installed and passed it once before, but after the python version became higher, I encountered many problems. There is currently no solution, and a solution is given for reference.

The first problem encountered when configuring cmake in 4.

This code will display the following error:

Solution: (Add two .. at the end)

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/astray/opencv_contrib-4.1.0/modules \#This line needs to be modified according to the path of your own installation
-D BUILD_EXAMPLES=ON \
-D WITH_LIBV4L=ON \
-D PYTHON3_EXECUTABLE=/usr/bin/python3.7 \
-D PYTHON_INCLUDE_DIR=/usr/include/python3.7 \
-D PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so \
-D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
.. ####These two points are crucial! ! ! ! 

You can continue after that, and this is the second problem you encountered.

CMake Error at cmake/OpenCVCompilerOptions.cmake:477 (else):
Flow control statements are not properly nested.
Call Stack (most recent call first):
CMakeLists.txt:692 (include)

Solution: Refer to this article https://blog.csdn.net/qq_57061492/article/details/126171010

Open cmake/OpenCVCompilerOptimizations.cmake in the opencv folder (remember sudo), find line 477 and comment out that section of code.

If the configuration is successful, it will display:

Continue to make, and the third problem occurs:

c++: error: unrecognized command-line option –param=ipcp-unit-growth=100000’; did you mean –param=ipa-cp-unit-growth=’?

Solution: Refer to https://blog.csdn.net/jiugeshao/article/details/121522829

Modify the files in the prompt directory and replace ‘ipcp-unit-growth’ with ‘ipa-cp-unit-growth’

Subsequently, when compiling the old version of opencv with the new version of ffmeg/libav, errors occurred in which many functions were removed and modified. It was too troublesome to modify, so I gave up. . .

Method 2: (Finally completed the recording installation process on 2023.10.16)

For the problem that the Raspberry Pi terminal cannot directly pip install

Create a virtual environment

python3 -m venv test_env
source ./test_env/bin/activate //Activate virtual environment

Install dependent libraries: piwheels – opencv-python, https://www.piwheels.org/project/opencv-contrib-python/

sudo pip3 install numpy
sudo apt-get install build-essential git cmake pkg-config -y
sudo apt install libwebpmux3 libssl3 libva-drm2 libxfixes3
sudo apt install libcairo-gobject2 libvpx7 libvorbisenc2 libxcb-shm0 libxcb-render0
sudo apt install libwayland-client0 libhwy1 libicu72
sudo apt install libjbig0 libaom3 libwebp7 libxdamage1 libxcb-dri3-0
sudo apt install libsvtav1enc1 libfreetype6 libfribidi0 libsrt1.5-gnutls
sudo apt install libgcrypt20 libcairo2 libavutil57 librabbitmq4 libepoxy0
sudo apt install libxrender1 libidn2-0 libsystemd0 libpango-1.0-0 libatk1.0-0 libzvbi0
sudo apt install libcodec2-1.0 libvdpau1 libavformat59 libchromaprint1 libdatrie1
sudo apt install libxcursor1 libatk-bridge2.0-0 libharfbuzz0b libp11-kit0
sudo apt install libxvidcore4 libglib2.0-0 libogg0 libnorm1
sudo apt install libtheora0 libmp3lame0 libx265-199 libsnappy1v5 libgme0 libopenmpt0
sudo apt install libmpg123-0 libgfortran5 libxinerama1 libxdmcp6
sudo apt install libxrandr2 libx11-xcb1 libxext6 libbluray2 libstdc++6 libdrm2 libva2
sudo apt install libatspi2.0-0 libswscale6 ocl-icd-libopencl1 libva-x11-2
sudo apt install libpgm-5.3-0 libxcb1 libk5crypto3 liblz4-1
sudo apt install libnettle8 libmd0 libjpeg62-turbo libgomp1 libsodium23 libwayland-egl1
sudo apt install librist4 libudfread0 libxkbcommon0 libtiff6 libatomic1
sudo apt install libssh-gcrypt-4 libhogweed6 libxml2 libthai0 librsvg2-2
sudo apt install libspeex1 libswresample4 libsoxr0 libfontconfig1 libgdk-pixbuf-2.0-0
sudo apt install libmount1 libffi8 libtasn1-6 libx264-164 libkrb5-3 libzmq5 libzstd1
sudo apt install libxcomposite1 libgnutls30 libpixman-1-0 libgraphite2-3 libgtk-3-0
sudo apt install libwayland-cursor0 libshine3 libtwolame0
sudo apt install libpng16-16 libgssapi-krb5-2 libdav1d6 libgmp10
sudo apt install libpangocairo-1.0-0 libpcre2-8-0 libgsm1 libavcodec59 libxau6
sudo apt install libvorbisfile3 libjxl0.7 libunistring2 libatlas3-base
sudo apt install liblerc4 libopus0 libcjson1 libmbedcrypto7 libbrotli1 libxi6
sudo apt install libpangoft2-1.0-0 liblcms2-2 libblkid1 libdeflate0
sudo apt install libopenjp2-7 libvorbis0a libkrb5support0 librav1e0 libbsd0 libx11-6

Install opencv, opencv-contrib-python

pip3 install opencv-python
pip3 install opencv-contrib-python

It takes a long, long time to succeed. (I’m sorry, the installation was successful and I can no longer use the server normally…)

After checking the reason, it was still a version incompatibility issue. The latest version installed was unstable.

Try to downgrade python and follow gpt exactly.

sudo apt-get update
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
curl https://pyenv.run | bash
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
source ~/.bashrc

That doesn’t work. . Let me write another article, finally the installation was successful.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. OpenCV skill tree Home page Overview 23386 people are learning the system