This example shows you how to deploy Sobel edge detection that uses Raspberry Pi Camera Module V2 and display on the NVIDIA Jetson Nano Hardware using the GPU Coder™ Support Package for NVIDIA® GPUs. In the example Sobel Edge Detection on NVIDIA Jetson Nano using Raspberry Pi Camera Module V2, 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.
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 the target board cannot be connected to a local network).
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 installing and setting up prerequisites for NVIDIA boards.
A monitor connected to the display port of the target.
Development Host Requirements
GPU Coder™ for code generation. For an overview and tutorials, visit the GPU Coder product page.
GPU Coder Support Package for NVIDIA GPUs. If you are new to this support package, we recommend completing the Getting Started with GPU Coder Support Package for NVIDIA GPUs example.
NVIDIA CUDA toolkit on the host.
Environment variables on the host for the compilers and libraries. For information on the supported versions of the compilers and libraries, see Third-party Products. For setting up the environment variables, see Environment Variables.
The GPU Coder Support Package for NVIDIA GPUs uses an SSH connection over TCP/IP to execute commands while building and running the generated CUDA code on the Jetson platform. You must therefore 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. Refer to the NVIDIA documentation on how to set up and configure your board.
To communicate with the NVIDIA Jetson Nano, you must create a live hardware connection object by using the jetson function. 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');
Use the coder.checkGpuInstall function 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);
Include the camera and display interfaces inside the Sobel edge detection application
hwobj = jetson; camObj = camera(hwobj,"vi-output, 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.
Sobel Edge MATLAB Application for Code Generation
function sobelEdgeDetection() %#codegen % Copyright 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
To generate a CUDA executable that can be deployed on to a NVIDIA target, create a GPU code configuration object for generating an executable.
cfg = coder.gpuConfig('exe');
Use the coder.hardware function to create a configuration object for the Jetson platform and assign it to the
Hardware property of the code configuration object
cfg.Hardware = coder.hardware('NVIDIA Jetson');
BuildDir property 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 to
cfg.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';
To generate CUDA code, use the codegen function and pass the GPU code configuration along with
sobelEdgeDetection entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target.
To run the generated executable on the target, use the MATLAB®
Set the appropriate display environment.
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.
Remove the files and return to the original folder.
This example introduced an application where Sobel edge detection application is running on the NVIDIA Jetson Nano on the live camera feed and displaying the output on the native display.