Skip to content

Instantly share code, notes, and snippets.

@PeterEmbedded
Last active February 19, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PeterEmbedded/f9cb3f4335b5fd387a598ded02f739d5 to your computer and use it in GitHub Desktop.
Save PeterEmbedded/f9cb3f4335b5fd387a598ded02f739d5 to your computer and use it in GitHub Desktop.
Install opencv
OR only: pip install opencv-python
- Step 1: Expanding File System
- Expanding the Filesystem is necessary so that the OpenCV library compiles properly. To expand your file system, go to the terminal and type the following command.
sudo raspi-config
- Step 2: Updating System
- Updating is necessary before proceeding with the further installation.
sudo apt-get update && sudo apt-get upgrade
- Next, we will update the apt-get package.
sudo apt-get update
- Step 3: Installing CMake
- CMake is necessary to compile the OpenCV Library. First, we will install snapd for installing the CMake package.
sudo apt install snapd
- We can now download and install the CMake package using the below command.
sudo snap install cmake --classic
- STEP doesn't work!!! Use the follwing to install cmake
pip install --upgrade cmake
- Step 4: Installing Python
- Now we will install the python 3 development headers using the below command.
sudo apt-get install python3-dev
- Step 5:Getting OpenCV packages
- First, we will download the source code package of OpenCV and compile it on our Raspberry Pi using CMake.
- The next step would be to download the OpenCV Zip file from GitHub. Use the following command to do the same.
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.0.0.zip
- OpenCV has some pre-built packages for python which will help us in developing stuff easier called the OpenCV contrib.
- So let’s also download that by using a similar command that is shown below.
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.0.0.zip
- Step 6: Installing OpenCV Packages
- Let's unzip the opencv-4.0.0 zip file using the following command.
unzip opencv.zip
- Similarly, also extract the opencv_contrib-4.0.0 using the command line
unzip opencv_contrib.zip
- Step 7: Installing NumPy
- OpenCV requires NumPy as a requirement to work. So let’s install it using the below command.
pip install numpy
- Step 8: Building Directory
- Now, we would have two directories named “opencv-4.0.0” and “opencv_contrib-4.0.0” in our home directory.
- The next step would be to compile the Open CV library, to do that we need to create a new directory called “build” inside the opencv-4.0.0 directory. Follow the below commands to do the same
cd ~/opencv-4.0.0
mkdir build
cd build
- Step 9: Instructions for Compiling
- Now, we have to run CMake for OpenCV. This is the place where we can configure how Open CV has to be compiled. Make sure you are in the path “~/opencv-4.0.0/build”. Then copy the below lines and paste in the terminal window.
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-4.0.0/modules \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D WITH_TBB=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D BUILD_EXAMPLES=OFF ..
- Step 10: Compiling OpenCV
- This would be the most time-consuming step. Again make sure you are in the path “~/opencv-4.0.0/build” and use the following command to compile OpenCV.
Make –j4
- He would start building OpenCV and you would be able to see the progress in percentage. The process would take around 30-40 Minutes and if it gets completely built, you should see a screen like this above.
- The command “make –j4” makes use of all four cores to compile OpenCV. At 99% percentage, some people might find it taking too long for the process to complete wait patiently and it should get finished.
- For me, it did not work even after waiting for 25 minutes and an error came like in the above image so I build it again using “make –j1” and it worked.
- Using make –j1 uses only a single core of pi and it would take a longer time than make j4 but it's stable, so it is recommended to use make j4 and then uses make j1 since most of the compilation would be done by make j4.
- In case your code is not compiling at 100% and it gets an error like above, try using "make -j1". Also if it gets stuck at 63% or in the middle somewhere, delete all the files from the build folder and rebuild again with proper steps.
- Step 11: Installing libopencv
- If you have reached this step then, you have sailed through the process. The final step would be to install libopecv using the following command.
sudo apt-get install libopencv-devpython-opencv
- Step 12: Testing OpenCV
- Finally, you can check if the library was added successfully by running a simple python command.
Python
import cv2
fix:
sudo apt-get install libatlas-base-dev
pip install -U numpy
@PeterEmbedded
Copy link
Author

So, it looks like I have an update on that...

I do not know why the old and the not so old version of armbian simply used the HDMI as a default display, while the other two versions fail. But it seems, that the issue is the presence of /dev/fb0 and /dev/fb1 and the missing /dv/fb. I added a new configuration file /etc/X11/xorg.conf.d/99-hdmi_fb0.conf

Section "Device"
Identifier "main"
driver "fbdev"
Option "fbdev" "/dev/fb0"
EndSection
That forces the x-server to use /dev/fb0 and the additional OLD doesn't interfere anymore.

So at least one issue is solved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment