admin
2022-07-12 c108e5ba42168841311b74034d89c31556d628c4
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "L2DataCapture.h"
#include <thread>
#include<fstream>
#include "THSActionUtil.h"
#include <thread>
bool L2DataCapture::inited;
 OpenCLExcuter* L2DataCapture::openCLExcuter[THS_FRAME_COUNT];
//ÊÇ·ñÕýÔÚÖ´ÐÐ
 bool L2DataCapture::running;
 bool L2DataCapture::runnings[THS_FRAME_COUNT];
 clock_t L2DataCapture::latest_running_times[THS_FRAME_COUNT];
 
 CallbackFun L2DataCapture::data_callback;
 
 string L2DataCapture::gpCodes[THS_FRAME_COUNT];
 
 void* L2DataCapture::context;
 
//ÔËÐÐ
void L2DataCapture::_run(int index)
{
    while (true) {
        if (running && runnings[index]&& gpCodes[index].length()>0) {
            latest_running_times[index] = clock();
            //ʶ±ðÊý¾Ý
            string code = gpCodes[index];
            try {
                list<TradeData> resultList = captureLevel2TradeData(CaptureUtil::capture(index), index);
                data_callback(index, code, resultList, context);
            }
            catch (...) {
            
            }
        }
        Sleep(2);
    }
}
 
void L2DataCapture::setGPCode(int index, string code) {
    int length = sizeof(gpCodes) / sizeof(gpCodes[0]);
    if (length <= index) {
        return;
    }
 
    gpCodes[index] = code;
}
 
string L2DataCapture::getGPCode(int index) {
    return    gpCodes[index];
}
 
static string getGPCode(int index);
 
L2DataCapture::L2DataCapture() {
 
}
void L2DataCapture::refreshHWND() {
    HWND win = THSActionUtil::getL2Win();
    if (win <= 0) {
        throw string("δ»ñÈ¡µ½Í¬»¨Ë³LEVEL2ÅÌ¿Ú");
    }
    CaptureUtil::init(win);
}
 
void L2DataCapture::init(CallbackFun callback, void* contex) {
    inited = true;
    data_callback = callback;
    context = contex;
 
 
    for (int i = 0;i < THS_FRAME_COUNT;i++)
    {
        openCLExcuter[i] = new OpenCLExcuter();
        openCLExcuter[i]->init();
    }
    //»ñȡͬ»¨Ë³´°¿Ú¾ä±ú
    try {
        refreshHWND();
    }
    catch (string st) {
        throw st;
    }
    running = false;
    int length = sizeof(runnings) / sizeof(runnings[0]);
    for (int i = 0;i < length;i++) {
        runnings[i] = false;
        thread rt(&(L2DataCapture::_run),i);
        rt.detach();
    }
 
}
 
bool L2DataCapture::isRunning() {
    return running;
}
 
void L2DataCapture::start(int index) {
    runnings[index] = true;
}
//½áÊø
void L2DataCapture::stop(int index) {
    runnings[index] = false;
}
//È«²¿¿ªÊ¼
void L2DataCapture::start() {
    running = true;
}
//È«²¿½áÊø
void L2DataCapture::stop() {
    running = false;
}
 
void L2DataCapture::startAll() {
    int length = sizeof(runnings) / sizeof(runnings[0]);
    for (int i = 0;i < length;i++) {
        runnings[i] = true;
    }
}
 
void L2DataCapture::stopAll() {
    int length = sizeof(runnings) / sizeof(runnings[0]);
    for (int i = 0;i < length;i++) {
        runnings[i] = false;
    }
}
 
bool L2DataCapture::isInited() {
    return inited;
}
 
list<TradeData>  L2DataCapture::captureLevel2TradeData(cv::Mat oimg, int identify) {
    if (oimg.rows == 0 || oimg.cols == 0) {
        throw ERROR_CODE_CAPTURE_FAIL;
    }
 
    clock_t  starttime = clock();
 
    /*
    if (1 > 0) {
     std:map<string, TradeData> map;
        return map;
    }
    */
 
 
    //LogUtil::debug("½ØÍ¼Íê³É");
    clock_t time_1 = clock();
    //std::cout << "½ØÍ¼Íê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << (time_1 - starttime) << endl;
 
    //»Ò¶È»¯
    cv::Mat img = 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[identify]->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    else {
        openCLExcuter[identify]->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    img.data = imgData;
    oimg.release();
 
 
    clock_t time_2 = clock();
    std::cout << "»Ò¶ÈÍê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << (time_2 - time_1) << endl;
    LogUtil::debug("»Ò¶ÈÍê³É");
 
    //½«Í¼ÏñתΪ1άÊý×é
    //unsigned char* imgData = img.data;
 
    
 
 
    //ͼÏñ·Ö¸î---¿ªÊ¼
    list<int*>  rowDataList;
    try {
        rowDataList = ImgUtil::divideImg(img);
        if (rowDataList.size() == 0) {
            throw int(ERROR_CODE_DIVIDE_IMG_FAIL);
        }
    }
    catch (...) {
        throw int(ERROR_CODE_DIVIDE_IMG_FAIL);
    }
    //×¼±¸Êý¾Ý
    int* rowDataOneLevel = (int*)malloc(sizeof(int) * rowDataList.size() * 4);
    list<int*>::iterator e;
    int index = 0;
    for (e = rowDataList.begin();e != rowDataList.end();e++) {
        int* indexs = *e;
        rowDataOneLevel[index * 4 + 0] = indexs[1];
        rowDataOneLevel[index * 4 + 1] = indexs[0];
        rowDataOneLevel[index * 4 + 2] = indexs[3];
        rowDataOneLevel[index * 4 + 3] = indexs[2];
        index++;
        free(indexs);
    }
 
    int* rowSplitDataOneLevel = (int*)malloc(sizeof(int) * rowDataList.size() * 4 * 7);
    openCLExcuter[identify]->splitL2RowData(imgData, img.cols, img.rows, rowDataOneLevel, rowDataList.size(), rowSplitDataOneLevel);
    free(rowDataOneLevel);
 
    /*
    for (int i = 0;i < rowDataList.size();i++) {
        string path = "C:\\Users\\Administrator\\Desktop\\ocr\\cancel_time\\";
        path.append(to_string(identify)).append("_").append(to_string(i)).append(".jpg");
        int start = 4 * 7 * i;
        start += 4 * (1);
        int startx = rowSplitDataOneLevel[start];
        int starty = rowSplitDataOneLevel[start + 1];
        int endx = rowSplitDataOneLevel[start + 2];
        int endy = rowSplitDataOneLevel[start + 3];
        if (startx > 0) {
            cv::imwrite(path, cv::Mat(img, cv::Rect(startx, starty, endx - startx + 1, endy - starty + 1)));
        }
    }
    */
 
    /*
    //±£´æ·Ö¸ôµÄͼƬ
    if (true) {
        int start = 4 * 7 * (558 - 1);
        start += 4 * (2);
        int startx=    rowSplitDataOneLevel[start];
        int starty = rowSplitDataOneLevel[start+1];
        int endx = rowSplitDataOneLevel[start+2];
        int endy = rowSplitDataOneLevel[start+3];
        cv::imshow("¼Û¸ñ", cv::Mat(img, cv::Rect(startx, starty, endx - startx + 1, endy - starty + 1)));
    }
    */
 
 
 
 
    clock_t time_3 = clock();
    //LogUtil::debug("·Ö¸ôÍê³É");
    std::cout << "·Ö¸ôÍê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_3 - time_2 << endl;
 
    //ͼÏñ·Ö¸î---½áÊø
 
 
 
 
 
 
 
    //ͼÏñʶ±ðµÄÊý×Ö×¼±¸
 
    unsigned char* totalNumberData = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowDataList.size()) * _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER);
 
 
 
    clock_t time_31 = clock();
    std::cout << "Êý¾Ý×¼±¸-ͼÏñÊý¾Ý×¼±¸: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_31 - time_3 << endl;
 
    int* pos = (int*)malloc(sizeof(int) * 4 * 4 * rowDataList.size());
 
 
    index = 0;
    for (e = rowDataList.begin();e != rowDataList.end();e++) {
 
        int startS = index * 4 * 7;
 
 
        int start = index * 4 * 4;
        pos[start] = rowSplitDataOneLevel[startS + 0];
        pos[start + 1] = rowSplitDataOneLevel[startS + 1];
        pos[start + 2] = rowSplitDataOneLevel[startS + 2];
        pos[start + 3] = rowSplitDataOneLevel[startS + 3];
 
 
        start = index * 4 * 4 + 4 * 1;
        pos[start] = rowSplitDataOneLevel[startS + 4 * 1 + 0];
        pos[start + 1] = rowSplitDataOneLevel[startS + 4 * 1 + 1];
        pos[start + 2] = rowSplitDataOneLevel[startS + 4 * 1 + 2];
        pos[start + 3] = rowSplitDataOneLevel[startS + 4 * 1 + 3];
 
 
        start = index * 4 * 4 + 4 * 2;
        pos[start] = rowSplitDataOneLevel[startS + 4 * 2 + 0];
        pos[start + 1] = rowSplitDataOneLevel[startS + 4 * 2 + 1];
        pos[start + 2] = rowSplitDataOneLevel[startS + 4 * 2 + 2];
        pos[start + 3] = rowSplitDataOneLevel[startS + 4 * 2 + 3];
 
 
 
        start = index * 4 * 4 + 4 * 3;
        pos[start] = rowSplitDataOneLevel[startS + 4 * 5 + 0];
        pos[start + 1] = rowSplitDataOneLevel[startS + 4 * 5 + 1];
        pos[start + 2] = rowSplitDataOneLevel[startS + 4 * 5 + 2];
        pos[start + 3] = rowSplitDataOneLevel[startS + 4 * 5 + 3];
 
        index++;
    }
 
 
    clock_t time_32 = clock();
    std::cout << "Êý¾Ý×¼±¸-λÖÃÊý¾Ý×¼±¸: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_32 - time_31 << endl;
 
    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];
        }
    }
 
    clock_t time_33 = clock();
    std::cout << "Êý¾Ý×¼±¸-0Êý¾Ý×¼±¸: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_33 - time_32 << endl;
 
    openCLExcuter[identify]->splitL2Num(imgData, img.cols, img.rows, pos, 4 * rowDataList.size(), zeroData, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER, totalNumberData);
    free(zeroData);
    free(pos);
    
 
 
    /*
    list<list<int*>>::iterator e;
    int index = 0;
    for (e = data.begin();e != data.end();e++) {
        index++;
        ImgUtil::splitRowNumDataForOpenCL(img, *e, totalNumberData,index-1);
    }
 
    */
    clock_t time_34 = clock();
    std::cout << "Êý¾Ý×¼±¸-Êý×Ö·Ö¸ôÍê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_34 - time_33 << endl;
 
    //×¼±¸Ä£°åÊý×Ö
    uchar* templateNums = (unsigned char*)malloc(sizeof(unsigned char) * (_NUMBER_L2_HEIGHT * rowDataList.size()) * _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER);
 
    openCLExcuter[identify]->createNumberTemplates(rowDataList.size(), _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER, ImgUtil::numsOneLevel_level2 ,templateNums);
 
    //ImgUtil::createTemplateNumData(data.size());
    clock_t time_4 = clock();
    std::cout << "Êý¾Ý×¼±¸-Ä£°åÊý×Ö×¼±¸Íê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_4 - time_34 << endl;
 
 
    std::cout << "Êý¾Ý×¼±¸Íê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_4 - time_3 << endl;
 
 
    //ͼÏñʶ±ð(³ý¿ªÊý×ֵIJ¿·Ö)
    list<TradeData> resultList;
 
 
    int* notNumberResult = (int*)malloc(sizeof(int)* rowDataList.size()*3);
    openCLExcuter[identify]->recognitionNotNum(img.data,img.cols,img.rows, rowSplitDataOneLevel,7, rowDataList.size(), notNumberResult);
 
 
 
    for (int i = 0;i < rowDataList.size();i++) {
        TradeData td = TradeData();
 
        switch (notNumberResult[i * 3])
        {
        case 0:
            td.cancelTimeUnit = TIME_SECOND;break;
        case 1:
            td.cancelTimeUnit = TIME_MINITE;break;
        case 2:
            td.cancelTimeUnit = TIME_HOUR;break;
 
        default:
            break;
        }
 
        switch (notNumberResult[i * 3+1])
        {
        case 0:
            td.limitPrice = LIMIT_PRICE_NORMAL;break;
        case 1:
            td.limitPrice = LIMIT_PRICE_UP;break;
        case 2:
            td.limitPrice = LIMIT_PRICE_DOWN;break;
 
        default:
            break;
        }
        switch (notNumberResult[i * 3 + 2])
        {
        case OPERATE_BUY:
            td.operateType = OPERATE_BUY;
            break;
        case OPERATE_BUY_CANCEL:
            td.operateType = OPERATE_BUY_CANCEL;
            break;
        case OPERATE_SELL:
            td.operateType = OPERATE_SELL;
            break;
        case OPERATE_SELL_CANCEL:
            td.operateType = OPERATE_SELL_CANCEL;
            break;
        case OPERATE_OPERATE_ERROR:
            td.operateType = OPERATE_OPERATE_ERROR;
            break;
        };
 
        resultList.push_back(td);
    }
 
    free(notNumberResult);
 
    /*
    try {
        resultList = (*recognitionManager).recognition(img, rowSplitDataOneLevel, rowDataList.size());
    }
    catch (...) {
        throw ERROR_CODE_RECOGNITION_FAIL;
    }
    */
    
    clock_t time_5 = clock();
    std::cout << "·ÇÊý×ÖÊý¾Ýʶ±ðÍê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_5 - time_4 << endl;
 
    //Êý×Öʶ±ð
    uchar** numberResult = openCLExcuter[identify]->recognition_numbers(totalNumberData, templateNums, rowDataList.size() * _NUMBER_L2_HEIGHT, _NUMBER_L2_WIDTH * 10 * _NUMBER_L2_TOTAL_NUMBER, _NUMBER_L2_WIDTH, _NUMBER_L2_HEIGHT, _NUMBER_L2_TOTAL_NUMBER);
    //ÊÍ·ÅÄÚ´æ
    free(totalNumberData);
    free(templateNums);
    //Ñ­»·¶ÁÈ¡Êý×Ö
    list<TradeData>::iterator tradeEle;
    index = 0;
    for (tradeEle = resultList.begin();tradeEle != resultList.end();tradeEle++) {
        uchar* lineData = numberResult[index];
        string time = "";
        time.append(to_string(lineData[0])).append(to_string(lineData[1]));
        time.append(":");
        time.append(to_string(lineData[2])).append(to_string(lineData[3]));
        time.append(":");
        time.append(to_string(lineData[4])).append(to_string(lineData[5]));
 
        string cancelTime = "";
        cancelTime.append(to_string(lineData[6])).append(to_string(lineData[7]));
 
 
        string price = "";
        price.append(to_string(lineData[6+2])).append(to_string(lineData[7+2])).append(to_string(lineData[8+2])).append(to_string(lineData[9+2]));
        price.append(".");
        price.append(to_string(lineData[10+2])).append(to_string(lineData[11+2]));
 
        string num = "";
        num.append(to_string(lineData[12+2])).append(to_string(lineData[13+2])).append(to_string(lineData[14+2])).append(to_string(lineData[15+2])).append(to_string(lineData[16+2]));
 
 
        (*tradeEle).time = time;
        (*tradeEle).num = stoi(num);
        (*tradeEle).price = price;
        (*tradeEle).index = index;
        (*tradeEle).cancelTime = stoi(cancelTime);
 
        //ÊÍ·ÅÄÚ´æ
        free(lineData);
 
        index++;
    }
 
    free(numberResult);
 
    clock_t time_6 = clock();
    //LogUtil::debug("ʶ±ðÍê³É");
    std::cout << "Êý×Öʶ±ðÍê³É: threadid-" << std::this_thread::get_id() << " ºÄʱ£º" << time_6 - time_5 << endl;
 
    //ÊÍ·ÅÄÚ´æ
    //img.release();
    free(imgData);
    img.release();
    rowDataList.clear();
    list<int*>().swap(rowDataList);
    free(rowSplitDataOneLevel);
 
    std::cout << "-------½áÊøÈÎÎñ: threadid-" << std::this_thread::get_id() << "  ÐòºÅ£º" << identify << " ºÄʱ£º" << clock() - starttime << endl;
 
    return resultList;
}
 
//²¶»ñlevel2µÄÅÌ¿ÚÊý¾Ý
list<TradeData> L2DataCapture::captureLevel2TradeData(HWND hwnd, int index) throw(int) {
    cv::Mat img = CaptureUtil::capture(hwnd);
 
    /*
    string path1 = "E:\\temp\\";
 
    path1.append(to_string(frameIndex));
    path1.append("\\");
    path1.append(to_string(clock()));
    path1.append(".jpg");
    cv::imwrite(path1, img);
    std::map<string, TradeData> map;
    */
 
    return captureLevel2TradeData(img, index);
}