Jetson Xavier NX Cuda-enabled OpenCV Python

Hi there,

I have found multiple tutorials on JetsonHacks websites on how to build CUDA-enabled python opencv for various Jetson board such as Nano, AGX Xavier etc etc. I just got myself a Jetson Xavier NX but I cant find any tutorials on how to build opencv for it. Do need some guidance. Thanks!

Link for AGX Xavier OpenCV build: Build OpenCV 3.4 on NVIDIA Jetson AGX Xavier Developer Kit - JetsonHacks

Sincerely,
Chun Zhe

Hi,

The installation does not depend on the platform so you could just follow those tutorial.

Awesome! Ill try it out and share the result here. :D Thanks for your prompt reply.

1 Like

hi - sorry to barge in ! :S

i have a rasp pi 4 running a realsense/OPENCV script with a python wrapper.
it runs ok but i could do with a faster refresh rate

ive tried jetson nano but i dont know how to leverage the cuda gpu support - so the python code runs slower than the pi4 as its processor is actually worse lol and its running sequentially…without using the GPU at all

HHHHOWWW ? do you go about getting the GPU to action the realsense stuff ? … sory - thick question but frankly everytime i google it i just get build videos from jetson hacks that may or may NOT be what im looking for and im trying not to disappear down too many rabbit holes ! :S

Either you follow the JetsonHacks-link above or try the script my @mdegans here: GitHub - mdegans/nano_build_opencv: Build OpenCV on Nvidia Jetson Nano

But compiling OpenCV with CUDA support does not automagically speed up your application, you will most likely need to adapt your code to use CUDA functionality…

1 Like

thankyou so much for the reply.

i thought i might need to adapt the code - but i dont know how or where to look?

would it just be a case of something like (this is a tongue in cheek example) import cuda_library and then use some sort of while loop that sends those processes to cuda ? … i just am not sure where to start with that ? … is there an example forum somewhere ??

steve

following could be a starting point (didn’t try it myself, it just popped up when searching for “opencv cuda python”):

1 Like

The basic procedure is to find instances of cv::foo()(or cv2.foo() in python) and replace them with cv::cuda::foo() where those functions are available.
Caveats are:

  • many things from plain old OpenCV are missing from the cv::cuda namespace
  • copies to and from the GPU are expensive, so it may not be worth it to do something on the GPU
  • the OpenCV CUDA stuff is not as optimized as Nvidia’s

Workarounds for the caveats include:

  • be creative with replacing CPU stuff with GPU stuff. You can do a lot with simple matrix operations.
  • pointers are available on GpuMat (eg. with ptr() accessor method or data) so you can pass the underlying data to CUDA functions not included with OpenCV.
  • You’ll find most OpenCV GPU functions, including upload and download methods, have a stream parameter. Streams can be used to overlap CPU and GPU work (or GPU and GPU work). Whether this can be used in your case depends on whether your code is necessarily sequential or not. In python there are some async cuda copy functions in pycuda you can use to do much the same thing for upload/download.
  • whether it’s “fast enough” (for example, under a frame duration) is often what matters – not if it’s the fastest. Readability counts and OpenCV is very, very, readable (IMO, even moreso in C++).

Here is a tutorial you could follow along with to get introduced to what’s possible.

Accelerating OpenCV with CUDA streams in Python - James Bowley

Good luck, and make sure to have fun!

2 Likes