Documentation

Deploy and Run Sobel Edge Detection with I/O on NVIDIA Jetson Nano

此示例向您展示了如何使用Raspberry Pi摄像头模块V2部署Sobel Edge检测并使用NVIDIA®GPU的GPU Coder™支持软件包在NVIDIA JETSON NANO硬件上显示。金宝app在示例中使用Raspberry Pi摄像头V2在Nvidia Jetson Nano上进行SOBEL边缘检测, we have seen accessing the Raspberry Pi Camera Module V2 on NVIDIA Jetson Nano hardware using the GPU Coder Support Package for NVIDIA GPUs. In the current example, we will generate code for accessing I/O (camera and display) to deploy on the NVIDIA Jetson Nano hardware. We will consider the same Sobel edge algorithm for showing this capability.

Prerequisites

Target Board Requirements

  • NVIDIA Jetson Nano embedded platform.

  • Raspberry Pi摄像头模块V2连接到目标的CSI主机端口。

  • 以太网交叉电缆连接目标板和主机PC(如果目标板无法连接到本地网络)。

  • V4L2 and SDL (v1.2) libraries on the target.

  • GStreamer libraries on the target.

  • Environment variables on the target for the compilers and libraries. For information on the supported versions of the compilers and libraries and their setup, see安装和设置prerequisites for NVIDIA boards

  • A monitor connected to the display port of the target.

Development Host Requirements

连接到Nvidia Jetson Nano

NVIDIA GPU的GPU金宝app编码器支持软件包在构建和运行Jetson平台上生成的CUDA代码时,使用TCP/IP上的SSH连接执行命令。因此,您必须将目标平台连接到与主机计算机同一网络,或使用以太网交叉电缆将板直接连接到主机计算机。请参阅有关如何设置和配置板的NVIDIA文档。

To communicate with the NVIDIA Jetson Nano, you must create a live hardware connection object by using thejetsonfunction. You must know the host name or IP address, username, and password of the target board to create a live hardware connection object. For example,

hwobj = jetson('jetson-nano-name','ubuntu','ubuntu');

验证GPU环境

Use theCoder.CheckgPuinstallfunction and verify that the compilers and libraries needed for running this example are set up correctly.

envcfg = coder.gpuenvconfig('Jetson');envCfg.BasicCodegen = 1; envCfg.Quiet = 1; envCfg.HardwareObject = hwobj; coder.checkGpuInstall(envCfg);

准备独立部署的Sobel边缘检测应用程序

Include the camera and display interfaces inside the Sobel edge detection application

hwobj = jetson; camObj = camera(hwobj,“ vi-utput,imx219 6-0010”,[640 480]); dispObj = imageDisplay(hwobj);

将相关文件复制到当前目录

The following line of code creates a folder in your current working directory (host), and copies all the relevant files into this folder.if you cannot generate files in this folder, change your current working directory before running this command.

gpucoderdemo_setup('sobel_edge_detection_deploy');

SOBEL EDGE MATLAB应用代码生成

typesobeLedgedEtection.m
function sobelEdgeDetection() %#codegen % Copyright 2018-2020 The MathWorks, Inc. hwobj = jetson; camObj = camera(hwobj,"vi-output, imx219 6-0010",[640 480]); dispObj = imageDisplay(hwobj); % Sobel kernel kern = [1 2 1; 0 0 0; -1 -2 -1]; % Main loop for k = 1:1000 % Capture the image from the camera on hardware. img = snapshot(camObj); % Finding horizontal and vertical gradients. h = conv2(img(:,:,2),kern,'same'); v = conv2(img(:,:,2),kern','same'); % Finding magnitude of the gradients. e = sqrt(h.*h + v.*v); % Threshold the edges edgeImg = uint8((e > 100) * 240); % Display image. image(dispObj,edgeImg); end end

使用GPU编码器为目标生成CUDA代码

要生成可以部署到NVIDIA目标的CUDA可执行文件,请创建一个GPU代码配置对象来生成可执行文件。

cfg = coder.gpuConfig('exe');

Use thecoder.hardware函数以创建Jetson平台的配置对象并将其分配给Hardware代码配置对象的属性cfg

cfg.Hardware = coder.hardware('NVIDIA Jetson');

Use thebuilddirproperty to specify the directory for performing remote build process on the target. If the specified build directory does not exist on the target then the software creates a directory with the given name. If no value is assigned tocfg.hardware.builddir, the remote build process happens in the last specified build directory. If there is no stored build directory value, the build process takes place in the home directory.

cfg.hardware.builddir='~/remoteBuildDir';

Set the GenerateExampleMain property to generate an example C++ main function and compile it. This example does not require modifications to the generated main files.

cfg.GenerateExampleMain ='GenerateCodeAndCompile';

要生成CUDA代码,请使用codegen功能并传递GPU代码配置以及sobeledgedEtection入口点功能。代码生成在主机上发生后,生成的文件将被复制并在目标上构建。

codegen ('-config ',cfg,'sobelEdgeDetection','-报告');

在目标上运行SOBEL边缘检测

要在目标上运行生成的可执行文件,请使用MATLAB®runApplicationfunction.

Set the appropriate display environment.

hwobj.setdisplayenvironment('1.0');

在目标上运行应用程序。

pid = runapplication(hwobj,'sobelEdgeDetection');

窗口在目标硬件显示屏上打开,显示了实时相机供稿的SOBEL边缘检测输出。

Cleanup

Remove the files and return to the original folder.

清理

概括

该示例介绍了一个应用程序,其中Sobel Edge检测应用程序在Live Camera Feed上的Nvidia Jetson Nano上运行,并在本机显示上显示输出。