-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOcrModule.cpp
More file actions
203 lines (149 loc) · 6.91 KB
/
OcrModule.cpp
File metadata and controls
203 lines (149 loc) · 6.91 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
#include <fstream>
#include <ctime>
#include <locale>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <alcommon/almodule.h>
#include <alvalue/alvalue.h>
#include "OcrModule.hpp"
#include "picProcessing.hpp"
#include "picSceneDetect.hpp"
#include "picSegmentation.hpp"
#include "APIwrappers.h"
#include <locale>
/*NAO*/
OcrModule::OcrModule(boost::shared_ptr<AL::ALBroker> broker, const std::string &name) : AL::ALModule(broker, name){
// Describe the module here. This will appear on the webpage
setModuleDescription("Module for text recognition on pictures.");
functionName("setPoolVariable", getName(), "Set a new value for pool variable.");
addParam("variableName","Name of variable to which is new value appended");
addParam("variableValue","New value for variable");
BIND_METHOD(OcrModule::setPoolVariable);
functionName("getPoolVariable", getName(), "Get pool variable value.");
addParam("variableName","Name of variable to read value");
setReturn("AL::ALValue","Variable value");
BIND_METHOD(OcrModule::getPoolVariable);
functionName("runSceneDetection", getName(), "Detection of text bounding frame");
BIND_METHOD(OcrModule::runSceneDetection);
functionName("runPictureProcessing", getName(), "Picture processing for text recognition.");
BIND_METHOD(OcrModule::runPictureProcessing);
functionName("runPictureSegmentation", getName(), "Picture segmentation for line by line recognition.");
setReturn ("int", "Returns number of segments");
BIND_METHOD(OcrModule::runPictureSegmentation);
functionName("runTextRecognition", getName(), "Text recognition of whole text body.");
setReturn("string", "Recognized text.");
BIND_METHOD(OcrModule::runTextRecognition);
functionName("runSegmentRecognition", getName(), "Get recognized text segment.");
addParam ("SegmentIndex", "Segment index to be recognized");
setReturn("string", "Recognized segment of text.");
BIND_METHOD(OcrModule::runSegmentRecognition);
}
/*Module functions*/
OcrModule::~OcrModule(){
reset();
}
void OcrModule::init(){
reset();
}
void OcrModule::reset(){
ocrLang.clear();
debugLocation.clear();
picOriginal.release();
picProcesed.release();
picProcesedBin.release();
debug = false;
}
/*SET ang GET functions*/
void OcrModule::setPoolVariable(const std::string & variableName, const AL::ALValue & variableValue){
if (variableName == "picOriginal") picOriginal = ALtoMAT(variableValue);
if (variableName == "picProcesed") picProcesed = ALtoMAT(variableValue);
if (variableName == "picProcesedBin") picProcesedBin = ALtoMAT(variableValue);
if (variableName == "segLines") segLines = ALtoSEGn(variableValue);
if (variableName == "segFrame") segFrame = ALtoSEG(variableValue);
if (variableName == "ocrLang") ocrLang = ALtoSTRING(variableValue);
if (variableName == "debugLocation") debugLocation = ALtoSTRING(variableValue);
if (variableName == "debug") debug = ALtoBOOL(variableValue);
if (variableName == "numAvgSegHeight") numAvgSegHeight = ALtoINT(variableValue);
}
AL::ALValue OcrModule::getPoolVariable(const std::string & variableName){
if (variableName == "picOriginal") return MATtoAL(picOriginal);
if (variableName == "picProcesed") return MATtoAL(picProcesed);
if (variableName == "picProcesedBin") return MATtoAL(picProcesedBin);
if (variableName == "segLines") return SEGntoAL(segLines);
if (variableName == "segFrame") return SEGtoAL(segFrame);
if (variableName == "ocrLang") return STRINGtoAL(ocrLang);
if (variableName == "debugLocation") return STRINGtoAL(debugLocation);
if (variableName == "debug") return BOOLtoAL(debug);
if (variableName == "numAvgSegHeight") return INTtoAL(numAvgSegHeight);
return 0;
}
/*RUN functions*/
void OcrModule::runSceneDetection(){
picSceneDetection frameDetect;
frameDetect.setPoolVariable("debug", debug);
frameDetect.setPoolVariable("debugLocation", debugLocation);
frameDetect.setPoolVariable("picture", picOriginal);
frameDetect.runSceneDetection();
frameDetect.getPoolVariable("segFrame", segFrame);
}
void OcrModule::runPictureProcessing(){
picProcessing picProc;
picProc.setPoolVariable("debug", debug);
picProc.setPoolVariable("debugLocation", debugLocation);
picProc.setPoolVariable("picOriginal", picOriginal);
picProc.setPoolVariable("segFrame", segFrame);
picProc.runPicProcessing();
picProc.getPoolVariable("picCutOriginal", picProcesed);
picProc.getPoolVariable("picCutBinary", picProcesedBin);
}
int OcrModule::runPictureSegmentation(){
picSegmentation picSeg;
picSeg.setPoolVariable("debug", debug);
picSeg.setPoolVariable("debugLocation", debugLocation);
picSeg.setPoolVariable("picOriginal", picProcesedBin);
picSeg.runPicSegmentation();
picSeg.getPoolVariable("segTextLines", segLines);
picSeg.getPoolVariable("numAvgSegHeight", numAvgSegHeight);
return segLines.size();
}
std::string OcrModule::runTextRecognition(){
std::string text;
tess = new tesseract::TessBaseAPI();
tess->Init(NULL, ocrLang.c_str());
std::cout<<"[Tesseract] Ready!"<<std::endl;
tess->SetPageSegMode (tesseract::PSM_AUTO);
tess->SetImage((uchar*)picProcesed.data, picProcesed.size().width, picProcesed.size().height, picProcesed.channels(), picProcesed.step1());
std::cout<<"[Tesseract] Image loaded!"<<std::endl;
tess->Recognize(0);
text = tess->GetUTF8Text();
std::cout<<"[Tesseract] OCR has finished!"<<std::endl;
tess->End();
return text;
}
std::string OcrModule::runSegmentRecognition(const int &segmentIndex){
if (segmentIndex >= segLines.size()) return "ERROR Index out of range.\n";
tess = new tesseract::TessBaseAPI();
tess->Init(NULL, ocrLang.c_str());
tess->SetImage((uchar*)picProcesed.data, picProcesed.size().width, picProcesed.size().height, picProcesed.channels(), picProcesed.step1());
std::string text;
int ULx=segLines[segmentIndex][0].x;
int ULy=segLines[segmentIndex][0].y;
int W=segLines[segmentIndex][1].x-segLines[segmentIndex][0].x;
int H=segLines[segmentIndex][3].y-segLines[segmentIndex][0].y;
tess->SetRectangle(ULx, ULy, W, H);
std::cout<<"[Tesseract] Segment "<< segmentIndex + 1 <<" Loaded!"<<std::endl;
if (H < numAvgSegHeight * 1.3)
tess->SetPageSegMode (tesseract::PSM_SINGLE_LINE);
else
tess->SetPageSegMode (tesseract::PSM_AUTO);
tess->Recognize(0);
text = tess->GetUTF8Text();
text = text.substr(0,text.size()-2);
std::cout<<"[Tesseract] Segment "<< segmentIndex + 1 <<" OCR finished!"<<std::endl;
tess->End();
return text;
}