| | |
| | | #include "pch.h" |
| | | #include "OcrUtil.h" |
| | | #include "SocketManager.h" |
| | | #include <curl/curl.h> |
| | | #include "MyNetWorkManager.h" |
| | | |
| | | list<OCRResult> OcrUtil::mat_ocr(string key_regex, cv::Mat mat) |
| | | { |
| | |
| | | time_t totalMicroSeconds = tpMicro.time_since_epoch().count(); |
| | | string matId = string(to_string(totalMicroSeconds)).append("_").append(to_string(mat.rows)).append("x").append(to_string(mat.cols)).append("_").append(to_string(rand())); |
| | | |
| | | int BUFFER_SIZE = 1024 * 2; |
| | | int BUFFER_SIZE = 1024 * 1024; |
| | | int maxSize = mat.rows * mat.cols; |
| | | int maxIndex = maxSize / BUFFER_SIZE; |
| | | |
| | |
| | | for (int i = 0; i < 10; i++) |
| | | { |
| | | try { |
| | | string result = SocketManager::sendOcrMsg(result_str.c_str()); |
| | | map<string, string> params; |
| | | params["data"] = result_str; |
| | | string result = MyNetWorkManager::getInstance()->PostHttpRequest(string("http://").append(SocketManager::ADDR).append(":").append(to_string(SocketManager::OCR_PORT)).append("/ocr") ,"",params); |
| | | return parseOCRResult(result); |
| | | } |
| | | catch (int code) { |
| | |
| | | break; |
| | | } |
| | | } |
| | | catch (...) { |
| | | catch (string st) { |
| | | throw st; |
| | | } |
| | | } |
| | | } |
| | | throw string("识别失败"); |
| | | } |
| | | |
| | | string OcrUtil::code_ocr_by_name(cv::Mat& mat) |
| | | { |
| | | int len = mat.rows * mat.cols; |
| | | chrono::time_point<chrono::system_clock, chrono::microseconds> tpMicro |
| | | = chrono::time_point_cast<chrono::microseconds>(chrono::system_clock::now()); |
| | | // (微秒精度的)时间点 => (微秒精度的)时间戳 |
| | | time_t totalMicroSeconds = tpMicro.time_since_epoch().count(); |
| | | string matId = string(to_string(totalMicroSeconds)).append("_").append(to_string(mat.rows)).append("x").append(to_string(mat.cols)).append("_").append(to_string(rand())); |
| | | |
| | | int BUFFER_SIZE = 1024 * 2; |
| | | int maxSize = mat.rows * mat.cols; |
| | | int maxIndex = maxSize / BUFFER_SIZE; |
| | | |
| | | //生成matID |
| | | //分块发送,每一行为1块 |
| | | for (int index = 0; index <= maxIndex; index++) { |
| | | Json::Value array; |
| | | for (int c = 0; c < BUFFER_SIZE; c++) { |
| | | int dataIndex = index * BUFFER_SIZE + c; |
| | | if (dataIndex >= maxSize) { |
| | | break; |
| | | } |
| | | array[c] = mat.data[index * BUFFER_SIZE + c]; |
| | | } |
| | | Json::Value data; |
| | | data["data"] = array; |
| | | data["matId"] = matId; |
| | | data["index"] = index; |
| | | data["maxIndex"] = maxIndex; |
| | | data["width"] = mat.cols; |
| | | data["height"] = mat.rows; |
| | | Json::Value root; |
| | | root["data"] = data; |
| | | root["type"] = 101; |
| | | string result_str = JsonUtil::toJsonStr(root); |
| | | for (int i = 0; i < 10; i++) |
| | | { |
| | | try { |
| | | map<string, string> params; |
| | | params["data"] = result_str; |
| | | string result = MyNetWorkManager::getInstance()->PostHttpRequest(string("http://").append(SocketManager::ADDR).append(":").append(to_string(SocketManager::OCR_PORT)).append("/ocr"), "", params); |
| | | Json::Value resultJSON = JsonUtil::parseJson(result); |
| | | if (resultJSON["code"].asInt() == 0) { |
| | | string code = resultJSON["data"]["code"].asString(); |
| | | return code; |
| | | } |
| | | else { |
| | | throw data["code"].asInt(); |
| | | } |
| | | throw string("识别不匹配"); |
| | | } |
| | | catch (int code) { |
| | | if (code == -1) { |
| | | //数据不完整 |
| | | Sleep(100); |
| | | continue; |
| | | } |
| | | else if (code == 1) { |
| | | //等待继续上传 |
| | | break; |
| | | } |
| | | else { |
| | | break; |
| | | } |
| | | } |
| | | catch (string st) { |
| | | throw st; |
| | | } |
| | | } |
| | | } |
| | | throw string("识别失败"); |
| | | } |
| | | |