#include "stdafx.h" #include "ImgUploadManager.h" #include "ImgUploader.h" #include "..\EvaluationUtil\HttpClient.h" #include "..\EvaluationUtil\IInputFiled.h" #include "..\EvaluationUtil\FileInputFiled.h" #include "..\EvaluationUtil\HTTP.h" #include "..\Identifier\CrossDetector.h" CImgUploadManager::CImgUploadManager():imgUploader_(new CImgUploader()) { m_threadPool.init(2, 100); } CImgUploadManager::~CImgUploadManager() { } boost::shared_ptr CImgUploadManager::GetInstance() { static boost::shared_ptr _instance(new CImgUploadManager); return _instance; } int CImgUploadManager::SetHandlerWndForImg(HWND hwnd) { return imgUploader_->SetHandlerWnd(hwnd); } int CImgUploadManager::StartUploadePaperImg() { return imgUploader_->Start(); } int CImgUploadManager::StopUploadePaperImg() { return imgUploader_->Stop(); } int CImgUploadManager::QueryUploadePaperImg(std::vector& tasks) { return imgUploader_->QueryUploadePaperImg(tasks); } int CImgUploadManager::GetCurrentUploadTaskInfo(IMG_UPLOAD_TASK& task) { return imgUploader_->GetCurrentUploadTaskInfo(task); } int CImgUploadManager::ClearUploadedPaperImg() { return imgUploader_->ClearUploadedPaperImg(); } const boost::shared_ptr& CImgUploadManager::GetImgUploader() { return imgUploader_; } //获取交叉点的方向 void GetDirection(AI_RET_RECT2& aiCross, string strDirection) { map mapDirect = { { "CROSS_TL", SIGND_TL }, { "CROSS_TR", SIGND_TR }, { "CROSS_TLR", SIGND_TLR }, { "CROSS_BL", SIGND_BL }, { "CROSS_BR", SIGND_BR }, { "CROSS_BLR", SIGND_BLR }, { "CROSS_TBL", SIGND_TBL }, { "CROSS_TBR", SIGND_TBR }, { "CROSS_TBLR", SIGND_TBLR }}; map::iterator iter = mapDirect.find(strDirection); if (iter != mapDirect.end()) { aiCross.directoin = iter->second; } } int CImgUploadManager::AiReadImgPaper(int subject, int id, std::vector aiVec, const std::vector imgPath, std::vector> &retList) { //CHttpClient httpClient; string response, strUrlStudengCode; vector inputs; retList.resize(imgPath.size()); for (int i = 0; i < imgPath.size(); i++) { //CFileInputFiled filed(_T("img_data"), _T("600.jpg"), imgPath[i]); CFileInputFiled *filed = new CFileInputFiled(_T("img_data"), imgPath[i], imgPath[i]); inputs.push_back(filed); } //CFileInputFiled filed(_T("img_data"), _T("600.jpg"), imgPath); //subject = 18; CString strSubject; strSubject.Format(_T("%d"), id); CInputFiled filed2(_T("subject"), strSubject, INPUT_FILED_VALUE); //inputs.push_back(&filed); inputs.push_back(&filed2); CString strQuestions = _T("{\"questions\":["); for (int i = 0; i < aiVec.size(); i++) { CString str; CString str1 = CA2T(aiVec[i].stu_no.c_str()); CString str2 = CA2T(aiVec[i].stu_type.c_str()); str.Format(_T("{\"questionID\":%s,\"type\":%s},"), str1, str2); strQuestions += str; } if (aiVec.size() > 0) strQuestions.Delete(strQuestions.GetLength() - 1); strQuestions += _T("]}"); CInputFiled filed3(_T("questions"), strQuestions, INPUT_FILED_VALUE); if (subject == 0) { inputs.push_back(&filed3); } CString url_str; //url_str = "http://192.168.1.167:18088/upload_imgs"; //url_str = "http://192.168.1.167:8000/segment/upload_imgs/"; WCHAR url[512]; TCHAR FilePath[MAX_PATH]; GetModuleFileName(NULL, FilePath, MAX_PATH); (_tcsrchr(FilePath, '\\'))[1] = 0; lstrcat(FilePath, _T("config.ini")); if (subject != 0) GetPrivateProfileString(_T("GLOBAL"), _T("server_url_AI"), _T(""), url, 512, FilePath); else GetPrivateProfileString(_T("GLOBAL"), _T("server_url_examPaperCut"), _T(""), url, 512, FilePath); //url_str = url; DWORD dwNum = WideCharToMultiByte(CP_OEMCP, NULL, url, -1, NULL, 0, NULL, FALSE); char *psText; psText = new char[dwNum]; WideCharToMultiByte(CP_OEMCP, NULL, url, -1, psText, dwNum, NULL, FALSE); std::string szDst; szDst = psText;// std::string?3?|ì delete[]psText;// psText|ì???3y CHTTP httpClient; int a = 150000, b = 150000, c = 150000; httpClient.Init(); httpClient.SetTimeout(a, b, c); httpClient.SetCodePage(CP_UTF8); httpClient.PostFile(response, szDst, inputs); logForDbg(response); //httpClient.HttpPostFileJpg(url_str, inputs, response); Json::Features features; Json::Reader re(features); Json::Value root; re.parse(response, root); for (int i = 0; i < inputs.size(); i++) { if (inputs[i]->GetType() == INPUT_FILED_FILE) { inputs[i]->Close(); } } if (root["is_success"].type() != Json::nullValue) { if (root["is_success"].asInt() == 1) { Json::Value object_imgs_info = root["imgs_info"]; if (subject != 0) { if (!object_imgs_info.isNull() && object_imgs_info.isArray()) { std::vector rcVec; int order = 0; for (int i = 0; i < object_imgs_info.size(); i++) { rcVec.clear(); Json::Value object_imgs = object_imgs_info[i]; string strImgName = object_imgs["img_name"].asString(); for (int k = 0; k < imgPath.size(); k++) { int nPos = imgPath[k].ReverseFind('/'); CString fileName = imgPath[k].Right(imgPath[k].GetLength() - nPos - 1); if (strImgName.compare(CT2A(fileName.GetString())) == 0) { order = k; break; } } Json::Value object_regions = object_imgs["regions"]; if (!object_regions.isNull() && object_regions.isArray()) { for (int j = 0; j < object_regions.size(); j++) { Json::Value obj_area = object_regions[j]; string strType = obj_area["class_name"].asString(); if (strType == "exam_number_col_row") //ì?í???o? { AI_RET_RECT2 temp; temp.rect_type = AI_TIANTUKAOHAO; Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; temp.directoin = obj_area["direction"].asInt(); temp.nRows = obj_area["rows"].asInt(); temp.nCols = obj_area["cols"].asInt(); temp.widthH = obj_area["single_width"].asInt(); temp.widthV = obj_area["single_height"].asInt(); temp.outputChar = obj_area["option"].asString(); rcVec.push_back(temp); } else if (strType == "bar_code" || strType == "qr_code") //ì?D??? ?t???? { AI_RET_RECT2 temp; Json::Value object_location = obj_area["bounding_box"]; temp.rect_type = AI_TIAOXINGMA; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; rcVec.push_back(temp); } else if (strType == "choice_m") //????ìa { AI_RET_RECT2 temp; temp.rect_type = AI_XUANZETI; //temp.tihao = row["number"].asInt(); Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; temp.widthH = obj_area["single_width"].asInt(); temp.widthV = obj_area["single_height"].asInt(); //temp.default_points = row["default_points"].asInt(); temp.outputChar = obj_area["option"].asString(); temp.nRows = obj_area["rows"].asInt(); temp.nCols = obj_area["cols"].asInt(); Json::Value object_tihao = obj_area["number"]; bool flag = false; if (!object_tihao.isNull() && object_tihao.isArray()) { for (int i = 0; i < object_tihao.size(); i++) { if (object_tihao[i].asInt() < 0) { flag = true; break; } bool flag2 = false; for (int j = i + 1; j < object_tihao.size(); j++) { if (object_tihao[i].asInt() == object_tihao[j].asInt()) { flag2 = true; break; } } if (flag2) { flag = true; break; } temp.tihao.push_back(object_tihao[i].asInt()); } } if (flag) temp.tihao.clear(); Json::Value object_points = obj_area["default_points"]; flag = false; if (!object_points.isNull() && object_points.isArray()) { for (int i = 0; i < object_points.size(); i++) { if (object_points[i].asDouble() < 0) { flag = true; break; } temp.default_points.push_back(object_points[i].asDouble()); } } if (flag) temp.default_points.clear(); temp.directoin = obj_area["direction"].asInt(); temp.duoxuanti = obj_area["multiple"].asBool(); if (temp.duoxuanti) { Json::Value object_half_points = obj_area["half_default_points"]; flag = false; for (int i = 0; i < object_half_points.size(); i++) { if (object_half_points[i].asDouble() < 0) { flag = true; break; } temp.half_points.push_back(object_half_points[i].asDouble()); } if (flag) temp.half_points.clear(); } rcVec.push_back(temp); } else if (strType == "cloze") //ì???ìa { AI_RET_RECT2 temp; temp.rect_type = AI_TIANKONGTI; if (!obj_area["number"].empty()) { if (obj_area["number"].asInt() > 0) temp.tihao.push_back(obj_area["number"].asInt()); } Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; if (!obj_area["default_points"].empty()) { if (obj_area["default_points"].asDouble() > 0) temp.default_points.push_back(obj_area["default_points"].asDouble()); } rcVec.push_back(temp); } else if (strType == "solve" || "composition" == strType) //?a′eìa { AI_RET_RECT2 temp; temp.rect_type = AI_ZHUGUANTI; if (!obj_area["number"].empty()) { if (obj_area["number"].asInt() > 0) temp.tihao.push_back(obj_area["number"].asInt()); } Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; if (!obj_area["default_points"].empty()) { if (obj_area["default_points"].asDouble() > 0) temp.default_points.push_back(obj_area["default_points"].asDouble()); } temp.showMarkTypeIndex = -1; temp.isSplit = obj_area["span"].asBool(); temp.unitID = obj_area["span_id"].asInt(); if ("composition" == strType && subject == 9)//ó??? { temp.showMarkTypeIndex = 1; temp.postMarkTypeID = 4; } else if ("composition" == strType && subject == 8)//ó¢ó? { temp.showMarkTypeIndex = 1; temp.postMarkTypeID = 4; } rcVec.push_back(temp); } else if (strType == "optional_solve") //??×?ìa { AI_RET_RECT2 temp; temp.rect_type = AI_XUANZUOTI; //temp.tihao.push_back(obj_area["number"].asInt()); Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; //temp.default_points.push_back(obj_area["default_points"].asInt()); Json::Value object_tihao = obj_area["number"]; if (!object_tihao.isNull() && object_tihao.isArray()) { for (int i = 0; i < object_tihao.size(); i++) temp.tihao.push_back(object_tihao[i].asInt()); } Json::Value object_points = obj_area["default_points"]; if (!object_points.isNull() && object_points.isArray()) { for (int i = 0; i < object_points.size(); i++) temp.default_points.push_back(object_points[i].asDouble()); } temp.isSplit = obj_area["span"].asBool(); temp.unitID = obj_area["span_id"].asInt(); rcVec.push_back(temp); } else if (strType == "optional_choice") //??×?ìaí?μ? { AI_RET_RECT2 temp; temp.rect_type = AI_XUANZUOTUDIAN; //temp.tihao.push_back(obj_area["number"].asInt()); Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; temp.widthH = obj_area["single_width"].asInt(); temp.widthV = obj_area["single_height"].asInt(); //temp.default_points = row["default_points"].asInt(); //temp.outputChar = obj_area["option"].asString(); temp.nRows = obj_area["rows"].asInt(); temp.nCols = obj_area["cols"].asInt(); temp.directoin = obj_area["direction"].asInt(); Json::Value object_tihao = obj_area["number"]; if (!object_tihao.isNull() && object_tihao.isArray()) { for (int i = 0; i < object_tihao.size(); i++) temp.tihao.push_back(object_tihao[i].asInt()); } rcVec.push_back(temp); } else if (strType == "lack") //缺考标记 { AI_RET_RECT2 temp; temp.rect_type = AI_QUEKAO; //temp.tihao.push_back(obj_area["number"].asInt()); Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; temp.widthH = temp.width; temp.widthV = temp.height; temp.nCols = temp.nRows = 1; rcVec.push_back(temp); } else if (strType == "anchor_point") //定位点json解析 { AI_RET_RECT2 temp; temp.rect_type = AI_DINGWEIDIAN; Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; rcVec.push_back(temp); } else if (strType == "position_area") //定位区json解析 { AI_RET_RECT2 temp; temp.rect_type = AI_DINGWEIQU; Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; rcVec.push_back(temp); } else if (strType == "cross_points") //交叉点json解析 { AI_RET_RECT2 temp; temp.rect_type = AI_JIAOCHADIAN; Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); Json::Value diretion = obj_area["direction"]; if (!diretion.isNull() && diretion.isString()) { string strDirectoin = obj_area["direction"].asString();//方向 GetDirection(temp, strDirectoin); } rcVec.push_back(temp); } } } //retList.push_back(rcVec); retList[order] = rcVec; //retList.insert(retList.begin() + order, rcVec); } } } else { if (!object_imgs_info.isNull() && object_imgs_info.isArray()) { std::vector rcVec; int order = 0; for (int i = 0; i < object_imgs_info.size(); i++) { rcVec.clear(); Json::Value object_imgs = object_imgs_info[i]; string strImgName = object_imgs["img_name"].asString(); for (int k = 0; k < imgPath.size(); k++) { int nPos = imgPath[k].ReverseFind('/'); CString fileName = imgPath[k].Right(imgPath[k].GetLength() - nPos - 1); if (strImgName.compare(CT2A(fileName.GetString())) == 0) { order = k; break; } } Json::Value object_regions = object_imgs["regions"]; if (!object_regions.isNull() && object_regions.isArray()) { for (int j = 0; j < object_regions.size(); j++) { Json::Value obj_area = object_regions[j]; AI_RET_RECT2 temp; temp.rect_type = AI_ZHUGUANTI2; if (obj_area["type"].asInt() == 1) //选择题有选项 { temp.postMarkTypeID = 1; } else { temp.postMarkTypeID = 0; } //temp.tihao.push_back(obj_area["number"].asInt()); temp.tihao_aiExamper = obj_area["number"].asString(); if (obj_area["issplit"].asInt() == 1) temp.isSplit = true; else temp.isSplit = false; temp.unitID = obj_area["splitId"].asInt(); Json::Value object_location = obj_area["bounding_box"]; temp.x = object_location["xmin"].asInt(); temp.y = object_location["ymin"].asInt(); temp.width = object_location["xmax"].asInt() - temp.x; temp.height = object_location["ymax"].asInt() - temp.y; rcVec.push_back(temp); } } retList[order] = rcVec; } } } } else { return -1; } } return 0; } ThreadPool *CImgUploadManager::getThreadPool() { return &m_threadPool; } #include void ThreadLog14(char* ThreadName, char* fileName) { TCHAR szCommandLine[MAX_PATH]; memset(szCommandLine, 0, sizeof(szCommandLine)); GetModuleFileName(NULL, szCommandLine, MAX_PATH); (_tcsrchr(szCommandLine, '\\'))[1] = 0; TCHAR pathFile[MAX_PATH]; lstrcpy(pathFile, szCommandLine); int nInputStrLen = wcslen(szCommandLine); int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, szCommandLine, nInputStrLen, NULL, 0, 0, 0) + 2; char* pszOut = new char[nOutputStrLen]; memset(pszOut, 0x00, nOutputStrLen); WideCharToMultiByte(CP_ACP, 0, szCommandLine, nInputStrLen, pszOut, nOutputStrLen, 0, 0); char buf[200] = {0}; //WideCharToMultiByte(CP_UTF8, 0, szCommandLine, -1, buf, 200, NULL, NULL); //_getcwd(buf, sizeof(buf)); //strcat_s(buf, "\\"); //strcat_s(pszOut, fileName); sprintf(buf, "%s%s", pszOut, fileName); FILE *file = NULL; printf(buf); fopen_s(&file, buf, "at+"); if (file == NULL) { printf("路径错误\n"); //system("pause"); return; } fseek(file, 0, SEEK_END); //char bufThread[10240]; //strcpy_s(bufThread, sizeof(bufThread), ThreadName); //fwrite(&bufThread, strlen(bufThread), 1, file); fwrite(ThreadName, strlen(ThreadName), 1, file); fclose(file); delete[] ThreadName; } void CImgUploadManager::logForDbg(string strLog) { //char logContent[300]; char *logContent; char logName[300]; char sYear[10], sMonth[5], sDay[25]; SYSTEMTIME st; GetLocalTime(&st); int j0 = 0; int m_nYear = st.wYear; ///年 int m_nMonth = st.wMonth; ///月 int m_nDay = st.wDay; ///日 int m_nHour = st.wHour; ///小时 int m_nMinute = st.wMinute; ///分钟 int m_nSecond = st.wSecond; ///秒 int m_nMillSecond = st.wMilliseconds; ///毫秒 char strTime[300]; sprintf(strTime, "----------------------------------%d_%02d_%02d_%d_%02d_%02d", m_nYear, m_nMonth, m_nDay, m_nHour, m_nMinute, m_nSecond); strLog += strTime; sprintf_s(sYear, sizeof(sYear), "%d", m_nYear); sprintf_s(sMonth, sizeof(sMonth), "_%02d", m_nMonth); sprintf_s(sDay, sizeof(sDay), "_%02d_log.txt", m_nDay); strcpy(logName, sYear); strcat(logName, sMonth); strcat(logName, sDay); //char strScore[MAX_PATH] = { 0 }; char *strScore = new char[strLog.size()+1]; memset(strScore, 0, strLog.size() + 1); //logContent = new char[strLog.size()+1]; int i = 0; for (; i < strLog.size(); i++) strScore[i] = strLog.at(i); strScore[strLog.size()] = '\n'; //sprintf_s(logContent, sizeof(char), "%s\n", strScore); //delete[] strScore; ThreadLog14(strScore, logName); }