2 Commits a1c027b988 ... 4d8502a7cc

Author SHA1 Message Date
  maoyehu 4d8502a7cc Merge branch 'translation' of http://gitz.zhixinhuixue.net:18880/zxhx-client-tool/pdfproject into translation 2 years ago
  maoyehu 43c16baba1 1 2 years ago

+ 1 - 0
MFCApplication1/Config.cpp

@@ -49,6 +49,7 @@ BOOL CConfig::LoadFromINI()
 	m_strCut = szData;
 	GetPrivateProfileString(_T("Set"), _T("Token"), _T("24.086aba1dfaa5d4fdba74f2c62b591b35.2592000.1668825495.282335-21782130"), szData, sizeof(szData), strIni);
 	m_strToken = szData;
+
 	return TRUE;
 }
 

+ 1 - 1
MFCApplication1/Config.h

@@ -41,7 +41,7 @@ public:
 	CString m_strImg;
 	CString m_strCut;
 
-	CString m_strToken;
+	CString m_strToken; // baidu token
 
 };
 

+ 45 - 1
MFCApplication1/CvxText.cpp

@@ -582,6 +582,25 @@ int dataCollectionPaper(int cols, int index, int fontSize, int lineGrayPix, bool
 	return 0;
 }
 
+// rect_label : 模板的切割区域  rect_ocr :检测到的手写区域
+double math_iou(cv::Rect rect_label, cv::Rect rect_ocr)
+{
+	int endx = max(rect_label.x + rect_label.width, rect_ocr.x + rect_ocr.width);
+	int startx = min(rect_label.x, rect_ocr.x);
+	int width = rect_label.width + rect_ocr.width - (endx - startx);
+
+	int endy = max(rect_label.y + rect_label.height, rect_ocr.y + rect_ocr.height);
+	int	starty = min(rect_label.y, rect_ocr.y);
+	int height = rect_label.height + rect_ocr.height - (endy - starty);
+
+	double area = 0.0, iou = 0.0;
+	if (width <= 0 || height <= 0)
+		return 0.0;
+	area = width * height*1.0;
+	iou = area / double(rect_ocr.area());
+	return iou;
+}
+
 int get_gray_num(cv::Mat & img, cv::Rect & box, int &meanGray, int threshold) {
 	uchar * ptr;
 	int count = 0;
@@ -629,7 +648,7 @@ bool analysis_ttbox_mark(cv::Mat & imgSrc, cv::Rect & rc) {
 	return false;
 }
 
-int cutPaper(int pageNum, std::string strJsonPath, std::string strPaperPath, std::string strSavePath)
+int cutPaper(int pageNum, std::string strJsonPath, std::string strPaperPath, std::string strSavePath, vector<std::tuple<cv::Rect, std::string>>& vecTranslate)
 {
 	ifstream in(strJsonPath, ios::binary);
 	if (!in.is_open())
@@ -718,6 +737,31 @@ int cutPaper(int pageNum, std::string strJsonPath, std::string strPaperPath, std
 
 		cv::Mat cut = src(rc_ansInfo);
 		cv::imwrite(szJpgPath, cut);
+
+		for (auto& iter : vecTranslate)
+		{
+			cv::Rect rcOcr = std::get<0>(iter);
+			std::string words = std::get<1>(iter);
+			if (math_iou(rc_ansInfo, rcOcr) > 0.5)
+			{
+				char szOcrJpgPath[MAX_PATH] = { 0 };
+				char szOcrJpgText[MAX_PATH] = { 0 };
+				sprintf(szOcrJpgPath, "%s\\%s\\%d_%s_%ld_%d_%d_%d_%d.jpg", strSavePath.c_str(), ret ? "abnormal_small" : "normal_small", pageNum, id.c_str(), dwCount,
+					rcOcr.x - rc_ansInfo.x, rcOcr.y - rc_ansInfo.y, rcOcr.width, rcOcr.height);
+				sprintf(szOcrJpgText, "%s\\%s\\%d_%s_%ld_%d_%d_%d_%d.txt", strSavePath.c_str(), ret ? "abnormal_small" : "normal_small", pageNum, id.c_str(), dwCount,
+					rcOcr.x - rc_ansInfo.x, rcOcr.y - rc_ansInfo.y, rcOcr.width, rcOcr.height);
+
+				CFile zip3;
+				zip3.Open(CA2T(szOcrJpgText), CFile::modeCreate | CFile::modeWrite);
+				zip3.Write((void*)words.c_str(), words.length());
+				zip3.Close();
+
+				cv::Mat cut = src(rcOcr);
+				cv::imwrite(szOcrJpgPath, cut);
+
+				break;
+			}
+		}
 	}
 
 	return 0;

+ 2 - 1
MFCApplication1/CvxText.h

@@ -57,10 +57,11 @@ int dataCollectionPaper(int cols, int index, int fontSize, int lineGrayPix, bool
 	*  @input    :  strJsonPath:	Ä£°åJson·¾¶
 	*  @input    :  strPaperPath:	´ðÌ⿨·¾¶
 	*  @input    :  strSavePath:	´æ´¢Â·¾¶
+	*  @input    :  vecTranslate:	°Ù¶Èʶ±ðºó·µ»ØµÄ
 	*  @return   :  > 0 ʧ°Ü
 	*  @author   :  qqm  2022/08/31 19:42
 *********************************************************/
-int cutPaper(int pageNum, std::string strJsonPath, std::string strPaperPath, std::string strSavePath);
+int cutPaper(int pageNum, std::string strJsonPath, std::string strPaperPath, std::string strSavePath, vector<std::tuple<cv::Rect, std::string>>& vecTranslate);
 
 
 int PareseModeJson(preinfo::templatesInfo&  temeplatInfo);

+ 48 - 6
MFCApplication1/MFCApplication1Dlg.cpp

@@ -264,13 +264,33 @@ int handwriting(string strImage, std::string &json_result, const std::string &ac
 	return is_success;
 }
 
-int IdentifyCallback(result::spinfo& pinfo, void* param)
+void ParseJson(std::string& strJson, vector<std::tuple<cv::Rect, std::string>>& vec)
 {
-	string strToken = UnicodeToGB2312(CConfig::Instance()->m_strToken);
-	string strResult;
-	string strEncode = GetImageEncodeString("d:\\202210122100_0001.jpg");
-	handwriting(strEncode, strResult, strToken);
+	Json::Features features;
+	Json::Reader reader(features);
+	Json::Value root;
+	if (!reader.parse(strJson, root))
+	{
+		return;
+	}
+
+	Json::Value words_result = root["words_result"];
+	if (!words_result.isArray())
+	{
+		return;
+	}
+
+	for (int i = 0; i < words_result.size(); i++)
+	{
+		Json::Value row = words_result[i];
+		Json::Value location = row["location"];
+		cv::Rect rc = cv::Rect(location["left"].asInt(), location["top"].asInt(), location["width"].asInt(), location["height"].asInt());
+		vec.push_back(make_tuple(rc, row["words"].asString()));
+	}
+}
 
+int IdentifyCallback(result::spinfo& pinfo, void* param)
+{
 	CMFCApplication1Dlg* pWnd = (CMFCApplication1Dlg*)theApp.m_pMainWnd;
 	if (pinfo.choiceRes.size() < 3 || pinfo.vecUrlAPath.size() == 0)
 	{
@@ -295,12 +315,20 @@ int IdentifyCallback(result::spinfo& pinfo, void* param)
 			number += pow(2, length - 1 - i);
 	}
 
+	string strToken = CT2A(CConfig::Instance()->m_strToken);
+	string strResult;
+	string strEncode = GetImageEncodeString(pinfo.vecUrlAPath[0]);
+	handwriting(strEncode, strResult, strToken);
+
+	vector<std::tuple<cv::Rect, std::string>> vecTranslate;
+	ParseJson(strResult, vecTranslate);
+
 	std::string strJsonDir = CT2A(CConfig::Instance()->m_strMode);
 	strJsonDir.append("/");
 	strJsonDir.append(std::to_string(number));
 	strJsonDir.append(".json");
 	std::string strSaveDir = CT2A(CConfig::Instance()->m_strCut);
-	cutPaper(number, strJsonDir, pinfo.vecUrlAPath[0], strSaveDir);
+	cutPaper(number, strJsonDir, pinfo.vecUrlAPath[0], strSaveDir, vecTranslate);
 	g_scanCount++;
 	pWnd->PostMessage(WM_PROCESS, NULL, NULL);
 	pWnd->FormatScanMsg("生成切割图像:%s", std::to_string(number).c_str());
@@ -813,6 +841,20 @@ void CMFCApplication1Dlg::OnBnClickedBtnScan()
 		::CreateDirectory(strAbnormal, 0);
 	}
 
+	CString strNormalSmall(m_strCutPath);
+	strNormalSmall.Append(L"/normal_small");
+	if (!PathIsDirectory(strNormalSmall))
+	{
+		::CreateDirectory(strNormalSmall, 0);
+	}
+
+	CString strAbnormalSmall(m_strCutPath);
+	strAbnormalSmall.Append(L"/abnormal_small");
+	if (!PathIsDirectory(strAbnormalSmall))
+	{
+		::CreateDirectory(strAbnormalSmall, 0);
+	}
+
 	SAFETY_EXIT_THREAD(_threadScan, 100);
 	_threadScan = CreateThread(NULL, 0, ScanThread, NULL, 0, NULL);
 }