|
|
#include "Wchtcr.h" |
|
|
#include "ImageOcr.h" |
|
|
#include <fstream> |
|
|
#include <chrono> |
|
|
#include <thread> |
|
|
#include <iostream> |
|
|
#include <filesystem> |
|
|
#include <regex> |
|
|
#include "nlohmann/json.hpp" |
|
|
|
|
|
using json = nlohmann::json; |
|
|
|
|
|
Wchtcr::Wchtcr() |
|
|
{ |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::string Wchtcr::rec(const std::string& pth_img) |
|
|
{ |
|
|
|
|
|
if (!std::filesystem::exists(pth_img)) |
|
|
{ |
|
|
return "文件不存在: " + pth_img; |
|
|
} |
|
|
|
|
|
try |
|
|
{ |
|
|
|
|
|
std::string result; |
|
|
|
|
|
|
|
|
std::atomic<bool> taskCompleted(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this->ocr.Run(pth_img, [&taskCompleted, &result](const std::string& imagePath, std::string& jsonstr) { |
|
|
|
|
|
result = jsonstr; |
|
|
|
|
|
|
|
|
taskCompleted = true; |
|
|
}); |
|
|
|
|
|
std::cout << "OCR任务已提交,等待结果..." << std::endl; |
|
|
|
|
|
|
|
|
int timeoutSeconds = 600; |
|
|
for (int i = 0; i < timeoutSeconds * 10 && !taskCompleted; i++) { |
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
|
|
} |
|
|
|
|
|
if (taskCompleted) { |
|
|
std::cout << "OCR任务已完成!" << std::endl; |
|
|
} |
|
|
else { |
|
|
std::cout << "OCR任务超时!" << std::endl; |
|
|
} |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
catch (const std::exception& ex) |
|
|
{ |
|
|
return "错误: " + std::string(ex.what()); |
|
|
} |
|
|
} |