How can the Python bindings for OpenCV 2 be installed in Ubuntu 18.04 LTS?

asked 2020-02-03 09:43:45 -0600

Hey there. I need to run a legacy OpenCV 2 Python program in Ubuntu 18.04 LTS. I have successfully compiled and installed OpenCV 2 and now need to install the Python bindings for it. The installation procedure was like the following:

sudo apt update
sudo apt install   \
    libasound-dev  \
    python-dev     \
    python-pyaudio \
    portaudio19-dev\
    python-tk

sudo apt install                    \
    build-essential                 \
    cmake                           \
    libgtk2.0-dev                   \
    pkg-config                      \
    python-dev                      \
    checkinstall                    \
    libavcodec-dev                  \
    libavcodec-dev                  \
    libavformat-dev                 \
    libavformat-dev                 \
    libdc1394-22-dev                \
    libgstreamer1.0-dev             \
    libgstreamer-plugins-base1.0-dev\
    libgtk2.0-dev                   \
    libjasper-dev                   \
    libjpeg-dev                     \
    libjpeg-dev                     \
    libmp3lame-dev                  \
    libopencore-amrnb-dev           \
    libopencore-amrwb-dev           \
    libopencv-dev                   \
    libpng-dev                      \
    libqt4-dev                      \
    libswscale-dev                  \
    libswscale-dev                  \
    libtbb-dev                      \
    libtheora-dev                   \
    libtiff5-dev                    \
    libv4l-dev                      \
    libvorbis-dev                   \
    libxine2                        \
    libxvidcore-dev                 \
    python-dev                      \
    python-numpy                    \
    v4l-utils                       \
    x264                            \
    yasm

wget https://github.com/opencv/opencv/archive/2.4.13.5.zip -O opencv-2.4.13.5.zip
unzip opencv-2.4.13.5.zip
cd opencv-2.4.13.5
mkdir release
cd release
cmake                                \
    -G "Unix Makefiles"              \
    -DCMAKE_CXX_COMPILER=/usr/bin/g++\
    CMAKE_C_COMPILER=/usr/bin/gcc    \
    -DCMAKE_BUILD_TYPE=RELEASE       \
    -DCMAKE_INSTALL_PREFIX=/usr/local\
    -DWITH_TBB=ON                    \
    -DBUILD_NEW_PYTHON_SUPPORT=ON    \
    -DWITH_V4L=ON                    \
    -DINSTALL_C_EXAMPLES=ON          \
    -DINSTALL_PYTHON_EXAMPLES=ON     \
    -DBUILD_EXAMPLES=ON              \
    -DWITH_QT=ON                     \
    -DWITH_OPENGL=ON                 \
    -DBUILD_FAT_JAVA_LIB=ON          \
    -DINSTALL_TO_MANGLED_PATHS=ON    \
    -DINSTALL_CREATE_DISTRIB=ON      \
    -DINSTALL_TESTS=ON               \
    -DENABLE_FAST_MATH=ON            \
    -DWITH_IMAGEIO=ON                \
    -DBUILD_SHARED_LIBS=OFF          \
    -DWITH_GSTREAMER=ON ..
make all -j"$(nproc)"
sudo make install

Now, the next step of installing the Python bindings on Ubuntu 16.04 was as follows:

sudo apt install python-opencv

For 16.04, we can see that this installs bindings with the version 2.4.9.1 stated here. However, for 18.04, we can see that the version stated here is 3.2.0, which is not appropriate.

How can the appropriate Python bindings be installed in 18.04?

If I attempt to install python-opencv_2.4.9.1+dfsg-1.5ubuntu1.1_amd64.deb manually on 18.04, I run into problems that I'm not sure how to address:

sudo dpkg -i python-opencv_2.4.9.1+dfsg-1.5ubuntu1.1_amd64.deb 
Selecting previously unselected package python-opencv.
(Reading database ... 592704 files and directories currently installed.)
Preparing to unpack python-opencv_2.4.9.1+dfsg-1.5ubuntu1.1_amd64.deb ...
Unpacking python-opencv (2.4.9.1+dfsg-1.5ubuntu1.1) ...
dpkg: dependency problems prevent configuration of python-opencv:
 python-opencv depends on libopencv-calib3d2.4v5; however:
  Package libopencv-calib3d2.4v5 is not installed.
 python-opencv depends on libopencv-contrib2.4v5; however:
  Package libopencv-contrib2.4v5 is not installed.
 python-opencv depends on libopencv-core2.4v5; however:
  Package libopencv-core2.4v5 is not installed.
 python-opencv depends on libopencv-features2d2.4v5; however:
  Package libopencv-features2d2.4v5 is not installed.
 python-opencv depends on libopencv-flann2.4v5; however:
  Package libopencv-flann2.4v5 is not installed.
 python-opencv depends on libopencv-highgui2.4v5; however:
  Package libopencv-highgui2.4v5 is not installed.
 python-opencv depends on libopencv-imgproc2.4v5; however:
  Package libopencv-imgproc2.4v5 is not installed.
 python-opencv depends on libopencv-legacy2.4v5; however:
  Package libopencv-legacy2.4v5 is not installed.
 python-opencv depends on libopencv-ml2.4v5; however:
  Package libopencv-ml2.4v5 is not installed.
 python-opencv depends on libopencv-objdetect2.4v5; however:
  Package libopencv-objdetect2.4v5 is not installed.
 python-opencv depends on libopencv-photo2.4v5; however:
  Package libopencv-photo2.4v5 is not installed.
 python-opencv depends on libopencv-video2.4v5; however:
  Package libopencv-video2.4v5 is not installed.

dpkg: error processing package python-opencv (--install):
 dependency problems - leaving unconfigured
Errors were encountered while processing:
 python-opencv
edit retag flag offensive close merge delete