I have been trying to install opencv on my raspberry pi zero loaded with buster. I have followed a couple of tutorials online with no success. I thought this tutorial worked until I tried to import cv2 in python and it returned "Illegal instruction" and then kicked me out of python. I opened up python again and typed "import garbage" and it returned ModuleNotFoundError: No module named 'garbage' and allowed me to stay in python. I read somewhere online that "Illegal instruction" is displayed when there is a discrepancy in the architecture. This makes sense considering the tutorial was for a raspberry pi 2,3,4 and I am trying to install this on a zero. I am pretty new to this and not certain how to fix this or if it can be fixed. Does anybody have a solution or can point me to a tutorial that was successful installing opencv on a pi zero? Thank you for your time.
2 Answers
I don't know what you have tried because I don't follow links to understand a question, but at a glance it seems that you tried to compile ``OpenCV` from source. That is not needed. Just install it from the repository with:
rpi ~$ sudo apt install python3-opencv
If you need additional modules look for them with:
rpi ~$ sudo apt list *opencv*
-
This will not work. The build in the repos is not for arm64 and will throw "Illegal operation" when cv2 is imported. On the pi zero, it seems one absolutely has to build from source. Commented Jan 4, 2022 at 18:47
I got bitten by this issue, too, and managed to solve it. The problem following the build instructions from the link is that the configuration command includes CPU instruction set optimization flags (neon, vfpv3) not supported by the slightly lesser CPU of pi zero.
Change the configuration command to
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
-D ENABLE_NEON=OFF \
-D ENABLE_VFPV3=OFF \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D CMAKE_SHARED_LINKER_FLAGS=-latomic \
-D BUILD_EXAMPLES=OFF ..
That should do the trick. Still, the build takes almost 24h. If you don't need the newest tricks from OpenCV, the solution from Ingo is the best, and I wish I had seen it before spending the days building from the source. (Installed from repository: 3.2.0, self-built: 4.4.0-pre.)