Building Python source with OpenCV and OpenCV Contrib | by Ashish Bansal | Towards Data Science

Installing Python 3.7 on Ubuntu 14.0 LTS from Source
First, update the packages list and install the packages necessary to build Python3.7 from source:
First, update the packages list and install the packages necessary to build Python3.7 from source:
sudo apt updatesudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
2. Download the latest release’s source code from the Python download page using the following wget command or Download it in the downloads folder.
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tgz
3. Once the download is complete, extract the gzipped tarball:
cd Downloads/tar -xf Python-3.7.4.tgz
4. Next, navigate to the Python source directory and run the configure script which will perform a number of checks to make sure all of the dependencies on your system are present The enable -optimizations will optimize the Python binary by running multiple tests. This makes the build process slower.
cd Python-3.7.4 ./configure --enable-optimization
5. Start the Python build process using make:
make -j 8
For faster build time, modify the -j flag according to the processor. If you don't know the number of cores of your processor, you can find it by tying nproc command. The system uses 8 cores, so I am using -j 8 flags.
6. When the build is done, install the Python binaries by running the following command:
sudo make altinstall
Do not use the standard make install as it will overwrite the default system python3 binary.
7. That’s it. Python 3.7 has been installed and ready to be used. Verify it by typing:
python3.7 --version
The output will show the Python version:
Python 3.7.4
Installing OpenCV and OpenCV_contrib source with Python3.7 source
DownloadOpenCV DownloadOpenCV_contrib from Github open cv releases .
DownloadOpenCV DownloadOpenCV_contrib from Github open cv releases .
2. First, update the packages list and install the packages necessary to build OpenCV from source:
sudo apt-get install build-essential checkinstall yasmsudo apt-get install cmake git gfortran libgtk2.0-dev libgtk-3-dev pkg-config libavcodec-dev libavformat-dev libswscale-devsudo apt-get install libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev jasper libdc1394-22-devsudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-devsudo apt-get install libxvidcore-dev libx264-dev libx265-devsudo apt-get install libatlas-base-dev gfortransudo apt-get install libwebp-devsudo apt-get install cmake-curses-guisudo apt install qtcreatorsudo apt install qt5-defaultsudo apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-doc gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudiosudo apt -y install libtiff5-devsudo apt -y install libxine2-devsudo apt -y install libfaac-dev libmp3lame-dev libtheora-devsudo apt -y install libvorbis-devsudo apt -y install libopencore-amrnb-dev libopencore-amrwb-devsudo apt -y install libavresample-devsudo apt -y install x264 v4l-utilssudo apt -y install libprotobuf-dev protobuf-compilersudo apt -y install libgoogle-glog-dev libgflags-devsudo apt -y install libgphoto2-dev libeigen3-dev libhdf5-dev doxygensudo apt-get install libqt5x11extras5-dev qttools5-devsudo apt-get install openexr libopenexr-devsudo apt-get install libx11-dev libboost-python-dev
3. Go to the Downloads and unzip the OpenCV and OpenCV_contrib modules.
cd Downloads/tar -xf opencv-4.1.2.tar.gztar -xf opencv_contrib-4.1.2.tar.gz
4. Create a new folder named Development to have a virtual python environment.
cd ..mkdir Developmentcd Developmentpython3.7 -m venv OpenCV_Python3.7
Now check whether your Python Virtual environment has been created or not. Go to OpenCV_Python3.7/bin and look for activate
5. Activate the Python virtual environment
cdsource /home/anshu/Development/OpenCV_Python3.7/bin/activatepip install numpysudo apt-get update
cd Downloads/cd opencv-4.1.2/mkdir buildcd buildccmake ..
press enter and enter
OPENCV_EXTRA_MODULES_PATH = /home/anshu/Downloads/opencv_contrib-4.1.2/modules
press up arrow and down arrow to navigate through page
press c to configure
press g to generate and exit
make -j4 sudo make install
Now OpenCV with OpenCV_contrib modules has been successfully installed .Go to the /Downloads/opencv-4.1.2/build/lib/python3/ and look for cv2.cpython-37m-x86_64-linux-gnu.so file . Copy the .so file and paste it in /Home/Development/OpenCV_Python3.7/lib/python3.7/site_packages/ and rename it to cv2.so .
PyCharm Installation
PyCharm is now also available as a snap package. If you’re on Ubuntu 16.04 or later, you can install PyCharm from the command line:
sudo apt-get install snapsudo snap install pycharm-community — classic
Open your Pycharm and create a new project and set the path of the interpreter to your OpenCV_Python3.7 virtual environment
Now in the new Project create a test.py and test openCV library. Trying “TrackerMedianFlow”, which is missing from the main OpenCV build is now available.
Follow me on my Youtube Channel
https://www.youtube.com/channel/UCSp0BoeXI_EK2W0GzG7TxEw
Connect with me here:
Linkedin: https://www.linkedin.com/in/ashishban...
Github: https://github.com/Ashishb21
Medium: https://medium.com/@ashishb21
Website: http://techplanetai.com/
Email : ashishb21@gmail.com , techplanetai@gmail.com