Skip to content

Commit 2f435e9

Browse files
committed
Refactored OCR reading class into several internal methods.
1 parent 381b033 commit 2f435e9

3 files changed

Lines changed: 56 additions & 17 deletions

File tree

ReadMagicCard/CardReader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,11 @@ bool CardReader::hasResultFailed(OcrDecodeResult result) {
157157
OcrDecodeResult CardReader::ocrReadTitle(vector<Mat> ocrTitles) {
158158

159159
OcrDecodeResult bestResult;
160+
ImageOcrHelper ocrReader(systemMethods);
160161

161162
for (Mat ocrTitle : ocrTitles) {
162163

163-
OcrDecodeResult result = ImageOcrHelper::DecodeTitle(ocrTitle, systemMethods);
164+
OcrDecodeResult result = ocrReader.DecodeImage(ocrTitle);
164165

165166
if (bestResult.Confidence < result.Confidence) {
166167
bestResult = result;

ReadMagicCard/ImageOcrHelper.cpp

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,57 @@ using namespace std;
55
using namespace cv;
66
using namespace tesseract;
77

8-
ImageOcrHelper::ImageOcrHelper()
8+
ImageOcrHelper::ImageOcrHelper(SystemMethods* systemMethods)
99
{
10+
this->systemMethods = systemMethods;
11+
12+
//Setup the OCR API.
13+
pathToTrainedData = getTrainedDataPath();
14+
setupTessBaseAPI(pathToTrainedData);
1015
}
1116

1217
ImageOcrHelper::~ImageOcrHelper()
1318
{
1419
}
1520

16-
OcrDecodeResult ImageOcrHelper::DecodeTitle(Mat originalImageData, SystemMethods* systemMethods) {
21+
OcrDecodeResult ImageOcrHelper::DecodeImage(Mat originalImageData) {
22+
23+
//Set the current image to read.
24+
setImage(originalImageData);
25+
26+
//Read the image.
27+
OcrDecodeResult result;
28+
result.Text = getUTF8Text(systemMethods);
29+
result.Confidence = ocr.MeanTextConf();
30+
31+
return result;
32+
}
33+
34+
string ImageOcrHelper::getTrainedDataPath() {
35+
36+
return systemMethods->ToString(systemMethods->GetPathToExeParentDirectory()) + "tessdata";
37+
}
1738

18-
string trainedDataFolderPath = systemMethods->ToString(systemMethods->GetPathToExeParentDirectory()) + "tessdata";
19-
const char* path = trainedDataFolderPath.c_str();
20-
tesseract::TessBaseAPI ocr;
21-
ocr.Init(path, "eng", OEM_TESSERACT_ONLY);
39+
void ImageOcrHelper::setupTessBaseAPI(string trainedDataPath) {
40+
41+
ocr.Init(trainedDataPath.c_str(), "eng", OEM_TESSERACT_ONLY);
2242
ocr.SetPageSegMode(PSM_SINGLE_LINE);
43+
}
44+
45+
void ImageOcrHelper::setImage(Mat originalImageData) {
46+
2347
ocr.SetImage((uchar*)originalImageData.data, originalImageData.cols, originalImageData.rows, originalImageData.channels(), originalImageData.step1());
48+
}
2449

25-
OcrDecodeResult result;
50+
wstring ImageOcrHelper::getUTF8Text(SystemMethods* systemMethods) {
2651

2752
char* rawOutText = ocr.GetUTF8Text();
2853
wstring outText = systemMethods->ToWString(string(rawOutText));
29-
delete[] rawOutText; //As instructed by documentation in the GetUTF8Text method.
54+
outText = removeTrailingNewline(outText);
3055

31-
result.Text = removeTrailingNewline(outText);
32-
result.Confidence = ocr.MeanTextConf();
56+
delete[] rawOutText; //As instructed by documentation in the GetUTF8Text method.
3357

34-
return result;
58+
return outText;
3559
}
3660

3761
wstring ImageOcrHelper::removeTrailingNewline(wstring title) {

ReadMagicCard/ImageOcrHelper.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,28 @@
66
class ImageOcrHelper
77
{
88
public:
9-
ImageOcrHelper();
9+
ImageOcrHelper(SystemMethods* systemMethods);
1010
~ImageOcrHelper();
1111

12-
//Decodes the title image and turns it into a wstring. The result also contains the Tesseract confidence.
13-
static OcrDecodeResult DecodeTitle(cv::Mat originalImageData, SystemMethods* systemMethods);
12+
//Decodes the image and turns it into a wstring. The result also contains the OCR API confidence.
13+
OcrDecodeResult DecodeImage(cv::Mat originalImageData);
1414

1515
private:
16-
16+
//Gets the path to the folder containing the trained language data files.
17+
std::string getTrainedDataPath();
18+
//Sets up the OCR API so it can be used for reading images.
19+
void setupTessBaseAPI(std::string trainedDataPath);
20+
//Sets the current image to read.
21+
void setImage(cv::Mat originalImageData);
22+
//Gets the text in the image (formated as UTF8).
23+
std::wstring getUTF8Text(SystemMethods* systemMethods);
1724
//Removes the newlines from the end of the title.
18-
static std::wstring removeTrailingNewline(std::wstring title);
25+
std::wstring removeTrailingNewline(std::wstring title);
26+
27+
//The OCR API used for reading images.
28+
tesseract::TessBaseAPI ocr;
29+
//The path to the folder containing the trained language data files.
30+
std::string pathToTrainedData;
31+
//Pointer to object handling the system dependent methods.
32+
SystemMethods* systemMethods;
1933
};

0 commit comments

Comments
 (0)