-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegenCRAFT.m
56 lines (45 loc) · 1.79 KB
/
codegenCRAFT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%% Code Generation for CRAFT
% The following code demonstrates code generation for a pre-trained
% CRAFT text detection network.
%% Setup
% Add path to the source directory.
addpath('src');
%% Download the Pre-trained Network
helper.downloadPretrainedCRAFT;
%% Read and Preprocess Input Image
% Read input image
orgImage = imread('businessCard.png');
% Preprocess the image
[image, imageScale] = helper.preprocess(orgImage);
% Provide location of the mat file of the trained network
matFile = 'model/craftNet.mat';
%% Run MEX code generation
% The craft_predict.m is entry-point function that takes an input
% image and give output as region and affinity score map. The function
% uses a persistent object craftObj to
% load the dlnetwork object and reuses the persistent object for prediction
% on subsequent calls.
%
% To generate CUDA code for the craftPredict entry-point function,
% create a GPU code configuration object for a MEX target and set the
% target language to C++.
%
% Use the coder.DeepLearningConfig (GPU Coder) function to create a CuDNN
% deep learning configuration object and assign it to the DeepLearningConfig
% property of the GPU code configuration object.
%
% Run the codegen command.
cfg = coder.gpuConfig('mex');
cfg.TargetLang = 'C++';
cfg.DeepLearningConfig = coder.DeepLearningConfig('cudnn');
args = {coder.Constant(matFile), im2single(image)};
codegen -config cfg craftPredict -args args -report
%% Run Generated MEX
% Call craft_predict_mex on the input image
out = craftPredict_mex(matFile,im2single(image));
% apply post-processing on the output
boundingBoxes = helper.postprocess(out,imageScale);
% Visualize results
outImg = insertShape(orgImage,'Polygon',boundingBoxes,'LineWidth',5,'Color',"yellow");
figure, imshow(outImg)
% Copyright 2021 The MathWorks, Inc.