#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)
|
{
|
|
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 * 1024;
|
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;
|
data["key"] = key_regex.c_str();
|
Json::Value root;
|
root["data"] = data;
|
root["type"] = 100;
|
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);
|
return parseOCRResult(result);
|
}
|
catch (int code) {
|
if (code == -1) {
|
//Êý¾Ý²»ÍêÕû
|
Sleep(100);
|
continue;
|
}
|
else if (code == 1) {
|
//µÈ´ý¼ÌÐøÉÏ´«
|
break;
|
}
|
else {
|
break;
|
}
|
}
|
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("ʶ±ðʧ°Ü");
|
}
|