-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognitionmodule.cpp
More file actions
189 lines (157 loc) · 5.03 KB
/
Copy pathrecognitionmodule.cpp
File metadata and controls
189 lines (157 loc) · 5.03 KB
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
#include "recognitionmodule.h"
#include "patternrecognitionmodule.h"
#include "infoclass.h"
#include <QImage>
#include <QBuffer>
RecognitionModule::RecognitionModule()
{
recognitionApi = new tesseract::TessBaseAPI();
if(recognitionApi->Init(NULL, "eng")){
qDebug() << "failed to init api";
exit(4);
}
}
RecognitionModule::~RecognitionModule()
{
recognitionApi->End();
}
QImage RecognitionModule::cropImage(QImage original, QRect rect)
{
QImage cropped = original.copy(rect);
return cropped;
}
RecognitionResults RecognitionModule::startRecognition(QImage objectRecognition, patternRecognitionModule pattern)
{
RecognitionResults res;
QVector<InfoClass> recArea = pattern.getRecognitionArea();
QImage objRecog = objectRecognition;
for (int i = 0; i < recArea.size(); i++) {
QRect rect = recArea[i].getQRect();
QImage recImage = cropImage(objRecog, rect);
QString result = recognize(recImage);
res.addRecognitionResults(result);
}
return res;
}
RecognitionResults RecognitionModule::startRecognition(QImage objectRecognition, InfoClass recognitionArea)
{
RecognitionResults recResults;
QRect rect = recognitionArea.getQRect();
QImage recImage = cropImage(objectRecognition, rect);
QString result = recognize(recImage);
recResults.addRecognitionResults(result);
return recResults;
}
RecognitionResults RecognitionModule::startRecognition(QImage objectRecognition, InfoClass recognitionArea, InfoClass answerRecArea)
{
RecognitionResults recResults;
QRect rect = recognitionArea.getQRect();
QRect aRect = answerRecArea.getQRect();
aRect.setX(rect.x() + aRect.x());
aRect.setY(rect.y() + aRect.y());
aRect.setWidth(answerRecArea.width());
aRect.setHeight(answerRecArea.height());
QImage recImage = cropImage(objectRecognition, aRect);
QString result = recognize(recImage);
recResults.addRecognitionResults(result);
return recResults;
}
void RecognitionModule::setPatternRecognition(patternRecognitionModule pattern)
{
patternRec = pattern;
}
patternRecognitionModule RecognitionModule::getPatternRecognition() const
{
return patternRec;
}
//PIX* RecognitionModule::makePIXFromQImage(const QImage &image)
//{
// QByteArray ba;
// QBuffer buf(&ba);
// buf.open(QIODevice::WriteOnly);
// image.save(&buf, "BMP");
// return pixReadMemBmp(ba.constData(), ba.size());
//}
QImage RecognitionModule::PIX2QImage(PIX *pixImage) {
int width = pixGetWidth(pixImage);
int height = pixGetHeight(pixImage);
int depth = pixGetDepth(pixImage);
int bytesPerLine = pixGetWpl(pixImage) * 4;
l_uint32 * s_data = pixGetData(pixEndianByteSwapNew(pixImage));
QImage::Format format;
if (depth == 1)
format = QImage::Format_Mono;
else if (depth == 8)
format = QImage::Format_Indexed8;
else
format = QImage::Format_RGB32;
QImage result((uchar*)s_data, width, height, bytesPerLine, format);
// Handle pallete
QVector<QRgb> _bwCT;
_bwCT.append(qRgb(255,255,255));
_bwCT.append(qRgb(0,0,0));
QVector<QRgb> _grayscaleCT(256);
for (int i = 0; i < 256; i++) {
_grayscaleCT.append(qRgb(i, i, i));
}
if (depth == 1) {
result.setColorTable(_bwCT);
} else if (depth == 8) {
result.setColorTable(_grayscaleCT);
} else {
result.setColorTable(_grayscaleCT);
}
if (result.isNull()) {
static QImage none(0,0,QImage::Format_Invalid);
qDebug() << "***Invalid format!!!";
return none;
}
return result.rgbSwapped();
}
PIX* RecognitionModule::qImage2PIX(QImage& qImage) {
PIX * pixs;
l_uint32 *lines;
qImage = qImage.rgbSwapped();
int width = qImage.width();
int height = qImage.height();
int depth = qImage.depth();
int wpl = qImage.bytesPerLine() / 4;
pixs = pixCreate(width, height, depth);
pixSetWpl(pixs, wpl);
pixSetColormap(pixs, NULL);
l_uint32 *datas = pixs->data;
for (int y = 0; y < height; y++) {
lines = datas + y * wpl;
QByteArray a((const char*)qImage.scanLine(y), qImage.bytesPerLine());
for (int j = 0; j < a.size(); j++) {
*((l_uint8 *)lines + j) = a[j];
}
}
return pixEndianByteSwapNew(pixs);
}
QString RecognitionModule::recognize(QImage objectRecognition)
{
Pix *pixImage = qImage2PIX(objectRecognition);
recognitionApi->SetImage(pixImage);
return recognitionApi->GetUTF8Text();
}
QString RecognitionModule::recognize(QImage objectRecognition, patternRecognitionModule pattern)
{
QString output = "";
return output;
}
QString RecognitionModule::recognize(QImage objectRecognition, InfoClass recognitionArea)
{
QString output = "";
return output;
}
QString RecognitionModule::recognize(QString objectRecognitionPathFile, InfoClass recognitionArea)
{
return QString();
}
void RecognitionModule::preliminaryProcessing(std::string objRecogPath)
{
//imshow(windowName, image); // Show our image inside the created window.
//waitKey(0); // Wait for any keystroke in the window
//destroyWindow(windowName);
}