OpenCV with CUDA in Python on Jetson

Поділитися
Вставка
  • Опубліковано 6 лис 2023
  • Building OpenCV with CUDA support is a task. Many folks get a surprise after the build when they try to run it in Python. The old version is there instead of the new! Here's how to fix it and take advantage of that GPU goodness. We run a face detection demo to make sure it works. Full article on JetsonHacks: wp.me/p7ZgI9-3Ne
    NOTE: If you use the cp command to save your build products, make sure to keep the same time stamps. For example:
    $ cp -r --preserve=timestamps build_opencv /path/to/destination
    Remember that the make utility keeps a list of the timestamps to help determine if something needs to be rebuilt. By default, cp marks copied files with the current timestamp. That means that make will rebuild everything if the timestamps are not preserved.
    This works for all models of NVIDIA Jetsons. The Jetson in the video is a NVIDIA Jetson Nano Developer Kit.
    For a Jetson Nano, you will need to modify the build_open_cv script.
    The Jetson Nano doesn't know about the 8.7 (Jetson Orin) architecture
    -D CUDA_ARCH_BIN=5.3,6.2,7.2,8.7
    changes to:
    -D CUDA_ARCH_BIN=5.3,6.2,7.2
    and change the CUDA version to match your machine. On the Jetson Nano, this is 8.2.
    -D CUDNN_VERSION='8.0'
    changes to:
    -D CUDNN_VERSION='8.2'
    Here's an earlier video that covers the build in slightly more detail: • Build OpenCV with CUDA...
    Jetson Nano Developer Kit: amzn.to/468IM8x
    As an Amazon Associate I earn from qualifying purchases.
    Visit the JetsonHacks storefront on Amazon: www.amazon.com/shop/jetsonhacks
    Visit the website at jetsonhacks.com
    Sign up for the newsletter! newsletter.jetsonhacks.com
    Github accounts: github.com/jetsonhacks
    github.com/jetsonhacksnano
    Twitter: / jetsonhacks
    Some of these links here are affiliate links. As an Amazon Associate I earn from qualifying purchases at no extra cost to you.
  • Наука та технологія

КОМЕНТАРІ • 83

  • @samham3408
    @samham3408 8 місяців тому +5

    Very excelent, learned 10 things in 8 minutes without reading a mile of wiki kak. More.... MORE!

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      Thank you for the kind words, and thanks for watching!

    • @siddharthrana512
      @siddharthrana512 4 місяці тому

      Can i do it by creating a env python=3.8?? and installed in that because my python version is 3.11 and for that version pytorch installation documentation is not able on nvidia page @@JetsonHacks

  • @viewer8221
    @viewer8221 8 місяців тому +1

    I love the way you explain concepts great video to help me with my senior project!

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      You are too kind. I appreciate your comment and thanks for watching!

  • @antoniodourado
    @antoniodourado 8 місяців тому +1

    Great video and work, as usual!

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      Thank you for the kind words, and thanks for watching!

  • @sy2532
    @sy2532 8 місяців тому +1

    Thanks Jim, exactly when I need it. I am still not sure how to figure out how to speed up inference .4 seconds per frame however, my gpu's are maxed out only getting 1 or 2 fps. just using standard Tensor model but was trying to learn how to use a TensorRT model and train it.

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      Sounds challenging! Good luck on your project.

  • @jameswho5517
    @jameswho5517 8 місяців тому +1

    Thanks for sharing!

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      You are welcome, and thanks for watching!

  • @siddharthrana512
    @siddharthrana512 5 місяців тому

    If you mean the version in the video, the opencv_zoo version has changed since OpenCV 4.5. You either need to upgrade to the current version of OpenCV, i have upgraded to 4.9.0 , but now i am getting onxx error

  • @AshokKumar-mg1wx
    @AshokKumar-mg1wx 8 місяців тому +1

    Nice work

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      Thank you for the kind words, and thanks for watching!

  • @MICKEYESS
    @MICKEYESS 6 місяців тому

    Hey Jim, great tutorial, I got it to build on Orin Nx 16gb took 1hr50. I went to try the opencv zoo demos and it looks like it's been updated to 4.9 about a week ago. I can't see the 4.8 version. Would you have a link to the 4.8 version? Thanks.

    • @JetsonHacks
      @JetsonHacks  6 місяців тому

      Thank you for the kind words. They don't do a very good job of versioning for the OpenCV zoo. You will need to roll back to an appropriate commit using git. Thanks for watching!

  • @sungminpark8803
    @sungminpark8803 7 місяців тому

    I had cuda with opencv 4.5.4 but the demo says that i need to upgrade to the latest opencv version. Do i have to rebuild the whole process (which took you 7 hours) again with 4.8.0?😢

    • @JetsonHacks
      @JetsonHacks  7 місяців тому

      If you mean the version in the video, the opencv_zoo version has changed since OpenCV 4.5. You either need to upgrade to the current version of OpenCV, or revert the opencv_zoo version back to one that ran under OpenCV 4.5. Thanks for watching!

  • @nightlifezz1619
    @nightlifezz1619 3 місяці тому

    Hi, I’ve been getting 3 fps after installing cuda. Previously i had 13-15fps when running the demo.py file. Is there any reason and solution to this problem? Thank you!

  • @bora7123
    @bora7123 2 місяці тому

    Thank you so much for the nice tutorial.
    I got into a problem during the build. It complained that my gcc vesion was 9 and stopped. Now, I fixed my gcc version to 8 and tried the build process again from the beginning. But it doesn't do that. In /tmp/build_opencv, there are two directories, opencv and opencv_contrib. But jtop shows that opencv version remained the same 4.1 with CUDA no. Any suggestions?

    • @JetsonHacks
      @JetsonHacks  2 місяці тому

      I'm not sure. Which Jetson are you on, and how did you do the make install?

    • @bora7123
      @bora7123 2 місяці тому +1

      ​@@JetsonHacks I am using Jetson Nano and I was trying to install OpenCV 4.8.0 following this tutorial using your script. Initially, I had an issue because of gcc version mismatch. But after removing gcc 9 and making gcc 8 as default, your script ran through and now I have OpenCV 4.8.0 with CUDA enabled. Thank you so much!
      But now I am facing another problem. I installed Python 3.8 before installing OpenCV 4.8.0 and set it as default: "python --version" gives 2.xx and "python3 --version" gives 3.8.0. Now I tried to follow your instruction to set path for OpenCV to link it with python 3.8.0. I executed "opencv_version -v" command. Then, it returns "Interpreter: /usr/bin/python3 (ver 3.8)
      Libraries: NO
      numpy: NO (Python3 wrappers can not be generated)
      install path: -
      "
      What should I do? I ran python3 without a problem. But install path is missing as you can see here. Then, how should I export its path to let OpenCV know? I am trying to follow your instructions from 4:45 in this video. I will greatly appreciate your help.

  • @fabianpalacios6711
    @fabianpalacios6711 2 дні тому +1

    June 2024 and works on jetson nano 4GB. But I have a separate question, why is it that when running jtop on the Jetpack item it says Jetpack: Missing?

    • @JetsonHacks
      @JetsonHacks  День тому +1

      Thanks for sharing this information. My guess is that the version of jtop needs to be updated to recognize the version you are running. Thanks for watching!

  • @paraskhosla9622
    @paraskhosla9622 2 місяці тому

    Hi thanks a lot for your video, when i did the last step to run the demo(on jetson tx2) i'm getting some error like "illegal instruction(core dumped)" . do you have any idea? I have the same version of opencv, python and jetpack.

    • @JetsonHacks
      @JetsonHacks  2 місяці тому

      No idea. Does jtop show the version of OpenCV that you built?

    • @paraskhosla9622
      @paraskhosla9622 2 місяці тому

      @@JetsonHacks Hi thanks for quick revert, Yea it shows 4.8.0 with CUDA

    • @JetsonHacks
      @JetsonHacks  2 місяці тому

      @@paraskhosla9622 You should check to see if the path you set actually points to the version of OpenCV you built.

    • @paraskhosla9622
      @paraskhosla9622 2 місяці тому +1

      @@JetsonHacks It worked when i downgrade numpy version from 1.19.5 to 1.19.4. It(illegal instruction(core dumped)) was because of numpy version. Thanks for the help and keep making video like this to help students like me.

    • @JetsonHacks
      @JetsonHacks  2 місяці тому +1

      @@paraskhosla9622 I'm glad you were able to get it to work!

  • @naufaltsanyqadamushidqi_0896
    @naufaltsanyqadamushidqi_0896 Місяць тому

    hi jim, do you know why it didnt work on my jetson? it freezed my jetson on 98% while building cuda on opencv

    • @JetsonHacks
      @JetsonHacks  Місяць тому

      It's hard to tell from your description, but it may have run out of memory.

  • @georgegu3374
    @georgegu3374 6 місяців тому

    great job explaining the build process. helps a lot.
    is there any way to build cv2 cuda for a newer version of Python say python3.10 or later? or is there anyway to import it from python3.10?
    i have python3.10 installed in pyenv on jetson nano. but i cant quit figure out how to import the built cv2 from python3.10.
    anyone can share some thoguhts? appreciate it

    • @JetsonHacks
      @JetsonHacks  6 місяців тому

      OpenCV has to be built against the version of Python that you are using.There are several flags you can set to point to which version you want to use. I don't know offhand how that plays against various virtual environments. Flags to look for are PYTHON3_EXECUTABLE, PYTHON3_PACKAGES, PYTHON3_INCLUDE_DIR. There are others, depending on your system layout. Hope this helps, and good luck on your project!

    • @georgegu3374
      @georgegu3374 6 місяців тому +1

      even though newer faster jetsons are available, but as a 5v device with cuda. it's still a good option for years to come, same reason as raspi being so popular.
      since we can't just swap a new ubuntu like pc. looking for ways to install new softwares onto these legacy os is a painful but interesting work.

    • @JetsonHacks
      @JetsonHacks  5 місяців тому

      @@georgegu3374 Great points!

  • @louieearle
    @louieearle 8 місяців тому +1

    I struggled with this, and wound up copying over the python files into the stock Python3 path. I prefer that to environmental variables, but not sure why... maybe just so every future process doesn't have some tiny table in its memory showing that I installed python files in a weird place. I hide my shame like a Boy named Sue.

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      It's because environment variables are evil. When the devil was cast out, the first thing he created was environment variables.
      About the only reason you have to pay programmers is so that they will deal with libraries. The libraries are always missing, or the version is incorrect, or are hiding somewhere in the system but no one knows where.
      Because Python is cross platform, it doesn't handle the problem well. On a Debian derived system like Ubuntu, locally installed Python packages are usually kept in a site-packages directory. If you install it using sudo, it goes into a dist-packages directory. Python packages installed using apt will install in /usr
      A locally installed version (when you build your own or use a Python package manager like pip) is kept in /usr/local by convention. Some people will store them in a hidden .local directory if it's only for a given user.
      Python doesn't know about any of this. Any sane person does not want to know about this. The only way to get programmers to deal with it is to pay them.

    • @louieearle
      @louieearle 8 місяців тому +1

      @JetsonHacks
      Sounds like I need to use venv more regularly. I use it every so often when I know some project has ntpicky python requirements, but not always. Does venv "just solve" this problem of python files being all over the place, or are there yet more hidden demons?

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      ​@@louieearle You made me laugh. More hidden demons of course! I wrote an article about some of the challenges of the regular installs: wp.me/p7ZgI9-3Ne Python virtual environments are useful in isolating different installations or builds of related modules. While the techniques vary depending on the virtual environment in use, the idea is that everything that you build or install goes into a separate area instead of the usual system area. The area is usually a hidden local directory, with the name being the same as what you name the environment. That's all well and good, but it doesn't automagically fix the issues of finding where the libraries are located. You can imagine building OpenCV in a virtual environment, and not being able to use it in another project because it's in a different virtual environment. Typically you want Python packages that you think of as system assets in the normal system area.
      With that said, virtual environments are very useful. You can have very different configurations on the same machine. OpenCV is a little bit outside of most projects as it's feels more like a system library than an application library. It might be better to build a OpenCV wheel (perhaps using a Docker image) and then install it where you need it.Thanks for watching!

  • @KILLDOZE-
    @KILLDOZE- 8 місяців тому

    how can i install this on virtual environment python3.8.
    It installed on python 2.7 instead of python3.8.
    "-D BUILD_opencv_python3=ON" command doesn't seem to work.

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      Why do you say "It installed on python 2.7 instead of python3.8. " ?
      When you run
      $ opencv_version -v
      what does the Python3 section say. What does the "Install path" say?
      Which virtual environment are you using?

    • @KILLDOZE-
      @KILLDOZE- 8 місяців тому

      @@JetsonHacks Thank you for your reply.
      I installed virtual environment by pip3 install virtualenv.
      I can see a opencv version when I run opencv_version -v but my python3.8 says "no module error, could not find cv2."
      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: /home/yasar/pythonUAV/py38/bin/python3 (ver 3.8)
      -- Libraries: NO
      -- numpy: /home/yasar/pythonUAV/py38/lib/python3.8/site-packages/numpy/core/include (ver 1.24.4)
      -- install path: -
      --
      -- Python (for build): /usr/bin/python2.7
      I want to install opencv on python3.8 so I can use yolov5.
      I would be glad if you can help me out.

    • @subhrajitnag4825
      @subhrajitnag4825 3 місяці тому

      @@JetsonHacks I am having the same problem. In my python3 section, install path is - , and numpy is NO, Libraries is NO and interpreter is /home/tokupuro/pruningenv/bin/python3 (ver 3.6.9). 'pruningenv' is the name of the virtual environment. Please help.

  • @3dimensionalrd
    @3dimensionalrd 5 місяців тому

    I have done all the steps but the system in jtop still shows me that the Opencv it is using is 4.5.1 with CUDA. When I use Python it tells me that it uses 4.9.0. How can I make the system tell me that it is using 4.9.0?

    • @JetsonHacks
      @JetsonHacks  5 місяців тому

      It is difficult to tell what is causing the issue from your description. If jtop is reporting OpenCV 4.5.1 with CUDA, that indicates that OpenCV was built with CUDA and installed on your system. The default OpenCV on Jetsons do not have CUDA installed.
      jtop calls /usr/bin/opencv_version to determine the version number. Your result indicates that this program is from an older version. Hope this helps.

  • @Key_Capz_
    @Key_Capz_ 8 місяців тому

    I'm using cuda 12.2, but for some reason it wants to use drivers from cuda 11.4. So I deleted cuda 10, 11, 12.0 and now I only have 12.2 with 12.2 drivers. I ran a code to check it, and it's still using 11.4 drivers even after deleting it.

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      I don't understand the issue from your description. How do you check which CUDA driver is installed, and which one you are using? How are you using the CUDA driver?

    • @Key_Capz_
      @Key_Capz_ 8 місяців тому

      @JetsonHacks I'm using c++. There's more to on adding vector of 50000 elements, but I will give the part that im talking about. Here's the code: in main() int driver =0; int runtime =0; cudaRuntimeGetVersiin(&runtime); cudaDriverGetVersion(&driver); std::cout

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      It sounds like you are not linked to the correct version of the library. This is beyond what can be answered on a UA-cam comment. Please ask your question in the official NVIDIA Jetson forum, where a large group of developers and NVIDIA engineers share their experience.

  • @bora7123
    @bora7123 2 місяці тому +1

    I am trying to install OpenCV 4.8.0 with Python3.8. Following your instruction, I was able to install OpenCV with CUDA enabled (confirmed in jtop). But in following your video from 4.:45, I got a problem in setting the python path for python 3.8.0. I executed "opencv_version -v" command. Then, it returns "Interpreter: /usr/bin/python3 (ver 3.8)
    Libraries: NO
    numpy: NO (Python3 wrappers can not be generated)
    install path: -
    "
    What should I do? I ran python3 without a problem. But install path is missing as you can see here. Then, how should I export its path to let OpenCV know? I will greatly appreciate your help.

    • @JetsonHacks
      @JetsonHacks  Місяць тому +1

      It's hard to tell without knowing how you exported your PYTHONPATH. Is the cv2 module present in the PYTHONPATH you specify?

    • @crydog282
      @crydog282 Місяць тому

      @@JetsonHacks I have same problem. For python2 there is openCV in dist-packages. But there is none in python3.8 or other. In time of instalation there was path for lib in PYTHONPATH. Same as @bora7123 i have upgraded to python 3.8.0 and dont have any problem with it yet.

    • @crydog282
      @crydog282 Місяць тому

      @@JetsonHacks Where is opencv installed ? And what should be in pythonpath

    • @JetsonHacks
      @JetsonHacks  Місяць тому

      @@crydog282 I am unable to explain it any more clearly than it is in the video and the associated article. If you could tell me where you got confused, it would be helpful.

    • @crydog282
      @crydog282 Місяць тому

      @@JetsonHacks Thanks for answering :D
      Well i do understand what is pythonpath for. My issue is, that python3 is not acesed by openCV, when i run opencv_version. Same as @bora7123 i am on Jetson NANO, python upgraded to 3.8, and set as default. So after installing, there are none opencv files in site-packages. So is there an option to specify path to python3.8 and reinstall it? For example i know that cmake has flags for PYTHON3_EXECUTABLE and so on.

  • @alexanderriosariz-xc8bn
    @alexanderriosariz-xc8bn 7 місяців тому

    Tengo error nvcc fatal : Unsupported gpu architecture 'compute_87'en Jetson Nano P3450 de 4gb
    al intentar instalar OpenCV con CUDA en Python en Jetson Nano P3450 de 4gb

    • @JetsonHacks
      @JetsonHacks  7 місяців тому +1

      You need to change the build script. See: 01:18
      The Jetson Nano doesn't know about the 8.7 (Jetson Orin) architecture
      -D CUDA_ARCH_BIN=5.3,6.2,7.2,8.7
      changes to:
      -D CUDA_ARCH_BIN=5.3,6.2,7.2
      and change the CUDA version to match your machine. On the Jetson Nano, this is 8.2.
      -D CUDNN_VERSION='8.0'
      changes to:
      -D CUDNN_VERSION='8.2'
      Make sure to save the file.
      Good luck on your build!

    • @alexanderriosariz-xc8bn
      @alexanderriosariz-xc8bn 7 місяців тому +1

      a @JetsonHacks . Mil gracia, instalación completada. -- Installing: /usr/local/bin/opencv_interactive-calibration
      -- Set runtime path of "/usr/local/bin/opencv_interactive-calibration" to "/usr/local/lib:/usr/local/cuda/lib64"
      -- Installing: /usr/local/bin/opencv_version
      -- Set runtime path of "/usr/local/bin/opencv_version" to "/usr/local/lib:/usr/local/cuda/lib64"
      -- Installing: /usr/local/bin/opencv_model_diagnostics
      -- Set runtime path of "/usr/local/bin/opencv_model_diagnostics" to "/usr/local/lib:/usr/local/cuda/lib64"
      Do you wish to remove temporary build files in /tmp/build_opencv ?
      (Doing so may make running tests on the build later impossible)
      Y/N n
      jetson-nano-jose@jetson-Jose:~/nano_build_opencv$

    • @JetsonHacks
      @JetsonHacks  7 місяців тому

      @@alexanderriosariz-xc8bn I am glad you got it to work.

  • @santosvision
    @santosvision Місяць тому

    Guys, I'm having a really difficult time trying to build for Python 3.8.0, which is the minimum version required for my project.
    Can someone please help or point me in the right direction? I have a Jetson Nano 2GB. All I need is to use opencv with CUDA in Python 3.8 or higher. Thank you so much in advance.

    • @santosvision
      @santosvision Місяць тому

      I was able to get it to work!... i tried to write the solution here in the comments but UA-cam took it down. Will make a video about.

    • @JetsonHacks
      @JetsonHacks  Місяць тому +1

      That's great, looking forward to the video.

  • @kiennguyenbao4231
    @kiennguyenbao4231 8 місяців тому

    Can you make a video on how to install mediapipe on jetson nano

    • @JetsonHacks
      @JetsonHacks  8 місяців тому +1

      What issues are you having with the install? Is there a particular feature of Mediapipe that you need?

    • @kiennguyenbao4231
      @kiennguyenbao4231 8 місяців тому

      @@JetsonHacks After successful installation, I tried running the test program but received an error " module 'mediapipe' has no attribute 'solutions' "

    • @JetsonHacks
      @JetsonHacks  8 місяців тому

      How did you install it? It sounds like you have a version mismatch with the test you are trying to run.

    • @kiennguyenbao4231
      @kiennguyenbao4231 8 місяців тому

      I followed the instructions on MelvinSajith’s github

    • @kiennguyenbao4231
      @kiennguyenbao4231 8 місяців тому

      @@JetsonHacks and error fail to build h5py

  • @mathieugendron-methot4994
    @mathieugendron-methot4994 5 місяців тому

    Hi again! i really enjoy all your tutorial. A lot of information about interesting stuff! I ran (again) into some problem.
    Traceback (most recent call last):
    File "demo.py", line 89, in
    targetId=target_id)
    File "/home/math/opencv_zoo/models/face_detection_yunet/yunet.py", line 30, in __init__
    target_id=self._targetId)
    cv2.error: OpenCV(4.8.0) /tmp/build_opencv/opencv/modules/dnn/src/onnx/onnx_importer.cpp:274: error: (-5:Bad argument) Can't read ONNX file: face_detection_yunet_2023mar.onnx in function 'ONNXImporter'
    i'm not able to run any demo :(
    Is it something easy to fix?
    Thx again for everything!

    • @JetsonHacks
      @JetsonHacks  5 місяців тому +1

      Thank you for the kind words. They don't do a very good job of versioning for the OpenCV zoo. They probably changed to support a newer version of OpenCV. You will need to roll back to an appropriate commit using git. Thanks for watching!

    • @mathieugendron-methot4994
      @mathieugendron-methot4994 5 місяців тому

      @JetsonHacks Thx! I just figured out how to do that but now that I started with a new jetson install, i'm just able to install the 4.4 version (i dont know what I did differente the first time but the 4.8 doesn't seems to be install with that git?)

    • @JetsonHacks
      @JetsonHacks  5 місяців тому

      @@mathieugendron-methot4994 From your description, it sounds like there's an issue. Maybe if you did something differently the result would be different.

    • @mathieugendron-methot4994
      @mathieugendron-methot4994 5 місяців тому

      @@JetsonHacks I finally succeeded installing opencv 4.9 and making the opencv_zoo working. Thank you so much! :)

    • @JetsonHacks
      @JetsonHacks  5 місяців тому

      @@mathieugendron-methot4994 I am glad you were able to get it to work!

  • @user-vv6zd8cs7k
    @user-vv6zd8cs7k Місяць тому

    Would it be a problem if I had 20.04 ubuntu on my jetson? ./build_opencv.sh throws error after 12% of download

    • @JetsonHacks
      @JetsonHacks  Місяць тому

      I don't know of any outstanding issues with 20.04. I don't know what 12% of download means.