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
gaoqiong
RapidOCR
Commits
ddf7bb5c
Commit
ddf7bb5c
authored
Oct 28, 2022
by
benjaminwan
Browse files
onnxruntime: 1.13.1, fix some warning
parent
7fbe8f05
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
119 additions
and
175 deletions
+119
-175
include/AngleNet.h
include/AngleNet.h
+2
-2
include/CrnnNet.h
include/CrnnNet.h
+3
-3
include/DbNet.h
include/DbNet.h
+3
-2
include/OcrUtils.h
include/OcrUtils.h
+3
-3
src/AngleNet.cpp
src/AngleNet.cpp
+12
-33
src/CrnnNet.cpp
src/CrnnNet.cpp
+13
-35
src/DbNet.cpp
src/DbNet.cpp
+15
-27
src/OcrLite.cpp
src/OcrLite.cpp
+14
-15
src/OcrUtils.cpp
src/OcrUtils.cpp
+52
-51
src/clipper.cpp
src/clipper.cpp
+1
-1
src/getopt.cpp
src/getopt.cpp
+1
-3
No files found.
include/AngleNet.h
View file @
ddf7bb5c
...
...
@@ -27,8 +27,8 @@ private:
Ort
::
SessionOptions
sessionOptions
=
Ort
::
SessionOptions
();
int
numThread
=
0
;
std
::
vector
<
char
*
>
inputNames
;
std
::
vector
<
char
*
>
outputNames
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
inputNames
Ptr
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
outputNames
Ptr
;
const
float
meanValues
[
3
]
=
{
127.5
,
127.5
,
127.5
};
const
float
normValues
[
3
]
=
{
1.0
/
127.5
,
1.0
/
127.5
,
1.0
/
127.5
};
...
...
include/CrnnNet.h
View file @
ddf7bb5c
...
...
@@ -25,8 +25,8 @@ private:
Ort
::
SessionOptions
sessionOptions
=
Ort
::
SessionOptions
();
int
numThread
=
0
;
std
::
vector
<
char
*
>
inputNames
;
std
::
vector
<
char
*
>
outputNames
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
inputNames
Ptr
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
outputNames
Ptr
;
const
float
meanValues
[
3
]
=
{
127.5
,
127.5
,
127.5
};
const
float
normValues
[
3
]
=
{
1.0
/
127.5
,
1.0
/
127.5
,
1.0
/
127.5
};
...
...
@@ -34,7 +34,7 @@ private:
std
::
vector
<
std
::
string
>
keys
;
TextLine
scoreToTextLine
(
const
std
::
vector
<
float
>
&
outputData
,
int
h
,
in
t
w
);
TextLine
scoreToTextLine
(
const
std
::
vector
<
float
>
&
outputData
,
size_t
h
,
size_
t
w
);
TextLine
getTextLine
(
const
cv
::
Mat
&
src
);
};
...
...
include/DbNet.h
View file @
ddf7bb5c
...
...
@@ -23,8 +23,9 @@ private:
Ort
::
Env
env
=
Ort
::
Env
(
ORT_LOGGING_LEVEL_ERROR
,
"DbNet"
);
Ort
::
SessionOptions
sessionOptions
=
Ort
::
SessionOptions
();
int
numThread
=
0
;
std
::
vector
<
char
*>
inputNames
;
std
::
vector
<
char
*>
outputNames
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
inputNamesPtr
;
std
::
vector
<
Ort
::
AllocatedStringPtr
>
outputNamesPtr
;
const
float
meanValues
[
3
]
=
{
0.485
*
255
,
0.456
*
255
,
0.406
*
255
};
const
float
normValues
[
3
]
=
{
1.0
/
0.229
/
255.0
,
1.0
/
0.224
/
255.0
,
1.0
/
0.225
/
255.0
};
...
...
include/OcrUtils.h
View file @
ddf7bb5c
...
...
@@ -79,9 +79,9 @@ std::vector<float> substractMeanNormalize(cv::Mat &src, const float *meanVals, c
std
::
vector
<
int
>
getAngleIndexes
(
std
::
vector
<
Angle
>
&
angles
);
std
::
vector
<
char
*
>
getInputNames
(
Ort
::
Session
*
session
);
std
::
vector
<
Ort
::
AllocatedStringPtr
>
getInputNames
(
Ort
::
Session
*
session
);
std
::
vector
<
char
*
>
getOutputNames
(
Ort
::
Session
*
session
);
std
::
vector
<
Ort
::
AllocatedStringPtr
>
getOutputNames
(
Ort
::
Session
*
session
);
void
saveImg
(
cv
::
Mat
&
img
,
const
char
*
imgPath
);
...
...
@@ -91,6 +91,6 @@ std::string getResultTxtFilePath(const char *path, const char *imgName);
std
::
string
getResultImgFilePath
(
const
char
*
path
,
const
char
*
imgName
);
std
::
string
getDebugImgFilePath
(
const
char
*
path
,
const
char
*
imgName
,
in
t
i
,
const
char
*
tag
);
std
::
string
getDebugImgFilePath
(
const
char
*
path
,
const
char
*
imgName
,
size_
t
i
,
const
char
*
tag
);
#endif //__OCR_UTILS_H__
src/AngleNet.cpp
View file @
ddf7bb5c
...
...
@@ -23,22 +23,8 @@ void AngleNet::setGpuIndex(int gpuIndex) {
AngleNet
::~
AngleNet
()
{
delete
session
;
for
(
auto
name
:
inputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
inputNames
.
clear
();
for
(
auto
name
:
outputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
outputNames
.
clear
();
inputNamesPtr
.
clear
();
outputNamesPtr
.
clear
();
}
void
AngleNet
::
setNumThread
(
int
numOfThread
)
{
...
...
@@ -69,14 +55,14 @@ void AngleNet::initModel(const std::string &pathStr) {
#else
session
=
new
Ort
::
Session
(
env
,
pathStr
.
c_str
(),
sessionOptions
);
#endif
inputNames
=
getInputNames
(
session
);
outputNames
=
getOutputNames
(
session
);
inputNames
Ptr
=
getInputNames
(
session
);
outputNames
Ptr
=
getOutputNames
(
session
);
}
Angle
scoreToAngle
(
const
std
::
vector
<
float
>
&
outputData
)
{
int
maxIndex
=
0
;
float
maxScore
=
0
;
for
(
in
t
i
=
0
;
i
<
outputData
.
size
();
i
++
)
{
for
(
size_
t
i
=
0
;
i
<
outputData
.
size
();
i
++
)
{
if
(
outputData
[
i
]
>
maxScore
)
{
maxScore
=
outputData
[
i
];
maxIndex
=
i
;
...
...
@@ -86,28 +72,21 @@ Angle scoreToAngle(const std::vector<float> &outputData) {
}
Angle
AngleNet
::
getAngle
(
cv
::
Mat
&
src
)
{
std
::
vector
<
float
>
inputTensorValues
=
substractMeanNormalize
(
src
,
meanValues
,
normValues
);
std
::
array
<
int64_t
,
4
>
inputShape
{
1
,
src
.
channels
(),
src
.
rows
,
src
.
cols
};
auto
memoryInfo
=
Ort
::
MemoryInfo
::
CreateCpu
(
OrtDeviceAllocator
,
OrtMemTypeCPU
);
Ort
::
Value
inputTensor
=
Ort
::
Value
::
CreateTensor
<
float
>
(
memoryInfo
,
inputTensorValues
.
data
(),
inputTensorValues
.
size
(),
inputShape
.
data
(),
inputShape
.
size
());
assert
(
inputTensor
.
IsTensor
());
std
::
vector
<
const
char
*>
inputNames
=
{
inputNamesPtr
.
data
()
->
get
()};
std
::
vector
<
const
char
*>
outputNames
=
{
outputNamesPtr
.
data
()
->
get
()};
auto
outputTensor
=
session
->
Run
(
Ort
::
RunOptions
{
nullptr
},
inputNames
.
data
(),
&
inputTensor
,
inputNames
.
size
(),
outputNames
.
data
(),
outputNames
.
size
());
inputNamesPtr
.
size
(),
outputNames
.
data
(),
outputNamesPtr
.
size
());
assert
(
outputTensor
.
size
()
==
1
&&
outputTensor
.
front
().
IsTensor
());
std
::
vector
<
int64_t
>
outputShape
=
outputTensor
[
0
].
GetTensorTypeAndShapeInfo
().
GetShape
();
int64_t
outputCount
=
std
::
accumulate
(
outputShape
.
begin
(),
outputShape
.
end
(),
1
,
std
::
multiplies
<
int64_t
>
());
float
*
floatArray
=
outputTensor
.
front
().
GetTensorMutableData
<
float
>
();
std
::
vector
<
float
>
outputData
(
floatArray
,
floatArray
+
outputCount
);
return
scoreToAngle
(
outputData
);
...
...
@@ -115,10 +94,10 @@ Angle AngleNet::getAngle(cv::Mat &src) {
std
::
vector
<
Angle
>
AngleNet
::
getAngles
(
std
::
vector
<
cv
::
Mat
>
&
partImgs
,
const
char
*
path
,
const
char
*
imgName
,
bool
doAngle
,
bool
mostAngle
)
{
in
t
size
=
partImgs
.
size
();
size_
t
size
=
partImgs
.
size
();
std
::
vector
<
Angle
>
angles
(
size
);
if
(
doAngle
)
{
for
(
in
t
i
=
0
;
i
<
size
;
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
size
;
++
i
)
{
double
startAngle
=
getCurrentTime
();
cv
::
Mat
angleImg
;
cv
::
resize
(
partImgs
[
i
],
angleImg
,
cv
::
Size
(
dstWidth
,
dstHeight
));
...
...
@@ -135,7 +114,7 @@ std::vector<Angle> AngleNet::getAngles(std::vector<cv::Mat> &partImgs, const cha
}
}
}
else
{
for
(
in
t
i
=
0
;
i
<
size
;
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
size
;
++
i
)
{
angles
[
i
]
=
Angle
{
-
1
,
0.
f
};
}
}
...
...
@@ -151,7 +130,7 @@ std::vector<Angle> AngleNet::getAngles(std::vector<cv::Mat> &partImgs, const cha
mostAngleIndex
=
1
;
}
//printf("Set All Angle to mostAngleIndex(%d)\n", mostAngleIndex);
for
(
in
t
i
=
0
;
i
<
angles
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
angles
.
size
();
++
i
)
{
Angle
angle
=
angles
[
i
];
angle
.
index
=
mostAngleIndex
;
angles
.
at
(
i
)
=
angle
;
...
...
src/CrnnNet.cpp
View file @
ddf7bb5c
...
...
@@ -24,22 +24,8 @@ void CrnnNet::setGpuIndex(int gpuIndex) {
CrnnNet
::~
CrnnNet
()
{
delete
session
;
for
(
auto
name
:
inputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
inputNames
.
clear
();
for
(
auto
name
:
outputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
outputNames
.
clear
();
inputNamesPtr
.
clear
();
outputNamesPtr
.
clear
();
}
void
CrnnNet
::
setNumThread
(
int
numOfThread
)
{
...
...
@@ -70,8 +56,8 @@ void CrnnNet::initModel(const std::string &pathStr, const std::string &keysPath)
#else
session
=
new
Ort
::
Session
(
env
,
pathStr
.
c_str
(),
sessionOptions
);
#endif
inputNames
=
getInputNames
(
session
);
outputNames
=
getOutputNames
(
session
);
inputNames
Ptr
=
getInputNames
(
session
);
outputNames
Ptr
=
getOutputNames
(
session
);
//load keys
std
::
ifstream
in
(
keysPath
.
c_str
());
...
...
@@ -94,18 +80,18 @@ inline static size_t argmax(ForwardIterator first, ForwardIterator last) {
return
std
::
distance
(
first
,
std
::
max_element
(
first
,
last
));
}
TextLine
CrnnNet
::
scoreToTextLine
(
const
std
::
vector
<
float
>
&
outputData
,
int
h
,
in
t
w
)
{
TextLine
CrnnNet
::
scoreToTextLine
(
const
std
::
vector
<
float
>
&
outputData
,
size_t
h
,
size_
t
w
)
{
auto
keySize
=
keys
.
size
();
auto
dataSize
=
outputData
.
size
();
std
::
string
strRes
;
std
::
vector
<
float
>
scores
;
in
t
lastIndex
=
0
;
in
t
maxIndex
;
size_
t
lastIndex
=
0
;
size_
t
maxIndex
;
float
maxValue
;
for
(
in
t
i
=
0
;
i
<
h
;
i
++
)
{
in
t
start
=
i
*
w
;
in
t
stop
=
(
i
+
1
)
*
w
;
for
(
size_
t
i
=
0
;
i
<
h
;
i
++
)
{
size_
t
start
=
i
*
w
;
size_
t
stop
=
(
i
+
1
)
*
w
;
if
(
stop
>
dataSize
-
1
)
{
stop
=
(
i
+
1
)
*
w
-
1
;
}
...
...
@@ -124,31 +110,23 @@ TextLine CrnnNet::scoreToTextLine(const std::vector<float> &outputData, int h, i
TextLine
CrnnNet
::
getTextLine
(
const
cv
::
Mat
&
src
)
{
float
scale
=
(
float
)
dstHeight
/
(
float
)
src
.
rows
;
int
dstWidth
=
int
((
float
)
src
.
cols
*
scale
);
cv
::
Mat
srcResize
;
resize
(
src
,
srcResize
,
cv
::
Size
(
dstWidth
,
dstHeight
));
std
::
vector
<
float
>
inputTensorValues
=
substractMeanNormalize
(
srcResize
,
meanValues
,
normValues
);
std
::
array
<
int64_t
,
4
>
inputShape
{
1
,
srcResize
.
channels
(),
srcResize
.
rows
,
srcResize
.
cols
};
auto
memoryInfo
=
Ort
::
MemoryInfo
::
CreateCpu
(
OrtDeviceAllocator
,
OrtMemTypeCPU
);
Ort
::
Value
inputTensor
=
Ort
::
Value
::
CreateTensor
<
float
>
(
memoryInfo
,
inputTensorValues
.
data
(),
inputTensorValues
.
size
(),
inputShape
.
data
(),
inputShape
.
size
());
assert
(
inputTensor
.
IsTensor
());
std
::
vector
<
const
char
*>
inputNames
=
{
inputNamesPtr
.
data
()
->
get
()};
std
::
vector
<
const
char
*>
outputNames
=
{
outputNamesPtr
.
data
()
->
get
()};
auto
outputTensor
=
session
->
Run
(
Ort
::
RunOptions
{
nullptr
},
inputNames
.
data
(),
&
inputTensor
,
inputNames
.
size
(),
outputNames
.
data
(),
outputNames
.
size
());
inputNamesPtr
.
size
(),
outputNames
.
data
(),
outputNamesPtr
.
size
());
assert
(
outputTensor
.
size
()
==
1
&&
outputTensor
.
front
().
IsTensor
());
std
::
vector
<
int64_t
>
outputShape
=
outputTensor
[
0
].
GetTensorTypeAndShapeInfo
().
GetShape
();
int64_t
outputCount
=
std
::
accumulate
(
outputShape
.
begin
(),
outputShape
.
end
(),
1
,
std
::
multiplies
<
int64_t
>
());
float
*
floatArray
=
outputTensor
.
front
().
GetTensorMutableData
<
float
>
();
std
::
vector
<
float
>
outputData
(
floatArray
,
floatArray
+
outputCount
);
return
scoreToTextLine
(
outputData
,
outputShape
[
1
],
outputShape
[
2
]);
...
...
src/DbNet.cpp
View file @
ddf7bb5c
...
...
@@ -22,22 +22,8 @@ void DbNet::setGpuIndex(int gpuIndex) {
DbNet
::~
DbNet
()
{
delete
session
;
for
(
auto
name
:
inputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
inputNames
.
clear
();
for
(
auto
name
:
outputNames
)
{
#ifdef _WIN32
_aligned_free
(
name
);
#else
free
(
name
);
#endif
}
outputNames
.
clear
();
inputNamesPtr
.
clear
();
outputNamesPtr
.
clear
();
}
void
DbNet
::
setNumThread
(
int
numOfThread
)
{
...
...
@@ -68,8 +54,8 @@ void DbNet::initModel(const std::string &pathStr) {
#else
session
=
new
Ort
::
Session
(
env
,
pathStr
.
c_str
(),
sessionOptions
);
#endif
inputNames
=
getInputNames
(
session
);
outputNames
=
getOutputNames
(
session
);
inputNames
Ptr
=
getInputNames
(
session
);
outputNames
Ptr
=
getOutputNames
(
session
);
}
std
::
vector
<
TextBox
>
findRsBoxes
(
const
cv
::
Mat
&
predMat
,
const
cv
::
Mat
&
dilateMat
,
ScaleParam
&
s
,
...
...
@@ -83,11 +69,11 @@ std::vector<TextBox> findRsBoxes(const cv::Mat &predMat, const cv::Mat &dilateMa
cv
::
findContours
(
dilateMat
,
contours
,
hierarchy
,
cv
::
RETR_LIST
,
cv
::
CHAIN_APPROX_SIMPLE
);
in
t
numContours
=
contours
.
size
()
>=
maxCandidates
?
maxCandidates
:
contours
.
size
();
size_
t
numContours
=
contours
.
size
()
>=
maxCandidates
?
maxCandidates
:
contours
.
size
();
std
::
vector
<
TextBox
>
rsBoxes
;
for
(
in
t
i
=
0
;
i
<
numContours
;
i
++
)
{
for
(
size_
t
i
=
0
;
i
<
numContours
;
i
++
)
{
if
(
contours
[
i
].
size
()
<=
2
)
{
continue
;
}
...
...
@@ -117,9 +103,9 @@ std::vector<TextBox> findRsBoxes(const cv::Mat &predMat, const cv::Mat &dilateMa
std
::
vector
<
cv
::
Point
>
intClipMinBoxes
;
for
(
int
p
=
0
;
p
<
clipMinBoxes
.
size
();
p
++
)
{
float
x
=
clipMinBox
es
[
p
]
.
x
/
s
.
ratioWidth
;
float
y
=
clipMinBox
es
[
p
]
.
y
/
s
.
ratioHeight
;
for
(
auto
&
clipMinBox
:
clipMinBoxes
)
{
float
x
=
clipMinBox
.
x
/
s
.
ratioWidth
;
float
y
=
clipMinBox
.
y
/
s
.
ratioHeight
;
int
ptX
=
(
std
::
min
)((
std
::
max
)(
int
(
x
),
0
),
s
.
srcWidth
-
1
);
int
ptY
=
(
std
::
min
)((
std
::
max
)(
int
(
y
),
0
),
s
.
srcHeight
-
1
);
cv
::
Point
point
{
ptX
,
ptY
};
...
...
@@ -142,8 +128,10 @@ DbNet::getTextBoxes(cv::Mat &src, ScaleParam &s, float boxScoreThresh, float box
inputTensorValues
.
size
(),
inputShape
.
data
(),
inputShape
.
size
());
assert
(
inputTensor
.
IsTensor
());
std
::
vector
<
const
char
*>
inputNames
=
{
inputNamesPtr
.
data
()
->
get
()};
std
::
vector
<
const
char
*>
outputNames
=
{
outputNamesPtr
.
data
()
->
get
()};
auto
outputTensor
=
session
->
Run
(
Ort
::
RunOptions
{
nullptr
},
inputNames
.
data
(),
&
inputTensor
,
inputNames
.
size
(),
outputNames
.
data
(),
outputNames
.
size
()
);
inputNames
.
size
(),
outputNames
.
data
(),
1
);
assert
(
outputTensor
.
size
()
==
1
&&
outputTensor
.
front
().
IsTensor
());
std
::
vector
<
int64_t
>
outputShape
=
outputTensor
[
0
].
GetTensorTypeAndShapeInfo
().
GetShape
();
int64_t
outputCount
=
std
::
accumulate
(
outputShape
.
begin
(),
outputShape
.
end
(),
1
,
...
...
@@ -152,9 +140,9 @@ DbNet::getTextBoxes(cv::Mat &src, ScaleParam &s, float boxScoreThresh, float box
std
::
vector
<
float
>
outputData
(
floatArray
,
floatArray
+
outputCount
);
//-----Data preparation-----
int
outHeight
=
outputShape
[
2
];
int
outWidth
=
outputShape
[
3
];
in
t
area
=
outHeight
*
outWidth
;
int
outHeight
=
(
int
)
outputShape
[
2
];
int
outWidth
=
(
int
)
outputShape
[
3
];
size_
t
area
=
outHeight
*
outWidth
;
std
::
vector
<
float
>
predData
(
area
,
0.0
);
std
::
vector
<
unsigned
char
>
cbufData
(
area
,
' '
);
...
...
src/OcrLite.cpp
View file @
ddf7bb5c
...
...
@@ -84,7 +84,7 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
}
else
{
resize
=
maxSideLen
;
}
resize
+=
2
*
padding
;
resize
+=
2
*
padding
;
cv
::
Rect
paddingRect
(
padding
,
padding
,
originSrc
.
cols
,
originSrc
.
rows
);
cv
::
Mat
paddingSrc
=
makePadding
(
originSrc
,
padding
);
ScaleParam
scale
=
getScaleParam
(
paddingSrc
,
resize
);
...
...
@@ -94,15 +94,14 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
return
result
;
}
OcrResult
OcrLite
::
detect
(
const
cv
::
Mat
&
mat
,
int
padding
,
int
maxSideLen
,
float
boxScoreThresh
,
float
boxThresh
,
float
unClipRatio
,
bool
doAngle
,
bool
mostAngle
)
{
OcrResult
OcrLite
::
detect
(
const
cv
::
Mat
&
mat
,
int
padding
,
int
maxSideLen
,
float
boxScoreThresh
,
float
boxThresh
,
float
unClipRatio
,
bool
doAngle
,
bool
mostAngle
)
{
cv
::
Mat
originSrc
=
mat
;
int
originMaxSide
=
(
std
::
max
)(
originSrc
.
cols
,
originSrc
.
rows
);
int
resize
;
if
(
maxSideLen
<=
0
||
maxSideLen
>
originMaxSide
)
{
resize
=
originMaxSide
;
}
else
{
}
else
{
resize
=
maxSideLen
;
}
resize
+=
2
*
padding
;
...
...
@@ -111,14 +110,14 @@ OcrResult OcrLite::detect(const cv::Mat& mat, int padding, int maxSideLen, float
ScaleParam
scale
=
getScaleParam
(
paddingSrc
,
resize
);
OcrResult
result
;
result
=
detect
(
NULL
,
NULL
,
paddingSrc
,
paddingRect
,
scale
,
boxScoreThresh
,
boxThresh
,
unClipRatio
,
doAngle
,
mostAngle
);
boxScoreThresh
,
boxThresh
,
unClipRatio
,
doAngle
,
mostAngle
);
return
result
;
}
std
::
vector
<
cv
::
Mat
>
OcrLite
::
getPartImages
(
cv
::
Mat
&
src
,
std
::
vector
<
TextBox
>
&
textBoxes
,
const
char
*
path
,
const
char
*
imgName
)
{
std
::
vector
<
cv
::
Mat
>
partImages
;
for
(
in
t
i
=
0
;
i
<
textBoxes
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
textBoxes
.
size
();
++
i
)
{
cv
::
Mat
partImg
=
getRotateCropImage
(
src
,
textBoxes
[
i
].
boxPoint
);
partImages
.
emplace_back
(
partImg
);
//OutPut DebugImg
...
...
@@ -149,7 +148,7 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
double
dbNetTime
=
endDbNetTime
-
startTime
;
Logger
(
"dbNetTime(%fms)
\n
"
,
dbNetTime
);
for
(
in
t
i
=
0
;
i
<
textBoxes
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
textBoxes
.
size
();
++
i
)
{
Logger
(
"TextBox[%d](+padding)[score(%f),[x: %d, y: %d], [x: %d, y: %d], [x: %d, y: %d], [x: %d, y: %d]]
\n
"
,
i
,
textBoxes
[
i
].
score
,
textBoxes
[
i
].
boxPoint
[
0
].
x
,
textBoxes
[
i
].
boxPoint
[
0
].
y
,
...
...
@@ -169,12 +168,12 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
angles
=
angleNet
.
getAngles
(
partImages
,
path
,
imgName
,
doAngle
,
mostAngle
);
//Log Angles
for
(
in
t
i
=
0
;
i
<
angles
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
angles
.
size
();
++
i
)
{
Logger
(
"angle[%d][index(%d), score(%f), time(%fms)]
\n
"
,
i
,
angles
[
i
].
index
,
angles
[
i
].
score
,
angles
[
i
].
time
);
}
//Rotate partImgs
for
(
in
t
i
=
0
;
i
<
partImages
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
partImages
.
size
();
++
i
)
{
if
(
angles
[
i
].
index
==
1
)
{
partImages
.
at
(
i
)
=
matRotateClockWise180
(
partImages
[
i
]);
}
...
...
@@ -183,10 +182,10 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
Logger
(
"---------- step: crnnNet getTextLine ----------
\n
"
);
std
::
vector
<
TextLine
>
textLines
=
crnnNet
.
getTextLines
(
partImages
,
path
,
imgName
);
//Log TextLines
for
(
in
t
i
=
0
;
i
<
textLines
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
textLines
.
size
();
++
i
)
{
Logger
(
"textLine[%d](%s)
\n
"
,
i
,
textLines
[
i
].
text
.
c_str
());
std
::
ostringstream
txtScores
;
for
(
in
t
s
=
0
;
s
<
textLines
[
i
].
charScores
.
size
();
++
s
)
{
for
(
size_
t
s
=
0
;
s
<
textLines
[
i
].
charScores
.
size
();
++
s
)
{
if
(
s
==
0
)
{
txtScores
<<
textLines
[
i
].
charScores
[
s
];
}
else
{
...
...
@@ -198,7 +197,7 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
}
std
::
vector
<
TextBlock
>
textBlocks
;
for
(
in
t
i
=
0
;
i
<
textLines
.
size
();
++
i
)
{
for
(
size_
t
i
=
0
;
i
<
textLines
.
size
();
++
i
)
{
std
::
vector
<
cv
::
Point
>
boxPoint
=
std
::
vector
<
cv
::
Point
>
(
4
);
int
padding
=
originRect
.
x
;
//padding conversion
boxPoint
[
0
]
=
cv
::
Point
(
textBoxes
[
i
].
boxPoint
[
0
].
x
-
padding
,
textBoxes
[
i
].
boxPoint
[
0
].
y
-
padding
);
...
...
@@ -232,8 +231,8 @@ OcrResult OcrLite::detect(const char *path, const char *imgName,
}
std
::
string
strRes
;
for
(
int
i
=
0
;
i
<
textBlocks
.
size
();
++
i
)
{
strRes
.
append
(
textBlock
s
[
i
]
.
text
);
for
(
auto
&
textBlock
:
textBlocks
)
{
strRes
.
append
(
textBlock
.
text
);
strRes
.
append
(
"
\n
"
);
}
...
...
src/OcrUtils.cpp
View file @
ddf7bb5c
...
...
@@ -93,8 +93,8 @@ void drawTextBox(cv::Mat &boxImg, const std::vector<cv::Point> &box, int thickne
}
void
drawTextBoxes
(
cv
::
Mat
&
boxImg
,
std
::
vector
<
TextBox
>
&
textBoxes
,
int
thickness
)
{
for
(
int
i
=
0
;
i
<
textBoxes
.
size
();
++
i
)
{
drawTextBox
(
boxImg
,
textBoxe
s
[
i
]
.
boxPoint
,
thickness
);
for
(
auto
&
textBoxe
:
textBoxes
)
{
drawTextBox
(
boxImg
,
textBoxe
.
boxPoint
,
thickness
);
}
}
...
...
@@ -125,9 +125,9 @@ cv::Mat getRotateCropImage(const cv::Mat &src, std::vector<cv::Point> box) {
cv
::
Mat
imgCrop
;
image
(
cv
::
Rect
(
left
,
top
,
right
-
left
,
bottom
-
top
)).
copyTo
(
imgCrop
);
for
(
int
i
=
0
;
i
<
points
.
size
();
i
++
)
{
point
s
[
i
]
.
x
-=
left
;
point
s
[
i
]
.
y
-=
top
;
for
(
auto
&
point
:
points
)
{
point
.
x
-=
left
;
point
.
y
-=
top
;
}
int
imgCropWidth
=
int
(
sqrt
(
pow
(
points
[
0
].
x
-
points
[
1
].
x
,
2
)
+
...
...
@@ -238,15 +238,15 @@ float boxScoreFast(const std::vector<cv::Point2f> &boxes, const cv::Mat &pred) {
pred
(
cv
::
Rect
(
minX
,
minY
,
maxX
-
minX
+
1
,
maxY
-
minY
+
1
))
.
copyTo
(
croppedImg
);
auto
score
=
cv
::
mean
(
croppedImg
,
mask
)[
0
];
auto
score
=
(
float
)
cv
::
mean
(
croppedImg
,
mask
)[
0
];
return
score
;
}
float
getContourArea
(
const
std
::
vector
<
cv
::
Point2f
>
&
box
,
float
unClipRatio
)
{
in
t
size
=
box
.
size
();
size_
t
size
=
box
.
size
();
float
area
=
0.0
f
;
float
dist
=
0.0
f
;
for
(
in
t
i
=
0
;
i
<
size
;
i
++
)
{
for
(
size_
t
i
=
0
;
i
<
size
;
i
++
)
{
area
+=
box
[
i
].
x
*
box
[(
i
+
1
)
%
size
].
y
-
box
[
i
].
y
*
box
[(
i
+
1
)
%
size
].
x
;
dist
+=
sqrtf
((
box
[
i
].
x
-
box
[(
i
+
1
)
%
size
].
x
)
*
...
...
@@ -274,8 +274,8 @@ cv::RotatedRect unClip(std::vector<cv::Point2f> box, float unClipRatio) {
offset
.
Execute
(
soln
,
distance
);
std
::
vector
<
cv
::
Point2f
>
points
;
for
(
in
t
j
=
0
;
j
<
soln
.
size
();
j
++
)
{
for
(
in
t
i
=
0
;
i
<
soln
[
soln
.
size
()
-
1
].
size
();
i
++
)
{
for
(
size_
t
j
=
0
;
j
<
soln
.
size
();
j
++
)
{
for
(
size_
t
i
=
0
;
i
<
soln
[
soln
.
size
()
-
1
].
size
();
i
++
)
{
points
.
emplace_back
(
soln
[
j
][
i
].
X
,
soln
[
j
][
i
].
Y
);
}
}
...
...
@@ -306,70 +306,71 @@ std::vector<float> substractMeanNormalize(cv::Mat &src, const float *meanVals, c
std
::
vector
<
int
>
getAngleIndexes
(
std
::
vector
<
Angle
>
&
angles
)
{
std
::
vector
<
int
>
angleIndexes
;
angleIndexes
.
reserve
(
angles
.
size
());
for
(
int
i
=
0
;
i
<
angles
.
size
();
++
i
)
{
angleIndexes
.
push_back
(
angle
s
[
i
]
.
index
);
for
(
auto
&
angle
:
angles
)
{
angleIndexes
.
push_back
(
angle
.
index
);
}
return
angleIndexes
;
}
std
::
vector
<
char
*
>
getInputNames
(
Ort
::
Session
*
session
)
{
std
::
vector
<
Ort
::
AllocatedStringPtr
>
getInputNames
(
Ort
::
Session
*
session
)
{
Ort
::
AllocatorWithDefaultOptions
allocator
;
size_t
numInputNodes
=
session
->
GetInputCount
();
std
::
vector
<
char
*>
inputNodeNames
(
numInputNodes
);
//std::vector<int64_t> inputNodeDims;
const
size_t
numInputNodes
=
session
->
GetInputCount
();
//printf("Number of inputs = %zu\n", numInputNodes);
std
::
vector
<
Ort
::
AllocatedStringPtr
>
inputNamesPtr
;
inputNamesPtr
.
reserve
(
numInputNodes
);
std
::
vector
<
int64_t
>
input_node_dims
;
for
(
int
i
=
0
;
i
<
numI
nput
N
odes
;
i
++
)
{
// print i
nput
n
ode
names
char
*
inputName
=
session
->
GetInputName
(
i
,
allocator
);
//printf("InputName[%d]=%s\n", i,
inputName);
inputNodeNames
[
i
]
=
inputName
;
// iterate over all i
nput
n
odes
for
(
size_t
i
=
0
;
i
<
numI
nput
N
ode
s
;
i
++
)
{
auto
inputName
=
session
->
GetInputName
Allocated
(
i
,
allocator
);
inputNamesPtr
.
push_back
(
std
::
move
(
inputName
)
)
;
/*printf("inputName[%zu] = %s\n", i,
inputName
.get())
;
// print input node types
//Ort::TypeInf
o typeInfo = session->GetInputTypeInfo(i);
//
auto tensorInfo = typeInfo.GetTensorTypeAndShapeInfo();
aut
o typeInfo = session->GetInputTypeInfo(i);
auto tensorInfo = typeInfo.GetTensorTypeAndShapeInfo();
//
ONNXTensorElementDataType type = tensorInfo.GetElementType();
//
printf("
I
nput
[%d] type=%d
\n", i, type);
ONNXTensorElementDataType type = tensorInfo.GetElementType();
printf("
i
nput
Type[%zu] = %u
\n", i, type);
// print input shapes/dims
//inputNodeDims = tensorInfo.GetShape();
//printf("Input[%d] num_dims=%zu\n", i, inputNodeDims.size());
/*for (int j = 0; j < inputNodeDims.size(); j++)
printf("Input[%d] dim%d=%jd\n", i, j, inputNodeDims[j]);*/
input_node_dims = tensorInfo.GetShape();
printf("Input num_dims = %zu\n", input_node_dims.size());
for (size_t j = 0; j < input_node_dims.size(); j++) {
printf("Input dim[%zu] = %llu\n",j, input_node_dims[j]);
}*/
}
return
inputN
odeN
ames
;
return
inputNames
Ptr
;
}
std
::
vector
<
char
*
>
getOutputNames
(
Ort
::
Session
*
session
)
{
std
::
vector
<
Ort
::
AllocatedStringPtr
>
getOutputNames
(
Ort
::
Session
*
session
)
{
Ort
::
AllocatorWithDefaultOptions
allocator
;
size_t
numOutputNodes
=
session
->
GetOutputCount
();
std
::
vector
<
char
*>
outputNodeNames
(
numOutputNodes
);
//std::vector<int64_t> outputNodeDims;
const
size_t
numOutputNodes
=
session
->
GetOutputCount
();
//printf("Number of outputs = %zu\n", numOutputNodes);
std
::
vector
<
Ort
::
AllocatedStringPtr
>
outputNamesPtr
;
outputNamesPtr
.
reserve
(
numOutputNodes
);
std
::
vector
<
int64_t
>
output_node_dims
;
for
(
int
i
=
0
;
i
<
numOutputNodes
;
i
++
)
{
// print input node names
char
*
outputName
=
session
->
GetOutputName
(
i
,
allocator
);
//printf("OutputName[%d]=%s\n", i, outputName);
outputNodeNames
[
i
]
=
outputName
;
for
(
size_t
i
=
0
;
i
<
numOutputNodes
;
i
++
)
{
auto
outputName
=
session
->
GetOutputNameAllocated
(
i
,
allocator
);
outputNamesPtr
.
push_back
(
std
::
move
(
outputName
));
/*printf("outputName[%zu] = %s\n", i, outputName.get());
// print input node types
//Ort::TypeInf
o type_info = session->GetOutputTypeInfo(i);
//
auto tensor
I
nfo = type_info.GetTensorTypeAndShapeInfo();
aut
o type_info = session->GetOutputTypeInfo(i);
auto tensor
_i
nfo = type_info.GetTensorTypeAndShapeInfo();
//
ONNXTensorElementDataType type = tensor
I
nfo.GetElementType();
//
printf("
O
utput
%d : type=%d
\n", i, type);
ONNXTensorElementDataType type = tensor
_i
nfo.GetElementType();
printf("
o
utput
Type[%zu] = %u
\n", i, type);
// print input shapes/dims
//outputNodeDims = tensorInfo.GetShape();
//printf("Output %d : num_dims=%zu\n", i, outputNodeDims.size());
/*for (int j = 0; j < outputNodeDims.size(); j++)
printf("Output %d : dim %d=%jd\n", i, j, outputNodeDims[j]);*/
output_node_dims = tensor_info.GetShape();
printf("output num_dims = %zu\n", output_node_dims.size());
for (size_t j = 0; j < output_node_dims.size(); j++) {
printf("output dim[%zu] = %llu\n",j, output_node_dims[j]);
}*/
}
return
outputN
odeN
ames
;
return
outputNames
Ptr
;
}
void
saveImg
(
cv
::
Mat
&
img
,
const
char
*
imgPath
)
{
...
...
@@ -394,7 +395,7 @@ std::string getResultImgFilePath(const char *path, const char *imgName) {
return
filePath
;
}
std
::
string
getDebugImgFilePath
(
const
char
*
path
,
const
char
*
imgName
,
in
t
i
,
const
char
*
tag
)
{
std
::
string
getDebugImgFilePath
(
const
char
*
path
,
const
char
*
imgName
,
size_
t
i
,
const
char
*
tag
)
{
std
::
string
filePath
;
filePath
.
append
(
path
).
append
(
imgName
).
append
(
tag
).
append
(
std
::
to_string
(
i
)).
append
(
".jpg"
);
return
filePath
;
...
...
src/clipper.cpp
View file @
ddf7bb5c
...
...
@@ -718,7 +718,7 @@ namespace ClipperLib {
inline
void
InitEdge
(
TEdge
*
e
,
TEdge
*
eNext
,
TEdge
*
ePrev
,
const
IntPoint
&
Pt
)
{
std
::
memset
(
e
,
0
,
sizeof
(
TEdge
));
std
::
memset
(
static_cast
<
void
*>
(
e
)
,
0
,
sizeof
(
TEdge
));
e
->
Next
=
eNext
;
e
->
Prev
=
ePrev
;
e
->
Curr
=
Pt
;
...
...
src/getopt.cpp
View file @
ddf7bb5c
...
...
@@ -176,9 +176,7 @@ static int _getopt_(int argc, char *const argv[],
"%s: option requires an argument -- %c
\n
"
,
argv
[
0
],
c
);
}
if
(
optstring
[
0
]
==
':'
||
(
optstring
[
0
]
==
'-'
||
optstring
[
0
]
==
'+'
)
&&
optstring
[
1
]
==
':'
)
{
if
(
optstring
[
0
]
==
':'
||
((
optstring
[
0
]
==
'-'
||
optstring
[
0
]
==
'+'
)
&&
optstring
[
1
]
==
':'
))
{
c
=
':'
;
}
else
{
c
=
'?'
;
...
...
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