Cannot use opencv on Xavier NX

Hi, I developed a vision inspection software on Xavier NX. This software uses OpenCV for image process. It works well on a few of Xavier NX devices which is installed with Jetpack 4.5. But when I deployed this software on a new Xavier NX recently, I encountered a problem. The new Xavier NX is installed with Jetpack 4.6 from your download web pages. Below is the screenshot of error message:


Please take note that Line 10 is this command in the script: import cv2. This means I cannot run the compiled package on Jetpack 4.6. How can I solve such problem?

Hi,
Not sure if this is the rootcause, but if you have upgraded from Jetpack 4,5 to 4.6, probably you would need to re-compile the sample on Jetpack 4.6.

Hi Dane:
Yes, I re-complied the software on Jetpack 4.6, and run the executable on the same machine, but still cannot run it. Below is the error image:


We can find the error message is somewhat different from previous one.

Hi Dane or Nvidia support team:
Can you please help on this issue? I’m waiting for your response, otherwise, we cannot use the software on the new machine. Our production line await it. Thanks in advance.

Hi,
Please check if you can run the commands:

nvidia@nvidia-desktop:~$ python
Python 2.7.17 (default, Feb 27 2021, 15:10:58)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1'
>>> quit()
nvidia@nvidia-desktop:~$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.1.1'

And does the sample run with python 2.7 or python 3.6?

Hi, Dane:
Below is my test result:


My sample run with Python 3.6.9

I write my script with Pycharm. If I run it from Pytharm, it works. While if I compile it with Pyinstaller, the executable cannot run on Jetpack 4.6, but can run on Jetpack 4.5.

Hi Dane and Nvidia support team:
Have you done any debugging or do you have any advice on debugging?

Hi,

We can reproduce this issue with pyinstaller 4.7.

The error seems to indicate some OpenCV versions are mismatching but we are still checking in deep.
Will share more information with you once we find the root cause.

Thanks.

Hi, Aasta and Nvidia support team:
Thank you for your works.
Do you have some progresses so far?

Hi,
We are still checking it. Will update once there is progress.

Hi,

We try this with OpenCV-Python 4.5.4 and it can work correctly.
Would you mind giving it a try?

$ pip3 install opencv-python

test.py

import cv2
print(cv2.__version__)
$ pyinstaller --onefile test.py
$ ./dist/test
4.5.4

Thanks.

HI Aasta:
I installed opencv 4.5.4, and rebuilt the executable. Now there is no problem with the line of “import cv2”. The software seems work, but still have another problem with Opencv. When the software uses cv2.VideoCapture(video_source) to operate the camera, it cannot open the camera. Below is the screen shot of the error in debugging:


So the software still cannot run.

When I operate the camera in Python3 with following codes, the camera works:
$ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! nvoverlaysink
or
$ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! ‘video/x-raw(memory:NVMM),width=3820, height=2464, framerate=21/1, format=NV12’ ! nvvidconv flip-method=0 ! ‘video/x-raw,width=960, height=616’ ! nvvidconv ! nvegltransform ! nveglglessink -e

Hi,
Please check if you can run this sample:
OpenCV Video Capture with GStreamer doesn't work on ROS-melodic - #3 by DaneLLL

Probably gstreamer is not enabled in the OpenCV 4.5.4.

Hi Dane:
I tried your suggested code, but end up with error. Below is the screen shot:

Hi,
Please try to manually build OpenCV 4.5.4 with this script:
GitHub - mdegans/nano_build_opencv: Build OpenCV on Nvidia Jetson Nano

The default package may not be built with -D WITH_GSTREAMER=ON. Would need to manually enable it and build the package.

Hi, Dane:
I downloaded the installation file and tried to install Opencv, but not successful. Please refer to the screen shot below:


I tried to install both 4.5.4 and the default version 4.4.0. All failed.
Please advise how to install. Thanks.

Hi:
I have managed to install opencv 4.5.4. Now GStreamer is enabled, and I can operate camera in Pycharm. But still encountered problem when running Exe compiled by Pyinstaller.

Found some solutions:
python - recursion is detected during loading of “cv2” binary extensions - Stack Overflow

Should I re-install opencv 4.5.3? If yes, how to uninstall opencv 4.5.4?

Hi,
It looks like Pyinstaller always searches for latest version of OpenCV. The latest is 4.5.4 so it may not work by downgrading to 4.5.3. Unless there is a way to force the version in Pyinstaller. This would need other users to share experience.