%% Pedestrian Detection
% This example shows code generation for pedestrian detection application that uses deep learning. Pedestrian detection is a key issue in computer vision. Pedestrian detection has several applications in the fields of autonomous driving, surveillance, robotics, and so on.
%% Third-Party Prerequisites
% Required
% This example generates CUDA® MEX and has the following third-party requirements.
% CUDA enabled NVIDIA® GPU and compatible driver.
% Optional
% For non-MEX builds such as static, dynamic libraries or executables, this example has the following additional requirements.
% NVIDIA toolkit.
% NVIDIA cuDNN library.
% Environment variables for the compilers and libraries. For more information, see Third-party Products and Environment Variables.
%% Verify GPU Environment
% Use the coder.checkGpuInstall function to verify that the compilers and libraries necessary for running this example are set up correctly.

envCfg = coder.gpuEnvConfig('host');
envCfg.DeepLibTarget = 'cudnn';
envCfg.DeepCodegen = 1;
envCfg.Quiet = 1;
coder.checkGpuInstall(envCfg);

%
%% The Pedestrian Detection Network
% The pedestrian detection network was trained by using images of pedestrians and non-pedestrians. This network is trained in MATLAB® by using the trainPedNet.m helper script. A sliding window approach crops patches from an image of size [64 32]. Patch dimensions are obtained from a heatmap, which represents the distribution of pedestrians in the images in the data set. It indicates the presence of pedestrians at various scales and locations in the images. In this example, patches of pedestrians close to the camera are cropped and processed. Non-Maximal Suppression (NMS) is applied on the obtained patches to merge them and detect complete pedestrians.
%
% The pedestrian detection network contains 12 layers which include convolution, fully connected, and classification output layers.

load('PedNet.mat');

% Use the analyzeNetwork function to display an interactive visualization of the deep learning network architecture.
% analyzeNetwork(PedNet);
%% The pedDetect_predict Entry-Point Function
% The pedDetect_predict.m entry-point function takes an image input and performs prediction on an image by using the deep learning network saved in the PedNet.mat file. The function loads the network object from the PedNet.mat file into a persistent variable pednet. Then function reuses the persistent object on subsequent calls.

type('pedDetect_predict.m')

%
%% Generate CUDA MEX for the pedDetect_predict Function
% Create a GPU Configuration object for a MEX target and set the target language to C++. Use the coder.DeepLearningConfig function to create a CuDNN deep learning configuration object and assign it to the DeepLearningConfig property of the GPU code configuration object. To generate CUDA MEX, use the codegen command and specify the size of the input image. This value corresponds to the input layer size of pedestrian detection network.

% Load an input image.
im = imread('test.jpg');
im = imresize(im,[480,640]);

cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn');
codegen -config cfg pedDetect_predict -args {im} -report

%
%% Run Generated MEX
% Call pednet_predict_mex on the input image.

imshow(im);
ped_bboxes = pedDetect_predict_mex(im);

%
% Display the final predictions.

outputImage = insertShape(im,'Rectangle',ped_bboxes,'LineWidth',3);
imshow(outputImage);

%
%% Classification on Video
% The included helper file pedDetect_predict.m grabs frames from a video, performs prediction, and displays the classification results on each of the captured video frames.
%   v = VideoReader('LiveData.avi');
%   fps = 0;
%   while hasFrame(v)
%      % Read frames from video
%      im = readFrame(v);
%      im = imresize(im,[480,640]);
%      % Call MEX function for pednet prediction
%      tic;
%      ped_bboxes = pedDetect_predict_mex(im);
%      newt = toc;
%      % fps
%      fps = .9*fps + .1*(1/newt);
%      % display
%      outputImage = insertShape(im,'Rectangle',ped_bboxes,'LineWidth',3);
%      imshow(outputImage)
%      pause(0.2)
%   end
% Clear the static network object that was loaded in memory.

clear mex;

%
% Copyright 2018-2021 The MathWorks, Inc.