Opencv Build with cuda on jetson agx orin for venv

Hi,

I created venv with python3.6 version on Jetson orin device and tried to build opencv with cuda, but I could not succeed. I am sharing the build frags and the output with you, and I don’t get any errors in the installation, I can’t see the gpu in venv.



Hello,

I moved your topic to the Jetson Orin category for visibility.

Hi,

Have you added it to the PYTHONPATH variable?
For example:

echo 'export PYTHONPATH=/usr/local/lib/python3.6/site-packages/:$PYTHONPATH' >> ~/.bashrc

Below is the steps to compile OpenCV with different python versions for your reference:

Thanks.

#!/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.5.3”
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 **
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=“8.7” -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=/home/jetson-orin/Headlamp/lib:$LD_LIBRARY_PATH’ >> ~/.bashrc
echo ‘export PYTHONPATH=/home/jetson-orin/Headlamp/lib/python3.6/site-packages/site-packages/:$PYTHONPATH’ >> ~/.bashrc
source ~/.bashrc

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

Hello, i try with this script and i changed echo path for my venv pyhon path but it doesnt work. Opencv didnt install, i try install with this script. Do I need to install opencv first with pip install opencv-python==4.5.3?

I found now my fault echo ‘export PYTHONPATH=/home/jetson-orin/Headlamp/lib/python3.6/site-packages/site-packages/:$PYTHONPATH’ >> ~/.bashrc i wrote site-packages twice than i fixed and i am trying now again. But i have a question, is it correct this script for opencv build and install for venv?

it doesnt work :(

i try for opencv=4.5.3

Hi,

Did you build it within or outside the env?
If you build it outside of the env, please remember to add --system-site-packages when creating the virtual environment.

Thanks.

You may disable python2:

cmake \
...
-D BUILD_opencv_python2=OFF \
-D PYTHON3_EXECUTABLE=$(which python3) \
-D BUILD_opencv_python3=ON \
-D PYTHON3_INCLUDE_DIR=$(python3 -c "from distutils.sysconfig import get_python_inc; print(get_python_inc())") \ 
-D PYTHON3_PACKAGES_PATH=$(python3 -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())") \
...

I tried but it doesnt work, thank u for your answer.

i did not create venv like this, i will try and i will give feedback.

Hi,

I will tell step by step
1-) I installed jetpack 5.0.2 version on my orin device and install python3.6(default python version is 3.8)
2-) I create venv with like this command virtualenv --python=python3.6 name --system-site-packages
3-) I use this souce JEP/install_opencv4.6.0_Jetson.sh at master · AastaNV/JEP · GitHub (but i change opencv version to 4.5.3 and also i change export paths like upside)
4-) Build was succesful but i cant import cv2 when i try in venv, every time build for python2.7
5-) I close opencv build for python2 with flag but still build for python2.7 :), i use opencv with gpu from python2.7
6-)So i dont build opencv for python3

When I install python3.6, opencv is not installed compared to other python versionsa and I create the virtual environment directly.

Hi,

I tested the script with the default python 3.8 only.
Will check it and update more information with you soon.

Thanks.

Thank u i am waiting

Hi,

Thanks for your patience.
Please give the following steps a try.

Setup

$ sudo add-apt-repository ppa:deadsnakes/ppa
$ sudo apt-get update
$ sudo apt-get install python3.6 python3.6-dev python3.6-venv

Create virtual environment (no need to use –system-site-packages)

$ virtualenv --python=python3.6 env
$ source env/bin/activate

Build OpenCV for python3.6

install_opencv4.6.0_py3.6.sh (2.6 KB)

./install_opencv4.6.0_py3.6.sh

Test

$ python3
Python 3.6.15 (default, Apr 25 2022, 01:55:53) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'4.5.3'

Thanks.

Thank you @AastaLLL

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