Skip to content

Instantly share code, notes, and snippets.

@Pelirrojo
Last active April 12, 2020 14:36
Show Gist options
  • Save Pelirrojo/781e2e7b854ef72b276ee3379a03fbb7 to your computer and use it in GitHub Desktop.
Save Pelirrojo/781e2e7b854ef72b276ee3379a03fbb7 to your computer and use it in GitHub Desktop.
Install OpenCV on Ubuntu 19.10 with MiniConda
#!/usr/bin/env bash
# Thanks to:
# https://linuxize.com/post/how-to-install-opencv-on-ubuntu-18-04/
# https://pydeeplearning.com/opencv/install-opencv3-with-anaconda-python3-6-on-ubuntu-18-04/`
python --version >/dev/null 2>&1 || { echo >&2 "I require python@3 environment but it's not installed. ¯\_(ツ)_/¯ Aborting."; exit 1; }
conda --version >/dev/null 2>&1 || { echo >&2 "I require conda utility but it's not installed. ¯\_(ツ)_/¯ Aborting."; exit 1; }
pip --version >/dev/null 2>&1 || { echo >&2 "I require pip packagem amaner but it's not installed. ¯\_(ツ)_/¯ Aborting."; exit 1; }
clear
echo "\nCreate a Conda environment"
conda create -n py37 python=3.7.5
conda activate py37
echo "\nInstall dependencies"
pip install numpy
pip install tesserocr
pip install pylint
pip install FLAKE8_EXECUTABLE
sudo apt -y remove x264 libx264-dev
sudo apt-get update
sudo apt-get check
sudo apt install -y \
build-essential \
cmake \
git \
pkg-config \
libgtk-3-dev \
libavcodec-dev \
libavformat-dev \
libswscale-dev \
libv4l-dev \
libxvidcore-dev \
libx264-dev \
libjpeg-dev \
libpng-dev \
libtiff-dev \
gfortran \
openexr \
libatlas-base-dev \
python3-dev \
python3-numpy \
libtbb2 \
libtbb-dev \
libdc1394-22-dev
sudo apt-get install -y \
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-pulseaudio
sudo apt-get install -y \
ccache \
libhdf5-serial-dev \
libogre-1.9-dev \
libopenblas-base \
libopenblas-dev \
tesseract-ocr \
libtesseract-dev \
libgflags-dev
echo "\nDownload and install from source"
mkdir ~/opencv_build && cd ~/opencv_build
pwd
git clone https://github.com/opencv/opencv.git
git clone https://github.com/opencv/opencv_contrib.git
cd ~/opencv_build/opencv
mkdir build && cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_GENERATE_PKGCONFIG=YES \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_build/opencv_contrib/modules \
-D PYTHON_EXECUTABLE=~/miniconda3/envs/py37/bin/python \
-D BUILD_EXAMPLES=ON ..
# run $> nproc to know your number of available processors
make -j8
sudo make install
sudo ldconfig
echo "\nUpdate module symlinks"
# SO Module compiled
cd /usr/local/lib/python3.7/site-packages/
sudo cp cv2/python-3.7/cv2.cpython-37m-x86_64-linux-gnu.so cv2.so
# Link in Conda base Python environment (optional)
#cd ~/miniconda3/lib/python3.7/site-packages
#ln -s /usr/local/lib/python3.7/site-packages/cv2.so cv2.so
# Link in Conda reciently created Python environment
cd ~/miniconda3/envs/py37/lib/python3.7/site-packages
ln -s /usr/local/lib/python3.7/site-packages/cv2.so cv2.so
echo "\nCheck installation"
pkg-config --modversion opencv4
python3 -c "import cv2; print(cv2.__version__)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment