admin
2022-09-09 60feedf43a35a9ca69d05095a01c5d1797b1bdc3
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
#include "THSGPDataCapture.h"
#include <set>
#include "Win32Util.h"
 
 
 
cv::Mat THSGPDataCapture::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->rgb2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    else {
        openCLExcuter->rgba2Gray(oimg.data, oimg.cols, oimg.rows, imgData);
    }
    grayImg.data = imgData;
 
    return grayImg;
}
 
 
 
void THSGPDataCapture::init() {
    openCLExcuter = new OpenCLExcuter();
    openCLExcuter->init();
}
 
 
 
 
list<list<GPDataStruct>> THSGPDataCapture::captureGPData(cv::Mat grayImg, SplitType types[], int length_per_num, int startColumn, int endColumn) {
 
 
    list<int*> rowData = THSActionUtil::splitPlateRowArea(grayImg);
    int count = 0;
    int* rowIndex = (int*)malloc(sizeof(int) * 4 * rowData.size());
    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 ele_count_per_line = sizeof(types) / sizeof(types[0]);
    int* splitResult = (int*)malloc(sizeof(int) * 4 * ele_count_per_line * rowData.size());
    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, startColumn, endColumn, splitResult);
 
    //·Ö¸îÊý×Ö
    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);
 
    uchar* types_char = (uchar*)malloc(sizeof(uchar) * ele_count_per_line);
    for (int i = 0;i < ele_count_per_line;i++) {
        types_char[i] = types[i];
    }
 
    UcharDataInfo typesData = UcharDataInfo();
    typesData.length = 2;
    typesData.data = types_char;
 
 
 
 
    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);
    free(types_char);
 
 
    //ʶ±ðÊý×Ö
    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<list<GPDataStruct>> dataList;
    //½âÎöÊý¾Ý£¬GPDataStruct
    for (int i = 0;i < rowData.size();i++) {
 
        list<GPDataStruct> lineData;
 
        for (int p = 0;p < ele_count_per_line;p++) {
            GPDataStruct industryData = GPDataStruct();
            switch (types[p]) {
            case NUM_TYPE_TIME: {
                //ʱ¼ä
                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 * p + j]));
                }
                industryData.type = NUM_TYPE_TIME;
                industryData.content = time;
            }break;
            case NUM_TYPE_MONEY: {
                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 * p + j]));
                }
 
                char* priceCode;
                priceCode = new char[100];
                sprintf_s(priceCode, 100, "%.2lf", stod(money));
                std::string _str(priceCode);
                delete[] priceCode;
                money = _str;
 
                industryData.type = NUM_TYPE_MONEY;
                industryData.content = money;
            }break;
            case NUM_TYPE_PRICE:
            {
                string price = "";
                for (int j = 0;j < length_per_num;j++)
                {
                    if (j == length_per_num - 2) {
                        price.append(".");
                    }
                    price.append(to_string(numberResult[i][length_per_num * p + j]));
                }
 
                char* priceCode;
                priceCode = new char[100];
                sprintf_s(priceCode, 100, "%.2lf", stod(price));
                std::string _str(priceCode);
                delete[] priceCode;
                price = _str;
 
                industryData.type = NUM_TYPE_PRICE;
                industryData.content = price;
            }
            break;
            case NUM_TYPE_CODE: {
                //¹ÉƱ´úÂë
                string code = "";
                for (int j = length_per_num - 6;j < length_per_num;j++)
                {
                    code.append(to_string(numberResult[i][length_per_num * p + j]));
                }
                industryData.type = NUM_TYPE_CODE;
                industryData.content = code;
            }
                              break;
 
            case NUM_TYPE_NUM:
                //Êý×Ö
                string num = "";
                for (int j = 0;j < length_per_num;j++)
                {
                    num.append(to_string(numberResult[i][length_per_num * p + j]));
                }
                num = to_string(stoi(num));
 
                industryData.type = NUM_TYPE_NUM;
                industryData.content = num;
                break;
            }
            lineData.push_back(industryData);
        }
        free(numberResult[i]);
        dataList.push_back(lineData);
    }
    free(numberResult);
 
 
 
    list<int> moneyIndexs;
    for (int p = 0;p < ele_count_per_line;p++) {
        if (types[p] == NUM_TYPE_MONEY) {
            moneyIndexs.push_back(p);
        }
    }
 
    if (moneyIndexs.size() > 0)
    {
 
        int* unitData = (int*)malloc(sizeof(int) * 4 * moneyIndexs.size() * rowData.size());
 
 
 
        //ʶ±ð½ð¶îµ¥Î»
        int line = 0;
        for (list<list<GPDataStruct>>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
            int index = 0;
            for (list<int>::iterator e = moneyIndexs.begin();e != moneyIndexs.end();e++) {
                int start_index = (line * moneyIndexs.size() + index) * 4;
                unitData[start_index + 0] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 0];
                unitData[start_index + 1] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 1];
                unitData[start_index + 2] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 2];
                unitData[start_index + 3] = splitResult[line * 4 * ele_count_per_line + *e * 4 + 3];
                string path = "C:\\Users\\Administrator\\Desktop\\ocr\\limit_up\\";
                path.append(to_string(line)).append("_1.jpg");
                //cv::imwrite(path, cv::Mat(grayImg,cv::Rect(unitData[start_index + 0], unitData[start_index + 1], unitData[start_index + 2]- unitData[start_index + 0]+1, unitData[start_index + 3]- unitData[start_index + 1]+1)));
                index++;
            }
            line++;
        }
        int* unitResult = (int*)malloc(sizeof(int) * moneyIndexs.size() * dataList.size());
        openCLExcuter->recognitionPlateMoneyUnit(grayImg.data, grayImg.cols, grayImg.rows, unitData, moneyIndexs.size(), dataList.size(), unitResult);
        int index = 0;
        for (list<list<GPDataStruct>>::iterator ele = dataList.begin();ele != dataList.end();ele++) {
 
            int data_index = index * moneyIndexs.size();
 
            int cIndex = 0;
            for (list<GPDataStruct>::iterator e = (*ele).begin();e != (*ele).end();e++) {
                if ((*e).type == NUM_TYPE_MONEY) {
                    switch (unitResult[data_index + cIndex])
                    {
                    case 0:
                        (*e).unit = MONEY_UNIT_Y;
                        break;
                    case 1:
                        (*e).unit = MONEY_UNIT_W;
                        break;
                    default:
                        break;
                    }
 
                    cIndex++;
                }
            }
            index++;
        }
        free(unitData);
        free(unitResult);
    }
 
    free(rowIndex);
    free(splitResult);
    free(zeroData);
    free(totalNumberData);
    free(templateNums);
    free(grayImg.data);
    grayImg.release();
 
 
    return dataList;
}
 
bool THSGPDataCapture::isInited() {
    return inited;
}