XANA
XANA

Reputation: 41

How to link python3 to a specific version of opencv?

I successfully compiled and installed OpenCV 4.5.0, but when I use python3 to import cv2, I find that the version of cv2 is 4.5.1. Like the following output:

Python 3.6.9 (default, Oct  8 2020, 12:12:24) 
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.getBuildInformation())

General configuration for OpenCV 4.5.1 =====================================
  Version control:               4.5.1-dirty

  Platform:
    Timestamp:                   2021-01-02T12:47:39Z
    Host:                        Linux 5.4.0-54-generic aarch64
    CMake:                       3.18.4
    CMake generator:             Unix Makefiles
    CMake build tool:            /bin/gmake
    Configuration:               Release

 .........

  Python 3:
    Interpreter:                 /opt/python/cp36-cp36m/bin/python (ver 3.6.12)
    Libraries:                   libpython3.6m.a (ver 3.6.12)
    numpy:                       /tmp/pip-build-env-c03ewnak/overlay/lib/python3.6/site-packages/numpy/core/include (ver 1.19.3)
    install path:                python

  Python (for build):            /bin/python2.7

  Install to:                    /tmp/pip-req-build-zuuo394f/_skbuild/linux-aarch64-3.6/cmake-install
-----------------------------------------------------------------

When I open /tmp, I can't find folder /pip-req-build-zuuo394f.

But when I use python(2.7.17), I get the right version of opencv:

Python 2.7.17 (default, Sep 30 2020, 13:38:04) 
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> print(cv2.getBuildInformation())

General configuration for OpenCV 4.5.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            /home/xana/opencv_contrib/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2021-04-07T04:04:29Z
    Host:                        Linux 4.9.140-tegra aarch64
    CMake:                       3.10.2
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               RELEASE

.......

  cuDNN:                         YES (ver 8.0.0)

  Python 2:
    Interpreter:                 /usr/bin/python2.7 (ver 2.7.17)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython2.7.so (ver 2.7.17)
    numpy:                       /usr/lib/python2.7/dist-packages/numpy/core/include (ver 1.13.3)
    install path:                lib/python2.7/dist-packages/cv2/python-2.7

  Python 3:
    Interpreter:                 /usr/bin/python3 (ver 3.6.9)
    Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.6m.so (ver 3.6.9)
    numpy:                       /usr/lib/python3/dist-packages/numpy/core/include (ver 1.13.3)
    install path:                lib/python3.6/dist-packages/cv2/python-3.6

  Python (for build):            /usr/bin/python2.7

  Install to:                    /usr
-----------------------------------------------------------------

And in python2's cv2, all functions in cv2 worked. So how can I change the cv2 version in python3? Thanks a lot!

Upvotes: 1

Views: 1106

Answers (1)

XANA
XANA

Reputation: 41

I follow @Peter's suggestion print(cv2). I find a cv2.so in /home/.local/lib/python3.6/site-packages/cv2 folder, but my OpenCV 4.5.0 was installed in usr/lib/python3.6/dist-packages/cv2.

Then, I removed the cv2 folder in /home/..., and I import cv2 again, and cv2.__version__ now is 4.5.0 and all functions worked.

Upvotes: 1

Related Questions