#include "LimitUpCapture.h"
|
#include "THSActionUtil.h"
|
#include <set>
|
OpenCLExcuter* LimitUpCapture::openCLExcuter;
|
bool LimitUpCapture::inited;
|
//ÊÇ·ñÕýÔÚÖ´ÐÐ
|
bool LimitUpCapture::running;
|
|
CallbackFun_Limit_Up LimitUpCapture::data_callback;
|
|
void* LimitUpCapture::context;
|
|
HWND LimitUpCapture::win;
|
|
RecognitionManager* LimitUpCapture::recognitionManager;
|
|
void LimitUpCapture::_run() {
|
while (true) {
|
if (running && inited) {
|
clock_t start = clock();
|
list<LimitUpData> codes = captureLimitUpCodes();
|
cout << "ºÄʱ:" << clock() - start << " ÊýÁ¿£º" << codes.size() << endl;
|
data_callback(codes, context);
|
codes.clear();
|
Sleep(3000);
|
}
|
}
|
}
|
|
|
|
void LimitUpCapture::init(CallbackFun_Limit_Up callback, void* contex) {
|
data_callback = callback;
|
context = contex;
|
running = false;
|
recognitionManager = new RecognitionManager();
|
openCLExcuter = new OpenCLExcuter();
|
openCLExcuter->init();
|
|
thread rt(&(LimitUpCapture::_run));
|
rt.detach();
|
inited = true;
|
refreshHWND();
|
}
|
void LimitUpCapture::refreshHWND() {
|
HWND hwnd = THSActionUtil::getThirdWindow();
|
if (hwnd <= 0)
|
throw string("ͬ»¨Ë³¸±ÆÁ2δ´ò¿ª");
|
|
HWND content = FindWindowExA(hwnd, NULL, "AfxFrameOrView100s", NULL);
|
|
|
if (content <= 0)
|
throw string("δ»ñÈ¡µ½ÄÚÈÝ´°¿Ú¾ä±ú");
|
|
win = content;
|
}
|
|
bool LimitUpCapture::isRunning() {
|
return running;
|
}
|
|
list<LimitUpData> LimitUpCapture::captureLimitUpCodes(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;
|
|
|
|
list<int*> rowData = THSActionUtil::splitPlateRowArea(grayImg);
|
|
//ÿÐеÄÔªËØ¸öÊý
|
int ele_count_per_line = 4;
|
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<int*>::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 = 4 * 6;
|
|
openCLExcuter->splitLimitUpRowData(grayImg.data, grayImg.cols, grayImg.rows, rowIndex, rowData.size(), ele_count_per_line, 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<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);
|
openCLExcuter->splitPlateNum(grayImg.data, grayImg.cols, grayImg.rows, splitResult, 4 * rowData.size(), zeroData, _NUMBER_GP_CODE_WIDTH, _NUMBER_GP_CODE_HEIGHT, line_number_count, totalNumberData);
|
|
|
|
|
//ʶ±ðÊý×Ö
|
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<LimitUpData> dataList;
|
for (int i = 0;i < rowData.size();i++) {
|
LimitUpData limitUpData = LimitUpData();
|
string code = "";
|
for (int j = 0;j < 6;j++)
|
{
|
code.append(to_string(numberResult[i][j]));
|
}
|
string time = "";
|
for (int j = 0;j < 6;j++)
|
{
|
if (j == 2 || j == 4) {
|
time.append(":");
|
}
|
time.append(to_string(numberResult[i][6 + j]));
|
}
|
|
string price = "";
|
for (int j = 0;j < 6;j++)
|
{
|
if (j == 4) {
|
price.append(".");
|
}
|
price.append(to_string(numberResult[i][6 * 2 + j]));
|
}
|
|
string money = "";
|
for (int j = 0;j < 6;j++)
|
{
|
if (j == 4) {
|
money.append(".");
|
}
|
money.append(to_string(numberResult[i][6 * 3 + j]));
|
}
|
|
limitUpData.index = i;
|
limitUpData.code = code;
|
limitUpData.time = time;
|
limitUpData.price = price;
|
limitUpData.limitMoney = money;
|
if (limitUpData.time != "00:00:00" && limitUpData.price != "0000.00" && limitUpData.limitMoney != "0000.00") {
|
dataList.push_back(limitUpData);
|
}
|
free(numberResult[i]);
|
}
|
free(numberResult);
|
int* unitData = (int*)malloc(sizeof(int) * 4 * dataList.size());
|
|
|
|
//ʶ±ð½ð¶îµ¥Î»
|
int index = 0;
|
for (list<LimitUpData>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
|
unitData[index * 4 + 0] = splitResult[(*ele).index * 4 * 4 + 4 * 3 + 0];
|
unitData[index * 4 + 1] = splitResult[(*ele).index * 4 * 4 + 4 * 3 + 1];
|
unitData[index * 4 + 2] = splitResult[(*ele).index * 4 * 4 + 4 * 3 + 2];
|
unitData[index * 4 + 3] = splitResult[(*ele).index * 4 * 4 + 4 * 3 + 3];
|
index++;
|
}
|
int* unitResult = (int*)malloc(sizeof(int) * dataList.size());
|
openCLExcuter->recognitionPlateMoneyUnit(grayImg.data, grayImg.cols, grayImg.rows, unitData, 1, dataList.size(), unitResult);
|
index = 0;
|
for (list<LimitUpData>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
|
switch (unitResult[index])
|
{
|
case 0:
|
(*ele).limitMoneyUnit = MONEY_UNIT_Y;
|
break;
|
case 1:
|
(*ele).limitMoneyUnit = MONEY_UNIT_W;
|
break;
|
default:
|
break;
|
}
|
index++;
|
}
|
free(rowIndex);
|
free(splitResult);
|
free(zeroData);
|
free(templateNums);
|
free(unitData);
|
free(unitResult);
|
free(imgData);
|
|
grayImg.release();
|
return dataList;
|
}
|
|
list<LimitUpData> LimitUpCapture::captureLimitUpCodes() {
|
RECT wrect;
|
GetWindowRect(win, &wrect);
|
list<LimitUpData> flist;
|
set<string> codesSet;
|
string codestr="";
|
for (int i = 0;i < 5;i++)
|
{
|
int x = wrect.right - 50;
|
int y = wrect.top + 50;
|
SendMessage(win, WM_MOUSEWHEEL, 0, MAKEWORD(x, y));
|
Sleep(200);
|
cv::Mat oimg = CaptureUtil::capture(win);
|
list<LimitUpData> codes = captureLimitUpCodes(oimg);
|
for (list<LimitUpData>::iterator ele = codes.begin();ele != codes.end();ele++) {
|
string code = (*ele).code;
|
if(codesSet.count(code)==0){
|
codesSet.insert(code);
|
flist.push_back(*ele);
|
}
|
}
|
codes.clear();
|
Sleep(50);
|
}
|
return flist;
|
|
}
|
|
//È«²¿¿ªÊ¼
|
void LimitUpCapture::start() {
|
running = true;
|
}
|
//È«²¿½áÊø
|
void LimitUpCapture::stop() {
|
running = false;
|
}
|
bool LimitUpCapture::isInited() {
|
return inited;
|
}
|