-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesseract.cc
More file actions
209 lines (189 loc) · 5.59 KB
/
Copy pathtesseract.cc
File metadata and controls
209 lines (189 loc) · 5.59 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
204
205
206
207
208
209
#include <Python.h>
#include <tesseract/baseapi.h>
#include <tesseract/img.h>
#include <tesseract/imgs.h>
#include <iulib/io_tiff.h>
#include <structmember.h>
//We'll use module methods to alter the object.
//In the future I'll create a new tesseractobject
//with object methods.
#ifdef _TIFFIO_
void read_tiff_image(TIFF* tif, IMAGE* image);
#endif
typedef struct {
PyObject_HEAD
TessBaseAPI *api;
PyObject *lang;
PyObject *filename;
PyObject *text;
int mode;
/* Type-specific fields go here. */
} Tesseract;
static void
Tesseract_dealloc(Tesseract* self)
{
Py_XDECREF(self->api);
Py_XDECREF(self->lang);
Py_XDECREF(self->filename);
Py_XDECREF(self->text);
self->ob_type->tp_free((PyObject*)self);
}
static PyObject *
Tesseract_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Tesseract *self;
self = (Tesseract *)type->tp_alloc(type, 0);
if (self != NULL) {
self->api = new TessBaseAPI();
if (self->api == NULL)
{
Py_DECREF(self);
return NULL;
}
self->lang = PyString_FromString("");
if (self->lang == NULL)
{
Py_DECREF(self);
return NULL;
}
self->filename = PyString_FromString("");
if (self->filename == NULL)
{
Py_DECREF(self);
return NULL;
}
self->text = PyString_FromString("");
if (self->text == NULL)
{
Py_DECREF(self);
return NULL;
}
self->mode = 0;
}
return (PyObject *)self;
}
static
PyObject *
get_text(Tesseract *self, PyObject *args)
{
IMAGE image;
IMAGE *imageptr = ℑ
char * inputname = PyString_AsString(self->filename);
TIFF* tif = TIFFOpen(inputname, "r");
if (tif) {
read_tiff_image(tif, &image);
TIFFClose(tif);
} else
return PyString_FromString("Could Not Open Tiff file.");
int bytes_per_line = check_legal_image_size(imageptr->get_xsize(),
imageptr->get_ysize(),
imageptr->get_bpp());
char * text;
if (self->mode == 0) {
text = (self->api)->TesseractRect(imageptr->get_buffer(),
imageptr->get_bpp()/8, bytes_per_line, 0, 0,
imageptr->get_xsize(), imageptr->get_ysize());
}
else if (self->mode == 1){
text = (self->api)->TesseractRectBoxes(imageptr->get_buffer(),
imageptr->get_bpp()/8, bytes_per_line, 0, 0,
imageptr->get_xsize(), imageptr->get_ysize(), imageptr->get_ysize());
}
else {
text = strdup("");
}
return PyString_FromString(text);
}
static int
Tesseract_init(Tesseract *self, PyObject *args, PyObject *kwds)
{
TessBaseAPI *api = new TessBaseAPI();
PyObject *lang=NULL, *filename=NULL, *stmp;
static char *kwlist[] = {strdup("lang") ,strdup("filename"), strdup("mode"), NULL};
if (! PyArg_ParseTupleAndKeywords(args,kwds, "|OOi",kwlist,
&lang,&filename,&self->mode))
return -1;
if (lang) {
stmp = self->lang;
Py_INCREF(lang);
self->lang = lang;
Py_XDECREF(stmp);
}
if (filename) {
stmp = self->filename;
Py_INCREF(filename);
self->filename = filename;
Py_XDECREF(stmp);
}
if (api) {
Py_INCREF(api);
self->api = api;
(self->api)->InitWithLanguage("tesseract",NULL, PyString_AsString(lang), NULL, false, 0, NULL);
}
return 0;
}
static PyMemberDef Tesseract_members[] = {
{strdup("lang"), T_OBJECT_EX, offsetof(Tesseract, lang),0,
strdup("Tesseract Language")},
{strdup("filename"), T_OBJECT_EX, offsetof(Tesseract, filename),0,
strdup("Tesseract Filename")},
{strdup("mode"), T_INT, offsetof(Tesseract, mode),0,
strdup("Tesseract mode")},
{NULL} /*Sentinel */
};
static PyMethodDef Tesseract_methods[] = {
{"get_text", (PyCFunction)get_text, METH_NOARGS},
{NULL} /* Sentinel */
};
static PyTypeObject tesseract_TesseractType = {
PyObject_HEAD_INIT(NULL)
0, /*ob_size*/
"tesseract.Tesseract", /*tp_name*/
sizeof(Tesseract), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Tesseract_dealloc, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash */
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
"Tesseract objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Tesseract_methods, /* tp_methods */
Tesseract_members, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
(initproc)Tesseract_init, /* tp_init */
0, /* tp_alloc */
Tesseract_new, /* tp_new */
};
PyMODINIT_FUNC inittesseract(void)
{
PyObject* m;
tesseract_TesseractType.tp_new = PyType_GenericNew;
if (PyType_Ready(&tesseract_TesseractType) < 0)
return;
m = Py_InitModule3("tesseract", Tesseract_methods,
"Module that provides an interface to tesseract.");
Py_INCREF(&tesseract_TesseractType);
PyModule_AddObject(m, "Tesseract", (PyObject *)&tesseract_TesseractType);
}