admin
2022-08-18 67a5e3d825fde17b7b00906ce42b0bd8cafebc4e
ConsoleApplication/TradeListCapture.cpp
@@ -25,11 +25,16 @@
      //1s一次
      if (running_s && inited) {
         clock_t start = clock();
         list<TradeSuccessData> datas = captureTradeSuccessInfo();
         cout << "耗时:" << clock() - start << "  数量:" << datas.size() << endl;
         data_callback_success(datas, context);
         datas.clear();
         Sleep(500);
         try {
            list<TradeSuccessData> datas = captureTradeSuccessInfo();
            cout << "耗时:" << clock() - start << "  数量:" << datas.size() << endl;
            data_callback_success(datas, context);
            datas.clear();
         }
         catch (...) {
         }
         Sleep(10);
      }
      else {
         Sleep(2000);
@@ -43,12 +48,17 @@
      //1s一次
      if (running_d && inited) {
         clock_t start = clock();
         list<TradeDelegateData> datas = captureTradeDelegateInfo();
         string money = getAvaiableMoney();
         cout << "耗时:" << clock() - start << "  数量:" << datas.size() << endl;
         data_callback_delegate(datas, money, context);
         datas.clear();
         Sleep(100);
         try {
            list<TradeDelegateData> datas = captureTradeDelegateInfo();
            string money = getAvaiableMoney();
            cout << "耗时:" << clock() - start << "  数量:" << datas.size() << endl;
            data_callback_delegate(datas, money, context);
            datas.clear();
         }
         catch (...) {
         }
         Sleep(5);
      }
      else {
         Sleep(2000);
@@ -209,6 +219,11 @@
   }
   if (rowData.size() < 2)
   {
      free(grayImg.data);
      grayImg.release();
      for (list<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
         free(*ele);
      }
      return dataList;
   }
   //ȥͷȥβ
@@ -331,6 +346,8 @@
      priceCode = new char[20];
      sprintf_s(priceCode, price.length(), "%.2lf", stod(price));
      std::string _str(priceCode);
      delete[] priceCode;
      price = _str;
      //金额
@@ -347,6 +364,7 @@
      chCode = new char[20];
      sprintf_s(chCode, money.length(), "%.2lf", stod(money));
      std::string str(chCode);
      delete[] chCode;
      money = str;
      //合同编号
@@ -396,7 +414,7 @@
      free(numberResult[i]);
   }
   free(totalNumberData);
   free(numberResult);
   free(rowIndex);
   free(splitResult);
@@ -423,249 +441,255 @@
list<TradeDelegateData> TradeListCapture::captureTradeDelegateInfo(cv::Mat oimg) {
   cv::Mat grayImg = grayImgs(oimg);
   list<TradeDelegateData> dataList;
   //分隔元素
   int empty_start = -1;
   int empty_end = -1;
   int data_start = -1;
   int data_end = -1;
   list<int*> rowData;
   for (int r = 0;r < grayImg.rows;r++) {
      if (ImgDivider::isRowEmpty(grayImg, r, 0, 50)) {
         if (empty_start < 0) {
            empty_start = r;
            empty_end = r;
      list<TradeDelegateData> dataList;
      //分隔元素
      int empty_start = -1;
      int empty_end = -1;
      int data_start = -1;
      int data_end = -1;
      list<int*> rowData;
      for (int r = 0;r < grayImg.rows;r++) {
         if (ImgDivider::isRowEmpty(grayImg, r, 0, 50)) {
            if (empty_start < 0) {
               empty_start = r;
               empty_end = r;
            }
            else {
               empty_end = r;
            }
            if (data_start > -1 && data_end > -1 && data_end - data_start > 5) {
               printf("%d-%d\n", data_start, data_end);
               int* d = (int*)malloc(sizeof(int) * 2);
               d[0] = data_start;
               d[1] = data_end;
               rowData.push_back(d);
               data_start = -1;
               data_end = -1;
            }
         }
         else {
            empty_end = r;
         }
         if (data_start > -1 && data_end > -1 && data_end - data_start > 5) {
            printf("%d-%d\n", data_start, data_end);
            int* d = (int*)malloc(sizeof(int) * 2);
            d[0] = data_start;
            d[1] = data_end;
            rowData.push_back(d);
            data_start = -1;
            data_end = -1;
         }
      }
      else {
         empty_start = -1;
         empty_end = -1;
            empty_start = -1;
            empty_end = -1;
         if (data_start < 0) {
            data_start = r;
            data_end = r;
         }
         else {
            data_end = r;
         }
      }
   }
   if (rowData.size() < 2)
   {
      return dataList;
   }
   //ȥͷȥβ
   list<int*>::iterator start = rowData.begin();
   list<int*>::iterator end = rowData.end();
   free(*start);
   rowData.erase(start);
   std::advance(end, -1);
   free(*end);
   rowData.erase(end);
   int* rowIndex = (int*)malloc(sizeof(int) * rowData.size() * 4);
   int index = 0;
   for (list<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
      rowIndex[index * 4 + 0] = 0;
      rowIndex[index * 4 + 1] = (*ele)[0];
      rowIndex[index * 4 + 2] = grayImg.cols - 1;
      rowIndex[index * 4 + 3] = (*ele)[1];
      free(*ele);
      index++;
   }
   int ele_count_per_line = 7;
   int length_per_num = 8;
   int* splitResult = (int*)malloc(sizeof(int) * 4 * ele_count_per_line * rowData.size());
   openCLExcuter->splitPlateContentRowData(grayImg.data, grayImg.cols, grayImg.rows, rowIndex, rowData.size(), ele_count_per_line, 0, 6, splitResult);
   if (false) {
      //保存分隔结果
      for (int i = 0;i < rowData.size();i++) {
         int start = i * ele_count_per_line * 4;
         for (int j = 0;j < ele_count_per_line;j++)
         {
            int startx = splitResult[start];
            int starty = splitResult[start + 1];
            int endx = splitResult[start + 2];
            int endy = splitResult[start + 3];
            start += 4;
            string path = "C:\\Users\\Administrator\\Desktop\\ocr\\trade\\";
            path.append(to_string(i)).append("_").append(to_string(j)).append(".jpg");
            cv::imwrite(path, cv::Mat(grayImg, cv::Rect(startx, starty, endx - startx + 1, endy - starty + 1)));
         }
      }
   }
   //分割数字
   unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_L2_WIDTH * _NUMBER_L2_HEIGHT);
   for (int r = 0;r < _NUMBER_L2_HEIGHT;r++) {
      for (int c = 0;c < _NUMBER_L2_WIDTH;c++)
      {
         zeroData[r * _NUMBER_L2_WIDTH + c] = ImgUtil::NUMS_LEVEL2[0].data.ptr<uchar>(r)[c];
      }
   }
   int line_number_count = ele_count_per_line * length_per_num;
   unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowData.size()) * _NUMBER_L2_WIDTH * 10 * line_number_count);
   UcharDataInfo typesData = UcharDataInfo();
   unsigned char types[] = { NUM_TYPE_TIME,NUM_TYPE_CODE,NUM_TYPE_CODE,NUM_TYPE_PRICE,NUM_TYPE_PRICE,NUM_TYPE_CODE, NUM_TYPE_CODE };
   typesData.length = ele_count_per_line;
   typesData.data = types;
   openCLExcuter->splitL2NumNew(grayImg, IntDataInfo({ splitResult,(int)(ele_count_per_line * rowData.size()) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, ele_count_per_line, length_per_num);
   //识别数字
   uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowData.size()) * _NUMBER_L2_WIDTH * 10 * line_number_count);
   openCLExcuter->createNumberTemplates(rowData.size(), _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, line_number_count, ImgUtil::numsOneLevel_level2, templateNums);
   uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, rowData.size() * _NUMBER_L2_HEIGHT, _NUMBER_L2_WIDTH * 10 * line_number_count, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, line_number_count);
   for (int i = 0;i < rowData.size();i++) {
      TradeDelegateData delegateData = TradeDelegateData();
      //时间
      string time = "";
      for (int j = length_per_num - 6;j < length_per_num;j++)
      {
         if (j - (length_per_num - 6) == 2 || j - (length_per_num - 6) == 4)
            time.append(":");
         time.append(to_string(numberResult[i][length_per_num * 0 + j]));
      }
      //代码
      string code = "";
      for (int j = length_per_num - 6;j < length_per_num;j++)
      {
         code.append(to_string(numberResult[i][length_per_num * 1 + j]));
      }
      //委托数量
      string num = "";
      for (int j = 0;j < length_per_num;j++)
      {
         num.append(to_string(numberResult[i][length_per_num * 2 + j]));
      }
      num = to_string(stoi(num));
      //委托价格
      string delegate_price = "";
      for (int j = 0;j < length_per_num;j++)
      {
         if(length_per_num-j==3)
            delegate_price.append(".");
         delegate_price.append(to_string(numberResult[i][length_per_num * 3 + j]));
      }
      delegate_price = to_string(stod(delegate_price));
      char* chCode;
      chCode = new char[20];
      sprintf_s(chCode, delegate_price.length(), "%.3lf", stod(delegate_price));
      std::string delegate_price_str(chCode);
      delegate_price = delegate_price_str;
      //成交均价
      string percent_price = "";
      for (int j = 0;j < length_per_num;j++)
      {
         if (length_per_num - j == 3)
            percent_price.append(".");
         percent_price.append(to_string(numberResult[i][length_per_num * 4 + j]));
      }
      chCode = new char[20];
      sprintf_s(chCode, percent_price.length(), "%.3lf", stod(percent_price));
      std::string percent_price_str(chCode);
      percent_price = percent_price_str;
      //成交数量
      string success_num = "";
      for (int j = 0;j < length_per_num;j++)
      {
         success_num.append(to_string(numberResult[i][length_per_num * 5 + j]));
      }
      success_num = to_string(stoi(success_num));
      //买入卖出
      int start = i * 4 * ele_count_per_line + 6 * 4;
      int startx = splitResult[start];
      int starty = splitResult[start + 1];
      int endx = splitResult[start + 2];
      int endy = splitResult[start + 3];
      int count = 0;
      for (int r = starty;r <= endy;r++) {
         for (int c = startx;c <= endx;c++) {
            uchar v = grayImg.ptr<uchar>(r)[c];
            if (v >= _IMG_BINARY_THRESHOLD) {
               count++;
            if (data_start < 0) {
               data_start = r;
               data_end = r;
            }
            else {
               data_end = r;
            }
         }
      }
      if (count > 50) {
         //卖出
         delegateData.type = TRADE_TYPE_SELL;
      if (rowData.size() < 2)
      {
         free(grayImg.data);
         grayImg.release();
         for (list<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
            free(*ele);
         }
         return dataList;
      }
      else {
         //买入
         delegateData.type = TRADE_TYPE_BUY;
      //ȥͷȥβ
      list<int*>::iterator start = rowData.begin();
      list<int*>::iterator end = rowData.end();
      free(*start);
      rowData.erase(start);
      std::advance(end, -1);
      free(*end);
      rowData.erase(end);
      int* rowIndex = (int*)malloc(sizeof(int) * rowData.size() * 4);
      int index = 0;
      for (list<int*>::iterator ele = rowData.begin();ele != rowData.end();ele++) {
         rowIndex[index * 4 + 0] = 0;
         rowIndex[index * 4 + 1] = (*ele)[0];
         rowIndex[index * 4 + 2] = grayImg.cols - 1;
         rowIndex[index * 4 + 3] = (*ele)[1];
         free(*ele);
         index++;
      }
      delegateData.index = i;
      delegateData.code = code;
      delegateData.time = time;
      delegateData.num = num;
      delegateData.price = delegate_price;
      delegateData.trade_price = percent_price;
      delegateData.trade_num = success_num;
      int ele_count_per_line = 7;
      int length_per_num = 8;
      dataList.push_back(delegateData);
      free(numberResult[i]);
   }
      int* splitResult = (int*)malloc(sizeof(int) * 4 * ele_count_per_line * rowData.size());
      openCLExcuter->splitPlateContentRowData(grayImg.data, grayImg.cols, grayImg.rows, rowIndex, rowData.size(), ele_count_per_line, 0, 6, splitResult);
      if (false) {
         //保存分隔结果
         for (int i = 0;i < rowData.size();i++) {
            int start = i * ele_count_per_line * 4;
            for (int j = 0;j < ele_count_per_line;j++)
            {
               int startx = splitResult[start];
               int starty = splitResult[start + 1];
               int endx = splitResult[start + 2];
               int endy = splitResult[start + 3];
               start += 4;
               string path = "C:\\Users\\Administrator\\Desktop\\ocr\\trade\\";
               path.append(to_string(i)).append("_").append(to_string(j)).append(".jpg");
               cv::imwrite(path, cv::Mat(grayImg, cv::Rect(startx, starty, endx - startx + 1, endy - starty + 1)));
            }
         }
      }
   free(numberResult);
   free(rowIndex);
   free(splitResult);
   free(zeroData);
   free(templateNums);
   free(grayImg.data);
   grayImg.release();
   return dataList;
      //分割数字
      unsigned char* zeroData = (unsigned char*)malloc(sizeof(unsigned char) * _NUMBER_L2_WIDTH * _NUMBER_L2_HEIGHT);
      for (int r = 0;r < _NUMBER_L2_HEIGHT;r++) {
         for (int c = 0;c < _NUMBER_L2_WIDTH;c++)
         {
            zeroData[r * _NUMBER_L2_WIDTH + c] = ImgUtil::NUMS_LEVEL2[0].data.ptr<uchar>(r)[c];
         }
      }
      int line_number_count = ele_count_per_line * length_per_num;
      unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowData.size()) * _NUMBER_L2_WIDTH * 10 * line_number_count);
      UcharDataInfo typesData = UcharDataInfo();
      unsigned char types[] = { NUM_TYPE_TIME,NUM_TYPE_CODE,NUM_TYPE_CODE,NUM_TYPE_PRICE,NUM_TYPE_PRICE,NUM_TYPE_CODE, NUM_TYPE_CODE };
      typesData.length = ele_count_per_line;
      typesData.data = types;
      openCLExcuter->splitL2NumNew(grayImg, IntDataInfo({ splitResult,(int)(ele_count_per_line * rowData.size()) }), UcharDataInfo({ totalNumberData, -1 }), typesData, zeroData, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, ele_count_per_line, length_per_num);
      //识别数字
      uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowData.size()) * _NUMBER_L2_WIDTH * 10 * line_number_count);
      openCLExcuter->createNumberTemplates(rowData.size(), _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, line_number_count, ImgUtil::numsOneLevel_level2, templateNums);
      uchar** numberResult = openCLExcuter->recognition_numbers(totalNumberData, templateNums, rowData.size() * _NUMBER_L2_HEIGHT, _NUMBER_L2_WIDTH * 10 * line_number_count, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, line_number_count);
      for (int i = 0;i < rowData.size();i++) {
         TradeDelegateData delegateData = TradeDelegateData();
         //时间
         string time = "";
         for (int j = length_per_num - 6;j < length_per_num;j++)
         {
            if (j - (length_per_num - 6) == 2 || j - (length_per_num - 6) == 4)
               time.append(":");
            time.append(to_string(numberResult[i][length_per_num * 0 + j]));
         }
         //代码
         string code = "";
         for (int j = length_per_num - 6;j < length_per_num;j++)
         {
            code.append(to_string(numberResult[i][length_per_num * 1 + j]));
         }
         //委托数量
         string num = "";
         for (int j = 0;j < length_per_num;j++)
         {
            num.append(to_string(numberResult[i][length_per_num * 2 + j]));
         }
         num = to_string(stoi(num));
         //委托价格
         string delegate_price = "";
         for (int j = 0;j < length_per_num;j++)
         {
            if (length_per_num - j == 3)
               delegate_price.append(".");
            delegate_price.append(to_string(numberResult[i][length_per_num * 3 + j]));
         }
         delegate_price = to_string(stod(delegate_price));
         char* chCode;
         chCode = new char[20];
         sprintf_s(chCode, delegate_price.length(), "%.3lf", stod(delegate_price));
         std::string delegate_price_str(chCode);
         delegate_price = delegate_price_str;
         //成交均价
         string percent_price = "";
         for (int j = 0;j < length_per_num;j++)
         {
            if (length_per_num - j == 3)
               percent_price.append(".");
            percent_price.append(to_string(numberResult[i][length_per_num * 4 + j]));
         }
         chCode = new char[20];
         sprintf_s(chCode, percent_price.length(), "%.3lf", stod(percent_price));
         std::string percent_price_str(chCode);
         percent_price = percent_price_str;
         //成交数量
         string success_num = "";
         for (int j = 0;j < length_per_num;j++)
         {
            success_num.append(to_string(numberResult[i][length_per_num * 5 + j]));
         }
         success_num = to_string(stoi(success_num));
         //买入卖出
         int start = i * 4 * ele_count_per_line + 6 * 4;
         int startx = splitResult[start];
         int starty = splitResult[start + 1];
         int endx = splitResult[start + 2];
         int endy = splitResult[start + 3];
         int count = 0;
         for (int r = starty;r <= endy;r++) {
            for (int c = startx;c <= endx;c++) {
               uchar v = grayImg.ptr<uchar>(r)[c];
               if (v >= _IMG_BINARY_THRESHOLD) {
                  count++;
               }
            }
         }
         if (count > 50) {
            //卖出
            delegateData.type = TRADE_TYPE_SELL;
         }
         else {
            //买入
            delegateData.type = TRADE_TYPE_BUY;
         }
         delegateData.index = i;
         delegateData.code = code;
         delegateData.time = time;
         delegateData.num = num;
         delegateData.price = delegate_price;
         delegateData.trade_price = percent_price;
         delegateData.trade_num = success_num;
         dataList.push_back(delegateData);
         free(numberResult[i]);
      }
      free(totalNumberData);
      free(numberResult);
      free(rowIndex);
      free(splitResult);
      free(zeroData);
      free(templateNums);
      free(grayImg.data);
      grayImg.release();
      return dataList;
}