admin
2022-07-22 9adb473067a993b4e0eacf3675baf14b29da3eca
ConsoleApplication/IndustryCapture.cpp
@@ -1,5 +1,4 @@
#include "IndustryCapture.h"
#include "THSActionUtil.h"
#include <set>
#include "Win32Util.h"
OpenCLExcuter* IndustryCapture::openCLExcuter;
@@ -7,7 +6,10 @@
HWND IndustryCapture::win;
 CallbackFun_Industry IndustryCapture::data_callback;
 void* IndustryCapture::context;
 bool  IndustryCapture::terminated;
RecognitionManager* IndustryCapture::recognitionManager;
@@ -32,11 +34,19 @@
void IndustryCapture::init() {
void IndustryCapture::init(CallbackFun_Industry callback, void* contex) {
   terminated = false;
   data_callback = callback;
   context = contex;
   recognitionManager = new RecognitionManager();
   openCLExcuter = new OpenCLExcuter();
   openCLExcuter->init();
   refreshHWND();
   try {
      refreshHWND();
   }
   catch (...) {
   }
}
void IndustryCapture::refreshHWND() {
   HWND hwnd = THSActionUtil::getIndustryWindow();
@@ -60,160 +70,171 @@
list<list<IndustryData>> IndustryCapture::captureIndustryCodes(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;
   //cv::imwrite("C:\\Users\\Administrator\\Desktop\\ocr\\industry.jpg", grayImg);
   cv::Mat grayImg_ = grayImgs(oimg);
   GPCodeArea menu = THSActionUtil::splitPlateMenuArea(grayImg_);
   list<int*> menus = splitMenu(menu, grayImg_);
   free(grayImg_.data);
   grayImg_.release();
   list<list<IndustryData>> flist;
   int menu_index = 0;
   
   for (list<int*>::iterator ele = menus.begin();ele != menus.end();ele++) {
      menu_index++;
      int x = ((*ele)[0] + (*ele)[2]) / 2;
      int y = ((*ele)[1] + (*ele)[3]) / 2;
      //点击
      SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(x, y));
      Sleep(10);
      SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(x, y));
      if (menu_index > 1)
      {
         //点击
         SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(x, y));
         Sleep(10);
         SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(x, y));
      }
      list<IndustryData> dataList;
      for (int i = 0;i < 3;i++) {
      for (int i = 0;i < 10;i++) {
         if (terminated) {
            break;
         }
         Sleep(500);
         //截图+灰度化
         
         list<IndustryData> itemList;
          cv::Mat   grayImg = grayImgs(CaptureUtil::capture(win));
         list<int*> rowData = THSActionUtil::splitPlateRowArea(grayImg);
         try {
         //每行的元素个数
         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<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
            cv::Mat   grayImg = grayImgs(CaptureUtil::capture(win));
            list<int*> rowData = THSActionUtil::splitPlateRowArea(grayImg);
            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 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<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
         int line_number_count = ele_count_per_line * 6;
         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<uchar>(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_CODE,NUM_TYPE_MONEY};
         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, 6);
         //识别数字
         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 < 6;j++)
            {
               code.append(to_string(numberResult[i][6*1+j]));
               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++;
            }
            string money = "";
            for (int j = 0;j < 6;j++)
            {
               if (j == 4) {
                  money.append(".");
            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<uchar>(r)[c];
               }
               money.append(to_string(numberResult[i][ j]));
            }
            unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_GP_CODE_HEIGHT * rowData.size()) * _NUMBER_GP_CODE_WIDTH * 10 * line_number_count);
            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());
            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);
         //识别金额单位
         int index = 0;
         for (list<IndustryData>::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<IndustryData>::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;
            //识别数字
            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]);
            }
            index++;
            free(numberResult);
            int* unitData = (int*)malloc(sizeof(int) * 4 * rowData.size());
            //识别金额单位
            int index = 0;
            for (list<IndustryData>::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<IndustryData>::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<IndustryData>::iterator el = itemList.begin();el != itemList.end();el++) {
               dataList.push_back(*el);
            }
         }
         free(rowIndex);
         free(splitResult);
         free(zeroData);
         free(templateNums);
         free(unitData);
         free(unitResult);
         free(grayImg.data);
         grayImg.release();
         for (list<IndustryData>::iterator el = itemList.begin();el != itemList.end();el++) {
            dataList.push_back(*el);
         catch (...) {
         }
         int x = oimg.cols - 50;
@@ -228,8 +249,12 @@
         Sleep(10);
         SendMessage(win, WM_MOUSEWHEEL, 0, MAKELONG(x, y));
      }
      data_callback(dataList, context);
      flist.push_back(dataList);
      //break;
      if (terminated) {
         break;
      }
   }
   return flist;
@@ -240,6 +265,7 @@
list<list<IndustryData>> IndustryCapture::captureIndustryCodes() {
   list<list<IndustryData>> flist;
   set<string> codesSet;
   //测试
   for (int i = 0;i < 3;i++) {
      Sleep(1000);
      cv::Mat oimg = CaptureUtil::capture(win);
@@ -247,17 +273,26 @@
      for (list<list<IndustryData>>::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(20, 100));
      SendMessage(win, WM_LBUTTONDOWN, 0, MAKELONG(50, 100));
      Sleep(10);
      SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(20, 100));
      SendMessage(win, WM_LBUTTONUP, 0, MAKELONG(50, 100));
      Sleep(10);
      SendMessage(win, WM_MOUSEWHEEL, 0, MAKEWORD(20, 100));
      SendMessage(win, WM_MOUSEWHEEL, 0, MAKEWORD(50, 100));
   }
   terminated = false;
   return flist;
}
bool IndustryCapture::isInited() {
   return inited;
}
}
void IndustryCapture::stop() {
   terminated = true;
}