Commit ddf7bb5c authored by benjaminwan's avatar benjaminwan
Browse files

onnxruntime: 1.13.1, fix some warning

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