Installing OpenCV in PiZero W. Most of us have faced issue in... | by Atul Singh | Towards Data Science

Most of us have faced issue in installing openCV in Pi.In this blog I will tell you step by step installation of opencv in your pi-zero W.
While the Pi Zero isn’t quite fast enough for advanced video processing, it’s still a great tool that you can use to learn the basics of computer vision and OpenCV. I recomend you following this blog during night time as the compilation of OpenCV alone will takes 12+ hours.
Lets Start....
1> pi Zero W Hardware
2> SD card (minimum 16 GB )
3> Any Raspbian OS (I have used Raspbian Buster) installed on your pi zero
Assuming you have the given requirements lets start with the setup.
Expand filesystem is rarely needed anymore as both NOOBs and the standalone install of Raspbian will automatically expand the filesystem on first boot. But just to make sure that the system is expanded run the following command:
pi@raspberrypi:~ $ raspi-config --expand-rootfs
Swap space is a portion of a hard disk drive (HDD) that is used for virtual memory. Having a swap file allows your computer’s operating system to pretend that you have more RAM than you actually do. This will increase up the compiling process of openCV. Otherwise you end up with memory exhausted error.
To increase the Swapsize open the swap file of your pi zero using the following command:
pi@raspberrypi:~ $ sudo nano /etc/dphys-swapfile
Go to Swap size and change it to 2048 from 100. as show below.
...# where we want the swapfile to be, this is the default#CONF_SWAPFILE=/var/swap# set size to absolute value, leaving empty (default) then uses computed value#   you most likely don't want this, unless you have an special disk situation#CONF_SWAPSIZE=100CONF_SWAPSIZE=2048...
Reboot your system.
pi@raspberrypi:~ $ sudo reboot
First lets update & upgrade existing packages:
pi@raspberrypi:~ $ sudo apt-get updatepi@raspberrypi:~ $ sudo apt-get upgrade
If you are using Raspbian Buster run the following command:
pi@raspberrypi:~ $sudo apt updatepi@raspberrypi:~ $sudo apt upgrade
Enter “Y” when ever it prompts.
Install developer tools:
pi@raspberrypi:~ $ sudo apt-get install build-essential cmake pkg-config
Install the IO packages:
pi@raspberrypi:~ $ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev
Along with some video I/O packages (although it’s unlikely that you’ll be doing a lot of video processing with the Raspberry Pi Zero):
pi@raspberrypi:~ $ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-devpi@raspberrypi:~ $ sudo apt-get install libxvidcore-dev libx264-dev
We’ll need to install the GTK development library for OpenCV’s GUI interface:
pi@raspberrypi:~ $ sudo apt-get install libgtk2.0-dev
And routine optimization packages leveraged by OpenCV:
pi@raspberrypi:~ $sudo apt-get install libatlas-base-dev gfortran
So Now we have all the dependencies. Lets pull the release of opencv from OpenCV github.
Note that I have pulled release version 3.4.1. You can try it for other version as well. Just replace the release version number.
pi@raspberrypi:~ $cd ~pi@raspberrypi:~ $ wget -O opencv.zip https://github.com/Itseez/opencv/archive/3.4.1.zippi@raspberrypi:~ $unzip opencv.zip
Along with it lets grab the opencv Contrib because SIFT and SURF have been removed from the default install of OpenCV:
pi@raspberrypi:~ $ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.4.1.zippi@raspberrypi:~ $ unzip opencv_contrib.zip
Once both of the repository are downloaded and expanded in your system It is a better idea to remove them so as to free some space.
pi@raspberrypi:~ $ rm opencv.zip opencv_contrib.zip
If you already have python2.7 installed in your system you can skip this step else install the Python 2.7 headers so wen can compile our OpenCV + Python bindings:
pi@raspberrypi:~ $ sudo apt-get install python2.7-dev
After which install pip , a python package manager.
pi@raspberrypi:~ $ wget https://bootstrap.pypa.io/get-pip.pypi@raspberrypi:~ $ sudo python get-pip.py
The only requirement to build Python + OpenCV bindings is to have NumPy installed, so install NumPy using pip:
pi@raspberrypi:~ $ pip install numpy
Now We are ready to compile and install OpenCV.
Build the files:
pi@raspberrypi:~ $ cd ~/opencv-3.4.1/pi@raspberrypi:~ $ mkdir buildpi@raspberrypi:~ $ cd buildpi@raspberrypi:~ $ 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=~/opencv_contrib-3.0.0/modules \    -D BUILD_EXAMPLES=ON ..
Now Its time to compile. Remember Compiling it self may take more than 9+ hours and there may be points when you will think that the system has freezed. But dont loose patience and let the system work till the time you dont see any error.
pi@raspberrypi:~$ make
Assuming your file compiled successfully without any error will Now install opencv using the following command.
pi@raspberrypi:~$ sudo make installpi@raspberrypi:~$ sudo ldconfig
Check the path for cv2.so file :
find / -name “cv2.so”
pi@raspberrypi:~$ /usr/local/python/cv2/python-2.7/cv2.so
All you have to do now is to sym-link the cv2.so file(binding file) into the site-packages of python lib.
pi@raspberrypi:~$ cd /usr/local/lib/python2.7/site-packages
Run the following commnad: ln -s [path to cv2.so file] cv2.so. In my case it is as follow:
pi@raspberrypi:~$/usr/local/lib/python2.7/site-packages $ ln -s /usr/local/python/cv2/python-2.7/cv2.so cv2.so
Now is the time to Verify our openCV installation.
fire up a Python shell and import the OpenCV bindings:
pi@raspberrypi:/usr/local/lib/python2.7/site-packages$ pythonimport cv2>>> cv2.__version__3.4.1’
Thats It Now you have a freshly install openCV installed on your system. Now export the path python can access it from anywhere.
pi@raspberrypi:~$ export PYTHONPATH=/usr/local/lib/python2.7/site-packages:$PYTHONPATH
You can now remove both the opencv-3.4.1 and opencv_contrib-3.4.1 directories, freeing up a bunch of space on your filesystem:
But be cautious before you run this command! Make sure OpenCV has been properly installed on your system before blowing away these directories, otherwise you will have to start the (long, 12+ hour) compile all over again!
If you encounter any error let me know in the comment section.
If you want OpenCV for python3 then you can simply use sym-link to point the binding file to your python3 site-package and export the path