返回结果加端口号, 批量运行 exe 脚本
Browse files- .gitignore +2 -0
- src/WeChatOcrCpp/0170.jpg +3 -0
- src/WeChatOcrCpp/Wchtcr.cpp +2 -2
- src/WeChatOcrCpp/Wchtcr.h +4 -0
- src/WeChatOcrCpp/WeChatOcrResult.cpp +37 -71
- src/WeChatOcrCpp/main.cpp +100 -31
- src/WeChatOcrCpp/ocr_aliyun.js +63 -44
- src/WeChatOcrCpp/package.json +6 -0
- src/WeChatOcrCpp/start_ocr_jobs.ps1 +78 -0
- src/WeChatOcrCpp/wechatocr_7789.json +8 -0
.gitignore
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
Debug/
|
|
|
|
|
|
|
| 2 |
Release/
|
| 3 |
x64/
|
| 4 |
bin/
|
|
|
|
| 1 |
Debug/
|
| 2 |
+
node_modules/
|
| 3 |
+
package-lock.json
|
| 4 |
Release/
|
| 5 |
x64/
|
| 6 |
bin/
|
src/WeChatOcrCpp/0170.jpg
ADDED
|
Git LFS Details
|
src/WeChatOcrCpp/Wchtcr.cpp
CHANGED
|
@@ -64,10 +64,10 @@ std::string Wchtcr::rec(const std::string& pth_img)
|
|
| 64 |
std::atomic<bool> taskCompleted(false);
|
| 65 |
|
| 66 |
// 创建 ImageOcr 实例
|
| 67 |
-
ImageOcr ocr;
|
| 68 |
|
| 69 |
// 设置回调函数并执行OCR
|
| 70 |
-
ocr.Run(pth_img, [&taskCompleted, &result](const std::string& imagePath, std::string& jsonstr) {
|
| 71 |
|
| 72 |
result = jsonstr;
|
| 73 |
|
|
|
|
| 64 |
std::atomic<bool> taskCompleted(false);
|
| 65 |
|
| 66 |
// 创建 ImageOcr 实例
|
| 67 |
+
//ImageOcr ocr;
|
| 68 |
|
| 69 |
// 设置回调函数并执行OCR
|
| 70 |
+
this->ocr.Run(pth_img, [&taskCompleted, &result](const std::string& imagePath, std::string& jsonstr) {
|
| 71 |
|
| 72 |
result = jsonstr;
|
| 73 |
|
src/WeChatOcrCpp/Wchtcr.h
CHANGED
|
@@ -1,11 +1,15 @@
|
|
| 1 |
#pragma once
|
| 2 |
#include <string>
|
| 3 |
#include <future>
|
|
|
|
|
|
|
| 4 |
#include "WeChatOcrResult.h"
|
| 5 |
#include "windows.h"
|
| 6 |
|
| 7 |
class __declspec(dllexport) Wchtcr
|
| 8 |
{
|
|
|
|
|
|
|
| 9 |
public:
|
| 10 |
Wchtcr();
|
| 11 |
|
|
|
|
| 1 |
#pragma once
|
| 2 |
#include <string>
|
| 3 |
#include <future>
|
| 4 |
+
#include "Wchtcr.h"
|
| 5 |
+
#include "ImageOcr.h"
|
| 6 |
#include "WeChatOcrResult.h"
|
| 7 |
#include "windows.h"
|
| 8 |
|
| 9 |
class __declspec(dllexport) Wchtcr
|
| 10 |
{
|
| 11 |
+
private:
|
| 12 |
+
ImageOcr ocr;
|
| 13 |
public:
|
| 14 |
Wchtcr();
|
| 15 |
|
src/WeChatOcrCpp/WeChatOcrResult.cpp
CHANGED
|
@@ -16,64 +16,6 @@
|
|
| 16 |
|
| 17 |
#include "utf8.h"
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
// see huggingface\ColorTextEditorV2\data\json
|
| 23 |
-
|
| 24 |
-
// Base64解码函数
|
| 25 |
-
//std::string ParseOcrResult::Base64Decode(const std::string& encoded_string) {
|
| 26 |
-
// if (encoded_string.empty()) return "";
|
| 27 |
-
//
|
| 28 |
-
// // Base64编码表
|
| 29 |
-
// static const std::string base64_chars =
|
| 30 |
-
// "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
| 31 |
-
// "abcdefghijklmnopqrstuvwxyz"
|
| 32 |
-
// "0123456789+/";
|
| 33 |
-
//
|
| 34 |
-
// // 解码过程
|
| 35 |
-
// size_t in_len = encoded_string.size();
|
| 36 |
-
// size_t i = 0;
|
| 37 |
-
// size_t j = 0;
|
| 38 |
-
// size_t in_ = 0;
|
| 39 |
-
// unsigned char char_array_4[4], char_array_3[3];
|
| 40 |
-
// std::string ret;
|
| 41 |
-
//
|
| 42 |
-
// while (in_len-- && (encoded_string[in_] != '=') &&
|
| 43 |
-
// (isalnum(encoded_string[in_]) || (encoded_string[in_] == '+') || (encoded_string[in_] == '/'))) {
|
| 44 |
-
// char_array_4[i++] = encoded_string[in_]; in_++;
|
| 45 |
-
// if (i == 4) {
|
| 46 |
-
// for (i = 0; i < 4; i++)
|
| 47 |
-
// char_array_4[i] = base64_chars.find(char_array_4[i]);
|
| 48 |
-
//
|
| 49 |
-
// char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
| 50 |
-
// char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
| 51 |
-
// char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
| 52 |
-
//
|
| 53 |
-
// for (i = 0; i < 3; i++)
|
| 54 |
-
// ret += char_array_3[i];
|
| 55 |
-
// i = 0;
|
| 56 |
-
// }
|
| 57 |
-
// }
|
| 58 |
-
//
|
| 59 |
-
// if (i) {
|
| 60 |
-
// for (j = i; j < 4; j++)
|
| 61 |
-
// char_array_4[j] = 0;
|
| 62 |
-
//
|
| 63 |
-
// for (j = 0; j < 4; j++)
|
| 64 |
-
// char_array_4[j] = base64_chars.find(char_array_4[j]);
|
| 65 |
-
//
|
| 66 |
-
// char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
| 67 |
-
// char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
| 68 |
-
// char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
| 69 |
-
//
|
| 70 |
-
// for (j = 0; j < i - 1; j++)
|
| 71 |
-
// ret += char_array_3[j];
|
| 72 |
-
// }
|
| 73 |
-
//
|
| 74 |
-
// return ret;
|
| 75 |
-
//}
|
| 76 |
-
|
| 77 |
// 解析JSON响应
|
| 78 |
WeChatOcrResult* ParseOcrResult::ParseJson(const std::string& jsonResponseStr) {
|
| 79 |
try {
|
|
@@ -240,14 +182,9 @@ std::string OcrResult::ToJson(std::string& pth_img) const {
|
|
| 240 |
continue;
|
| 241 |
}
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
// todo: 改成一个字一个框
|
| 246 |
-
// 好像 wechat 的原始结果对同一个单词中的每个字母就是给出相同的 x, y, w, h ? 但是有时 OneStrUtf8 它给的是不止一个字符,所以后面这里又自已拆一次?
|
| 247 |
-
// 虽说是单字结果,但是有可能是一个单词的多个字。这种情况拆出每一个字符,让它们在图片中有相同的坐标和宽高。既是整个单词的大框
|
| 248 |
for (size_t j = 0; j < oneResult.OneStrUtf8.size();) {
|
| 249 |
-
|
| 250 |
-
size_t len = utf8codepointcalcsize(&oneResult.OneStrUtf8[j]); // 计算字符字节数
|
| 251 |
std::string word = oneResult.OneStrUtf8.substr(j, len);
|
| 252 |
|
| 253 |
jsonOneResult["word"] = word; // 单个字文本
|
|
@@ -284,15 +221,44 @@ std::string OcrResult::ToJson(std::string& pth_img) const {
|
|
| 284 |
//cv::imshow("line", tmt);
|
| 285 |
//cv::waitKey(0);
|
| 286 |
|
| 287 |
-
num_char += 1;
|
| 288 |
j += len;
|
| 289 |
}
|
| 290 |
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
}
|
| 298 |
jsonSingleResult["charInfo"] = oneResultArray;
|
|
|
|
| 16 |
|
| 17 |
#include "utf8.h"
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
// 解析JSON响应
|
| 20 |
WeChatOcrResult* ParseOcrResult::ParseJson(const std::string& jsonResponseStr) {
|
| 21 |
try {
|
|
|
|
| 182 |
continue;
|
| 183 |
}
|
| 184 |
|
| 185 |
+
// 虽说是单字结果,但是有可能是一个单词的多个字。这种情况拆出每一个字符,让它们在图片中有相同的坐标和宽高。既是整个单词的大框
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
for (size_t j = 0; j < oneResult.OneStrUtf8.size();) {
|
| 187 |
+
size_t len = utf8codepointcalcsize((const utf8_int8_t*)&oneResult.OneStrUtf8[j]); // 计算字符字节数
|
|
|
|
| 188 |
std::string word = oneResult.OneStrUtf8.substr(j, len);
|
| 189 |
|
| 190 |
jsonOneResult["word"] = word; // 单个字文本
|
|
|
|
| 221 |
//cv::imshow("line", tmt);
|
| 222 |
//cv::waitKey(0);
|
| 223 |
|
|
|
|
| 224 |
j += len;
|
| 225 |
}
|
| 226 |
|
| 227 |
+
|
| 228 |
+
//jsonOneResult["word"] = oneResult.OneStrUtf8; // 单个字文本
|
| 229 |
+
|
| 230 |
+
//int one_x = -1, one_y = -1, one_w = -1, one_h = h;
|
| 231 |
+
//
|
| 232 |
+
// if (oneResult.OnePos) {
|
| 233 |
+
// nlohmann::json posArray = nlohmann::json::array();
|
| 234 |
+
// for (const auto& pos : oneResult.OnePos->Pos) { // 理论上应该是有四个角的坐标,但是 wechat ocr 实际上只给出了一个左上角的坐标
|
| 235 |
+
// //posArray.push_back({ {"x", pos.X},{"y", pos.Y} });
|
| 236 |
+
// one_x = pos.X;
|
| 237 |
+
// one_y = pos.Y;
|
| 238 |
+
// break;
|
| 239 |
+
// }
|
| 240 |
+
// //jsonOneResult["position"] = posArray;
|
| 241 |
+
// }
|
| 242 |
+
|
| 243 |
+
// if (i == singleResult.OneResult.size() - 1) {
|
| 244 |
+
// one_w = rht - one_x;
|
| 245 |
+
// }
|
| 246 |
+
// else {
|
| 247 |
+
// one_w = singleResult.OneResult[i + 1].OnePos->Pos[0].X - one_x; // 获取下一个单字的左上角坐标
|
| 248 |
+
// }
|
| 249 |
+
|
| 250 |
+
// jsonOneResult["x"] = one_x;
|
| 251 |
+
// jsonOneResult["y"] = one_y;
|
| 252 |
+
// jsonOneResult["w"] = one_w;
|
| 253 |
+
// jsonOneResult["h"] = one_h;
|
| 254 |
+
//
|
| 255 |
+
// oneResultArray.push_back(jsonOneResult);
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
//cv::Mat tmt = mt.clone();
|
| 259 |
+
//cv::rectangle(tmt, cv::Rect(one_x, one_y, one_w, one_h), cv::Scalar(0, 0, 255));
|
| 260 |
+
//cv::imshow("line", tmt);
|
| 261 |
+
//cv::waitKey(0);
|
| 262 |
|
| 263 |
}
|
| 264 |
jsonSingleResult["charInfo"] = oneResultArray;
|
src/WeChatOcrCpp/main.cpp
CHANGED
|
@@ -22,6 +22,8 @@
|
|
| 22 |
#include <opencv2/videoio.hpp>
|
| 23 |
#include <opencv2/imgproc.hpp>
|
| 24 |
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
using json = nlohmann::json;
|
|
@@ -56,9 +58,32 @@ extern "C" {
|
|
| 56 |
void StartHttpServer(int port) {
|
| 57 |
httplib::Server svr;
|
| 58 |
|
|
|
|
|
|
|
| 59 |
// 添加POST接口,接收base64编码的图片
|
| 60 |
-
svr.Post("/wechatocr", [](const httplib::Request& req, httplib::Response& res) {
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
// 检查Content-Type
|
| 64 |
const auto& content_type = req.get_header_value("Content-Type");
|
|
@@ -80,7 +105,10 @@ void StartHttpServer(int port) {
|
|
| 80 |
base64_image = request_data["img"].get<std::string>();
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
}
|
| 85 |
|
| 86 |
std::vector<unsigned char> b64_buf = Utilities::Base64Decode_Raw(base64_image);
|
|
@@ -90,7 +118,9 @@ void StartHttpServer(int port) {
|
|
| 90 |
char tempPath[MAX_PATH], tempFile[MAX_PATH];
|
| 91 |
GetTempPathA(MAX_PATH, tempPath); // 获取系统临时目录
|
| 92 |
if (GetTempFileNameA(tempPath, "TMP", 0, tempFile) == 0) { // "TMP"为前缀,0表示自动生成唯一编号
|
| 93 |
-
|
|
|
|
|
|
|
| 94 |
return;
|
| 95 |
}
|
| 96 |
strcat_s(tempFile, ".jpg");
|
|
@@ -103,61 +133,100 @@ void StartHttpServer(int port) {
|
|
| 103 |
|
| 104 |
// 调用rec函数进行OCR识别
|
| 105 |
std::string result = rec(tempFile);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
|
|
|
|
|
|
| 107 |
// 返回识别结果
|
|
|
|
| 108 |
res.set_content(result, "application/json");
|
| 109 |
-
//res.set_content("{\"msg\":\"ok. 正常\" }", "application/json");
|
| 110 |
|
| 111 |
// 删除临时文件
|
| 112 |
std::remove(tempFile);
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
catch (const std::exception& e) {
|
| 115 |
-
|
| 116 |
-
res.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
|
|
|
|
|
|
| 118 |
});
|
| 119 |
|
| 120 |
// 添加一个简单的GET接口,用于测试服务器是否正常运行
|
| 121 |
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
|
| 122 |
-
res.set_content("OCR
|
| 123 |
});
|
| 124 |
|
| 125 |
-
std::cout << "
|
| 126 |
svr.listen("0.0.0.0", port);
|
| 127 |
}
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
int main()
|
| 132 |
{
|
| 133 |
//Wchtcr wchtcr;
|
| 134 |
//
|
| 135 |
-
// std::string result = wchtcr.rec("
|
| 136 |
//json jsn;
|
| 137 |
//std::string jsn_str;
|
| 138 |
|
| 139 |
-
//
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
// 分离线程,让它在后台运行
|
| 143 |
server_thread.detach();
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
| 161 |
|
| 162 |
return 0;
|
| 163 |
}
|
|
|
|
| 22 |
#include <opencv2/videoio.hpp>
|
| 23 |
#include <opencv2/imgproc.hpp>
|
| 24 |
|
| 25 |
+
#include <format>
|
| 26 |
+
|
| 27 |
|
| 28 |
|
| 29 |
using json = nlohmann::json;
|
|
|
|
| 58 |
void StartHttpServer(int port) {
|
| 59 |
httplib::Server svr;
|
| 60 |
|
| 61 |
+
std::atomic<int> totalTasks(0); // 使用原子类型确保线程安全
|
| 62 |
+
|
| 63 |
// 添加POST接口,接收base64编码的图片
|
| 64 |
+
svr.Post("/wechatocr", [&totalTasks, &port](const httplib::Request& req, httplib::Response& res) {
|
| 65 |
+
|
| 66 |
+
std::string msg_Warning_waiting = std::vformat("{{\"msg\":\"warning: waiting previous task done. port {}\" }}", std::make_format_args(port));
|
| 67 |
+
std::string msg_Err_noParam = std::vformat("{{\"msg\":\"error: not have img param. port {}\" }}", std::make_format_args(port));
|
| 68 |
+
std::string msg_Err_genFilename = std::vformat("{{\"msg\":\"error: gen tmp filename fail. port {}\" }}", std::make_format_args(port));
|
| 69 |
+
std::string msg_Err_rec_fail = std::vformat("{{\"msg\":\"error: rec fail port {}\" }}", std::make_format_args(port));
|
| 70 |
+
|
| 71 |
+
try {
|
| 72 |
+
|
| 73 |
+
// while (totalTasks + 1 > 1) {
|
| 74 |
+
// std::cout << "########## Warning: Max tasks overflow, Total tasks " << totalTasks << " waitting now... " << std::endl;
|
| 75 |
+
//std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // 等待 1 秒
|
| 76 |
+
// }
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
if (totalTasks > 0) {
|
| 81 |
+
res.status = 201;
|
| 82 |
+
res.set_content(msg_Warning_waiting, "application/json");
|
| 83 |
+
return;
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
std::cout << "********* Received a new task. Total tasks: " << ++totalTasks << std::endl;
|
| 87 |
|
| 88 |
// 检查Content-Type
|
| 89 |
const auto& content_type = req.get_header_value("Content-Type");
|
|
|
|
| 105 |
base64_image = request_data["img"].get<std::string>();
|
| 106 |
}
|
| 107 |
else {
|
| 108 |
+
--totalTasks;
|
| 109 |
+
res.status = 500;
|
| 110 |
+
res.set_content(msg_Err_noParam, "application/json");
|
| 111 |
+
return;
|
| 112 |
}
|
| 113 |
|
| 114 |
std::vector<unsigned char> b64_buf = Utilities::Base64Decode_Raw(base64_image);
|
|
|
|
| 118 |
char tempPath[MAX_PATH], tempFile[MAX_PATH];
|
| 119 |
GetTempPathA(MAX_PATH, tempPath); // 获取系统临时目录
|
| 120 |
if (GetTempFileNameA(tempPath, "TMP", 0, tempFile) == 0) { // "TMP"为前缀,0表示自动生成唯一编号
|
| 121 |
+
--totalTasks;
|
| 122 |
+
res.status = 201;
|
| 123 |
+
res.set_content(msg_Err_genFilename, "application/json");
|
| 124 |
return;
|
| 125 |
}
|
| 126 |
strcat_s(tempFile, ".jpg");
|
|
|
|
| 133 |
|
| 134 |
// 调用rec函数进行OCR识别
|
| 135 |
std::string result = rec(tempFile);
|
| 136 |
+
if (result.empty()) {
|
| 137 |
+
--totalTasks;
|
| 138 |
+
res.status = 201;
|
| 139 |
+
res.set_content(msg_Err_rec_fail, "application/json");
|
| 140 |
+
return;
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
json jsn = json::parse(result);
|
| 144 |
+
jsn["port"] = port; // 添加端口号到结果中
|
| 145 |
+
result = jsn.dump(4);
|
| 146 |
+
|
| 147 |
+
--totalTasks;
|
| 148 |
|
| 149 |
+
std::cout << "one task done. Total tasks:" << totalTasks << std::endl;
|
| 150 |
+
|
| 151 |
// 返回识别结果
|
| 152 |
+
res.status = 200;
|
| 153 |
res.set_content(result, "application/json");
|
|
|
|
| 154 |
|
| 155 |
// 删除临时文件
|
| 156 |
std::remove(tempFile);
|
| 157 |
+
|
| 158 |
+
return;
|
| 159 |
}
|
| 160 |
catch (const std::exception& e) {
|
| 161 |
+
--totalTasks;
|
| 162 |
+
res.status = 201;
|
| 163 |
+
std::string error_msg = e.what();
|
| 164 |
+
std::string msg_exception = std::vformat("{\"msg\":\"error: server error. {} port {}\" }", std::make_format_args(error_msg, port) );
|
| 165 |
+
res.set_content(msg_exception, "application/json");
|
| 166 |
+
return;
|
| 167 |
}
|
| 168 |
+
|
| 169 |
+
|
| 170 |
});
|
| 171 |
|
| 172 |
// 添加一个简单的GET接口,用于测试服务器是否正常运行
|
| 173 |
svr.Get("/", [](const httplib::Request&, httplib::Response& res) {
|
| 174 |
+
res.set_content("OCR Sever running ....", "text/plain");
|
| 175 |
});
|
| 176 |
|
| 177 |
+
std::cout << "OCR Sever running ... : " << port << std::endl;
|
| 178 |
svr.listen("0.0.0.0", port);
|
| 179 |
}
|
| 180 |
|
| 181 |
+
int main(int argc, char* argv[])
|
|
|
|
|
|
|
| 182 |
{
|
| 183 |
//Wchtcr wchtcr;
|
| 184 |
//
|
| 185 |
+
// std::string result = wchtcr.rec("t2.jpg");
|
| 186 |
//json jsn;
|
| 187 |
//std::string jsn_str;
|
| 188 |
|
| 189 |
+
// 默认端口号
|
| 190 |
+
int port = 7788;
|
| 191 |
+
|
| 192 |
+
// 如果提供了命令行参数,使用第一个参数作为端口号
|
| 193 |
+
if (argc > 1) {
|
| 194 |
+
try {
|
| 195 |
+
port = std::stoi(argv[1]);
|
| 196 |
+
if (port <= 0 || port > 65535) {
|
| 197 |
+
std::cout << "Invalid port number. Using default port 7788." << std::endl;
|
| 198 |
+
port = 7788;
|
| 199 |
+
}
|
| 200 |
+
} catch (const std::exception& e) {
|
| 201 |
+
std::cout << "Invalid port number format. Using default port 7788." << std::endl;
|
| 202 |
+
port = 7788;
|
| 203 |
+
}
|
| 204 |
+
}
|
| 205 |
+
|
| 206 |
+
// 启动HTTP服务器,使用指定的端口
|
| 207 |
+
std::thread server_thread(StartHttpServer, port);
|
| 208 |
|
| 209 |
// 分离线程,让它在后台运行
|
| 210 |
server_thread.detach();
|
| 211 |
|
| 212 |
+
do {
|
| 213 |
+
// 主线程可以做其他事情,或者等待用户输入退出
|
| 214 |
+
//std::cout << "ok." << std::endl;
|
| 215 |
+
std::cin.get();
|
| 216 |
+
} while (true);
|
| 217 |
+
|
| 218 |
+
std::string result = rec("t2.jpg");
|
| 219 |
+
json jsn;
|
| 220 |
+
std::string jsn_str;
|
| 221 |
+
|
| 222 |
+
try {
|
| 223 |
+
jsn = json::parse(result);
|
| 224 |
+
jsn_str = jsn.dump(4);
|
| 225 |
+
}
|
| 226 |
+
catch (const std::exception& e) {
|
| 227 |
+
std::cerr << "OCR识别异常: " << e.what() << std::endl;
|
| 228 |
+
return 1;
|
| 229 |
+
}
|
| 230 |
|
| 231 |
return 0;
|
| 232 |
}
|
src/WeChatOcrCpp/ocr_aliyun.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
| 1 |
(async () => {
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
async function ocr(imgData) {
|
| 5 |
// const api = 'https://gjbsb.market.alicloudapi.com/ocrservice/advanced';
|
|
@@ -12,50 +20,56 @@
|
|
| 12 |
const data = await new Promise((resolve, reject) => {
|
| 13 |
request.post({
|
| 14 |
url: api,
|
| 15 |
-
timeout: 1000 * 60 *
|
| 16 |
headers: {
|
| 17 |
-
|
| 18 |
-
|
| 19 |
},
|
| 20 |
body: JSON.stringify({
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
})
|
| 28 |
}, (error, response, body) => {
|
| 29 |
if (error) {
|
| 30 |
console.log('#####ERROR: aliyun ocr fail.');
|
| 31 |
console.log(error);
|
| 32 |
-
resolve(
|
| 33 |
|
| 34 |
// reject(error);
|
| 35 |
}
|
| 36 |
else {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
| 49 |
resolve([aliResult, null]);
|
| 50 |
}
|
| 51 |
})
|
| 52 |
});
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
if (data.error_code !== undefined && data.error_code !== null) {
|
| 55 |
-
return [
|
| 56 |
}
|
| 57 |
|
| 58 |
-
return [
|
| 59 |
|
| 60 |
// //JSON写入
|
| 61 |
// fs.writeFileSync(jsonPath, JSON.stringify(data));
|
|
@@ -64,27 +78,32 @@
|
|
| 64 |
}
|
| 65 |
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
let fs = require('fs')
|
| 70 |
let md5 = require('md5')
|
| 71 |
|
| 72 |
-
let bytes = fs.readFileSync("
|
| 73 |
let buf = Buffer.from(bytes)
|
| 74 |
let m5 = md5(buf)
|
| 75 |
let b64 = buf.toString('base64')
|
| 76 |
|
| 77 |
-
let [ re, err ] = await ocr(b64)
|
| 78 |
|
| 79 |
-
let
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
bookNO: 'lrx333',
|
| 84 |
-
imgName: '1032.jpg',
|
| 85 |
-
originImgData: b64
|
| 86 |
}
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
// json = {
|
| 89 |
// md5: '7468efae7d7ab7333d0197a8ca1bf32c',
|
| 90 |
// imgData: 'hasTest',
|
|
@@ -93,21 +112,21 @@
|
|
| 93 |
// imgName: '1032.jpg'
|
| 94 |
// }
|
| 95 |
|
| 96 |
-
let bent = require('bent')
|
| 97 |
-
let formurlencoded = require('form-urlencoded')
|
| 98 |
|
| 99 |
-
let formurlencoded_body = formurlencoded(json)
|
| 100 |
|
| 101 |
-
let post = bent('http://webapi.xfyun.cn', 'POST', 'json', 200)
|
| 102 |
-
// let post = bent('http://127.0.0.1:11112', 'POST', 'json', 200)
|
| 103 |
-
let response = await post('/v1/service/v1/ocr/general', json)
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
-
data = JSON.parse(response).data.test
|
| 108 |
|
| 109 |
-
let s = JSON.stringify(response)
|
| 110 |
|
| 111 |
-
console.log(response)
|
| 112 |
|
| 113 |
})()
|
|
|
|
| 1 |
(async () => {
|
| 2 |
|
| 3 |
+
// New-NetFirewallRule -DisplayName "Allow Port 7788" -Direction Inbound -Protocol TCP -LocalPort 7788 -Action Allow
|
| 4 |
+
|
| 5 |
+
async function sleep(ms) {
|
| 6 |
+
return new Promise((resolve) => {
|
| 7 |
+
setTimeout(resolve, ms)
|
| 8 |
+
})
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
|
| 12 |
async function ocr(imgData) {
|
| 13 |
// const api = 'https://gjbsb.market.alicloudapi.com/ocrservice/advanced';
|
|
|
|
| 20 |
const data = await new Promise((resolve, reject) => {
|
| 21 |
request.post({
|
| 22 |
url: api,
|
| 23 |
+
timeout: 1000 * 60 * 60 * 24,
|
| 24 |
headers: {
|
| 25 |
+
"Authorization": `APPCODE ${appCode}`,
|
| 26 |
+
"Content-Type": "application/json;charset=UTF-8"
|
| 27 |
},
|
| 28 |
body: JSON.stringify({
|
| 29 |
+
img: imgData,
|
| 30 |
+
prob: true,
|
| 31 |
+
charInfo: true,
|
| 32 |
+
table: true,
|
| 33 |
+
sortPage: true,
|
| 34 |
+
NeedRotate: true
|
| 35 |
})
|
| 36 |
}, (error, response, body) => {
|
| 37 |
if (error) {
|
| 38 |
console.log('#####ERROR: aliyun ocr fail.');
|
| 39 |
console.log(error);
|
| 40 |
+
resolve([null, error])
|
| 41 |
|
| 42 |
// reject(error);
|
| 43 |
}
|
| 44 |
else {
|
| 45 |
+
if (response.statusCode != 200) {
|
| 46 |
+
console.log('#####ERROR: aliyun ocr fail');
|
| 47 |
+
return resolve([null, { "error_code": response.statusCode, "error_msg": body }])
|
| 48 |
+
}
|
| 49 |
+
let aliResult = null;
|
| 50 |
+
try {
|
| 51 |
+
aliResult = JSON.parse(body);
|
| 52 |
+
} catch (ex) {
|
| 53 |
+
console.log('#####ERROR: aliyun ocr fail.');
|
| 54 |
+
console.log(ex.message);
|
| 55 |
+
console.log(response.statusCode);
|
| 56 |
+
console.log(error);
|
| 57 |
+
console.log(body);
|
| 58 |
+
}
|
| 59 |
resolve([aliResult, null]);
|
| 60 |
}
|
| 61 |
})
|
| 62 |
});
|
| 63 |
|
| 64 |
+
if (data[0] != null) {
|
| 65 |
+
console.log(`one task done.`)
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
if (data.error_code !== undefined && data.error_code !== null) {
|
| 69 |
+
return [null, { "code": data.error_code, "msg": data.error_msg }]
|
| 70 |
}
|
| 71 |
|
| 72 |
+
return [data, null]
|
| 73 |
|
| 74 |
// //JSON写入
|
| 75 |
// fs.writeFileSync(jsonPath, JSON.stringify(data));
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
|
|
|
|
|
|
|
| 81 |
let fs = require('fs')
|
| 82 |
let md5 = require('md5')
|
| 83 |
|
| 84 |
+
let bytes = fs.readFileSync("0170.jpg")
|
| 85 |
let buf = Buffer.from(bytes)
|
| 86 |
let m5 = md5(buf)
|
| 87 |
let b64 = buf.toString('base64')
|
| 88 |
|
|
|
|
| 89 |
|
| 90 |
+
for (let i = 0; i < 5000; i++) {
|
| 91 |
+
console.log(`Send ${i}`)
|
| 92 |
+
ocr(b64);
|
| 93 |
+
await sleep(300);
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
+
// let [ re, err ] = await ocr(b64)
|
| 97 |
+
|
| 98 |
+
// let json = {
|
| 99 |
+
// md5: m5,
|
| 100 |
+
// imgData: b64,
|
| 101 |
+
// guid: '73ffc72d-bfce-43bc-9e90-0007605c6fdb',
|
| 102 |
+
// bookNO: 'lrx333',
|
| 103 |
+
// imgName: '1032.jpg',
|
| 104 |
+
// originImgData: b64
|
| 105 |
+
// }
|
| 106 |
+
|
| 107 |
// json = {
|
| 108 |
// md5: '7468efae7d7ab7333d0197a8ca1bf32c',
|
| 109 |
// imgData: 'hasTest',
|
|
|
|
| 112 |
// imgName: '1032.jpg'
|
| 113 |
// }
|
| 114 |
|
| 115 |
+
// let bent = require('bent')
|
| 116 |
+
// let formurlencoded = require('form-urlencoded')
|
| 117 |
|
| 118 |
+
// let formurlencoded_body = formurlencoded(json)
|
| 119 |
|
| 120 |
+
// let post = bent('http://webapi.xfyun.cn', 'POST', 'json', 200)
|
| 121 |
+
// // let post = bent('http://127.0.0.1:11112', 'POST', 'json', 200)
|
| 122 |
+
// let response = await post('/v1/service/v1/ocr/general', json)
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
+
// data = JSON.parse(response).data.test
|
| 127 |
|
| 128 |
+
// let s = JSON.stringify(response)
|
| 129 |
|
| 130 |
+
// console.log(response)
|
| 131 |
|
| 132 |
})()
|
src/WeChatOcrCpp/package.json
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"dependencies": {
|
| 3 |
+
"md5": "^2.3.0",
|
| 4 |
+
"request": "^2.88.2"
|
| 5 |
+
}
|
| 6 |
+
}
|
src/WeChatOcrCpp/start_ocr_jobs.ps1
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# start_ocr_jobs - 使用后台作业启动多个OCR进程
|
| 2 |
+
# 使用默认参数(端口7789 开始,10个进程):
|
| 3 |
+
# .\start_ocr_jobs.ps1
|
| 4 |
+
# 自定义参数:
|
| 5 |
+
# .\start_ocr_jobs.ps1 -StartPort 7789 -Count 3
|
| 6 |
+
|
| 7 |
+
param(
|
| 8 |
+
[int]$StartPort = 7789,
|
| 9 |
+
[int]$Count = 2,
|
| 10 |
+
[string]$ExePath = ".\WeChatOcrCpp.exe"
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
Write-Host "Starting $Count OCR processes as background jobs from port $StartPort..." -ForegroundColor Yellow
|
| 14 |
+
|
| 15 |
+
# 清理之前可能存在的OCR作业
|
| 16 |
+
$existingJobs = Get-Job -Name "OCR_*" -ErrorAction SilentlyContinue
|
| 17 |
+
if ($existingJobs) {
|
| 18 |
+
Write-Host "Cleaning up existing OCR jobs..." -ForegroundColor Yellow
|
| 19 |
+
$existingJobs | Stop-Job -PassThru | Remove-Job -Force
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# 启动新的后台作业
|
| 23 |
+
for ($i = 0; $i -lt $Count; $i++) {
|
| 24 |
+
$port = $StartPort + $i
|
| 25 |
+
$jobName = "OCR_$port"
|
| 26 |
+
|
| 27 |
+
try {
|
| 28 |
+
# 创建后台作业
|
| 29 |
+
$job = Start-Job -Name $jobName -ScriptBlock {
|
| 30 |
+
param($exePath, $port)
|
| 31 |
+
|
| 32 |
+
# 检查exe文件是否存在
|
| 33 |
+
if (-not (Test-Path $exePath)) {
|
| 34 |
+
throw "Executable not found: $exePath"
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# 启动进程
|
| 38 |
+
$process = Start-Process -FilePath $exePath -ArgumentList $port -PassThru -NoNewWindow
|
| 39 |
+
|
| 40 |
+
# 等待进程结束或被停止
|
| 41 |
+
$process.WaitForExit()
|
| 42 |
+
|
| 43 |
+
return @{
|
| 44 |
+
Port = $port
|
| 45 |
+
ProcessId = $process.Id
|
| 46 |
+
ExitCode = $process.ExitCode
|
| 47 |
+
ExitTime = Get-Date
|
| 48 |
+
}
|
| 49 |
+
} -ArgumentList $ExePath, $port
|
| 50 |
+
|
| 51 |
+
Write-Host "✓ Started OCR job '$jobName' for port $port (Job ID: $($job.Id))" -ForegroundColor Green
|
| 52 |
+
Start-Sleep -Milliseconds 2000 # 稍微延迟避免资源冲突
|
| 53 |
+
}
|
| 54 |
+
catch {
|
| 55 |
+
Write-Host "✗ Failed to start job for port $port`: $($_.Exception.Message)" -ForegroundColor Red
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
Write-Host "\n=== Job Management Commands ===" -ForegroundColor Cyan
|
| 60 |
+
Write-Host "查看所有OCR作业状态: Get-Job -Name 'OCR_*'"
|
| 61 |
+
Write-Host "查看作业详细信息: Get-Job -Name 'OCR_*' | Format-Table Name, State, HasMoreData"
|
| 62 |
+
Write-Host "接收作业输出: Receive-Job -Name 'OCR_7789' -Keep"
|
| 63 |
+
Write-Host "停止所有OCR作业: Get-Job -Name 'OCR_*' | Stop-Job"
|
| 64 |
+
Write-Host "删除所有OCR作业: Get-Job -Name 'OCR_*' | Remove-Job -Force"
|
| 65 |
+
|
| 66 |
+
# 显示当前作业状态
|
| 67 |
+
Write-Host "\n=== Current Job Status ===" -ForegroundColor Cyan
|
| 68 |
+
$jobs = Get-Job -Name "OCR_*" -ErrorAction SilentlyContinue
|
| 69 |
+
if ($jobs) {
|
| 70 |
+
$jobs | Format-Table Name, State, HasMoreData, PSBeginTime -AutoSize
|
| 71 |
+
} else {
|
| 72 |
+
Write-Host "No OCR jobs found." -ForegroundColor Yellow
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
Write-Host "\nAll OCR jobs have been started. Use the commands above to manage them." -ForegroundColor Green
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
src/WeChatOcrCpp/wechatocr_7789.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "WeChatOCR_7789",
|
| 3 |
+
"script": "WeChatOcrCpp.exe",
|
| 4 |
+
"args": ["7789"],
|
| 5 |
+
"cwd": "E:\\WeChatOCR_Server",
|
| 6 |
+
"exec_interpreter": "none"
|
| 7 |
+
}
|
| 8 |
+
|