|
|
|
|
|
#include <iostream> |
|
|
#include <sstream> |
|
|
#include <regex> |
|
|
|
|
|
#include <opencv2/core.hpp> |
|
|
#include <opencv2/highgui.hpp> |
|
|
#include <opencv2/imgcodecs.hpp> |
|
|
#include <opencv2/videoio.hpp> |
|
|
#include <opencv2/imgproc.hpp> |
|
|
|
|
|
#include "nlohmann/json.hpp" |
|
|
|
|
|
#include "WeChatOcrResult.h" |
|
|
#include "Utilities.h" |
|
|
|
|
|
#include "utf8.h" |
|
|
|
|
|
|
|
|
WeChatOcrResult* ParseOcrResult::ParseJson(const std::string& jsonResponseStr) { |
|
|
try { |
|
|
|
|
|
auto ocrResponse = ocr_protobuf::OcrResponse(); |
|
|
ocrResponse.ParseFromString(jsonResponseStr); |
|
|
|
|
|
|
|
|
WeChatOcrResult* result = new WeChatOcrResult(); |
|
|
result->TaskId = ocrResponse.task_id(); |
|
|
result->OcrResult = std::make_shared<OcrResult>(); |
|
|
|
|
|
|
|
|
if (ocrResponse.has_ocr_result()) { |
|
|
const auto& protoOcrResult = ocrResponse.ocr_result(); |
|
|
|
|
|
|
|
|
for (int i = 0; i < protoOcrResult.single_result_size(); i++) { |
|
|
const auto& protoSingleResult = protoOcrResult.single_result(i); |
|
|
SingleResult singleResult; |
|
|
|
|
|
|
|
|
if (!protoSingleResult.single_str_utf8().empty()) { |
|
|
singleResult.SingleStrUtf8 = Utilities::Base64Decode(protoSingleResult.single_str_utf8()); |
|
|
} |
|
|
|
|
|
|
|
|
singleResult.SingleRate = protoSingleResult.single_rate(); |
|
|
singleResult.Left = protoSingleResult.left(); |
|
|
singleResult.Top = protoSingleResult.top(); |
|
|
singleResult.Right = protoSingleResult.right(); |
|
|
singleResult.Bottom = protoSingleResult.bottom(); |
|
|
|
|
|
|
|
|
if (protoSingleResult.has_single_pos()) { |
|
|
singleResult.SinglePos = std::make_shared<SinglePos>(); |
|
|
const auto& protoSinglePos = protoSingleResult.single_pos(); |
|
|
|
|
|
for (int j = 0; j < protoSinglePos.pos_size(); j++) { |
|
|
const auto& protoPosXY = protoSinglePos.pos(j); |
|
|
Pos pos; |
|
|
pos.X = protoPosXY.x(); |
|
|
pos.Y = protoPosXY.y(); |
|
|
singleResult.SinglePos->Pos.push_back(pos); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (int j = 0; j < protoSingleResult.one_result_size(); j++) { |
|
|
const auto& protoOneResult = protoSingleResult.one_result(j); |
|
|
OneResult oneResult; |
|
|
|
|
|
|
|
|
if (!protoOneResult.one_str_utf8().empty()) { |
|
|
oneResult.OneStrUtf8 = Utilities::Base64Decode(protoOneResult.one_str_utf8()); |
|
|
} |
|
|
|
|
|
|
|
|
if (protoOneResult.has_one_pos()) { |
|
|
oneResult.OnePos = std::make_shared<OnePos>(); |
|
|
const auto& protoOnePos = protoOneResult.one_pos(); |
|
|
|
|
|
for (int k = 0; k < protoOnePos.pos_size(); k++) { |
|
|
const auto& protoPosXY = protoOnePos.pos(k); |
|
|
Pos pos; |
|
|
pos.X = protoPosXY.x(); |
|
|
pos.Y = protoPosXY.y(); |
|
|
oneResult.OnePos->Pos.push_back(pos); |
|
|
} |
|
|
} |
|
|
|
|
|
singleResult.OneResult.push_back(oneResult); |
|
|
} |
|
|
|
|
|
result->OcrResult->SingleResult.push_back(singleResult); |
|
|
} |
|
|
} |
|
|
|
|
|
return result; |
|
|
} |
|
|
catch (const std::exception& ex) { |
|
|
std::cerr << "Error parsing OCR result: " << ex.what() << std::endl; |
|
|
return nullptr; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::string OcrResult::ToJson(std::string& pth_img) const { |
|
|
|
|
|
cv::Mat mt = cv::imread(pth_img, cv::ImreadModes::IMREAD_COLOR_BGR); |
|
|
int width_img = mt.cols; |
|
|
int height_img = mt.rows; |
|
|
|
|
|
|
|
|
|
|
|
nlohmann::ordered_json root; |
|
|
|
|
|
root["width"] = width_img; |
|
|
root["height"] = height_img; |
|
|
|
|
|
nlohmann::ordered_json jsonResult = nlohmann::ordered_json::array(); |
|
|
|
|
|
for (const auto& singleResult : SingleResult) { |
|
|
nlohmann::ordered_json jsonSingleResult; |
|
|
|
|
|
|
|
|
jsonSingleResult["word"] = singleResult.SingleStrUtf8; |
|
|
jsonSingleResult["prob"] = singleResult.SingleRate; |
|
|
|
|
|
int lft = (int)(singleResult.Left + 0.5); |
|
|
int tp = (int)(singleResult.Top + 0.5); |
|
|
int rht = (int)(singleResult.Right + 0.5); |
|
|
int btm = (int)(singleResult.Bottom + 0.5); |
|
|
int w = rht - lft; |
|
|
int h = btm - tp; |
|
|
|
|
|
lft = lft > width_img ? width_img : lft; |
|
|
tp = tp > height_img ? height_img : tp; |
|
|
rht = rht > width_img ? width_img : rht; |
|
|
btm = btm > height_img ? height_img : btm; |
|
|
w = w > width_img ? width_img : w; |
|
|
h = h > height_img ? height_img : h; |
|
|
|
|
|
jsonSingleResult["x"] = lft; |
|
|
jsonSingleResult["y"] = tp; |
|
|
jsonSingleResult["width"] = w; |
|
|
jsonSingleResult["height"] = h; |
|
|
|
|
|
nlohmann::json pos = nlohmann::json::array(); |
|
|
pos.push_back({ {"x", lft}, {"y", tp} }); |
|
|
pos.push_back({ {"x", rht}, {"y", tp} }); |
|
|
pos.push_back({ {"x", rht}, {"y", btm } }); |
|
|
pos.push_back({ {"x", lft }, {"y", btm } }); |
|
|
|
|
|
jsonSingleResult["pos"] = pos; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (singleResult.SinglePos) { |
|
|
nlohmann::json posArray = nlohmann::json::array(); |
|
|
for (const auto& pos : singleResult.SinglePos->Pos) { |
|
|
posArray.push_back({ |
|
|
{"x", pos.X}, |
|
|
{"y", pos.Y} |
|
|
}); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if (!singleResult.OneResult.empty()) { |
|
|
nlohmann::json oneResultArray = nlohmann::json::array(); |
|
|
|
|
|
for (int i = 0; i < singleResult.OneResult.size(); i++) { |
|
|
|
|
|
const auto& oneResult = singleResult.OneResult[i]; |
|
|
nlohmann::ordered_json jsonOneResult; |
|
|
|
|
|
if (oneResult.OneStrUtf8.empty()) { |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
for (size_t j = 0; j < oneResult.OneStrUtf8.size();) { |
|
|
size_t len = utf8codepointcalcsize((const utf8_int8_t*)&oneResult.OneStrUtf8[j]); |
|
|
std::string word = oneResult.OneStrUtf8.substr(j, len); |
|
|
|
|
|
jsonOneResult["word"] = word; |
|
|
|
|
|
int one_x = -1, one_y = -1, one_w = -1, one_h = h; |
|
|
|
|
|
if (oneResult.OnePos) { |
|
|
nlohmann::json posArray = nlohmann::json::array(); |
|
|
for (const auto& pos : oneResult.OnePos->Pos) { |
|
|
|
|
|
one_x = pos.X; |
|
|
one_y = pos.Y; |
|
|
break; |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if (i == singleResult.OneResult.size() - 1) { |
|
|
one_w = rht - one_x; |
|
|
} |
|
|
else { |
|
|
one_w = singleResult.OneResult[i + 1].OnePos->Pos[0].X - one_x; |
|
|
} |
|
|
|
|
|
jsonOneResult["x"] = one_x; |
|
|
jsonOneResult["y"] = one_y; |
|
|
jsonOneResult["w"] = one_w; |
|
|
jsonOneResult["h"] = one_h; |
|
|
|
|
|
oneResultArray.push_back(jsonOneResult); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
j += len; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
jsonSingleResult["charInfo"] = oneResultArray; |
|
|
} |
|
|
|
|
|
jsonResult.push_back(jsonSingleResult); |
|
|
} |
|
|
|
|
|
root["prism_wordsInfo"] = jsonResult; |
|
|
|
|
|
return root.dump(4); |
|
|
} |