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 ?
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.
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)
@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.
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?
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.
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).
You should have removed OpenCV4Tegra by executing those ‘apt-get purge’ commands.
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)
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).
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.
@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.
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;
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).
(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.
@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.
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: