imradv3 / src /WeChatOcrCpp /WeChatOcrResult.cpp
fasdfsa's picture
返回结果加端口号, 批量运行 exe 脚本
a1d4680
#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"
// 解析JSON响应
WeChatOcrResult* ParseOcrResult::ParseJson(const std::string& jsonResponseStr) {
try {
// 使用 nlohmann/json 解析 JSON 字符串
auto ocrResponse = ocr_protobuf::OcrResponse();
ocrResponse.ParseFromString(jsonResponseStr);
// 创建结果对象
WeChatOcrResult* result = new WeChatOcrResult();
result->TaskId = ocrResponse.task_id();
result->OcrResult = std::make_shared<OcrResult>();
// 解析 OCR 结果
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;
}
}
// 将SingleResult转换为JSON字符串
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;
//cv::imshow("orig", mt);
//cv::waitKey(0);
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;
//cv::Mat tmt = mt.clone();
//cv::rectangle(tmt, cv::Rect(lft, tp, w, h), cv::Scalar(0, 0, 255));
//cv::imshow("line", tmt);
//cv::waitKey(0);
// 添加位置信息
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}
});
}
//jsonSingleResult["position"] = posArray; // 和前面的结果有一点点差异,但是很小。可以不要它
}
// 添加单字结果
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()) { // || std::regex_match(oneResult.OneStrUtf8, std::regex("^[\\s]+$"))
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) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
//posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
one_x = pos.X;
one_y = pos.Y;
break;
}
//jsonOneResult["position"] = posArray;
}
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);
//cv::Mat tmt = mt.clone();
//cv::rectangle(tmt, cv::Rect(one_x, one_y, one_w, one_h), cv::Scalar(0, 0, 255));
//cv::imshow("line", tmt);
//cv::waitKey(0);
j += len;
}
//jsonOneResult["word"] = oneResult.OneStrUtf8; // 单个字文本
//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) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
// //posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
// one_x = pos.X;
// one_y = pos.Y;
// break;
// }
// //jsonOneResult["position"] = posArray;
// }
// 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);
//cv::Mat tmt = mt.clone();
//cv::rectangle(tmt, cv::Rect(one_x, one_y, one_w, one_h), cv::Scalar(0, 0, 255));
//cv::imshow("line", tmt);
//cv::waitKey(0);
}
jsonSingleResult["charInfo"] = oneResultArray;
}
jsonResult.push_back(jsonSingleResult);
}
root["prism_wordsInfo"] = jsonResult;
return root.dump(4); // 使用4个空格的缩进来保留空白
}