-
Notifications
You must be signed in to change notification settings - Fork 0
/
recogniser_tesseract.cpp
151 lines (138 loc) · 3.98 KB
/
recogniser_tesseract.cpp
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
#include <iostream>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <leptonica/bmp.h>
#include "recogniser_tesseract.h"
#include "util.h"
namespace ocr {
using namespace std;
using namespace tesseract;
RecogniserTesseract::RecogniserTesseract(const char *lang, const char *data_dir, const char *whitelist) : api(NULL), image(NULL)
{
api = new TessBaseAPI();
if (api->Init(data_dir, lang)) {
cerr
<< "Could not initialize tesseract with language " << lang
<< " and data directory "
<< (data_dir ? "" : "'")
<< (data_dir || "<default>")
<< (data_dir ? "" : "'")
<< endl;
delete_api();
}
if (!api->SetVariable("load_system_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_freq_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_unambig_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_punc_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_number_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_fixed_length_dawgs", "F")) {
delete_api();
}
if (!api->SetVariable("load_bigram_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("load_bigram_dawg", "F")) {
delete_api();
}
if (!api->SetVariable("wordrec_enable_assoc", "F")) {
delete_api();
}
#if 0 /* Requires ScrollView */
if (!api->SetVariable("interactive_display_mode", "T")) {
delete_api();
}
#endif
if (whitelist && !api->SetVariable("tessedit_char_whitelist", whitelist)) {
delete_api();
}
if (!api) {
throw("API init failed");
}
}
RecogniserTesseract::~RecogniserTesseract(void)
{
if (api) {
api->End();
delete api;
api = NULL;
}
if (image) {
pixDestroy(&image);
image = NULL;
}
}
void RecogniserTesseract::set_image(Pix *image)
{
// Don't set self->image, because we don't own it
api->SetImage(image);
}
void RecogniserTesseract::set_image(
const unsigned char *imagedata,
int width,
int height,
int bytes_per_pixel,
int bytes_per_line
)
{
// Don't set self->image, because it is being bypassed
api->SetImage(imagedata, width, height, bytes_per_pixel, bytes_per_line);
}
void RecogniserTesseract::set_image_bmp(const void *bmp_data)
{
const unsigned char *ptr =
reinterpret_cast<const unsigned char *>(bmp_data);
const struct BMP_FileHeader *hdr = reinterpret_cast<const struct BMP_FileHeader *>(ptr);
const struct BMP_InfoHeader *info_hdr = reinterpret_cast<const struct BMP_InfoHeader *>(ptr + sizeof(struct BMP_FileHeader));
const unsigned char *pixel_data = ptr + hdr->bfOffBits;
set_image(pixel_data, info_hdr->biWidth, info_hdr->biHeight, info_hdr->biPlanes, ROUND_4(info_hdr->biWidth));
}
void RecogniserTesseract::set_image(const char *filename)
{
if (image) {
pixDestroy(&image);
image = NULL;
}
// Open input image with leptonica library
// set self->image, because now we own the image
image = pixRead(filename);
set_image(image);
}
void RecogniserTesseract::ocr(void)
{
#ifdef SIMPLE
// Get OCR result
char *outText = api->GetUTF8Text();
cout << "OCR output:" << endl << outText << endl;
delete [] outText;
#else
api->Recognize(0);
ResultIterator* ri = api->GetIterator();
PageIteratorLevel level = tesseract::RIL_WORD;
if (ri != 0) {
do {
const char* word = ri->GetUTF8Text(level);
float conf = ri->Confidence(level);
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
cout << "word: '" << word << "'; \tconf: " << conf << "; BoundingBox: " << x1 << "," << y1 << "," << x2 << "," << y2 << ";" << endl;
delete[] word;
} while (ri->Next(level));
}
#endif
}
char RecogniserTesseract::recognise(const cv::Mat & img, bool black_on_white)
{
// TODO
return '?';
}
}