#include "CurrentPriceCapture.h" #include "THSActionUtil.h" #include #include "Win32Util.h" #include "TaskChecker.h" #include "GPUtil.h" bool CurrentPriceCapture::tradeTimeCapture; OpenCLExcuter* CurrentPriceCapture::openCLExcuter; bool CurrentPriceCapture::inited; //ÊÇ·ñÕýÔÚÖ´ÐÐ bool CurrentPriceCapture::running; bool CurrentPriceCapture::kill; CallbackFun_Current_Price CurrentPriceCapture::data_callback; std::list CurrentPriceCapture::pointList; void* CurrentPriceCapture::context; HWND CurrentPriceCapture::win; RecognitionManager* CurrentPriceCapture::recognitionManager; void CurrentPriceCapture::_run() { while (true) { if (kill) break; if (tradeTimeCapture) { if (!GPUtil::isTradeTime()) { Sleep(100); continue; } } TaskChecker::clientLiveTime.limitUp = clock(); if (running && inited) { clock_t start = clock(); try { cout << "ÏÖ¼Ûʶ±ð" << endl; list codes = capture(); cout << "ÏÖ¼Ûʶ±ðºÄʱ:" << clock() - start << " ÊýÁ¿£º" << codes.size() << endl; data_callback(codes, context); codes.clear(); } catch (...) { cout<<"ÏÖ¼Ûʶ±ðerror"<init(); thread rt(&(CurrentPriceCapture::_run)); rt.detach(); inited = true; try { refreshHWND(); } catch (string st) { } } void CurrentPriceCapture::reCreateRunning() { kill = TRUE; Sleep(4000); kill = FALSE; thread rt(&(CurrentPriceCapture::_run)); rt.detach(); } void CurrentPriceCapture::refreshHWND() { HWND hwnd = THSActionUtil::getSecondWindow(); if (hwnd <= 0) throw string("ͬ»¨Ë³¸±ÆÁ1δ´ò¿ª"); //²ÉÓÿì½Ý¼ü´ò¿ª Win32Util::focus(hwnd); Win32Util::keyboardNum("51", 200); Win32Util::keyboard(VK_RETURN, 10); Sleep(1000); HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL); if (content <= 0) throw string("δ»ñÈ¡µ½ÄÚÈÝ´°¿Ú¾ä±ú"); win = content; } bool CurrentPriceCapture::isRunning() { return running; } list CurrentPriceCapture::capture(cv::Mat oimg, OpenCLExcuter* openCLExcuter) { 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, imgData); } else { openCLExcuter->rgba2Gray(oimg, imgData); } grayImg.data = imgData; list rowData; try { rowData = THSActionUtil::splitPlateRowArea(grayImg); } catch (string st) { free(imgData); throw st; } //ÿÐеÄÔªËØ¸öÊý int ele_count_per_line = 3; int length_per_num = 6; int* splitResult = (int*)malloc(sizeof(int) * 4 * 4 * 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 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,3,splitResult); /* *²âÊÔ for (int i = 0;i < rowData.size();i++) { for (int j = 0;j < line_number_count;j++) { int sx = splitResult[(line_number_count * i + j) * 4 + 0]; int sy = splitResult[(line_number_count * i + j) * 4 + 1]; int ex = splitResult[(line_number_count * i + j) * 4 + 2]; int ey = splitResult[(line_number_count * i + j) * 4 + 3]; string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\"; path.append(to_string(i)).append("_").append(to_string(j)).append(".jpg"); //cv::imwrite(path, cv::Mat(grayImg, cv::Rect(sx, sy, ex - sx + 1, ey - sy + 1))); } } */ int start = 4 * 4 * 1 + 4 * 1; string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\"; path.append(to_string(start)).append(".jpg"); //cv::imwrite(path, cv::Mat(grayImg,cv::Rect(splitResult[start], splitResult[start+1], splitResult[start+2]- splitResult[start]+1, splitResult[start+3]- splitResult[start+1]+1))); //cv::imwrite(path, cv::Mat(grayImg, cv::Rect(447, 102, 454-447+1,111-102+1 ))); //·Ö¸îÊý×Ö 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, NUM_TYPE_PRICE }; typesData.length = 3; 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); list dataList; for (int i = 0;i < rowData.size();i++) { CurrentPriceData currentData = CurrentPriceData(); string volumn = ""; for (int j = 0;j < length_per_num;j++) { if (j == 4) { volumn.append("."); } volumn.append(to_string(numberResult[i][length_per_num * 0 + j])); } string code = ""; for (int j = 0;j < length_per_num;j++) { code.append(to_string(numberResult[i][length_per_num * 1 + j])); } string price = ""; for (int j = 0;j < length_per_num;j++) { if (j == 4) { price.append("."); } price.append(to_string(numberResult[i][length_per_num * 2 + j])); } currentData.index = i; currentData.code = code; currentData.volumn = volumn; currentData.price = price; if ( currentData.price != "0000.00") { dataList.push_back(currentData); } free(numberResult[i]); } free(numberResult); if (dataList.size() > 0) { int* unitData = (int*)malloc(sizeof(int) * 4 * 1 * dataList.size()); //»ñÈ¡Á¿µÄµ¥Î» int index = 0; for (list::iterator ele = dataList.begin();ele != dataList.end();ele++) { unitData[index * 4 * 1 + 0] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 0]; unitData[index * 4 * 1 + 1] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 1]; unitData[index * 4 * 1 + 2] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 2]; unitData[index * 4 * 1 + 3] = splitResult[(*ele).index * 4 * ele_count_per_line + 4 * 0 + 3]; index++; } int* unitResult = (int*)malloc(sizeof(int) * dataList.size() * 1); openCLExcuter->recognitionPlateMoneyUnit(grayImg.data, grayImg.cols, grayImg.rows, unitData, 1, dataList.size(), unitResult); index = 0; for (list::iterator ele = dataList.begin();ele != dataList.end();ele++) { switch (unitResult[index * 1]) { case 0: (*ele).volumnUnit = MONEY_UNIT_Y; break; case 1: (*ele).volumnUnit = MONEY_UNIT_W; break; case 2: (*ele).volumnUnit = MONEY_UNIT_NO; (*ele).volumn = to_string((int)round(stof((*ele).volumn) * 100)); default: break; } //cout<< (*ele).code<<" "<< (*ele).price <<" "<< (*ele).volumn; //if ((*ele).volumnUnit == MONEY_UNIT_Y) { // cout <<"ÒÚ"< CurrentPriceCapture::capture() { if (win <= 0) { throw string("δÕÒµ½Ä¿±ê±êµÄ´°¿Ú"); } cout << "¿ªÊ¼ÔËÐÐ" << endl; RECT wrect; GetWindowRect(win,&wrect); list flist; set codesSet; for (int i = 0;i < 2;i++) { if (kill) { break; } int x = wrect.right - 50; int y = wrect.top + 50; PostMessage(win, WM_MOUSEWHEEL, 0, MAKEWORD(x, y)); Sleep(1000); cv::Mat oimg = CaptureUtil::capture(win); list codes = capture(oimg,openCLExcuter); for (list::iterator ele = codes.begin();ele != codes.end();ele++) { string code = (*ele).code; if (codesSet.count(code) == 0&&stof((*ele).price)>0) { codesSet.insert(code); flist.push_back(*ele); } } codes.clear(); Sleep(50); } return flist; } //È«²¿¿ªÊ¼ void CurrentPriceCapture::start() { running = true; } //È«²¿½áÊø void CurrentPriceCapture::stop() { running = false; } bool CurrentPriceCapture::isInited() { return inited; }