Deploy and Run Sobel Edge Detection with I/O on NVIDIA Jetson Nano
This example shows you how to deploy Sobel edge detection application that uses a Raspberry Pi Camera Module V2 and displays the edge detected output on the NVIDIA Jetson Nano Hardware. The Sobel Edge Detection on NVIDIA Jetson Nano using Raspberry Pi Camera Module V2 example showed how to capture image frames from the Raspberry Pi Camera Module V2 on an NVIDIA Jetson Nano hardware and process them in the MATLAB® environment. This example shows how to generate code for accessing I/O peripherals (camera and display) and perform processing on the NVIDIA Jetson Nano hardware.
Prerequisites
Target Board Requirements
NVIDIA Jetson Nano embedded platform.
Raspberry Pi Camera Module V2 connected to the CSI host port of the target.
Ethernet crossover cable to connect the target board and host PC (if you cannot connect the target board to a local network).
NVIDIA CUDA toolkit installed on the board.
V4L2 and SDL (v1.2) libraries on the board.
GStreamer libraries on the board.
Environment variables on the target for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.
Development Host Requirements
GPU Coder for CUDA code generation. For a tutorial, see Get Started with GPU Coder (GPU Coder).
Environment variables for the compilers and libraries. For more information, see Third-Party Hardware (GPU Coder) and Setting Up the Prerequisite Products (GPU Coder).
Create a Folder and Copy Relevant Files
The following line of code creates a folder in your current working folder on the host and copies all the relevant files into this folder. If you cannot generate files in this folder, before running this command, change your current working folder.
nvidiademo_setup('sobel_edge_detection_deploy');
Connect to NVIDIA Jetson Nano
The support package uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA code on the Jetson Nano platforms. Connect the target platform to the same network as the host computer or use an Ethernet crossover cable to connect the board directly to the host computer. For information on how to set up and configure your board, see NVIDIA documentation.
To communicate with the NVIDIA hardware, create a live hardware connection object by using the jetson
function. You must know the host name or IP address, user name, and password of the target board to create a live hardware connection object. For example, when connecting to the target board for the first time, create a live object for Jetson hardware by using the command:
hwobj = jetson('jetson-nano-name','ubuntu','ubuntu');
During the hardware live object creation, the support package performs hardware and software checks, IO server installation, and gathers peripheral information on target. This information is displayed in the Command Window.
Run the getCameraList
function of the hwobj
object to find the available cameras. If this function outputs an empty table, then try re-connecting the camera and execute the function again.
camlist = getCameraList(hwobj);
Verify GPU Environment on Target Board
To verify that the compilers and libraries necessary for running this example are set up correctly, use the coder.checkGpuInstall
(GPU Coder) function.
envCfg = coder.gpuEnvConfig('jetson');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
envCfg.HardwareObject = hwobj;
coder.checkGpuInstall(envCfg);
Prepare Sobel Edge Detection Application for Deployment
The following lines of code enable code generation for the camera and display interfaces. For example, to capture from a camera named vi-output, imx219 6-0010
, use:
hwobj = jetson;
camObj = camera(hwobj,"vi-output, imx219 6-0010",[640 480]);
dispObj = imageDisplay(hwobj);
The getCameraList
function lists the optimum resolutions supported by the camera sensor. At these resolutions, the image acquisition pipeline works efficiently. Based on the requirements of your algorithm, you can also choose a lower resolution.
Add these lines of code to the sobelEdgeDetection.m
entry-point function.
function sobelEdgeDetection() %#codegen % Copyright 2020-2021 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
Generate CUDA Code for the Jetson Target Using GPU Coder
To generate a CUDA executable that you can deploy on to an NVIDIA target, create a GPU code configuration object for generating an executable.
cfg = coder.gpuConfig('exe');
To create a configuration object for the Jetson platform and assign it to the Hardware
property of the code configuration object cfg
, use the coder.hardware
function.
cfg.Hardware = coder.hardware('NVIDIA Jetson');
To specify the folder for performing remote build process on the target board, use the BuildDir
property. If the specified build folder does not exist on the target board, then the software creates a folder with the given name. If no value is assigned to cfg.Hardware.BuildDir
, the remote build process occurs in the last specified build folder. If there is no stored build folder value, the build process takes place in the home folder.
cfg.Hardware.BuildDir = '~/remoteBuildDir';
Set the GenerateExampleMain
property to generate an example C++ main file and compile it. This example does not require modifications to the generated main files.
cfg.GenerateExampleMain = 'GenerateCodeAndCompile';
To generate CUDA code, use the codegen
function and pass the GPU code configuration and the size of the inputs for and sobelEdgeDetection.m
entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target board.
codegen('-config ',cfg,'sobelEdgeDetection','-report');
Run Sobel Edge Detection on Target Board
To run the generated executable on the target board, use the runApplication
function.
Set the appropriate display environment.
hwobj.setDisplayEnvironment('1.0');
Run the application on target.
pid = runApplication(hwobj,'sobelEdgeDetection');
A window opens on the target hardware display showing the Sobel edge detection output of the live camera feed.
Cleanup
To remove the example files and return to the original folder, call the cleanup
function.
cleanup