@@ -5,33 +5,57 @@ using namespace std;
55using namespace cv ;
66using 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
1217ImageOcrHelper::~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
3761wstring ImageOcrHelper::removeTrailingNewline (wstring title) {
0 commit comments