-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmrtesseract.h
77 lines (75 loc) · 1.5 KB
/
mrtesseract.h
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
#ifndef MRTESSERACT_H
#define MRTESSERACT_H
#define TESS_WITH_OPENCV 1
#include "baseapi.h"
#include "string"
#ifdef _WIN32
#if _DEBUG
#pragma comment(lib,"tesseract40d.lib")
#else
#pragma comment(lib,"tesseract40.lib")
#endif
#endif
#if TESS_WITH_OPENCV
#include "opencv2/opencv.hpp"
#else
#include "allheaders.h"
#endif
class MRTesseract
{
public:
static MRTesseract *getInstance()
{
static MRTesseract instance;
return &instance;
}
#if TESS_WITH_OPENCV
std::string recog(const cv::Mat &img){
std::string str = "";
if (inited == 0 && img.data)
{
api.Clear();
api.ClearAdaptiveClassifier();
api.SetImage((uchar*)img.data, img.cols, img.rows, img.channels(), img.step);
char * ocrresult = api.GetUTF8Text();
str = ocrresult;
delete[]ocrresult;
}
return str;
}
std::string recog(const std::string filepath)
{
std::string str="";
cv::Mat img=cv::imread(filepath);
if(!img.data)
return str;
else
return recog(img);
}
#else
std::string recog(const std::string filepath)
{
Pix* pixs = pixRead(filepath.c_str());
if (pixs == NULL)
{
std::cout << "cannot load " << filepath << std::endl;
return "";
}
api.SetImage(pixs);
std::string ret = api.GetUTF8Text();
pixDestroy(&pixs);
return ret;
}
#endif
private:
MRTesseract()
{
inited=api.Init("./", "eng");
api.SetVariable("tessedit_char_whitelist", "0123456789");
api.SetPageSegMode(tesseract::PSM_SINGLE_CHAR);
}
tesseract::TessBaseAPI api;
int inited = -1;
~MRTesseract(){api.End();}
};
#endif