Newbie question: how to install OpenCV and setup the path?

Tried to install OpenCV 3 by following the instruction of JetsonHacks and JK Jung’s blog:

  1. Remove OpenCV4Tegra from JetPack 3.1
    sudo apt-get purge libopencv4tegra-python libopencv4tegra-dev libopencv4tegra sudo apt-get purge libopencv4tegra-repo

  2. Run JetsonHacks’ scripts
    git clone https://github.com/jetsonhacks/buildOpenCVTX2.git cd buildOpenCVTX2
    ./buildOpenCV.sh cd ~/opencv/build
    $ sudo make install

  3. Download “tegra-cam.py” from JK Jung’s blog

  4. Run the Python code
    cd ~/Downloads/python python3 tegra-cam.py

    But got error message:

The execution failed with error message:
Traceback (most recent call last):
File "tegra-cam.py", line 15, in <module>
import cv2
ImportError: No module named 'cv2'

Apparently Python was unable to import OpenCV library. Then I ran the command to check out the path.

$ sudo find / -name ‘libopencv_*’

Found two paths that seem containing the same set of library files

/usr/lib/
/home/nvidia/opencv/build/lib/

How to set up the path to the OpenCV library? Is it in .bashrc ?

References:
JetsonHacks, “Build OpenCV on the NVIDIA Jetson TX2” http://www.jetsonhacks.com/2017/04/05/build-opencv-nvidia-jetson-tx2/
JK Jung, “How to Capture and Display Camera Video with Python on Jetson TX2” https://jkjung-avt.github.io/tx2-camera-with-python/

Since you are using python3, make sure you see the following lines in the cmake log during build process of opencv.

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /usr/local/lib/python3.5/dist-packages/numpy/core/include (ver 1.13.3)
--     packages path:               lib/python3.5/dist-packages

After you have successfully built and installed opencv, you should see the following .so file which binds opencv to python3.

$ ls /usr/local/lib/pyhton3.5/dist-packages/cv2.*
cv2.cpython-35m-aarch64-linux-gut.so

All detailed could be found in my blog “How to Install OpenCV (3.3.0) on Jetson TX2”: https://jkjung-avt.github.io/opencv3-on-tx2/

@JK, thanks for your reply.

I went to check the directory /usr/local/lib/pyhton3.5/dist-packages/. It’s empty.

Also, do you know if I need to modify the .bash? Which path is needed to be added to the environment (if indeed necessary)?

If you follow what I did in the blog post, you don’t need to modify ~/.bashrc at all.

The file ‘/usr/local/lib/pyhton3.5/dist-packages/cv2.cpython-35m-aarch64-linux-gut.so’ should be built and installed when you build your own opencv. Please make sure you install necessary dependencies before ‘cmake’ on opencv-3.3.0.

### Install dependencies for python3
$ sudo apt-get install python3-dev python3-pip
$ sudo pip3 install numpy

Also make sure that cmake finds and would build python3 bindings, before you do ‘make’.

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   /usr/lib/aarch64-linux-gnu/libpython3.5m.so (ver 3.5.2)
--     numpy:                       /usr/local/lib/python3.5/dist-packages/numpy/core/include (ver 1.13.3)
--     packages path:               lib/python3.5/dist-packages

Duplicated post removed…

Hi @jkjung13,
To try your script, I first uninstalled the opencv under ~/opencv/build, then followed your steps to build the devices. But the library and numpy seem haven’t yet been setup correctly.

– Python 3:^M
– Interpreter: /usr/bin/python3 (ver 3.5.2)^M
– Libraries: NO
– numpy: NO (Python3 wrappers can not be generated)

Any suggestion on how to debug?

Have you done this before ‘cmake’?

### Install dependencies for python3
$ sudo apt-get install python3-dev python3-pip
$ sudo pip3 install numpy

@jkjung13, I did. Here is my bash script, which is a mix of JetsonHacks and yours.I realized that JetsonHacks script was written for OpenCV 2.7 so it’s not for OpenCV3. So I put your code into the script. BTW, I have purged the OpenCV4Tegra alreaedy. The purge commends are not in the script.

#!/bin/bash
# License: MIT. See license file in root directory
# Copyright(c) JetsonHacks (2017), JK Jung (2017)
cd $HOME
sudo apt-get install python3-dev python3-pip
sudo pip3 install numpy
### Install dependencies based on the Jetson Installing OpenCV Guide
sudo apt-get install -y \
    build-essential \
    libavformat-dev \
    make \
    cmake \
    cmake-curses-gui \
    g++ \
    libavformat-dev \
    libavutil-dev \
    libswscale-dev \
    libv4l-dev \
    libeigen3-dev \
    libglew-dev \
    libgtk2.0-dev \
    libavformat-dev \
    libglew-dev \
    pkg-config

cd $HOME/opencv
# mkdir build
cd build
# Jetson TX2 
cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DBUILD_PNG=OFF \
    -DBUILD_TIFF=OFF \
    -DBUILD_TBB=OFF \
    -DBUILD_JPEG=OFF \
    -DBUILD_JASPER=OFF \
    -DBUILD_ZLIB=OFF \
    -DBUILD_EXAMPLES=ON \
    -DBUILD_opencv_java=OFF \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=ON \
    -DENABLE_PRECOMPILED_HEADERS=OFF \
    -DWITH_OPENCL=OFF \
    -DWITH_OPENMP=OFF \
    -DWITH_FFMPEG=ON \
    -DWITH_GSTREAMER=ON \
    -DWITH_GSTREAMER_0_10=OFF \
    -DWITH_CUDA=ON \
    -DWITH_GTK=ON \
    -DWITH_VTK=OFF \
    -DWITH_TBB=ON \
    -DWITH_1394=OFF \
    -DWITH_OPENEXR=OFF \
    -DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-8.0 \
    -DCUDA_ARCH_BIN=6.2 \
    -DCUDA_ARCH_PTX="" \
    -DINSTALL_C_EXAMPLES=ON \
    -DINSTALL_TESTS=ON \
    -DOPENCV_TEST_DATA_PATH=../opencv_extra/testdata \
    ../

# Consider using all 6 cores; $ sudo nvpmodel -m 2 or $ sudo nvpmodel -m 0
sudo nvpmodel -m 0
make -j6

So does ‘cmake’ manage to find python3-dev and numpy libraries now?

I’m not sure how to tell if cmake manager can find python3 and numpy.

I also wonder if I should clean up the OpenCV installation. I don’t see the OpenCV in “Ubuntu Software” center. What’s the best to clean it up? Is it necessary?

  1. If you start with a freshly installed JetPack-3.1 environment, you should be able to build opencv-3.3.0 by following my blog post exactly.

  2. Please check the ‘Python 3:’ section of cmake output log. If you find it like what I’ve highlighted in post #4 then it should be OK (cmake has found/included python3-dev and numpy libraries).

  3. You should have removed OpenCV4Tegra by executing those ‘apt-get purge’ commands.

  4. If you have previously built/installed opencv on your JTX2 (by running JatsonHacks’ script), you can manually remove them by: (could do an ‘ls’ to check whether those files exist beforehand)

$ sudo rm -f /usr/lib/libopencv*
$ sudo rm -rf /usr/include/opencv*

p.s. If you follow my blog post, opencv-3.3.0 libraries and header files would be installed to /usr/local instead (due to ‘-D CMAKE_INSTALL_PREFIX=/usr/local’ in the cmake command).

Thanks JK.

The Python 3 section seems indicating the libraries and numby are not loaded.

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.5.2)
--     Libraries:                   NO
--     numpy:                       NO (Python3 wrappers can not be generated)
--     packages path:               lib/python3.5/dist-packages

CMAKE also complained at beginning that

-- Looking for linux/videodev.h - not found
-- Looking for linux/videodev2.h
-- Looking for linux/videodev2.h - found
-- Looking for sys/videoio.h
-- Looking for sys/videoio.h - not found
-- Checking for module 'libavresample'
--   No package 'libavresample' found
-- Checking for module 'libgphoto2'
--   No package 'libgphoto2' found
-- Found TBB: /usr/lib/aarch64-linux-gnu/libtbb.so
-- CUDA detected: 8.0
-- CUDA NVCC target flags: -gencode;arch=compute_62,code=sm_62;-D_FORCE_INLINES
-- Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off
-- Could NOT find Atlas (missing:  Atlas_CLAPACK_INCLUDE_DIR) 
-- Looking for dgemm_
-- Looking for cheev_
-- Looking for cheev_ - found
-- A library with LAPACK API found.
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 
-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
-- Could NOT find Doxygen (missing:  DOXYGEN_EXECUTABLE) 
-- Could NOT find JNI (missing:  JAVA_AWT_LIBRARY JAVA_JVM_LIBRARY JAVA_INCLUDE_PATH JAVA_INCLUDE_PATH2 JAVA_AWT_INCLUDE_PATH) 
-- Could NOT find Pylint (missing:  PYLINT_EXECUTABLE) 
-- Could NOT find Matlab (missing:  MATLAB_MEX_SCRIPT MATLAB_INCLUDE_DIRS MATLAB_ROOT_DIR MATLAB_LIBRARIES MATLAB_LIBRARY_DIRS MATLAB_MEXEXT MATLAB_ARCH MATLAB_BIN) 
-- Excluding from source files list: /home/nvidia/opencv/modules/core/src/convert.sse4_1.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/core/src/convert.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/core/mathfuncs_core.sse2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/core/mathfuncs_core.avx.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/core/mathfuncs_core.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/core/stat.sse4_2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/core/stat.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/undistort.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/corner.avx.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/resize.sse4_1.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/filter.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/imgwarp.sse4_1.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/resize.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/imgproc/src/imgwarp.avx2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/imgproc/accum.sse2.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/imgproc/accum.avx.cpp
-- Excluding from source files list: /home/nvidia/opencv/modules/objdetect/src/haar.avx.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/dnn/layers/layers_common.avx.cpp
-- Excluding from source files list: /home/nvidia/opencv/build/modules/dnn/layers/layers_common.avx2.cpp

Please let me know if the process raises any flags.

I haven’t read the instructions from the blog posted…

But this is the instructions i use. So far it hasnt failed on a fresh jetson install.
https://github.com/jetsonhacks/buildOpenCVTX2

@JArchy, JetsonHacks’ script is for Python2. Another concern, which I’m not 100% sure about it, is the missing of purging the build-in OpenCV4Tegra. There could be other problems, too.

Basically, from what I have seen, OpenCV4Tegra may not work with Python3. There were also discussions about the lack of gstreamer video capture support in OpenCV 2, from which OpenCV4Tegra is optimized to operate.
https://devtalk.nvidia.com/default/topic/1000777/jetson-tx2/opencv4tegra-python-3-and-samples/
http://petermoran.org/csi-cameras-on-tx2/ (see under the section “OpenCV”)

I think that’s why JK Jung made his script. What I did was to simply take JK’s purge commands and combined with JetsonHacks script. Then the whole thing got messed up. Likely some dependency libraries get lost.

Still swimming under the water, trying to figure out who’s who and what’s what.

@jkjung13, the build went fine, but make -j4 failed.

At first I thought to jump start with opencv 3.3.1, the “make -j4” failed at:

[ 41%] Generating precomp.hpp.gch/opencv_cudaimgproc_RELEASE.gch
In file included from /home/nvidia/src/opencv-3.3.1/build/modules/video/precomp.hpp:49:0:
/home/nvidia/src/opencv-3.3.1/modules/core/include/opencv2/core/ocl.hpp:776:2: internal compiler error: Segmentation fault
 }}

Then I went back to download opencv 3.3.0, (I placed files in my Downloads this time), the build seemed doing good. but make -j4 failed again, at a different location

[ 44%] Generating precomp.hpp.gch/opencv_videostab_RELEASE.gch
In file included from /home/nvidia/Downloads/opencv330/opencv-3.3.0/build/modules/stitching/precomp.hpp:96:0:
/home/nvidia/Downloads/opencv330/opencv-3.3.0/modules/core/include/opencv2/core/private.hpp:625:9: internal compiler error: Segmentation fault
 CvStatus;

Any idea?

Finally got openCV compiled and installed on TX2. Let me share my note about the problem and solution.

(1) If you ran the “cmake” from the “build” directory and failed, you need to delete all the contents underneath before running “cmake” again in the directory.
cd build rm -rf *

(2) If you use JetsonHacks script, need to purge the customized OpenCV 2 for Tegra (OpenCV4Tegra).

(3) Install the dependencies
sudo apt-get install python3-dev python3-pip sudo pip3 install numpy

(4) Clone and check out the latest OpenCV and OpenCV extra, for example

cd ~/opencv_extra git tag
$ git checkout 3.3.1

cd ~/opencv git tag
$ git checkout 3.3.1

mkdir build cd build

(5) If to use JetsonHacks script, change the setting of option of python2 or python3.
-DBUILD_opencv_python2=OFF \
-DBUILD_opencv_python3=ON \

(6) JetsonHacks uses a different folder from JK Jung’s. The former uses directory /usr, while the latter users directory /usr/local/

(7) In order to use jetsonHack’s script for building a new makefile, need to fix the cmake’s parameters:
-DBUILD_opencv_python2=ON
-DBUILD_opencv_python3=OFF
Change it to
-DBUILD_opencv_python2=OFF
-DBUILD_opencv_python3=ON \

After installation, you may test the environment by running a python demo. I downloaded JK Jung’s demo code
tegra-cam.py
and got it working after setting the width and height to a fixed value.

Yeah you are correct about those changes, personally i skipped the install of the OpenCV4Tegra when running through the jetpack install.

Seems like you got it working though!

@JArchy, did you get OpenCV3 up for Python 3 using JetsonHacks’ script? I struggled a bit earlier, that was why I tried JK’s. Eventually I had to merged the codes from both of them.

Frankly, I don’t know if it’s necessary to remove the pre-installed OpenCV4Tegra before installing the OpenCV 3. For the purpose of reducing the risk of potential conflicts, it seems to me be safer to purge OpenCV4Tegra, which is a customized OpenCV 2.

@JK, I have a related question about video quality. I couldn’t get the video quality as good as shown in your sample image which looks clean and bright ( https://jkjung-avt.github.io/assets/2017-10-19-tx2-camera-with-python/tegra-cam.png ). Mine has static in the shade area, looks snowy. I started a thread to discuss the quality issue. Please take a look when you get a chance.

By the way, any idea on how to expand your python code to get two video streams? I tried to duplicate the cap into cap0 and cap1, but wouldn’t work.

Thanks in advance.

If you want to split your stream, you can use tee. It provides the stream as output, and sets a name for using same stream in duplicated pipelines. Usually, each subsequent pipeline starts with plugin queue for synchronization. For example:

gst-launch-1.0 nvcamerasrc ! 'video/x-raw(memory:NVMM), format=NV12, width=640, height=480, framerate=30/1' ! tee name=yourname ! queue ! nvvidconv ! 'video/x-raw' ! xvimagesink              yourname. ! queue ! nvegltransform ! nveglglessink