deepStream学习0----环境搭建(ubuntu18.04+vscode + docker + deepstream + opencv + c++)

用到的工具:

  1. docker

  2. docker pull nvcr.io/nvidia/deepstream:5.1-21.02-devel      (这里一定要下载-devel 后缀的,因为这个包含了开发sdk)

  3. 安装vscode(vscode可以直接在docker远程开发非常方便,直接在Ubuntu软件中搜索 vscode就有了)

===================== docker 安装 =======

安装 nvidia-docker

    在使用带有 cuda 环境的 docker 容器之前,首先需要安装 nvidia-docker 组件

添加 nvidia-docker 源

curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add -
curl -s -L https://nvidia.github.io/nvidia-docker/ubuntu18.04/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
sudo apt-get update

安装 nvidia-docker2

    安装 nvidia-docker2 后重启 docker 使得 nvidia-docker2 生效。

sudo apt-get install nvidia-docker2
sudo systemctl restart docker

==========  下载deepstream 镜像 一定要是  devel 版本的 =====

dGPU 的 Docker 容器

NGC 网络门户中的容器页面提供了拉取和运行容器的说明,以及对其内容的描述。dGPU 容器被调用deepstream,Jetson 容器被调用deepstream-l4t。与 DeepStream 3.0 中的容器不同,dGPU DeepStream 5.1 容器支持容器内的 DeepStream 应用程序开发。它包含与 DeepStream 5.1 SDK 相同的构建工具和开发库。在典型场景中,您在 DeepStream 容器内构建、执行和调试 DeepStream 应用程序。一旦您的应用程序准备就绪,您就可以使用 DeepStream 5.1 容器作为基础镜像来创建您自己的 Docker 容器来保存您的应用程序文件(二进制文件、库、模型、配置文件等)。这是一个示例片段用于创建您自己的 Docker 容器的Dockerfile:

# Replace with required container type e.g. base, devel etc in the following line FROM nvcr.io/nvidia/ deepstream:5.1-21.02- COPY myapp  /root/apps/myapp # To get video driver libraries at runtime (libnvidia-encode.so/libnvcuvid.so) ENV NVIDIA_DRIVER_CAPABILITIES $NVIDIA_DRIVER_CAPABILITIES,video

此Dockerfile将您的应用程序(从目录mydsapp)复制到容器 ( ) 中。请注意,您必须确保来自 NGC 的 DeepStream 5.1 图像位置准确。pathname /root/apps

下表列出了与 DeepStream 5.1 一起发布的用于 dGPU 的 docker 容器:

dGPU 的 Docker 容器

容器拉取命令 

docker pull nvcr.io/nvidia/deepstream:5.1-21.02-devel

==== docker + opencv + imshow 支持opencv窗口显示 ===

解决方法:通过在宿主机(比如我的是Ubuntu18.04)安装xserver,将docker容器视为客户端,这样可以将容器中需要显示的图像通过挂载的方式显示在宿主机的屏幕上,简单的讲就是将宿主机的屏幕共享给docker容器.

详细步骤:

第一步: 在宿主机上安装 xserver

    ​sudo apt install x11-xserver-utils

第二步: 在宿主机上修改权限,允许所有用户访问显示接口

        在你的要显示的电脑上执行 xhost +  #如果出现下面语句就可以了,记得每次重启都要执行一次,以防万一:(如果不想每次都输入,可以直接  把  xhost +   这个命令写到  ~/.bashrc 里面,然后 source  ~/.bashrc    这样每次打开终端都能自动生效了)

sy@sy:~$ xhost +
access control disabled, clients can connect from any host
sy@sy:~$ 

第三步:开启docker《这里一定要先之执行上面xhost +, 再开启docker,不然,会在执行demo的时候卡在running。。。》

 docker run --gpus all -it -p 127.0.0.1:6666:6666 --shm-size=5g --name=deepstream5 -v /tmp/.X11-unix:/tmp/.X11-unix -v /home/sy/work:/home/sy/work -e DISPLAY=$DISPLAY -e GDK_SCALE -e GDK_DPI_SCALE -w /opt/nvidia/deepstream/deepstream-5.1 deepstream:v5.1 /bin/bash

======== 安装vscode,配置vscode 的 docker 开发环境 -========

首先,创建一个CMakelists.txt 文件,写下一下内容:

cmake_minimum_required(VERSION 2.8)
project(DeepstreamApp)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DPLATFORM_TEGRA")
set(CMAKE_CXX_STANDARD 14)
set(SRC_PATH /opt/nvidia/deepstream/deepstream-5.1)
set(SRC_FOLDER ${SRC_PATH}/sources/apps/sample_apps/deepstream-app)
set(SRC_INC ${SRC_PATH}/sources/apps/apps-common/includes)
set(SRC_INC2 ${SRC_PATH}/sources/includes)
set(SRC_SRC ${SRC_PATH}/sources/apps/apps-common/src)
find_package(OpenCV REQUIRED)#REQUIRED 表示一定要找到包,找不到就停止
# message(STATUS "version: ${OpenCV_VERSION}$\n")
# message(STATUS "libraries:${OpenCV_LIBS}$\n")
# message(STATUS "INCLUDE PATH:${OpenCV_INCLUDE_DIRS}$\n")
include_directories(${PROJECT_SOURCE_DIR}
${SRC_FOLDER}
${SRC_INC}
${SRC_INC2}
${SRC_SRC}
/usr/include
/usr/include/opencv
/usr/include/opencv2
/usr/local/cuda/include
/usr/include/gstreamer-1.0
/usr/include/glib-2.0
/usr/lib/x86_64-linux-gnu/glib-2.0/include
/usr/include/json-glib-1.0)
link_directories(${SRC_PATH}/lib
/usr/local/cuda/lib64
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/gstreamer-1.0)
# list(REMOVE_ITEM SRCS1 "${SRC_INC}/deepstream_osd.h")
# list(REMOVE_ITEM SRCS1 "${SRC_SRC}/deepstream_osd_bin.c")
file(GLOB SRCS2 ${PROJECT_SOURCE_DIR}/*.c ${PROJECT_SOURCE_DIR}/*.cpp ${PROJECT_SOURCE_DIR}/*.h)
#message("sources1 ${SRCS1}")
#message("sources2 ${SRCS2}")
# add_executable(deepstreamApp ${SRCS1} ${SRCS2})
add_executable(deepstreamApp ${SRCS2})
# include cuda
# target_link_libraries(deepstreamApp -lcudart -lnvdsgst_meta -lnvds_meta -lnvdsgst_helper -lnvds_utils -lnvds_msgbroker -lgstrtp-1.0 -lX11 -lm -lgstreamer-1.0 -lgstrtspserver-1.0 -lglib-2.0 -lgobject-2.0 -lgstvideo-1.0 -lnvdsgst_smartrecord -ljson-glib-1.0)
target_link_libraries(deepstreamApp ${OpenCV_LIBS} -lcudart -lnvdsgst_meta -lnvds_meta -lnvdsgst_helper -lnvds_utils -lnvds_msgbroker -lgstrtp-1.0 -lX11 -lm -lgstreamer-1.0 -lgstrtspserver-1.0 -lglib-2.0 -lgobject-2.0 -lgstvideo-1.0 -lnvdsgst_smartrecord -ljson-glib-1.0)

这里要先启动容器,然后,才能 Ctrl+ship+P 里面看到  “C/Cpp: Edit configurations” 然后,你才能生成 c_cpp_properties.json 文件

然后,在vscode 包含的c++头文件的,c_cpp_properties.json 文件(这个文件不知道怎么生成的,可以自行百度)写如下一内容:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "/opt/nvidia/deepstream/deepstream-5.1/sources/apps/sample_apps/deepstream-app",
                "/opt/nvidia/deepstream/deepstream-5.1/sources/apps/apps-common/includes",
                "/opt/nvidia/deepstream/deepstream-5.1/sources/includes",
                "/opt/nvidia/deepstream/deepstream-5.1/sources/apps/apps-common/src",
                
                "/usr/include",
                "/usr/include/opencv",
                "/usr/include/opencv2",
                "/usr/include/gstreamer-1.0",
                "/usr/include/glib-2.0",
                "/usr/lib/x86_64-linux-gnu/glib-2.0/include",
                "/usr/include/json-glib-1.0",

                "${workspaceFolder}/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

然后,创建一个build 文件夹用来存放编译的文件;

然后cmake ..    ,

然后make 生成可执行文件;因为debug 需要有可执行文件才可以用

然后 Ctrl+ship+P  输入Tasks: Configure task生成配置文件 tasks.json

然后输入下面内容,这个就是 一个快捷命令,这样,你在界面点击 build  debug 之类的才会有相应:
 

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "make build",//编译的项目名,build
            "type": "shell",
            "command": "cd ./build ;cmake ../ ;make",//编译命令
            "group": {
                "kind": "build",
                "isDefault": true
            }
        },
        {
            "label": "clean",
            "type": "shell",
            "command": "make clean",
            

        }
    ]
}

然后,在vscode 下面就会出现(如果没有出现就,关闭,重新打开一下这个工程)

deepStream学习0----环境搭建(ubuntu18.04+vscode + docker + deepstream + opencv + c++)_第1张图片

你可能感兴趣的:(DeepStream,vscode,docker,ubuntu,opencv)