Cannot use opencv on Xavier NX

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.

Hi,

It looks like that OpenCV 4.5 support is added in pyinstaller-hooks-contrib v2021.3:
https://github.com/pyinstaller/pyinstaller-hooks-contrib/blob/master/CHANGELOG.rst#updated-hooks

We try pyinstaller 4.2 with pyinstaller-hooks-contrib 2021.2.
It can work on the default OpenCV python 4.1.1 with the following command:

$ sudo pip3 install pyinstaller==4.2
$ sudo pip3 install pyinstaller-hooks-contrib==2021.2
$ pyinstaller --onefile --paths="/usr/lib/python3.6/dist-packages/cv2/python-3.6" test.py

Would you mind also giving it a try?
Thanks.

Hi, Aast:
I formatted the Micro SD card and re-built Jetpack 4.6 with default opencv 4.1.1. Then followed your recommendation to install Pyinstaller 4.2 instead of Pyinstaller 4.7. After first trial of building Exe, the exe cannot work with error with tendo, then I disabled this function and tried building again. Fortunately the exe works this time. The tendo function is not important in our application and we can run the software without it. Thank you all very much for your great supports.

P.S. I think you better to find a final solution to this exe building issue.

Hi,

Good to know it works now. Thanks for all the testing.

This issue comes from the different expected versions from pyinstaller and JetPack.
In the future release, we do have a plan to upgrade the OpenCV version.
After the upgrading, pyinstaller should work as default without downgrading.

Thanks.

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