Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ModelZoo
crnn_migraphx
Commits
4fe0fdcb
Commit
4fe0fdcb
authored
Nov 15, 2023
by
liucong
Browse files
重新格式化Cpp代码
parent
2f028833
Changes
9
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
865 additions
and
840 deletions
+865
-840
Src/Crnn.cpp
Src/Crnn.cpp
+55
-60
Src/Crnn.h
Src/Crnn.h
+6
-6
Src/Utility/CommonDefinition.h
Src/Utility/CommonDefinition.h
+16
-17
Src/Utility/CommonUtility.cpp
Src/Utility/CommonUtility.cpp
+14
-12
Src/Utility/CommonUtility.h
Src/Utility/CommonUtility.h
+4
-4
Src/Utility/Filesystem.cpp
Src/Utility/Filesystem.cpp
+576
-570
Src/Utility/Filesystem.h
Src/Utility/Filesystem.h
+27
-19
Src/Utility/SimpleLog.h
Src/Utility/SimpleLog.h
+136
-117
Src/main.cpp
Src/main.cpp
+31
-35
No files found.
Src/Crnn.cpp
View file @
4fe0fdcb
...
...
@@ -4,24 +4,17 @@
#include <Filesystem.h>
#include <SimpleLog.h>
namespace
migraphxSamples
{
Crnn
::
Crnn
()
{
Crnn
::
Crnn
()
{}
}
Crnn
::~
Crnn
()
{
configurationFile
.
release
();
}
Crnn
::~
Crnn
()
{
configurationFile
.
release
();
}
ErrorCode
Crnn
::
Initialize
(
InitializationParameterOfOcr
initializationParameterOfOcr
,
bool
dynamic
)
{
// 读取配置文件
std
::
string
configFilePath
=
initializationParameterOfOcr
.
configFilePath
;
std
::
string
configFilePath
=
initializationParameterOfOcr
.
configFilePath
;
if
(
!
Exists
(
configFilePath
))
{
LOG_ERROR
(
stdout
,
"no configuration file!
\n
"
);
...
...
@@ -36,60 +29,60 @@ ErrorCode Crnn::Initialize(InitializationParameterOfOcr initializationParameterO
// 获取配置文件参数
cv
::
FileNode
netNode
=
configurationFile
[
"CrnnDynamic"
];
std
::
string
modelPath
=
(
std
::
string
)
netNode
[
"ModelPath"
];
std
::
string
modelPath
=
(
std
::
string
)
netNode
[
"ModelPath"
];
// 加载模型
if
(
!
Exists
(
modelPath
))
{
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
LOG_ERROR
(
stdout
,
"%s not exist!
\n
"
,
modelPath
.
c_str
());
return
MODEL_NOT_EXIST
;
}
if
(
dynamic
)
{
migraphx
::
onnx_options
onnx_options
;
onnx_options
.
map_input_dims
[
"input"
]
=
{
1
,
1
,
32
,
512
};
onnx_options
.
map_input_dims
[
"input"
]
=
{
1
,
1
,
32
,
512
};
net
=
migraphx
::
parse_onnx
(
modelPath
,
onnx_options
);
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入/输出节点信息
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
int
C
=
inputShape
.
lens
()[
1
];
int
H
=
inputShape
.
lens
()[
2
];
int
W
=
inputShape
.
lens
()[
3
];
inputSize
=
cv
::
Size
(
W
,
H
);
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
int
C
=
inputShape
.
lens
()[
1
];
int
H
=
inputShape
.
lens
()[
2
];
int
W
=
inputShape
.
lens
()[
3
];
inputSize
=
cv
::
Size
(
W
,
H
);
// log输出日志信息
LOG_INFO
(
stdout
,
"InputMaxSize:%dx%d
\n
"
,
inputSize
.
width
,
inputSize
.
height
);
LOG_INFO
(
stdout
,
"InputName:%s
\n
"
,
inputName
.
c_str
());
LOG_INFO
(
stdout
,
"InputMaxSize:%dx%d
\n
"
,
inputSize
.
width
,
inputSize
.
height
);
LOG_INFO
(
stdout
,
"InputName:%s
\n
"
,
inputName
.
c_str
());
}
else
{
migraphx
::
onnx_options
onnx_options
;
onnx_options
.
map_input_dims
[
"input"
]
=
{
1
,
1
,
32
,
100
};
onnx_options
.
map_input_dims
[
"input"
]
=
{
1
,
1
,
32
,
100
};
net
=
migraphx
::
parse_onnx
(
modelPath
,
onnx_options
);
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
LOG_INFO
(
stdout
,
"succeed to load model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// 获取模型输入/输出节点信息
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
int
C
=
inputShape
.
lens
()[
1
];
int
H
=
inputShape
.
lens
()[
2
];
int
W
=
inputShape
.
lens
()[
3
];
inputSize
=
cv
::
Size
(
W
,
H
);
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
inputs
=
net
.
get_inputs
();
std
::
unordered_map
<
std
::
string
,
migraphx
::
shape
>
outputs
=
net
.
get_outputs
();
inputName
=
inputs
.
begin
()
->
first
;
inputShape
=
inputs
.
begin
()
->
second
;
int
N
=
inputShape
.
lens
()[
0
];
int
C
=
inputShape
.
lens
()[
1
];
int
H
=
inputShape
.
lens
()[
2
];
int
W
=
inputShape
.
lens
()[
3
];
inputSize
=
cv
::
Size
(
W
,
H
);
// log输出日志信息
LOG_INFO
(
stdout
,
"InputSize:%dx%d
\n
"
,
inputSize
.
width
,
inputSize
.
height
);
LOG_INFO
(
stdout
,
"InputName:%s
\n
"
,
inputName
.
c_str
());
LOG_INFO
(
stdout
,
"InputSize:%dx%d
\n
"
,
inputSize
.
width
,
inputSize
.
height
);
LOG_INFO
(
stdout
,
"InputName:%s
\n
"
,
inputName
.
c_str
());
}
// 设置模型为GPU模式
...
...
@@ -97,22 +90,23 @@ ErrorCode Crnn::Initialize(InitializationParameterOfOcr initializationParameterO
// 编译模型
migraphx
::
compile_options
options
;
options
.
device_id
=
0
;
// 设置GPU设备,默认为0号设备
options
.
offload_copy
=
true
;
net
.
compile
(
gpuTarget
,
options
);
LOG_INFO
(
stdout
,
"succeed to compile model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
options
.
device_id
=
0
;
// 设置GPU设备,默认为0号设备
options
.
offload_copy
=
true
;
net
.
compile
(
gpuTarget
,
options
);
LOG_INFO
(
stdout
,
"succeed to compile model: %s
\n
"
,
GetFileName
(
modelPath
).
c_str
());
// warm up
std
::
unordered_map
<
std
::
string
,
migraphx
::
argument
>
inputData
;
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
};
inputData
[
inputName
]
=
migraphx
::
argument
{
inputShape
};
net
.
eval
(
inputData
);
return
SUCCESS
;
}
ErrorCode
Crnn
::
Infer
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
char
>
&
resultsChar
,
bool
raw
,
bool
dynamic
)
ErrorCode
Crnn
::
Infer
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
char
>&
resultsChar
,
bool
raw
,
bool
dynamic
)
{
if
(
srcImage
.
empty
()
||
srcImage
.
type
()
!=
CV_8UC3
)
if
(
srcImage
.
empty
()
||
srcImage
.
type
()
!=
CV_8UC3
)
{
LOG_ERROR
(
stdout
,
"image error!
\n
"
);
return
IMAGE_ERROR
;
...
...
@@ -135,9 +129,9 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
}
inputBlob
=
cv
::
dnn
::
blobFromImage
(
inputImage
);
for
(
int
i
=
0
;
i
<
width
*
height
;
i
++
)
for
(
int
i
=
0
;
i
<
width
*
height
;
i
++
)
{
*
((
float
*
)
inputBlob
.
data
+
i
)
=
((
*
((
float
*
)
inputBlob
.
data
+
i
))
/
255.
f
-
0.5
)
/
0.5
;
*
((
float
*
)
inputBlob
.
data
+
i
)
=
((
*
((
float
*
)
inputBlob
.
data
+
i
))
/
255.
f
-
0.5
)
/
0.5
;
}
// 创建输入数据
...
...
@@ -145,7 +139,8 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
if
(
dynamic
)
{
std
::
vector
<
std
::
size_t
>
dynamicShape
=
{
1
,
1
,
32
,
width
};
inputData
[
inputName
]
=
migraphx
::
argument
{
migraphx
::
shape
(
inputShape
.
type
(),
dynamicShape
),
(
float
*
)
inputBlob
.
data
};
inputData
[
inputName
]
=
migraphx
::
argument
{
migraphx
::
shape
(
inputShape
.
type
(),
dynamicShape
),
(
float
*
)
inputBlob
.
data
};
}
else
{
...
...
@@ -161,18 +156,18 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
// 转换为cv::Mat
migraphx
::
shape
outputShape
=
result
.
get_shape
();
int
shape
[]
=
{
outputShape
.
lens
()[
0
],
outputShape
.
lens
()[
1
],
outputShape
.
lens
()[
2
]};
cv
::
Mat
out
(
3
,
shape
,
CV_32F
);
memcpy
(
out
.
data
,
result
.
data
(),
sizeof
(
float
)
*
outputShape
.
elements
());
int
shape
[]
=
{
outputShape
.
lens
()[
0
],
outputShape
.
lens
()[
1
],
outputShape
.
lens
()[
2
]};
cv
::
Mat
out
(
3
,
shape
,
CV_32F
);
memcpy
(
out
.
data
,
result
.
data
(),
sizeof
(
float
)
*
outputShape
.
elements
());
outs
.
push_back
(
out
);
std
::
vector
<
int
>
predChars
;
const
std
::
string
alphabet
=
"-0123456789abcdefghijklmnopqrstuvwxyz"
;
//获取字符索引序列
//
获取字符索引序列
for
(
uint
i
=
0
;
i
<
outs
[
0
].
size
[
0
];
i
++
)
{
cv
::
Mat
scores
=
cv
::
Mat
(
1
,
outs
[
0
].
size
[
2
],
CV_32F
,
outs
[
0
].
ptr
<
float
>
(
i
));
cv
::
Mat
scores
=
cv
::
Mat
(
1
,
outs
[
0
].
size
[
2
],
CV_32F
,
outs
[
0
].
ptr
<
float
>
(
i
));
cv
::
Point
charIdPoint
;
double
maxCharScore
;
cv
::
minMaxLoc
(
scores
,
0
,
&
maxCharScore
,
0
,
&
charIdPoint
);
...
...
@@ -180,8 +175,8 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
predChars
.
push_back
(
maxIdx
);
}
//字符转录处理
for
(
uint
i
=
0
;
i
<
predChars
.
size
();
i
++
)
//
字符转录处理
for
(
uint
i
=
0
;
i
<
predChars
.
size
();
i
++
)
{
if
(
raw
)
{
...
...
@@ -191,7 +186,7 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
{
if
(
predChars
[
i
]
!=
0
)
{
if
(
!
(
i
>
0
&&
predChars
[
i
-
1
]
==
predChars
[
i
]))
if
(
!
(
i
>
0
&&
predChars
[
i
-
1
]
==
predChars
[
i
]))
{
resultsChar
.
push_back
(
alphabet
[
predChars
[
i
]]);
}
...
...
@@ -201,4 +196,4 @@ ErrorCode Crnn::Infer(const cv::Mat &srcImage, std::vector<char> &resultsChar, b
return
SUCCESS
;
}
}
}
// namespace migraphxSamples
Src/Crnn.h
View file @
4fe0fdcb
...
...
@@ -9,24 +9,24 @@ namespace migraphxSamples
class
Crnn
{
public:
public:
Crnn
();
~
Crnn
();
ErrorCode
Initialize
(
InitializationParameterOfOcr
initializationParameterOfOcr
,
bool
dynamic
);
ErrorCode
Infer
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
char
>
&
resultsChar
,
bool
raw
,
bool
dynamic
);
ErrorCode
Infer
(
const
cv
::
Mat
&
srcImage
,
std
::
vector
<
char
>&
resultsChar
,
bool
raw
,
bool
dynamic
);
private:
private:
cv
::
FileStorage
configurationFile
;
migraphx
::
program
net
;
cv
::
Size
inputSize
;
std
::
string
inputName
;
migraphx
::
shape
inputShape
;
};
}
}
// namespace migraphxSamples
#endif
Src/Utility/CommonDefinition.h
View file @
4fe0fdcb
...
...
@@ -19,21 +19,21 @@ namespace migraphxSamples
typedef
enum
_ErrorCode
{
SUCCESS
=
0
,
// 0
SUCCESS
=
0
,
// 0
MODEL_NOT_EXIST
,
// 模型不存在
CONFIG_FILE_NOT_EXIST
,
// 配置文件不存在
FAIL_TO_LOAD_MODEL
,
// 加载模型失败
FAIL_TO_OPEN_CONFIG_FILE
,
// 加载配置文件失败
IMAGE_ERROR
,
// 图像错误
}
ErrorCode
;
}
ErrorCode
;
typedef
struct
_ResultOfPrediction
{
float
confidence
;
int
label
;
_ResultOfPrediction
()
:
confidence
(
0.0
f
),
label
(
0
){}
_ResultOfPrediction
()
:
confidence
(
0.0
f
),
label
(
0
)
{}
}
ResultOfPrediction
;
}
ResultOfPrediction
;
typedef
struct
_ResultOfDetection
{
...
...
@@ -43,15 +43,15 @@ typedef struct _ResultOfDetection
std
::
string
className
;
bool
exist
;
_ResultOfDetection
()
:
confidence
(
0.0
f
),
classID
(
0
),
exist
(
true
){}
_ResultOfDetection
()
:
confidence
(
0.0
f
),
classID
(
0
),
exist
(
true
)
{}
}
ResultOfDetection
;
}
ResultOfDetection
;
typedef
struct
_InitializationParameterOfDetector
{
std
::
string
parentPath
;
std
::
string
configFilePath
;
}
InitializationParameterOfDetector
;
}
InitializationParameterOfDetector
;
typedef
struct
_InitializationParameterOfDetector
InitializationParameterOfClassifier
;
typedef
struct
_InitializationParameterOfDetector
InitializationParameterOfSuperresolution
;
...
...
@@ -61,7 +61,6 @@ typedef struct _InitializationParameterOfDetector InitializationParameterOfOcr;
typedef
struct
_InitializationParameterOfDetector
InitializationParameterOfDB
;
typedef
struct
_InitializationParameterOfDetector
InitializationParameterOfSVTR
;
}
}
// namespace migraphxSamples
#endif
Src/Utility/CommonUtility.cpp
View file @
4fe0fdcb
...
...
@@ -3,34 +3,37 @@
namespace
migraphxSamples
{
bool
CompareConfidence
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
)
bool
CompareConfidence
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
)
{
return
L
.
confidence
>
R
.
confidence
;
}
bool
CompareArea
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
)
bool
CompareArea
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
)
{
return
L
.
boundingBox
.
area
()
>
R
.
boundingBox
.
area
();
}
void
NMS
(
std
::
vector
<
ResultOfDetection
>
&
detections
,
float
IOUThreshold
)
void
NMS
(
std
::
vector
<
ResultOfDetection
>&
detections
,
float
IOUThreshold
)
{
// sort
std
::
sort
(
detections
.
begin
(),
detections
.
end
(),
CompareConfidence
);
for
(
int
i
=
0
;
i
<
detections
.
size
();
++
i
)
for
(
int
i
=
0
;
i
<
detections
.
size
();
++
i
)
{
if
(
detections
[
i
].
exist
)
if
(
detections
[
i
].
exist
)
{
for
(
int
j
=
i
+
1
;
j
<
detections
.
size
();
++
j
)
for
(
int
j
=
i
+
1
;
j
<
detections
.
size
();
++
j
)
{
if
(
detections
[
j
].
exist
)
if
(
detections
[
j
].
exist
)
{
// compute IOU
float
intersectionArea
=
(
detections
[
i
].
boundingBox
&
detections
[
j
].
boundingBox
).
area
();
float
intersectionRate
=
intersectionArea
/
(
detections
[
i
].
boundingBox
.
area
()
+
detections
[
j
].
boundingBox
.
area
()
-
intersectionArea
);
float
intersectionArea
=
(
detections
[
i
].
boundingBox
&
detections
[
j
].
boundingBox
).
area
();
float
intersectionRate
=
intersectionArea
/
(
detections
[
i
].
boundingBox
.
area
()
+
detections
[
j
].
boundingBox
.
area
()
-
intersectionArea
);
if
(
intersectionRate
>
IOUThreshold
)
if
(
intersectionRate
>
IOUThreshold
)
{
detections
[
j
].
exist
=
false
;
}
...
...
@@ -38,7 +41,6 @@ void NMS(std::vector<ResultOfDetection> &detections, float IOUThreshold)
}
}
}
}
}
}
// namespace migraphxSamples
Src/Utility/CommonUtility.h
View file @
4fe0fdcb
...
...
@@ -9,12 +9,12 @@ namespace migraphxSamples
{
// 排序规则: 按照置信度或者按照面积排序
bool
CompareConfidence
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
);
bool
CompareArea
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
);
bool
CompareConfidence
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
);
bool
CompareArea
(
const
ResultOfDetection
&
L
,
const
ResultOfDetection
&
R
);
// 非极大抑制
void
NMS
(
std
::
vector
<
ResultOfDetection
>
&
detections
,
float
IOUThreshold
);
void
NMS
(
std
::
vector
<
ResultOfDetection
>&
detections
,
float
IOUThreshold
);
}
}
// namespace migraphxSamples
#endif
Src/Utility/Filesystem.cpp
View file @
4fe0fdcb
This diff is collapsed.
Click to expand it.
Src/Utility/Filesystem.h
View file @
4fe0fdcb
...
...
@@ -10,19 +10,19 @@ namespace migraphxSamples
{
// 路径是否存在
bool
Exists
(
const
std
::
string
&
path
);
bool
Exists
(
const
std
::
string
&
path
);
// 路径是否为目录
bool
IsDirectory
(
const
std
::
string
&
path
);
bool
IsDirectory
(
const
std
::
string
&
path
);
// 是否是路径分隔符(Linux:‘/’,Windows:’\\’)
bool
IsPathSeparator
(
char
c
);
// 路径拼接
std
::
string
JoinPath
(
const
std
::
string
&
base
,
const
std
::
string
&
path
);
std
::
string
JoinPath
(
const
std
::
string
&
base
,
const
std
::
string
&
path
);
// 创建多级目录,注意:创建多级目录的时候,目标目录是不能有文件存在的
bool
CreateDirectories
(
const
std
::
string
&
directoryPath
);
bool
CreateDirectories
(
const
std
::
string
&
directoryPath
);
/** 生成符合指定模式的文件名列表(支持递归遍历)
*
...
...
@@ -36,25 +36,33 @@ bool CreateDirectories(const std::string &directoryPath);
5. 不能返回子目录名
*
*/
void
GetFileNameList
(
const
std
::
string
&
directory
,
const
std
::
string
&
pattern
,
std
::
vector
<
std
::
string
>
&
result
,
bool
recursive
,
bool
addPath
);
void
GetFileNameList
(
const
std
::
string
&
directory
,
const
std
::
string
&
pattern
,
std
::
vector
<
std
::
string
>&
result
,
bool
recursive
,
bool
addPath
);
// 与GetFileNameList的区别在于如果有子目录,在addPath为true的时候会返回子目录路径(目录名最后有"/")
void
GetFileNameList2
(
const
std
::
string
&
directory
,
const
std
::
string
&
pattern
,
std
::
vector
<
std
::
string
>
&
result
,
bool
recursive
,
bool
addPath
);
void
GetFileNameList2
(
const
std
::
string
&
directory
,
const
std
::
string
&
pattern
,
std
::
vector
<
std
::
string
>&
result
,
bool
recursive
,
bool
addPath
);
// 删除文件或者目录,支持递归删除
void
Remove
(
const
std
::
string
&
directory
,
const
std
::
string
&
extension
=
""
);
void
Remove
(
const
std
::
string
&
directory
,
const
std
::
string
&
extension
=
""
);
/** 获取路径的文件名和扩展名
*
* 示例:path为D:/1/1.txt,则GetFileName()为1.txt,GetFileName_NoExtension()为1,GetExtension()为.txt,GetParentPath()为D:/1/
*/
std
::
string
GetFileName
(
const
std
::
string
&
path
);
std
::
string
GetFileName_NoExtension
(
const
std
::
string
&
path
);
std
::
string
GetExtension
(
const
std
::
string
&
path
);
std
::
string
GetParentPath
(
const
std
::
string
&
path
);
*/
std
::
string
GetFileName
(
const
std
::
string
&
path
);
std
::
string
GetFileName_NoExtension
(
const
std
::
string
&
path
);
std
::
string
GetExtension
(
const
std
::
string
&
path
);
std
::
string
GetParentPath
(
const
std
::
string
&
path
);
// 拷贝文件
bool
CopyFile
(
const
std
::
string
srcPath
,
const
std
::
string
dstPath
);
bool
CopyFile
(
const
std
::
string
srcPath
,
const
std
::
string
dstPath
);
/** 拷贝目录
*
...
...
@@ -63,8 +71,8 @@ bool CopyFile(const std::string srcPath,const std::string dstPath);
1.第一个参数的最后不能加”/”
2.不能拷贝隐藏文件
*/
bool
CopyDirectories
(
std
::
string
srcPath
,
const
std
::
string
dstPath
);
bool
CopyDirectories
(
std
::
string
srcPath
,
const
std
::
string
dstPath
);
}
}
// namespace migraphxSamples
#endif
Src/Utility/SimpleLog.h
View file @
4fe0fdcb
...
...
@@ -8,7 +8,7 @@
#include <map>
#include <thread>
#include <mutex>
#if
(defined WIN32 || defined _WIN32)
#if(defined WIN32 || defined _WIN32)
#include <Windows.h>
#else
#include <sys/time.h>
...
...
@@ -16,13 +16,13 @@
using
namespace
std
;
/** 简易日志
*
* 不依赖于其他第三方库,只需要包含一个头文件就可以使用。提供了4种日志级别,包括INFO,DEBUG,WARN和ERROR。
*
* 示例1:
// 初始化日志,在./Log/目录下创建两个日志文件log1.log和log2.log(注意:目录./Log/需要存在,否则日志创建失败)
//
初始化日志,在./Log/目录下创建两个日志文件log1.log和log2.log(注意:目录./Log/需要存在,否则日志创建失败)
LogManager::GetInstance()->Initialize("./Log/","log1");
LogManager::GetInstance()->Initialize("./Log/","log2");
...
...
@@ -50,44 +50,43 @@ using namespace std;
class
LogManager
{
private:
LogManager
(){}
private:
LogManager
()
{}
public:
~
LogManager
(){}
inline
void
Initialize
(
const
string
&
parentPath
,
const
string
&
logName
)
public:
~
LogManager
()
{}
inline
void
Initialize
(
const
string
&
parentPath
,
const
string
&
logName
)
{
// 日志名为空表示输出到控制台
if
(
logName
.
size
()
==
0
)
if
(
logName
.
size
()
==
0
)
return
;
// 查找该日志文件,如果没有则创建
std
::
map
<
string
,
FILE
*>::
const_iterator
iter
=
logMap
.
find
(
logName
);
if
(
iter
==
logMap
.
end
())
if
(
iter
==
logMap
.
end
())
{
string
pathOfLog
=
parentPath
+
logName
+
".log"
;
FILE
*
logFile
=
fopen
(
pathOfLog
.
c_str
(),
"a"
);
// w:覆盖原有文件,a:追加
if
(
logFile
!=
NULL
)
string
pathOfLog
=
parentPath
+
logName
+
".log"
;
FILE
*
logFile
=
fopen
(
pathOfLog
.
c_str
(),
"a"
);
// w:覆盖原有文件,a:追加
if
(
logFile
!=
NULL
)
{
logMap
.
insert
(
std
::
make_pair
(
logName
,
logFile
));
}
}
}
inline
FILE
*
GetLogFile
(
const
string
&
logName
)
inline
FILE
*
GetLogFile
(
const
string
&
logName
)
{
std
::
map
<
string
,
FILE
*>::
const_iterator
iter
=
logMap
.
find
(
logName
);
if
(
iter
==
logMap
.
end
())
std
::
map
<
string
,
FILE
*>::
const_iterator
iter
=
logMap
.
find
(
logName
);
if
(
iter
==
logMap
.
end
())
{
return
NULL
;
}
return
(
*
iter
).
second
;
}
inline
void
Close
(
const
string
&
logName
)
inline
void
Close
(
const
string
&
logName
)
{
std
::
map
<
string
,
FILE
*>::
const_iterator
iter
=
logMap
.
find
(
logName
);
if
(
iter
==
logMap
.
end
())
std
::
map
<
string
,
FILE
*>::
const_iterator
iter
=
logMap
.
find
(
logName
);
if
(
iter
==
logMap
.
end
())
{
return
;
}
...
...
@@ -95,10 +94,7 @@ public:
fclose
((
*
iter
).
second
);
logMap
.
erase
(
iter
);
}
inline
std
::
mutex
&
GetLogMutex
()
{
return
logMutex
;
}
inline
std
::
mutex
&
GetLogMutex
()
{
return
logMutex
;
}
// Singleton
static
LogManager
*
GetInstance
()
...
...
@@ -106,17 +102,18 @@ public:
static
LogManager
logManager
;
return
&
logManager
;
}
private:
private:
std
::
map
<
string
,
FILE
*>
logMap
;
std
::
mutex
logMutex
;
};
#ifdef LOG_MUTEX
#define LOCK
LogManager::GetInstance()->GetLogMutex().lock()
#define UNLOCK
LogManager::GetInstance()->GetLogMutex().unlock()
#define LOCK
LogManager::GetInstance()->GetLogMutex().lock()
#define UNLOCK
LogManager::GetInstance()->GetLogMutex().unlock()
#else
#define LOCK
#define UNLOCK
#define LOCK
#define UNLOCK
#endif
// log time
...
...
@@ -131,53 +128,53 @@ typedef struct _LogTime
string
millisecond
;
// ms
string
microsecond
;
// us
string
weekDay
;
}
LogTime
;
}
LogTime
;
inline
LogTime
GetTime
()
{
LogTime
currentTime
;
#if
(defined WIN32 || defined _WIN32)
#if(defined WIN32 || defined _WIN32)
SYSTEMTIME
systemTime
;
GetLocalTime
(
&
systemTime
);
char
temp
[
8
]
=
{
0
};
char
temp
[
8
]
=
{
0
};
sprintf
(
temp
,
"%04d"
,
systemTime
.
wYear
);
currentTime
.
year
=
string
(
temp
);
currentTime
.
year
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
systemTime
.
wMonth
);
currentTime
.
month
=
string
(
temp
);
currentTime
.
month
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
systemTime
.
wDay
);
currentTime
.
day
=
string
(
temp
);
currentTime
.
day
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
systemTime
.
wHour
);
currentTime
.
hour
=
string
(
temp
);
currentTime
.
hour
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
systemTime
.
wMinute
);
currentTime
.
minute
=
string
(
temp
);
currentTime
.
minute
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
systemTime
.
wSecond
);
currentTime
.
second
=
string
(
temp
);
currentTime
.
second
=
string
(
temp
);
sprintf
(
temp
,
"%03d"
,
systemTime
.
wMilliseconds
);
currentTime
.
millisecond
=
string
(
temp
);
currentTime
.
millisecond
=
string
(
temp
);
sprintf
(
temp
,
"%d"
,
systemTime
.
wDayOfWeek
);
currentTime
.
weekDay
=
string
(
temp
);
currentTime
.
weekDay
=
string
(
temp
);
#else
struct
timeval
tv
;
struct
tm
*
p
;
struct
tm
*
p
;
gettimeofday
(
&
tv
,
NULL
);
p
=
localtime
(
&
tv
.
tv_sec
);
char
temp
[
8
]
=
{
0
};
sprintf
(
temp
,
"%04d"
,
1900
+
p
->
tm_year
);
currentTime
.
year
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
1
+
p
->
tm_mon
);
currentTime
.
month
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_mday
);
currentTime
.
day
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_hour
);
currentTime
.
hour
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_min
);
currentTime
.
minute
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_sec
);
currentTime
.
second
=
string
(
temp
);
sprintf
(
temp
,
"%03d"
,(
int
)(
tv
.
tv_usec
/
1000
));
char
temp
[
8
]
=
{
0
};
sprintf
(
temp
,
"%04d"
,
1900
+
p
->
tm_year
);
currentTime
.
year
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
1
+
p
->
tm_mon
);
currentTime
.
month
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_mday
);
currentTime
.
day
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_hour
);
currentTime
.
hour
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_min
);
currentTime
.
minute
=
string
(
temp
);
sprintf
(
temp
,
"%02d"
,
p
->
tm_sec
);
currentTime
.
second
=
string
(
temp
);
sprintf
(
temp
,
"%03d"
,
(
int
)(
tv
.
tv_usec
/
1000
));
currentTime
.
millisecond
=
string
(
temp
);
sprintf
(
temp
,
"%03d"
,
(
int
)(
tv
.
tv_usec
%
1000
));
currentTime
.
microsecond
=
string
(
temp
);
...
...
@@ -188,60 +185,82 @@ inline LogTime GetTime()
}
#define LOG_TIME(logFile) \
do\
{\
LogTime currentTime=GetTime(); \
fprintf(((logFile == NULL) ? stdout : logFile), "%s-%s-%s %s:%s:%s.%s\t",currentTime.year.c_str(),currentTime.month.c_str(),currentTime.day.c_str(),currentTime.hour.c_str(),currentTime.minute.c_str(),currentTime.second.c_str(),currentTime.millisecond.c_str()); \
}while (0)
#define LOG_INFO(logFile,logInfo, ...) \
do\
{\
do \
{ \
LogTime currentTime = GetTime(); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"%s-%s-%s %s:%s:%s.%s\t", \
currentTime.year.c_str(), \
currentTime.month.c_str(), \
currentTime.day.c_str(), \
currentTime.hour.c_str(), \
currentTime.minute.c_str(), \
currentTime.second.c_str(), \
currentTime.millisecond.c_str()); \
} while(0)
#define LOG_INFO(logFile, logInfo, ...) \
do \
{ \
LOCK; \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "INFO\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ## __VA_ARGS__); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while
(0)
} while(0)
#define LOG_DEBUG(logFile,logInfo, ...) \
do
\
{
\
#define LOG_DEBUG(logFile,
logInfo, ...)
\
do
\
{
\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "DEBUG\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "DEBUG\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while
(0)
} while(0)
#define LOG_ERROR(logFile,logInfo, ...) \
do
\
{
\
#define LOG_ERROR(logFile,
logInfo, ...)
\
do
\
{
\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "ERROR\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "ERROR\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while
(0)
} while(0)
#define LOG_WARN(logFile,logInfo, ...) \
do
\
{
\
#define LOG_WARN(logFile,
logInfo, ...)
\
do
\
{
\
LOCK; \
LOG_TIME(logFile);\
fprintf(((logFile==NULL)?stdout:logFile), "WARN\t"); \
fprintf(((logFile==NULL)?stdout:logFile), "[%s:%d (%s) ]: ", __FILE__, __LINE__, __FUNCTION__); \
fprintf(((logFile==NULL)?stdout:logFile),logInfo, ## __VA_ARGS__); \
LOG_TIME(logFile); \
fprintf(((logFile == NULL) ? stdout : logFile), "WARN\t"); \
fprintf(((logFile == NULL) ? stdout : logFile), \
"[%s:%d (%s) ]: ", \
__FILE__, \
__LINE__, \
__FUNCTION__); \
fprintf(((logFile == NULL) ? stdout : logFile), logInfo, ##__VA_ARGS__); \
fflush(logFile); \
UNLOCK; \
} while
(0)
} while(0)
#endif // __SIMPLE_LOG_H__
Src/main.cpp
View file @
4fe0fdcb
...
...
@@ -16,32 +16,29 @@ void MIGraphXSamplesUsage(char* programName)
void
Sample_Crnn
();
void
Sample_Crnn_Dynamic
();
int
main
(
int
argc
,
char
*
argv
[])
int
main
(
int
argc
,
char
*
argv
[])
{
if
(
argc
<
2
||
argc
>
2
)
if
(
argc
<
2
||
argc
>
2
)
{
MIGraphXSamplesUsage
(
argv
[
0
]);
return
-
1
;
}
if
(
!
strncmp
(
argv
[
1
],
"-h"
,
2
))
if
(
!
strncmp
(
argv
[
1
],
"-h"
,
2
))
{
MIGraphXSamplesUsage
(
argv
[
0
]);
return
0
;
}
switch
(
*
argv
[
1
])
{
case
'0'
:
switch
(
*
argv
[
1
])
{
case
'0'
:
{
Sample_Crnn
();
break
;
}
case
'1'
:
{
case
'1'
:
{
Sample_Crnn_Dynamic
();
break
;
}
default
:
{
default:
{
MIGraphXSamplesUsage
(
argv
[
0
]);
break
;
}
...
...
@@ -56,7 +53,7 @@ void Sample_Crnn()
migraphxSamples
::
InitializationParameterOfOcr
initParamOfOcrCRNN
;
initParamOfOcrCRNN
.
configFilePath
=
CONFIG_FILE
;
migraphxSamples
::
ErrorCode
errorCode
=
crnn
.
Initialize
(
initParamOfOcrCRNN
,
false
);
if
(
errorCode
!
=
migraphxSamples
::
SUCCESS
)
if
(
errorCode
!
=
migraphxSamples
::
SUCCESS
)
{
LOG_ERROR
(
stdout
,
"fail to initialize crnn!
\n
"
);
exit
(
-
1
);
...
...
@@ -64,7 +61,7 @@ void Sample_Crnn()
LOG_INFO
(
stdout
,
"succeed to initialize crnn
\n
"
);
// 读取测试图片
cv
::
Mat
srcImage
=
cv
::
imread
(
"../Resource/Images/text.jpg"
,
1
);
cv
::
Mat
srcImage
=
cv
::
imread
(
"../Resource/Images/text.jpg"
,
1
);
// 推理
std
::
vector
<
char
>
resultRaw
;
...
...
@@ -73,7 +70,7 @@ void Sample_Crnn()
crnn
.
Infer
(
srcImage
,
resultSim
,
false
,
false
);
// 获取推理结果
LOG_INFO
(
stdout
,
"========== Ocr Results ==========
\n
"
);
LOG_INFO
(
stdout
,
"========== Ocr Results ==========
\n
"
);
for
(
int
i
=
0
;
i
<
resultRaw
.
size
();
i
++
)
{
std
::
cout
<<
resultRaw
.
at
(
i
);
...
...
@@ -93,7 +90,7 @@ void Sample_Crnn_Dynamic()
migraphxSamples
::
InitializationParameterOfOcr
initParamOfOcrCRNN
;
initParamOfOcrCRNN
.
configFilePath
=
CONFIG_FILE
;
migraphxSamples
::
ErrorCode
errorCode
=
crnn
.
Initialize
(
initParamOfOcrCRNN
,
true
);
if
(
errorCode
!
=
migraphxSamples
::
SUCCESS
)
if
(
errorCode
!
=
migraphxSamples
::
SUCCESS
)
{
LOG_ERROR
(
stdout
,
"fail to initialize crnn!
\n
"
);
exit
(
-
1
);
...
...
@@ -104,16 +101,16 @@ void Sample_Crnn_Dynamic()
std
::
vector
<
cv
::
Mat
>
srcImages
;
cv
::
String
folder
=
"../Resource/Images/CrnnDynamicPic"
;
std
::
vector
<
cv
::
String
>
imagePathList
;
cv
::
glob
(
folder
,
imagePathList
);
for
(
int
i
=
0
;
i
<
imagePathList
.
size
();
++
i
)
cv
::
glob
(
folder
,
imagePathList
);
for
(
int
i
=
0
;
i
<
imagePathList
.
size
();
++
i
)
{
cv
::
Mat
srcImage
=
cv
::
imread
(
imagePathList
[
i
],
1
);
cv
::
Mat
srcImage
=
cv
::
imread
(
imagePathList
[
i
],
1
);
srcImages
.
push_back
(
srcImage
);
}
// 获取推理结果
LOG_INFO
(
stdout
,
"========== Ocr Results ==========
\n
"
);
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
LOG_INFO
(
stdout
,
"========== Ocr Results ==========
\n
"
);
for
(
int
i
=
0
;
i
<
srcImages
.
size
();
++
i
)
{
std
::
vector
<
char
>
resultSim
;
crnn
.
Infer
(
srcImages
[
i
],
resultSim
,
false
,
true
);
...
...
@@ -124,5 +121,4 @@ void Sample_Crnn_Dynamic()
}
std
::
cout
<<
std
::
endl
;
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment