|
|
|
|
|
|
|
|
#define WIN32_LEAN_AND_MEAN |
|
|
|
|
|
#include <iostream> |
|
|
#include <thread> |
|
|
#include <chrono> |
|
|
#include <atomic> |
|
|
#include <windows.h> |
|
|
|
|
|
#include "ImageOcr.h" |
|
|
#include "Wchtcr.h" |
|
|
#include "Utilities.h" |
|
|
|
|
|
#include "nlohmann/json.hpp" |
|
|
|
|
|
#include "httplib.h" |
|
|
|
|
|
#include <opencv2/core.hpp> |
|
|
#include <opencv2/highgui.hpp> |
|
|
#include <opencv2/imgcodecs.hpp> |
|
|
#include <opencv2/videoio.hpp> |
|
|
#include <opencv2/imgproc.hpp> |
|
|
|
|
|
#include <format> |
|
|
|
|
|
|
|
|
|
|
|
using json = nlohmann::json; |
|
|
|
|
|
|
|
|
BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) { |
|
|
switch (ul_reason_for_call) { |
|
|
case DLL_PROCESS_ATTACH: |
|
|
|
|
|
break; |
|
|
case DLL_THREAD_ATTACH: |
|
|
break; |
|
|
case DLL_THREAD_DETACH: |
|
|
break; |
|
|
case DLL_PROCESS_DETACH: |
|
|
|
|
|
break; |
|
|
} |
|
|
return TRUE; |
|
|
} |
|
|
|
|
|
extern "C" { |
|
|
__declspec(dllexport) const char* rec(const char* pth_img) { |
|
|
thread_local static std::string result; |
|
|
Wchtcr wchtcr; |
|
|
result = wchtcr.rec(pth_img); |
|
|
return result.c_str(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void StartHttpServer(int port) { |
|
|
httplib::Server svr; |
|
|
|
|
|
std::atomic<int> totalTasks(0); |
|
|
|
|
|
|
|
|
svr.Post("/wechatocr", [&totalTasks, &port](const httplib::Request& req, httplib::Response& res) { |
|
|
|
|
|
std::string msg_Warning_waiting = std::vformat("{{\"msg\":\"warning: waiting previous task done. port {}\" }}", std::make_format_args(port)); |
|
|
std::string msg_Err_noParam = std::vformat("{{\"msg\":\"error: not have img param. port {}\" }}", std::make_format_args(port)); |
|
|
std::string msg_Err_genFilename = std::vformat("{{\"msg\":\"error: gen tmp filename fail. port {}\" }}", std::make_format_args(port)); |
|
|
std::string msg_Err_rec_fail = std::vformat("{{\"msg\":\"error: rec fail port {}\" }}", std::make_format_args(port)); |
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (totalTasks > 0) { |
|
|
res.status = 201; |
|
|
res.set_content(msg_Warning_waiting, "application/json"); |
|
|
return; |
|
|
} |
|
|
|
|
|
std::cout << "********* Received a new task. Total tasks: " << ++totalTasks << std::endl; |
|
|
|
|
|
|
|
|
const auto& content_type = req.get_header_value("Content-Type"); |
|
|
std::string body_content = req.body; |
|
|
|
|
|
|
|
|
if (content_type.find("application/json") != std::string::npos && |
|
|
req.body.find("%") != std::string::npos) { |
|
|
|
|
|
body_content = httplib::detail::decode_path(req.body, true); |
|
|
} |
|
|
|
|
|
|
|
|
json request_data = json::parse(body_content); |
|
|
|
|
|
|
|
|
std::string base64_image; |
|
|
if (request_data.contains("img")) { |
|
|
base64_image = request_data["img"].get<std::string>(); |
|
|
} |
|
|
else { |
|
|
--totalTasks; |
|
|
res.status = 500; |
|
|
res.set_content(msg_Err_noParam, "application/json"); |
|
|
return; |
|
|
} |
|
|
|
|
|
std::vector<unsigned char> b64_buf = Utilities::Base64Decode_Raw(base64_image); |
|
|
|
|
|
cv::Mat img_orgin = cv::imdecode(b64_buf, -1); |
|
|
|
|
|
char tempPath[MAX_PATH], tempFile[MAX_PATH]; |
|
|
GetTempPathA(MAX_PATH, tempPath); |
|
|
if (GetTempFileNameA(tempPath, "TMP", 0, tempFile) == 0) { |
|
|
--totalTasks; |
|
|
res.status = 201; |
|
|
res.set_content(msg_Err_genFilename, "application/json"); |
|
|
return; |
|
|
} |
|
|
strcat_s(tempFile, ".jpg"); |
|
|
|
|
|
cv::imwrite(tempFile, img_orgin); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string result = rec(tempFile); |
|
|
if (result.empty()) { |
|
|
--totalTasks; |
|
|
res.status = 201; |
|
|
res.set_content(msg_Err_rec_fail, "application/json"); |
|
|
return; |
|
|
} |
|
|
|
|
|
json jsn = json::parse(result); |
|
|
jsn["port"] = port; |
|
|
result = jsn.dump(4); |
|
|
|
|
|
--totalTasks; |
|
|
|
|
|
std::cout << "one task done. Total tasks:" << totalTasks << std::endl; |
|
|
|
|
|
|
|
|
res.status = 200; |
|
|
res.set_content(result, "application/json"); |
|
|
|
|
|
|
|
|
std::remove(tempFile); |
|
|
|
|
|
return; |
|
|
} |
|
|
catch (const std::exception& e) { |
|
|
--totalTasks; |
|
|
res.status = 201; |
|
|
std::string error_msg = e.what(); |
|
|
std::string msg_exception = std::vformat("{\"msg\":\"error: server error. {} port {}\" }", std::make_format_args(error_msg, port) ); |
|
|
res.set_content(msg_exception, "application/json"); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
svr.Get("/", [](const httplib::Request&, httplib::Response& res) { |
|
|
res.set_content("OCR Sever running ....", "text/plain"); |
|
|
}); |
|
|
|
|
|
std::cout << "OCR Sever running ... : " << port << std::endl; |
|
|
svr.listen("0.0.0.0", port); |
|
|
} |
|
|
|
|
|
int main(int argc, char* argv[]) |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int port = 7788; |
|
|
|
|
|
|
|
|
if (argc > 1) { |
|
|
try { |
|
|
port = std::stoi(argv[1]); |
|
|
if (port <= 0 || port > 65535) { |
|
|
std::cout << "Invalid port number. Using default port 7788." << std::endl; |
|
|
port = 7788; |
|
|
} |
|
|
} catch (const std::exception& e) { |
|
|
std::cout << "Invalid port number format. Using default port 7788." << std::endl; |
|
|
port = 7788; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::thread server_thread(StartHttpServer, port); |
|
|
|
|
|
|
|
|
server_thread.detach(); |
|
|
|
|
|
do { |
|
|
|
|
|
|
|
|
std::cin.get(); |
|
|
} while (true); |
|
|
|
|
|
std::string result = rec("t2.jpg"); |
|
|
json jsn; |
|
|
std::string jsn_str; |
|
|
|
|
|
try { |
|
|
jsn = json::parse(result); |
|
|
jsn_str = jsn.dump(4); |
|
|
} |
|
|
catch (const std::exception& e) { |
|
|
std::cerr << "OCR识别异常: " << e.what() << std::endl; |
|
|
return 1; |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|