#include "IndustryCapture.h" #include #include "Win32Util.h" OpenCLExcuter* IndustryCapture::openCLExcuter; bool IndustryCapture::inited; HWND IndustryCapture::win; CallbackFun_Industry IndustryCapture::data_callback; void* IndustryCapture::context; bool IndustryCapture::terminated; RecognitionManager* IndustryCapture::recognitionManager; cv::Mat IndustryCapture::grayImgs(cv::Mat oimg) { cv::Mat grayImg = cv::Mat::zeros(oimg.rows, oimg.cols, CV_8UC1);//ImgUtil::grayImage(oimg); uchar* imgData = (uchar*)malloc(sizeof(uchar) * oimg.rows * oimg.cols); if (oimg.channels() == 3) { openCLExcuter->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData); } else { openCLExcuter->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData); } grayImg.data = imgData; return grayImg; } void IndustryCapture::init(CallbackFun_Industry callback, void* contex) { terminated = false; data_callback = callback; context = contex; recognitionManager = new RecognitionManager(); openCLExcuter = new OpenCLExcuter(); openCLExcuter->init(); try { refreshHWND(); } catch (...) { } } void IndustryCapture::refreshHWND() { HWND hwnd = THSActionUtil::getIndustryWindow(); if (hwnd <= 0) throw string("ͬ»¨Ë³ÐÐÒµÒ³ÃæÎ´´ò¿ª"); HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL); if (content <= 0) throw string("δ»ñÈ¡µ½ÄÚÈÝ´°¿Ú¾ä±ú"); win = content; } list splitMenu(GPCodeArea menuArea, cv::Mat grayImg) { //Çзֲ˵¥ return THSActionUtil::splitPlateRowArea(grayImg, menuArea.starty + 10, menuArea.startx, menuArea.endy, menuArea.endx - 20); } list> IndustryCapture::captureIndustryCodes(cv::Mat oimg) { cv::Mat grayImg_ = grayImgs(oimg); GPCodeArea menu = THSActionUtil::splitPlateMenuArea(grayImg_); list menus = splitMenu(menu, grayImg_); free(grayImg_.data); grayImg_.release(); list> flist; int menu_index = 0; for (list::iterator ele = menus.begin();ele != menus.end();ele++) { menu_index++; int x = ((*ele)[0] + (*ele)[2]) / 2; int y = ((*ele)[1] + (*ele)[3]) / 2; if (menu_index > 1) { //µã»÷ SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(x, y)); Sleep(10); SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(x, y)); } list dataList; for (int i = 0;i < 10;i++) { if (terminated) { break; } Sleep(500); //½ØÍ¼+»Ò¶È»¯ list itemList; try { cv::Mat grayImg = grayImgs(CaptureUtil::capture(win)); list rowData = THSActionUtil::splitPlateRowArea(grayImg); //ÿÐеÄÔªËØ¸öÊý int ele_count_per_line = 2; int* splitResult = (int*)malloc(sizeof(int) * 4 * ele_count_per_line * rowData.size()); int* rowIndex = (int*)malloc(sizeof(int) * 4 * rowData.size()); int count = 0; for (std::list::iterator ele = rowData.begin();ele != rowData.end();ele++) { string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\"; path.append(to_string(count)).append(".jpg"); //cv::imwrite(path, cv::Mat(grayImg, cv::Rect((*ele)[0], (*ele)[1], (*ele)[2] - (*ele)[0] + 1, (*ele)[3] - (*ele)[1] + 1))); rowIndex[4 * count + 0] = (*ele)[0]; rowIndex[4 * count + 1] = (*ele)[1]; rowIndex[4 * count + 2] = (*ele)[2]; rowIndex[4 * count + 3] = (*ele)[3]; free(*ele); count++; } int length_per_num = 6; int line_number_count = ele_count_per_line * length_per_num; openCLExcuter->splitPlateContentRowData(grayImg.data, grayImg.cols, grayImg.rows, rowIndex, rowData.size(), ele_count_per_line, 1, 2, splitResult); //·Ö¸îÊý×Ö unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_GP_CODE_WIDTH * _NUMBER_GP_CODE_HEIGHT); for (int r = 0;r < _NUMBER_GP_CODE_HEIGHT;r++) { for (int c = 0;c < _NUMBER_GP_CODE_WIDTH;c++) { zeroData[r * _NUMBER_GP_CODE_WIDTH + c] = ImgUtil::NUMS_GP_CODE[0].data.ptr(r)[c]; } } unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_GP_CODE_HEIGHT * rowData.size()) * _NUMBER_GP_CODE_WIDTH * 10 * line_number_count); UcharDataInfo typesData = UcharDataInfo(); unsigned char types[] = { NUM_TYPE_MONEY,NUM_TYPE_CODE }; typesData.length = 2; typesData.data = types; openCLExcuter->splitPlateNum(grayImg, IntDataInfo({ splitResult,(int)(ele_count_per_line * rowData.size()) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, ele_count_per_line, length_per_num); //ʶ±ðÊý×Ö uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_GP_CODE_HEIGHT * rowData.size()) * _NUMBER_GP_CODE_WIDTH * 10 * line_number_count); openCLExcuter->createNumberTemplates(rowData.size(), _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, line_number_count, ImgUtil::numsOneLevel_gpcode, templateNums); uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, rowData.size() * _NUMBER_GP_CODE_HEIGHT, _NUMBER_GP_CODE_WIDTH * 10 * line_number_count, _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, line_number_count); for (int i = 0;i < rowData.size();i++) { IndustryData industryData = IndustryData(); string code = ""; for (int j = 0;j < length_per_num;j++) { code.append(to_string(numberResult[i][6 * 1 + j])); } string money = ""; for (int j = 0;j < length_per_num;j++) { if (j == 4) { money.append("."); } money.append(to_string(numberResult[i][j])); } industryData.index = i; industryData.code = code; industryData.zyltMoney = money; itemList.push_back(industryData); free(numberResult[i]); } free(numberResult); int* unitData = (int*)malloc(sizeof(int) * 4 * rowData.size()); //ʶ±ð½ð¶îµ¥Î» int index = 0; for (list::iterator ele = itemList.begin();ele != itemList.end();ele++) { unitData[index * 4 + 0] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 0]; unitData[index * 4 + 1] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 1]; unitData[index * 4 + 2] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 2]; unitData[index * 4 + 3] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 3]; string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\"; path.append(to_string(index)).append("_1.jpg"); //cv::imwrite(path, cv::Mat(grayImg,cv::Rect(unitData[index * 4 + 0], unitData[index * 4 + 1], unitData[index * 4 + 2]- unitData[index * 4 + 0]+1, unitData[index * 4 + 3]- unitData[index * 4 + 1]+1))); index++; } int* unitResult = (int*)malloc(sizeof(int) * itemList.size()); openCLExcuter->recognitionPlateMoneyUnit(grayImg.data, grayImg.cols, grayImg.rows, unitData, 1, itemList.size(), unitResult); index = 0; for (list::iterator ele = itemList.begin();ele != itemList.end();ele++) { switch (unitResult[index]) { case 0: (*ele).zyltMoneyUnit = MONEY_UNIT_Y; break; case 1: (*ele).zyltMoneyUnit = MONEY_UNIT_W; break; default: break; } index++; } free(rowIndex); free(splitResult); free(zeroData); free(totalNumberData); free(templateNums); free(unitData); free(unitResult); free(grayImg.data); grayImg.release(); for (list::iterator el = itemList.begin();el != itemList.end();el++) { dataList.push_back(*el); } } catch (...) { } int x = oimg.cols - 50; int y = 200; RECT rect; GetWindowRect(win,&rect); SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(x, y)); Sleep(10); SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(x, y)); Sleep(10); SendMessage(win, WM_MOUSEWHEEL, 0, MAKELONG(x, y)); } data_callback(dataList, context); flist.push_back(dataList); //break; if (terminated) { break; } } return flist; } list> IndustryCapture::captureIndustryCodes() { list> flist; set codesSet; //²âÊÔ for (int i = 0;i < 3;i++) { Sleep(1000); cv::Mat oimg = CaptureUtil::capture(win); list> codes = captureIndustryCodes(oimg); for (list>::iterator el = codes.begin();el != codes.end();el++) { flist.push_back(*el); } if (terminated) { break; } //»¬¶¯µ½ÏÂÒ»Ò³ RECT rect; GetWindowRect(win, &rect); SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(50, 100)); Sleep(10); SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(50, 100)); Sleep(10); SendMessage(win, WM_MOUSEWHEEL, 0, MAKEWORD(50, 100)); } terminated = false; return flist; } bool IndustryCapture::isInited() { return inited; } void IndustryCapture::stop() { terminated = true; }