Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get Python bindings that reference the installed version of OpenCV? #7

Open
monocongo opened this issue May 20, 2019 · 2 comments

Comments

@monocongo
Copy link

Thanks for providing these scripts. I was able to complete the build/installation of OpenCV v4.0 today using the Jetson Nano script. However, the Python bindings accessible by the base Python installations appear to reference a previous version (3.2) rather than the OpenCV version (4.0) installed by the script from this repository:

$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'

$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'

When I launch a virtual environment the OpenCV bindings aren't present at all, and I can't seem to install them via pip:

$ workon cvtest
(cvtest) $ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'
>>> 
(cvtest) $ pip install opencv-python
Collecting opencv-python
  ERROR: Could not find a version that satisfies the requirement opencv-python (from versions: none)
ERROR: No matching distribution found for opencv-python

The bindings are installed (I think) by the final line of the script:

sudo apt-get install -y python-opencv python3-opencv

However, it appears that the bindings that are installed by the commands above are for version 3.2 and bear no relation to those we built with the script from this repository:

$ sudo apt list --installed | grep opencv | grep python

python-opencv/bionic-updates,bionic-security,now 3.2.0+dfsg-4ubuntu0.1 arm64 [installed]
python3-opencv/bionic-updates,bionic-security,now 3.2.0+dfsg-4ubuntu0.1 arm64 [installed]

Can anyone advise? Thanks in advance for any help. BTW I'm new at this, my apologies for dumb/naive questions...

@monocongo
Copy link
Author

I may have worked this out:

Add the OpenCV Python bindings into the virtual environment by creating a link to the OpenCV Python module within the virtual environment's site-packages directory. For example if the OpenCV module was built and installed as /usr/local/python/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so and the virtual environment is named nano then we'll create the link like so:

$ ln -s /usr/local/python/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so  ~/.virtualenvs/cvtest/lib/python3.6/site-packages/cv2.so

Rather than doing this for the individual virtual environments we can create a link to the OpenCV Python bindings within /usr/lib/python3.6 which seems to be included in the PYTHONPATH of all virtual environments:

$ sudo ln -s /usr/local/python/cv2/python-3.6/cv2.cpython-36m-aarch64-linux-gnu.so /usr/lib/python3.6/cv2.cpython-36m-aarch64-linux-gnu.so

At this point we should see the OpenCV bindings with the correct version available to the Python virtual environment:

$ workon nano
(nano) $ python
Python 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.0.0'

Is this something that should be addressed within this script, and/or does it indicate something missing from the script itself? Or am I the only one who has run across this issue?

@zhizhang77
Copy link

It seems the script reinstalled python-opencv v3.2 unintendedly. Replacing sudo apt-get install -y python-opencv python3-opencv with sudo ldconfig will resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants