Bulid opencv4 on Jetson Xavier nx but failed - opencv

I'm trying to implentment openpilot on Jetson Xavier nx. So I'm following https://github.com/eFiniLan/xnxpilot instruction to install dependence.
But when I'm installing opencv4, I get the following errors in ".../opencv/build/CMakeFiles/CMakeError.log"
CMakeFiles/cmTC_ee78d.dir/CheckIncludeFile.c.o -c /home/tshu/opencv/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c
/home/tshu/opencv/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c:1:10: fatal error: sys/videoio.h: No such file or directory
#include <sys/videoio.h>
^~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/cmTC_ee78d.dir/build.make:65: recipe for target ‘CMakeFiles/cmTC_ee78d.dir/CheckIncludeFile.c.o’ failed
make[1]: * [CMakeFiles/cmTC_ee78d.dir/CheckIncludeFile.c.o] Error 1
make[1]: Leaving directory ‘/home/tshu/opencv/build/CMakeFiles/CMakeTmp’
Makefile:126: recipe for target ‘cmTC_ee78d/fast’ failed
make: * [cmTC_ee78d/fast] Error 2
The build command I used is
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D WITH_CUDA=ON \
-D CUDA_ARCH_PTX="" \
-D CUDA_ARCH_BIN="7.2" \
-D WITH_CUDNN=ON \
-D CUDNN_VERSION="8.0" \
-D BUILD_opencv_python3=ON \
-D BUILD_opencv_python2=OFF \
-D BUILD_opencv_java=OFF \
-D WITH_GSTREAMER=ON \
-D WITH_GTK=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_FFMPEG=ON \
-D OPENCV_DNN_CUDA=ON \
-D ENABLE_FAST_MATH=ON \
-D CUDA_FAST_MATH=ON \
-D WITH_QT=ON \
-D ENABLE_NEON=ON \
-D ENABLE_VFPV3=ON \
-D BUILD_TESTS=OFF \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D PYTHON_EXECUTABLE=/home/`whoami`/.pyenv/versions/3.8.5/bin/python \
-D PYTHON_DEFAULT_EXECUTABLE=/home/`whoami`/.pyenv/versions/3.8.5/bin/python \
-D PYTHON_PACKAGES_PATH=/home/`whoami`/.pyenv/versions/3.8.5/lib/python3.8/site-packages/ \
-D OPENCV_EXTRA_MODULES_PATH=/home/`whoami`/opencv_contrib/modules ..
The version of opencv I tried to install is opencv-4.5.2
Can someone give me some advices? Thank you.

Jetpack comes with opencv preinstalled. JetPack 4.4 includes OpenCV 4.1.1. JetPack 4.6 includes OpenCV 4.1.1.
Let me look at the link you sent and I get back to you. You may need to install and compile OpenCV 4.5.2 from source. I wrote some instructions a while a go.

What Jetpack version are you using? OPENCV 4.4, is CUDA GPU accelerated. Using version 4.4 or higher to fully use the Super Resolution function provided by OpenCV. If you are using Jetpack 4.4, you will need to delete the OpenCV 4.1.1 version of JetPack 4.4 and install 4.4 newly.
Try this script file:
#!/bin/bash
#
# Copyright (c) 2018, 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.
#
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <Install Folder>"
exit
fi
folder="$1"
user="nvidia"
passwd="nvidia"
echo "** Remove OpenCV4.1 first"
sudo apt-get purge *libopencv*
echo "** Install requirement"
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 python2.7-dev python3.6-dev python-dev python-numpy python3-numpy
sudo apt-get install -y libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
sudo apt-get install -y libv4l-dev v4l-utils qv4l2 v4l2ucp
sudo apt-get install -y curl
sudo apt-get update
echo "** Download opencv-4.5.1"
cd $folder
curl -L https://github.com/opencv/opencv/archive/4.5.1.zip -o opencv-4.5.1.zip
curl -L https://github.com/opencv/opencv_contrib/archive/4.5.1.zip -o opencv_contrib-4.5.1.zip
unzip opencv-4.5.1.zip
unzip opencv_contrib-4.5.1.zip
cd opencv-4.5.1/
echo "** Building..."
mkdir release
cd release/
cmake -D WITH_CUDA=ON -D ENABLE_PRECOMPILED_HEADERS=OFF -D CUDA_ARCH_BIN="7.2" -D CUDA_ARCH_PTX="" -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-4.4.0/modules -D WITH_GSTREAMER=ON -D WITH_LIBV4L=ON -D BUILD_opencv_python2=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 -j6
sudo make install
echo "** Install opencv-4.5.1 successfully"
echo "** Bye :)"
If you are using NX -D CUDA_ARCH_BIN="7.2"
Run following script with on path:
$./opencv4.5_xavier_nx.sh /home/TH-Dev/src/

Related

ldd: exited with unknown exit code when use qemu in docker

I'm trying to use docker with qemu to build an arm image in my x86 host computer
I use arm64v8/ubuntu as the based image
I make a simple opencv program and try to use ldd command to see the dependencies.
however, ldd always shows:
ldd: exited with unknown exit code (132)
ldd would work if I save this image and load it in an arm computer.
However, my main project's size is too large (or my arm computer's space is too small ) to import to the arm computer, I'd like to use ldd to find out which libraries are really necessary for this project.
I also try nvcr.io/nvidia/l4t-base:r32.4.4 as based image and ldd shows
ldd: exited with unknown exit code (139)
what should I do to use ldd command if i'm in a arm image in the docker and my host computer is x86?
my dockerfile are like this:
FROM arm64v8/ubuntu
ENV TZ=Asia/Taipei
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update && apt-get install -y \
build-essential -y \
cmake -y \
git -y \
libgtk2.0-dev -y \
libjpeg-dev -y \
libpng-dev -y \
libtiff-dev -y \
pkg-config -y && \
apt-get clean
WORKDIR /home/gino/
RUN git clone https://github.com/opencv/opencv.git && \
cd ./opencv && \
mkdir build && \
cd build && \
cmake \
-D BUILD_SHARED_LIBS=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=OFF \
-D FORCE_VTK=OFF \
-D WITH_TBB=OFF \
-D WITH_GDAL=OFF \
-D WITH_V4L=ON \
-D WITH_XINE=OFF \
-D BUILD_EXAMPLES=OFF \
-D ENABLE_PRECOMPILED_HEADERS=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_opencv_apps=OFF \
-D CMAKE_INSTALL_PREFIX=/home/gino/opencv_install \
.. && \
make -j8 && \
make install && \
ldconfig && \
apt-get clean
WORKDIR /home/gino/
RUN rm -R ./opencv
ADD ./ /home/gino/cvtest/
WORKDIR /home/gino/cvtest/
RUN make clean all
ENV HOME /home/developer
CMD /home/gino/cvtest/dockertest
main.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat frame;
VideoCapture video(-1);
while(video.isOpened())
{
video>>frame;
imshow("frame",frame);
waitKey(1);
}
cout << "Hello World!" << endl;
return 0;
}

OpenCV Docker multistage build - cannot install prebuilt source

I'm trying to build a Docker image including a very particular configuration of OpenCV with CUDA and GPU support.
The build succeeds, and if I make install it from the same context that built the image, it works with no problems.
The problem happens when I try to use a multi stage build, to avoid keeping all the dependencies needed to build OpenCV. Before you continue reading, what follows might actually be an XY problem, if you have a better solution on how to copy OpenCV build artifacts (including Python bindings!) in a Docker multistage build, that is my actual intent.
Now for my attempted solution and the struggle I have:
I run COPY --from=requirements /opencv /opencv and it works and it apparently copies everything in the right path (I checked the filesystem). But, when I run from the build folder make install, I get this CMake error:
CMake Error: The source directory "" does not exist.
Specify --help for usage, or press the help button on the CMake GUI.
Makefile:2724: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
Again, the same command, from the same folder, but without multistage build, works.
Here is my Dockerfile:
# Stage 1: Build
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04 AS requirements
# Install dependencies
RUN echo "deb http://es.archive.ubuntu.com/ubuntu eoan main universe" | tee -a /etc/apt/sources.list
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install build-essential cmake unzip pkg-config libjpeg-dev libpng-dev libtiff-dev libavcodec-dev \
libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libgtk-3-dev libatlas-base-dev \
gfortran python3-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libxvidcore-dev x264 \
libx264-dev libfaac-dev libmp3lame-dev libtheora-dev libfaac-dev libmp3lame-dev libvorbis-dev \
libjpeg-dev libpng-dev libtiff-dev git python3-pip libtbb-dev libprotobuf-dev protobuf-compiler \
libgoogle-glog-dev libgflags-dev libgphoto2-dev libeigen3-dev libhdf5-dev wget libtbb-dev gcc-8 g++-8 llvm \
python3-venv libgirepository1.0-dev
# Install my project requirements
WORKDIR /venv
RUN python3 -m venv /venv
ENV PATH="/venv/bin:$PATH"
ADD requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
# Build OpenCV
WORKDIR /opencv
RUN wget https://github.com/opencv/opencv/archive/4.4.0.zip && mv 4.4.0.zip opencv.zip && unzip opencv.zip && rm opencv.zip
RUN wget https://github.com/opencv/opencv_contrib/archive/4.4.0.zip && mv 4.4.0.zip opencv_contrib.zip && unzip opencv_contrib.zip && rm opencv_contrib.zip
WORKDIR /opencv/opencv-4.4.0/build
ENV SITE_PACKAGES /venv/lib/python3.7/site-packages
ENV EXTRA_MODULES /opencv/opencv_contrib-4.4.0/modules
ENV CUDA_ARCH 7.5
ADD docker/build_opencv.sh .
RUN ./build_opencv.sh
# Stage 2: runtime
FROM nvidia/cuda:10.2-cudnn7-runtime-ubuntu18.04
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install build-essential cmake python3-venv
# Install OpenCV
COPY --from=requirements /opencv /opencv
WORKDIR /opencv/opencv-4.4.0/build
RUN make install && ldconfig
# build fails here and the rest is specific to my project so I've ommitted it
The build_opencv.sh script has this options:
#!/bin/bash
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_C_COMPILER=/usr/bin/gcc-8 \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=OFF \
-D INSTALL_C_EXAMPLES=OFF \
-D WITH_TBB=ON \
-D WITH_CUDA=ON \
-D BUILD_opencv_cudacodec=OFF \
-D ENABLE_FAST_MATH=1 \
-D CUDA_FAST_MATH=1 \
-D WITH_CUBLAS=1 \
-D WITH_V4L=ON \
-D WITH_QT=OFF \
-D WITH_OPENGL=ON \
-D WITH_GSTREAMER=ON \
-D OPENCV_GENERATE_PKGCONFIG=ON \
-D OPENCV_PC_FILE_NAME=opencv.pc \
-D OPENCV_ENABLE_NONFREE=ON \
-D OPENCV_PYTHON3_INSTALL_PATH=$SITE_PACKAGES \
-D OPENCV_EXTRA_MODULES_PATH=$EXTRA_MODULES \
-D PYTHON_EXECUTABLE=/usr/bin/python3 \
-D WITH_CUDNN=ON \
-D OPENCV_DNN_CUDA=ON \
-D CUDA_ARCH_BIN=$CUDA_ARCH \
-D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-10.2 \
-D WITH_GTK_2_X=OFF \
-D BUILD_EXAMPLES=OFF ..
make -j16
You need at least numpy in your requirements.txt file.
In order to reproduce the issue, a minimal setup would have this structure:
- docker
- Dockerfile
- build_opencv.sh
- requirements.txt
Build using from the root of the build context:
docker build -t opencvmultistage:latest -f docker/Dockerfile .
Am I doing something wrong? Maybe CMake has some weird cache that I'm not copying to the new image and makes the build fail?
For the sake of clarity, if I add make install in the build_opencv.sh script it works, but I have OpenCV installed in the build context and not the runtime, which is not what I pretend to do. make install runs in the same directory, and the same files should be present, so I don't really know what's going on.
It is simpler to run cmake & make and make install in the same stage and then copy the install folders. It will allow to not have any build tools like cmake or build-essential in the final docker image.
We will use a custom CMAKE_INSTALL_PREFIX so that OpenCV binaries are installed to a directory and we can copy it straight to the next stage. Using a custom prefix will avoid having to copy CUDA installation or development libraries no longer required. Then we will run ldconfig on that directory to link the libraries as usual.
Modify the build script to use a custom CMAKE_INSTALL_PREFIX:
mkdir /prefix
cmake -D CMAKE_BUILD_TYPE=RELEASE \
# all compiler flags...
-D CMAKE_INSTALL_PREFIX=/prefix
Modifying the Dockerfile
to run make install in stage 1
# Stage 1: Build
FROM nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04 AS requirements
...
ADD build_opencv.sh .
RUN ./build_opencv.sh && make install
copy the installation in stage 2
# Stage 2: runtime
FROM nvidia/cuda:10.2-cudnn7-runtime-ubuntu18.04
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install build-essential python3-venv
# Install OpenCV
COPY --from=requirements /prefix /prefix
COPY --from=requirements /venv /venv
ENV PATH="/venv/bin:$PATH"
RUN ldconfig /prefix

How to get an X11 socket connection between a docker container and the desktop

I wish to run Pycharm community within Docker on my desktop. I have created a Dockerfile (below) and seen it work fine on Mac.
FROM debian:buster-slim
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y \
ca-certificates \
curl \
apt-utils \
dirmngr \
gnupg \
libasound2 \
libdbus-glib-1-2 \
libgtk-3-0 \
libxrender1 \
libx11-xcb-dev \
libx11-xcb1 \
libxt6 \
xz-utils \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/*
ENV HOME /home/user
RUN useradd --create-home --home-dir $HOME user \
&& chown -R user:user $HOME
ENV LANG C.UTF-8
RUN apt-get update && \
apt-get install -y python-pip \
vim \
wget \
x11-utils \
xfonts-base \
xpra
# install PyCharm
RUN cd / && \
wget -q http://download.jetbrains.com/python/pycharm-community-2019.1.1.tar.gz && \
tar xvfz pycharm-community-2019.1.1.tar.gz && \
rm pycharm-community-2019.1.1.tar.gz
USER user
CMD [ "/pycharm-community-2019.1.1/bin/pycharm.sh"]
However when I try to run this on Ubuntu I get X11 errors from the Pycharm code:
Start Failed: Failed to initialize graphics environment
java.awt.AWTError: Can't connect to X11 window server using ':1' as
the value of the DISPLAY variable. at
java.desktop/sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
The command to invoke the container is:
docker run -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=${DISPLAY} <<image_id>>
I have tried many variations on the DISPLAY var (eg unix$DISPLAY), but none have worked.
Update:
I ran:
docker run -it --rm -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=${DISPLAY} --entrypoint /bin/sh <<image_id>>
to get access into the container, and then ran:
$ ls -l /tmp
total 0
I am confused - I thought the X11 socket residing in my host machine would have been bound to the same location in the container. Is this a red-herring?

Riofs - fuse device not found

I am trying to run riofs inside a docker container, however when I try to run riofs I get the following error:
fuse: device not found, try 'modprobe fuse' first
ERROR! Failed to mount FUSE partition !
ERROR! Failed to create FUSE fs ! Mount point: /path/to/dir
Here is what my DockerFile looks like:
FROM ubuntu:16.04
RUN apt-get update -qq
RUN apt-get install -y \
build-essential \
gcc \
make \
automake \
autoconf \
libtool \
pkg-config \
intltool \
libglib2.0-dev \
libfuse-dev \
libxml2-dev \
libevent-dev \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
RUN curl -L https://github.com/skoobe/riofs/archive/v${VERSION}.tar.gz | tar zxv -C /usr/src
RUN cd /usr/src/riofs-${VERSION} && ./autogen.sh && ./configure --prefix=/usr && make && make install
WORKDIR /opt/riofs/bin
CMD ["bash"]
I needed to add the runtime privilege SYS_ADMIN because fuse needs permissions to mount/umount.
docker run -it --cap-add SYS_ADMIN --device /dev/fuse [IMAGE] bash

Dockerfile build RUN cmd fails but works from interactive

I'm running into an issue where building a Dockerfile fails at a 'make' command but if I run -it the container and make again it succeeds. Specifically the make command seems to be not seeing a file:
make[2]: *** No rule to make target /usr/local/nvidia/lib64/libcuda.so, needed by bin/gpu-example-alpha_comp. Stop.
But this is based on nvidia/cuda:8.0-cudnn5-devel and libcuda.so is there, i.e. if I run -it the container:
root#cf5637b85467:/opt/opencv/opencv/build# ll /usr/local/nvidia/lib64/libcuda*.*
lrwxrwxrwx 1 999 999 17 Nov 21 20:42 /usr/local/nvidia/lib64/libcuda.so -> libcuda.so.370.28
lrwxrwxrwx 1 999 999 17 Nov 21 20:42 /usr/local/nvidia/lib64/libcuda.so.1 -> libcuda.so.370.28
-rw-r--r-- 2 root root 8219624 Sep 2 02:25 /usr/local/nvidia/lib64/libcuda.so.370.28
and make completes without issue if I manually run it (I tried running from both /bin/bash and /bin/sh).
The only thing I can see is the user & group 999 of the .so link, but I don't know if that's relevant. In the worst case I could commit after building, but this seems a bad approach going forward. Any suggestions? Full Dockerfile below:
FROM nvidia/cuda:8.0-cudnn5-devel
RUN apt-get update &&\
apt-get install -y --no-install-recommends \
build-essential \
cmake \
curl \
gfortran \
git \
libatlas-base-dev \
libavcodec-dev \
libavformat-dev \
libfreetype6-dev \
libgtk2.0-dev \
libjasper-dev \
libjpeg8-dev \
libpng12-dev \
libswscale-dev \
libtiff4-dev \
libv4l-dev \
pkg-config \
python2.7-dev \
&& \
apt-get clean && \
apt-get autoremove && \
rm -rf /var/lib/apt/lists/*
# Get pip and pip installs
RUN curl -O https://bootstrap.pypa.io/get-pip.py && \
python get-pip.py && \
rm get-pip.py
RUN pip --no-cache-dir --timeout=60 install \
numpy
# Install opencv and opencv_contrib
WORKDIR /opt/opencv
RUN git clone --depth 1 https://github.com/opencv/opencv.git && \
git clone --depth 1 https://github.com/opencv/opencv_contrib.git
WORKDIR /opt/opencv/opencv/build
RUN cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_C_EXAMPLES=ON \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D OPENCV_EXTRA_MODULES_PATH=/opt/opencv/opencv_contrib/modules \
-D CUDA_CUDA_LIBRARY=/usr/local/nvidia/lib64/libcuda.so \
-D BUILD_EXAMPLES=ON ..
RUN make -j8
RUN make install

Resources