Cannot build opencv for python3.9

Hi, I am here to get some information I am missing when I want to build opencv with CUDA for python3.9
The point is not about building with CUDA, but for python3.9

I installed Jetpack 4.6.1 on my Jetson Nano b01 in which native python3 is 3.6.9 under ubuntu 18.04.
I want to discover object detection and starting using yolov5 at a first time. This one need python>=3.8 so I have installed 3.9 with update-alternatives.

I have python2.7, python3.6 and python3.9 on my machine.
I have used Mr. @mdegans nano_build_opencv.sh to build opencv, however, it has built it for python2.
I used these cmake options :

-D BUILD_opencv_python3=ON
-D BUILD_opencv_python2=OFF
-D PYTHON_DEFAULT_EXECUTABLE=/usr/bin/python3
-D HAVE_opencv_python3=ON

I always have this result :

cv2

I have noted 2 points :

  1. I use /usr/bin/python3 in the PYTHON_DEFAULT_EXECUTABLE cmake option which links to /erc/alternatives/python3 as you can see below, should I use /usr/bin/python3.9 ? I have not tried with this one I was thinking that using /etc/alternatives/python3 would be useful if I want to upgrade python to 3.10 for example.

  1. In the configure.log file, I can read :

Python 3:
– Interpreter: /usr/bin/python3 (ver 3.9.12)
– Libraries:
– numpy: NO (Python3 wrappers can not be generated)
– install path:

– Python (for build): /usr/bin/python3

There is no install path.

Here are the logs :
build.log (151.8 KB)
configure.log (28.3 KB)
install.log (80.8 KB)

I have read the following topics before posting, without success :

I also have read this Install OpenCV 4.5 on Jetson Nano and AastaNV GitHub but didn’t tried because it looks similar the way I did.

I have seen the solution to use the docker image NVIDIA L4T ML, which I will try, but I would understand what is wrong in the way I am doing things (I guess the tricky point is the python3.9 location).

Thanks for help,
Dodo

This method works for me —> Install OpenCV 4.5 on Jetson Nano - Q-engineering
I Just installed it again … today for use with yolov5

$ python3 -c 'import cv2 as cv; print(cv.__version__)'

opencv

I did this for python version 3.6.x but it might point you in the right direction.

your cmake look at
-D PYTHON3_PACKAGES_PATH=/usr/lib/python3/dist-packages
and make sure you are using the link to python3.9

question … i’m using yolov5 and openCV with python3.6.x no issue. why do you need python >= 3.8.x ???

Thank you for your answer,

On the official GitHub it is mentionned that python >= 3.7 need to be used, moreover, I remember I already had issue trying to install yolov5 depencies which required a python >= 3.8. Sorry I can’t point out the precise issue I had but I think it was because of tensorboard or pytorch.

Whatever, the opencv-python downloaded by yolov5 is not built with CUDA and I want to build it with. I am going to try with this flag on python3.9 now that it is installed but you made me skeptical with the fact it works for you with 3.6 and I think I will retry with this version too. At least I will gain skills building from source.

Thanks for the link to Github ultralytics/yolov5. Thats what I used without issue! used python 3.6.x
Here are my notes

install dependencys
$ sudo -H pip3 install onnx

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
remove the opencv-python>=4.1.2 from the requierments.txt ( we have the full openCV version installed )
$ sudo -H pip3 install -r requirements.txt

However I’m in the middle of a giant build make athon after I found

Oh its been a rabbit hole for sure but I just got it working and well its fast

I’m going to install python 3.9.x now and build it all from the start again. I’ll let you know what I run into. I’ll post my notes here as we are both doing the same thing with yolov5 :)

UPDATE (07:34 EST)

Success!
python 3.9.x with openCV 4.5.5
p3.9ocv

I forgot about not wanting jetson’s python2 to be updated. I’ll fix that soon :( I’m putting together a howto now and making a github page on this. As I move forward with pytorch 1.9.0 and torchvision 0.10.0 under python3.9.x

Stand by; hope to have you something by 5pm EST…

Hey I may have found your issue with openCV
can you type this into your terminal?

python3 -c "from distutils.sysconfig import *; print(get_python_lib())"

It should link to your python3.9
that is the location for: -D PYTHON3_PACKAGES_PATH=

Good news for you !! Have you followed the steps from Q-engineering to success ?

For my part, I get this location with the command you mentioned :
/usr/lib/python3.9/site-packages
Nevertheless, I don’t have this directory in /usr/lib/python3.9
This one is located in /home/nvidia/.local/lib/python3.9/site-packages
:
I tried to build opencv with this two locations for PYTHON3_PACKAGES_PATH without success.

I also tried to build opencv with CUDA for python3.6 in order to try yolov5 with this python version as you achieved, but I get the following

>>> print(cv2.cuda.getCudaEnabledDeviceCount())
0
>>> cv2.cuda.getDevice()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/core/include/opencv2/core/private.cuda.hpp:107: error: (-216:No CUDA support) The library is compiled without CUDA support in function 'throw_no_cuda'

Your HowTo will be welcome :) I will try again building with the Q-engineering steps and then if it doesn’t work too, uninstalling python3.9 and try for python3.6 again until I achieve

I got it to work VIA my notes. Then I made the howto above. Now on a Fresh install of jetson 4.6.1 and am following the howto I made step by step.

Currently in make for openCV 4.5.5.

Ill know if it works right when thats done.

I recommend at this point that you make a fresh install of jetpack 4.6.1 and start clean.
I make clones of my sd card as known good stop points for this kind of work. see —> Create an image of my SD-CARD so someone else can reuse it - #2 by meeki007

Do check out the python3.9.X install notes as im sure its not what you used/did. I ran into a few issues installing it where it would install fine but openCV would not see it or complain about a library. But I got it working with this method.

Goal is to also cover installing Torch, TorchVision, and then yolov5
You can’t use the pywheels built with other python versions so you have to build/make

The one thing I’m dreading having to build/make again is ONNX Runtime. needed for sparseml’s yolov5 … man that was a pain to get built right in python3.6.x I’m hoping its a bit easier or the same as my notes for python 3.6.x

1 Like

Tested working!

Its behaving just the way I want it to based on the howto I made.
I just finished updating the howto’s last steps.

It does take a while the first time you import cv2 but after that its fine.

Hope this helps you.

It works !!
I reinstalled python3.9 using your HowTo, it locates it in /usr/local/bin/ directory (instead of /home/nvidia/.local in my previous configuration)
Then I installed opencv following your HowTo, it first build it for python3.6 with something like below in cmake :

Python 3:
--     Interpreter:                 /usr/local/bin/python3.9 (**ver 3.6.9**)

Trying, it built it for python3.6
I removed the /opencv/build directory to clean it and running cmake & make did the job !

For my part, I used these flags for the cmake :

-D CMAKE_INSTALL_PREFIX=/usr/local 
-D PYTHON3_EXECUTABLE=/usr/local/bin/python3.9 
-D BUILD_opencv_python3=ON 
-D BUILD_opencv_python2=OFF 
-D PYTHON3_PACKAGES_PATH=$(python3.9 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") 

I will make a bash files with your instructions and post it, if you are ok, to help anyone which could read this post

Here are the files :

enlargeMemorySwap.sh (384 Bytes)
installOpencv455.sh (4.2 KB)
installPython39.sh (1.7 KB)

Thank you again for your time. I guess I misused @mdegans script by providing bad python3.9 location too, however, it works now.

Have a nice day !

1 Like

Working on PyTorch python3.9.x documentation now…

It takes some time to build the wheel

*** Update - Added PyTorch and Torchvision install for python3.9.x or other python version of your choice. ***

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.