admin
2022-07-22 9adb473067a993b4e0eacf3675baf14b29da3eca
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include "TradeSuccessCapture.h"
#include <set>
#include "Win32Util.h"
OpenCLExcuter* BuySuccessCapture::openCLExcuter;
bool BuySuccessCapture::inited;
HWND BuySuccessCapture::win;
RecognitionManager* BuySuccessCapture::recognitionManager;
 
//ÊÇ·ñÕýÔÚÖ´ÐÐ
bool BuySuccessCapture::running;
 
CallbackFun_Trade_Success BuySuccessCapture::data_callback;
 
 
void* BuySuccessCapture::context;
 
void BuySuccessCapture::_run() {
    while (true) {
        //1sÒ»´Î
        if (running && inited) {
            clock_t start = clock();
            list<TradeSuccessData> datas = captureTradeSuccessInfo();
            cout << "ºÄʱ:" << clock() - start << "  ÊýÁ¿£º" << datas.size() << endl;
            data_callback(datas, context);
            datas.clear();
            Sleep(2000);
        }
    }
}
 
 
 
cv::Mat BuySuccessCapture::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->rgb2ThresholdInvert(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    else {
        openCLExcuter->rgba2ThresholdInvert(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    grayImg.data = imgData;
 
    return grayImg;
}
 
 
 
 
void BuySuccessCapture::init(CallbackFun_Trade_Success callback, void* contex) {
    data_callback = callback;
    context = contex;
    running = false;
 
    recognitionManager = new RecognitionManager();
    openCLExcuter = new OpenCLExcuter();
    openCLExcuter->init();
 
    thread rt(&(BuySuccessCapture::_run));
    rt.detach();
    inited = true;
    try {
        refreshHWND();
    }
    catch (...) {
    
    }
 
    
}
void BuySuccessCapture::refreshHWND() {
    HWND hwnd = THSActionUtil::getTradeWindow();
    if (hwnd <= 0)
        throw string("ͬ»¨Ë³×¨ÒµÏµ¥Ò³ÃæÎ´´ò¿ª");
 
    HWND content = GetDlgItem(hwnd, 0x00002EE6);
 
 
    if (content <= 0)
        throw string("δ»ñÈ¡µ½ÄÚÈÝ´°¿Ú¾ä±ú");
 
    win = content;
}
 
 
 
 
list<TradeSuccessData> BuySuccessCapture::captureTradeSuccessInfo(cv::Mat oimg) {
    cv::Mat grayImg = grayImgs(oimg);
    list<TradeSuccessData> 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_start = -1;
            empty_end = -1;
 
            if (data_start < 0) {
                data_start = r;
                data_end = r;
            }
            else {
                data_end = r;
            }
        }
    }
    //ȥͷȥβ
    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 = 10;
 
 
    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_CODE,NUM_TYPE_CODE,NUM_TYPE_TIME,NUM_TYPE_CODE,NUM_TYPE_PRICE,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++) {
        TradeSuccessData industryData = TradeSuccessData();
        //ÈÕÆÚ
        string date = "";
        for (int j = length_per_num - 8;j < length_per_num;j++)
        {
            date.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 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 * 2 + j]));
        }
        //ÊýÁ¿
        string num = "";
        for (int j = 0;j < length_per_num;j++)
        {
            num.append(to_string(numberResult[i][length_per_num * 3 + j]));
        }
        num = to_string(stoi(num));
 
        //½ð¶î
        string money = "";
        for (int j = 0;j < length_per_num;j++)
        {
            if (j == length_per_num - 2) {
                money.append(".");
            }
            money.append(to_string(numberResult[i][length_per_num * 4 + j]));
        }
 
        char* chCode;
        chCode = new char[20];
        sprintf_s(chCode, money.length(), "%.2lf", stod(money));
        std::string str(chCode);
        money = str;
 
        //ºÏͬ±àºÅ
        string trade_num = "";
        for (int j = 0;j < length_per_num;j++)
        {
            trade_num.append(to_string(numberResult[i][length_per_num * 5 + j]));
        }
 
        //ÂòÈëÂô³ö
        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) {
            //Âô³ö
            industryData.type = TRADE_TYPE_SELL;
        }
        else {
            //ÂòÈë
            industryData.type = TRADE_TYPE_BUY;
        }
 
 
        industryData.index = i;
        industryData.date = date;
        industryData.code = code;
        industryData.time = time;
        industryData.num = num;
        industryData.money = money;
        industryData.trade_num = trade_num;
        
 
 
        dataList.push_back(industryData);
        free(numberResult[i]);
    }
 
 
    free(numberResult);
    free(rowIndex);
    free(splitResult);
    free(zeroData);
    free(templateNums);
    free(grayImg.data);
    grayImg.release();
    return dataList;
}
 
 
 
list<TradeSuccessData> BuySuccessCapture::captureTradeSuccessInfo() {
    cv::Mat oimg = CaptureUtil::capture(win);
    if (oimg.cols < 10 || oimg.rows < 10) {
        throw string("½ØÍ¼³ö´í");
    }
    list<TradeSuccessData> codes = captureTradeSuccessInfo(oimg);
    return codes;
}
 
bool BuySuccessCapture::isInited() {
    return inited;
}
 
bool BuySuccessCapture::isRunning() {
    return running;
}
 
 
 
//È«²¿¿ªÊ¼
void BuySuccessCapture::start() {
    running = true;
}
//È«²¿½áÊø
void BuySuccessCapture::stop() {
    running = false;
}