Cannot import OpenCV4.6.0 from Python3.8

Hi,

I’m using Jetson nano, and tried to install OpenCV4.6.0 with CUDA ON.
From JEP/script/install_opencv4.6.0_Jetson.sh at master · AastaNV/JEP · GitHub , I have downloaded “install_opencv4.6.0_Jetson.sh”.


#!/bin/bash

Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.

NVIDIA Corporation and its licensors retain all intellectual property

and proprietary rights in and to this software, related documentation

and any modifications thereto. Any use, reproduction, disclosure or

distribution of this software and related documentation without an express

license agreement from NVIDIA Corporation is strictly prohibited.

version=“4.6.0”
folder=“workspace”

set -e

for (( ; ; ))
do
echo “Do you want to remove the default OpenCV (yes/no)?”
read rm_old

if [ "$rm_old" = "yes" ]; then
    echo "** Remove other OpenCV first"
    sudo apt -y purge *libopencv*
break
elif [ "$rm_old" = "no" ]; then
break
fi

done

echo “------------------------------------”
echo “** Install requirement (1/4)”
echo “------------------------------------”
sudo apt-get update
sudo apt-get install -y build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install -y libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
sudo apt-get install -y python3.8-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl

echo “------------------------------------”
echo “** Download opencv “${version}” (2/4)”
echo “------------------------------------”
mkdir $folder
cd ${folder}
curl -L https://github.com/opencv/opencv/archive/${version}.zip -o opencv-${version}.zip
curl -L https://github.com/opencv/opencv_contrib/archive/${version}.zip -o opencv_contrib-${version}.zip
unzip opencv-${version}.zip
unzip opencv_contrib-${version}.zip
rm opencv-${version}.zip opencv_contrib-${version}.zip
cd opencv-${version}/

echo “------------------------------------”
echo “** Build opencv “${version}” (3/4)”
echo “------------------------------------”
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D WITH_CUDNN=ON -D CUDA_ARCH_BIN=“5.3” -D CUDA_ARCH_PTX=“” -D OPENCV_GENERATE_PKGCONFIG=ON -D OPENCV_EXTRA_MODULES_PATH=…/…/opencv_contrib-${version}/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python3=ON -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_EXAMPLES=OFF -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local …
make -j$(nproc)

echo “------------------------------------”
echo “** Install opencv “${version}” (4/4)”
echo “------------------------------------”
sudo make install
echo ‘export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH’ >> ~/.bashrc
echo ‘export PYTHONPATH=/usr/local/lib/python3.8/site-packages/:$PYTHONPATH’ >> ~/.bashrc
source ~/.bashrc

echo “** Install opencv “${version}” successfully”
echo “** Bye :)”


I had modified only one point → CUDA_ARCH_BIN=“5.3”, and compile has finished.
I want to use openCV with python3.8, but not succeed.

~$ python3.8
Python 3.8.0 (default, Dec 9 2021, 17:53:27)

import cv2
ModuleNotFoundError: No module named ‘cv2’

~$ python3
Python 3.6.9 (default, Mar 10 2023, 16:46:00)

import cv2
ModuleNotFoundError: No module named ‘cv2’

~$ python2
Python 2.7.17 (default, Mar 8 2023, 18:40:28)

import cv2
a=1
b=2
a+b
3

It seems like OpenCV4.6.0 is linked with Python2.7, not Python3.8.

Q1: Is ther any way to link Python3.8 to OpenCV ?
Q2: If it is not possible, should I need to modify “install_opencv4.6.0_Jetson.sh” and try compile again ? (nightmare…)

Thanks for your great help in advance.

Hi,

Do you have Python 3.8 installed on your environment?
Since JetPack 4 by default uses Python 3.6, you will need to install it manually.

Thanks.

Hi,

Thank you verymuch for your reply.
Before trying to install openCV4.6.0 with CUDA ON, I installed Python3.8.0 + OpenCV with No CUDA, and “import cv2” worked well.
Actually, in my current system, I have not installed Python3.8.0 before installing openCV4.6.0 with CUDA ON, I found there was “sudo apt-get install -y python3.8-dev python-dev python-numpy python3-numpy” in “install_opencv4.6.0_Jetson.sh” and I thought I do not need to install Python3.8.0 before run “install_opencv4.6.0_Jetson.sh”.
Please correct me if my understanding is wrong.

Thank you, and best regards,

Hi,

The script is for JetPack 5 users so there is a command to install the Python 3.8 dev package.

Do you just need the CUDA OpenCV package?
If yes, please try the below script which builds OpenCV for Python 3.6:

But could you try to import it with python3.8 to see if it works?

$ python3.8
> import cv2

Thanks.

Hi,

Thank you for your reply.

Do you just need the CUDA OpenCV package?

Yes, OpenCV4.5.0 is fine with me.

Regarding the Python version, I had installed python3.8, because VSCODE accept only python version 3.8 or higher.
So, now I have also OpenCv (no CUDA support ) and "import cv2"works well, as follows:

$ python3.8
Python 3.8.0 (default, Dec 9 2021, 17:53:27)
[GCC 8.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import cv2

Let me try the script “install_opencv4.5.0_Jetson.sh”.

Thank you !

Hi,

Can I ask you one question?
I found the line as follows ,in the script “install_opencv4.5.0_Jetson.sh”.

sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy

Since I already installed python3.8, is it OK to modify the python version from "python3.6-dev " to “python3.8-dev” on this line ?

Thank you,

Hi,

It looks like there is some issue when building with Python 3.8.
If python 3.6 is acceptable, please keep the command to sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy.

Thanks.

1 Like

Hi,

>It looks like there is some issue when building with Python 3.8.

Thank you very much for your advise.
I will keep the command sudo apt-get install -y python2.7-dev python3.6-dev python-dev python-numpy python3-numpy. accordingly.

Thank you!

Hi,

I tried:

/bin/bash ./install_opencv4.5.0_Jetson.sh

“Truncated”

– Installing: /usr/local/bin/opencv_version
– Set runtime path of “/usr/local/bin/opencv_version” to “/usr/local/lib:/usr/local/cuda/lib64”
** Install opencv-4.5.0 successfully
** Bye :)

It worked well , and found OpenCV: 4.5.0 - with CUDA: YES

~$ jetson_release -v
Software part of jetson-stats 4.2.7 - (c) 2024, Raffaello Bonghi
Model: NVIDIA Jetson Nano Developer Kit - Jetpack 4.6.1 [L4T 32.7.1]
NV Power Mode[0]: MAXN
Serial Number: [XXX Show with: jetson_release -s XXX]
Hardware:

  • 699-level Part Number: 699-13448-0000-402 K.0
  • P-Number: p3448-0000
  • BoardIDs: p3448
  • Module: NVIDIA Jetson Nano (4 GB ram)
  • SoC: tegra210
  • CUDA Arch BIN: 5.3
  • Codename: Porg
    Platform:
  • Machine: aarch64
  • System: Linux
  • Distribution: Ubuntu 18.04 Bionic Beaver
  • Release: 4.9.253-tegra
  • Python: 3.6.9
    jtop:
  • Version: 4.2.7
  • Service: Active
    Libraries:
  • CUDA: 10.2.300
  • cuDNN: 8.2.1.32
  • TensorRT: 8.2.1.8
  • VPI: 1.2.3
  • Vulkan: 1.2.70
  • OpenCV: 4.5.0 - with CUDA: YES

and I could import OpenCv from both python3.6 and python3.8 as follows:

~$ python3.8
Python 3.8.0 (default, Dec 9 2021, 17:53:27)
[GCC 8.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import cv2

and I could import OpenCv from both python3.6 and python3.8 as follows:

~$ python3.6
Python 3.6.9 (default, Mar 10 2023, 16:46:00)
[GCC 8.4.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import cv2

Thank you very much for your great help !

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