How to install opencv-python for python3.6

I can’t install opencv-python by “pip3 install opencv-python”.
If anyone have installed opencv-python on nano, please give me guidance.
Thanks a lot.

Yes we need it badly

Me too!

Hi,

You can enable the python OpenCV by building it from source.

Here is an script for Xavier. Suppose you can get it work by replacing the compute capability.
https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Xavier.sh

--- a/install_opencv4.0.0_Xavier.sh
+++ b/install_opencv4.0.0_Xavier.sh
@@ -38,9 +38,9 @@ cd opencv-4.0.0/
 echo "** Building..."
 mkdir release
 cd release/
-cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="7.2" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=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 ..
+cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=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 -j8
+make -j3
 
 echo "** Install opencv-4.0.0 successfully"
 echo "** Bye :)"

We are also working on a script for Jetson Nano.
Will share the script after validation. Stay tuned.

Thanks.

@AastaLLL

Hi, thanks for your quick reply. I will try later.
Before, I still want to confirm that is it work for python3.6? Cause showed in https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Xavier.sh seems compiled for python2.7.

And how large storage Opencv package need to use? Because my SD card storage is only 1GB left. I’m afraid it’s not enough.

This is still not the answer as the original question for Nano

Hi,

The python wrapper is installed via SDK manager by default.
Please give it a try after setup:

$ python3
>> import cv2

If there is no cv2 module, you can install it via apt-get directly:

sudo apt-get install python-opencv
sudo apt-get install python3-opencv

If you meet the numpy error, please install numpy to fix the issue.
ImportError: numpy.core.multiarray failed to import

sudo apt-get install python3-numpy

Building from source is only required when an extra features is needed. Ex. GStreamer
Thanks.

We need gstreamer support

when compiling from source im getting the gfollowing error

[ 86%] Building CXX object modules/ximgproc/CMakeFiles/opencv_ximgproc.dir/opencl_kernels_ximgproc.cpp.o
[ 86%] Linking CXX shared library ../../lib/libopencv_ximgproc.so
[ 86%] Built target opencv_ximgproc
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

We need gstreamer support

so when compiling from source im getting the following error

[ 86%] Building CXX object modules/ximgproc/CMakeFiles/opencv_ximgproc.dir/opencl_kernels_ximgproc.cpp.o
[ 86%] Linking CXX shared library ../../lib/libopencv_ximgproc.so
[ 86%] Built target opencv_ximgproc
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

after rerunning the make -j4 now getting this error

[ 99%] Built target opencv_stitching
In file included from /opt/opencv-4.0.0/modules/core/include/opencv2/core/cuda/functional.hpp:50:0,
                 from /opt/opencv_contrib-4.0.0/modules/cudaoptflow/src/cuda/pyrlk.cu:47:
/usr/local/cuda/include/device_functions.h:54:2: warning: #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead." [-Wcpp]
 #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead."
  ^~~~~~~
In file included from /opt/opencv-4.0.0/modules/core/include/opencv2/core/cuda/functional.hpp:50:0,
                 from /opt/opencv_contrib-4.0.0/modules/cudaoptflow/src/cuda/pyrlk.cu:47:
/usr/local/cuda/include/device_functions.h:54:2: warning: #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead." [-Wcpp]
 #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead."
  ^~~~~~~
In file included from /opt/opencv-4.0.0/modules/core/include/opencv2/core/cuda/functional.hpp:50:0,
                 from /opt/opencv_contrib-4.0.0/modules/cudaoptflow/src/cuda/pyrlk.cu:47:
/usr/local/cuda/include/device_functions.h:54:2: warning: #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead." [-Wcpp]
 #warning "device_functions.h is an internal header file and must not be used directly.  This file will be removed in a future CUDA release.  Please use cuda_runtime_api.h or cuda_runtime.h instead."
  ^~~~~~~
Killed
CMake Error at cuda_compile_1_generated_pyrlk.cu.o.RELEASE.cmake:281 (message):
  Error generating file
  /opt/opencv-4.0.0/release/modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_pyrlk.cu.o


modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/build.make:676: recipe for target 'modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_pyrlk.cu.o' failed
make[2]: *** [modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_pyrlk.cu.o] Error 1
CMakeFiles/Makefile2:9951: recipe for target 'modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all' failed
make[1]: *** [modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all] Error 2
Makefile:162: recipe for target 'all' failed
make: *** [all] Error 2

Hi,

I had success with this guide:

Pay attention to the Python version during the cmake process.

This is not applicable for us as we need to enable CUDA and Gstreamer

Finally I managed to install after few changes, might be useful for others

Btw I had multiple thermal shutdowns during this process

and followed this article https://jkjung-avt.github.io/opencv3-on-tx2/

after this step as mentioned above

cmake -D WITH_CUDA=ON -D CUDA_ARCH_BIN="5.3" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.0.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=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 -j4

make install

ldconfig

cd /usr/local/python/cv2/python-3.6

mv cv2.cpython-36m-aarch64-linux-gnu.so cv2.so

Now works Perfectly

Hi,

Here is a script to build opencv from source on Jetson Nano for your reference:
https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Nano.sh

Thanks.

I installed opencv using the script provided above but when checking version, it shows 3.2.0.
Shouldn’t it be 4.0.0?

To check the opencv version I did the following:
$python3

import cv2
print(cv2.version)
3.2.0

I have the same result.
$python3

import cv2
print(cv2.version)
3.2.0

When I try to run the demo ./nvx_sample_opencv_npp_interop, I get

NVXIO and samples were built without OpenCV support.
Install OpenCV (2.4.13) and rebuild the sample.

I re-installed opencv4.1.0 on a “clean” flashed micro sd card as the first operation using https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Nano.sh but replacing “4.0.0” by “4.1.0” in the script.

The installation worked and now it shows 4.1.0 (print(cv2.version)) so I can now import cv2.dnn.

Hi,

Sorry for the late reply.

For the previous question, please remember to update the python path to the new OpenCV:

export PYTHONPATH=$PYTHONPATH:${OpenCV DIR}/release/python

Thanks.

This script won’t work on the base image. I tried as 4.1.0, and 4.0.0
They both fail to compile at 98 or 99%. I just received this yesterday, so I’m sure a lot more people will be asking. Any idea’s?

[ 98%] Linking CXX shared library …/…/lib/libopencv_stitching.so
[ 98%] Built target opencv_stitching
Killed
CMake Error at cuda_compile_1_generated_pyrlk.cu.o.RELEASE.cmake:281 (message):
Error generating file
/home/papaw/opencv-4.0.0/release/modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/./cuda_compile_1_generated_pyrlk.cu.o

modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/build.make:70: recipe for target ‘modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_pyrlk.cu.o’ failed
make[2]: *** [modules/cudaoptflow/CMakeFiles/cuda_compile_1.dir/src/cuda/cuda_compile_1_generated_pyrlk.cu.o] Error 1
CMakeFiles/Makefile2:9951: recipe for target ‘modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all’ failed
make[1]: *** [modules/cudaoptflow/CMakeFiles/opencv_cudaoptflow.dir/all] Error 2
Makefile:162: recipe for target ‘all’ failed
make: *** [all] Error 2

The issue is you run out of ram.

I created a 4GB swapfile and followed the above script, but did replace 4.0.0 with 4.1.0. I know that it used at least 2.2GB of swap, so the 4GB of swap is probably a safe size. I’m pretty confident that the script would work as is, as long as you had swap space. I also had a fan to keep things cool. This will take a while, and the temps slowly rise.

Total success.

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile