Main Content

Deploy and Run Fog Rectification for Video on NVIDIA Jetson

This example shows how to generate and deploy a CUDA® executable for a video-based fog rectification application. The example shows the deployable code generation capabilities that the GPU Coder™ Support Package for NVIDIA® GPU provides for the MATLAB® VideoReader function. This example generates a CUDA application that reads the contents of a video file, performs fog rectification operation, and displays the output video on the NVIDIA hardware.

Prerequisites

Target Board Requirements

  • NVIDIA Jetson embedded platform.

  • Ethernet crossover cable to connect the target board and host PC (if the target board cannot be connected to a local network).

  • GStreamer and SDL libraries on the target.

  • Environment variables on the target for the compilers and libraries. For more information, see Install and Setup Prerequisites for NVIDIA Boards.

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

Development Host Requirements

  • GPU Coder for CUDA code generation. For a tutorial, see Get Started with GPU Coder.

  • Image Processing Toolbox™

  • Computer Vision Toolbox™

  • NVIDIA CUDA toolkit on the host.

  • GPU Coder Interface for Deep Learning Libraries support package. To install this support package, use the MATLAB® Add-On Explorer.

  • Environment variables for the compilers and libraries. For more information, see Third-Party Hardware and Setting Up the Prerequisite Products.

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.

gpucoderdemo_setup('fog_rectification_videoreader');

Connect to the NVIDIA Hardware

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 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-tx2-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.

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 function.

envCfg = coder.gpuEnvConfig('jetson');
envCfg.BasicCodegen = 1;
envCfg.Quiet = 1;
envCfg.HardwareObject = hwobj;
coder.checkGpuInstall(envCfg);

The fog_rectification Entry-Point Function

The fog_rectification.m function takes a foggy video as an input and displays the defogged video.

type fog_rectification
function fog_rectification() %#codegen

%   Copyright 2019 The MathWorks, Inc.

coder.gpu.kernelfun;
hwobj = jetson();
width = 600;
height = 404;
videoFileName = 'foggy.mp4';
vobj = VideoReader(hwobj,videoFileName,'Width',width,'Height',height);

while vobj.hasFrame
    input = vobj.readFrame;
    
    % run fog rectification on the frame
    out = fog_rectification_algorithm (input,width,height);
end
end

Prepare Fog Rectification Application

Include the VideoReader and display interfaces inside the fog rectification application for code generation.

hwobj = jetson;
videoFileName = 'foggy.mp4';
vobj = VideoReader(hwobj,videoFileName,'Width',640,'Height',480);
dispObj = imageDisplay(hwobj);

The video file name input accepts full and relative paths. For code generation, provide the video file path on the target. For example, if the video file is available at the location '/home/ubuntu/video/foggy.mp4', then you must provide this path.

Generate CUDA Code for the Target Board 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 fog_rectification entry-point function. After the code generation takes place on the host, the generated files are copied over and built on the target.

codegen('-config ',cfg,'fog_rectification','-report');

Copy Video File onto Target Board

Move the video file to the target board.

putFile(hwobj,'foggy.mp4', hwobj.workspaceDir);

Run Fog Rectification on Target Board

To run the generated executable on the target board, use the MATLAB runApplication function.

pid = runApplication(hwobj,'fog_rectification');

A window opens on the target hardware display showing the fog rectification output of the recorded video.

Cleanup

To remove the example files and return to the original folder, call the cleanup function. cleanup