This example shows you how to deploy Sobel edge detection that uses webcam and display on the NVIDIA® Hardware using the GPU Coder™ Support Package for NVIDIA® GPUs. In the example Sobel Edge Detection using Webcam on NVIDIA Hardware, we have seen accessing the webcam on NVIDIA hardware using the GPU Coder Support Package for NVIDIA GPUs. In the current example, we will generate code for accessing I/O (webcam and display) to deploy on the NVIDIA hardware. We will consider the same Sobel edge algorithm for showing this capability.
Target Board Requirements
NVIDIA Jetson TX1/TX2 embedded platform.
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.
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.
USB webcam connected to the USB host port of the target.
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 hardware, 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-tx2-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 webcam and display interfaces inside the Sobel edge detection application
hwobj = jetson; w = webcam(hwobj); d = imageDisplay(hwobj);
Sobel Edge MATLAB Application for Code Generation
function sobelEdgeDetection() %#codegen % Copyright 2018 The MathWorks, Inc. hwobj = jetson; % To redirect to the code generatable functions. w = webcam(hwobj,1); d = imageDisplay(hwobj); % Main loop kern = [1 2 1; 0 0 0; -1 -2 -1]; for k = 1:1800 % Capture the image from the webcam on hardware. img = snapshot(w); % 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(d,edgeImg); end end
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.
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.
Run the generated executable on the target.
pid = hwobj.runApplication('sobelEdgeDetection');
A window opens on the target hardware display showing the Sobel edge detection output of the live webcam 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 hardware on the live webcam feed and displaying the output on the native display.