Skip to content

Commit a4b260a

Browse files
committed
solve linux issues for starting api
1 parent e0b0ca6 commit a4b260a

File tree

1 file changed

+81
-49
lines changed

1 file changed

+81
-49
lines changed

SimpleTesseractPythonWrapper.ipynb

+81-49
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
"execution_count": 1,
66
"metadata": {
77
"ExecuteTime": {
8-
"end_time": "2019-03-23T12:57:28.040474Z",
9-
"start_time": "2019-03-23T12:57:28.006379Z"
8+
"end_time": "2019-03-24T19:19:47.035418Z",
9+
"start_time": "2019-03-24T19:19:46.989260Z"
1010
}
1111
},
1212
"outputs": [],
1313
"source": [
14+
"import ctypes\n",
15+
"import locale\n",
1416
"import os\n",
1517
"import platform\n",
18+
"from ctypes.util import find_library\n",
1619
"\n",
1720
"import cffi\n",
1821
"from PIL import Image, ImageDraw, ImageFont"
@@ -23,8 +26,8 @@
2326
"execution_count": 2,
2427
"metadata": {
2528
"ExecuteTime": {
26-
"end_time": "2019-03-23T12:57:29.331915Z",
27-
"start_time": "2019-03-23T12:57:28.951892Z"
29+
"end_time": "2019-03-24T19:19:48.504555Z",
30+
"start_time": "2019-03-24T19:19:48.097205Z"
2831
}
2932
},
3033
"outputs": [],
@@ -41,8 +44,37 @@
4144
"execution_count": 3,
4245
"metadata": {
4346
"ExecuteTime": {
44-
"end_time": "2019-03-23T12:57:30.073874Z",
45-
"start_time": "2019-03-23T12:57:29.961576Z"
47+
"end_time": "2019-03-24T19:19:49.435764Z",
48+
"start_time": "2019-03-24T19:19:49.428745Z"
49+
}
50+
},
51+
"outputs": [],
52+
"source": [
53+
"def get_abs_path_of_library(library):\n",
54+
" \"\"\"Get absolute path of library.\"\"\"\n",
55+
" abs_path = None\n",
56+
" lib_name = find_library(library)\n",
57+
" if os.path.exists(lib_name):\n",
58+
" abs_path = os.path.abspath(lib_name)\n",
59+
" return abs_path\n",
60+
" libdl = ctypes.CDLL(lib_name)\n",
61+
" if not libdl:\n",
62+
" return abs_path # None\n",
63+
" try:\n",
64+
" dlinfo = libdl.dlinfos\n",
65+
" except AttributeError as err:\n",
66+
" # Workaroung for linux\n",
67+
" abs_path = str(err).split(\":\")[0]\n",
68+
" return abs_path"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 4,
74+
"metadata": {
75+
"ExecuteTime": {
76+
"end_time": "2019-03-24T19:19:51.359887Z",
77+
"start_time": "2019-03-24T19:19:51.212488Z"
4678
}
4779
},
4880
"outputs": [],
@@ -173,11 +205,11 @@
173205
},
174206
{
175207
"cell_type": "code",
176-
"execution_count": 4,
208+
"execution_count": 5,
177209
"metadata": {
178210
"ExecuteTime": {
179-
"end_time": "2019-03-23T12:57:30.832997Z",
180-
"start_time": "2019-03-23T12:57:30.821967Z"
211+
"end_time": "2019-03-24T19:19:52.516956Z",
212+
"start_time": "2019-03-24T19:19:52.507932Z"
181213
}
182214
},
183215
"outputs": [],
@@ -211,11 +243,11 @@
211243
},
212244
{
213245
"cell_type": "code",
214-
"execution_count": 5,
246+
"execution_count": 7,
215247
"metadata": {
216248
"ExecuteTime": {
217-
"end_time": "2019-03-23T12:57:31.655190Z",
218-
"start_time": "2019-03-23T12:57:31.646162Z"
249+
"end_time": "2019-03-24T19:20:28.472550Z",
250+
"start_time": "2019-03-24T19:20:28.461520Z"
219251
}
220252
},
221253
"outputs": [],
@@ -225,20 +257,20 @@
225257
"dll_dir = \"\"\n",
226258
"if platform.architecture()[1].lower().startswith('windows'):\n",
227259
" dll_dir = \"win\"\n",
260+
" if platform.architecture()[0] == '64bit':\n",
261+
" dll_dir += \"64\"\n",
262+
" elif platform.architecture()[0] == '32bit':\n",
263+
" dll_dir += \"32\"\n",
264+
" abs_path = os.path.join(os.getcwd(), dll_dir)\n",
228265
"\n",
229-
"if platform.architecture()[0] == '64bit':\n",
230-
" dll_dir += \"64\"\n",
231-
"# dll_dir += \"64\"\n",
232-
"elif platform.architecture()[0] == '32bit':\n",
233-
" dll_dir += \"32\"\n",
234-
"\n",
235-
"abs_path = os.path.join(os.getcwd(), dll_dir)\n",
236-
"\n",
237-
"env_path = os.environ['PATH']\n",
238-
"if abs_path not in env_path:\n",
239-
" os.environ['PATH'] = abs_path + \";\" + env_path\n",
240-
"tess_libname = os.path.join(abs_path, \"tesseract41.dll\")\n",
241-
"lept_libname = os.path.join(abs_path, \"leptonica-1.78.0.dll\")\n",
266+
" env_path = os.environ['PATH']\n",
267+
" if abs_path not in env_path:\n",
268+
" os.environ['PATH'] = abs_path + \";\" + env_path\n",
269+
" tess_libname = os.path.join(abs_path, \"tesseract41.dll\")\n",
270+
" lept_libname = os.path.join(abs_path, \"leptonica-1.78.0.dll\")\n",
271+
"else:\n",
272+
" tess_libname = get_abs_path_of_library('tesseract')\n",
273+
" lept_libname = get_abs_path_of_library('lept')\n",
242274
"\n",
243275
"# Use project tessdata\n",
244276
"tessdata = os.path.join(os.getcwd(), \"tessdata\")\n",
@@ -247,11 +279,11 @@
247279
},
248280
{
249281
"cell_type": "code",
250-
"execution_count": 6,
282+
"execution_count": 8,
251283
"metadata": {
252284
"ExecuteTime": {
253-
"end_time": "2019-03-23T12:57:32.405182Z",
254-
"start_time": "2019-03-23T12:57:32.382116Z"
285+
"end_time": "2019-03-24T19:20:34.971855Z",
286+
"start_time": "2019-03-24T19:20:31.138664Z"
255287
}
256288
},
257289
"outputs": [
@@ -284,29 +316,11 @@
284316
},
285317
{
286318
"cell_type": "code",
287-
"execution_count": 7,
288-
"metadata": {
289-
"ExecuteTime": {
290-
"end_time": "2019-03-23T12:57:33.259451Z",
291-
"start_time": "2019-03-23T12:57:33.254435Z"
292-
}
293-
},
294-
"outputs": [],
295-
"source": [
296-
"# Create tesseract API\n",
297-
"if api:\n",
298-
" tesseract.TessBaseAPIEnd(api)\n",
299-
" tesseract.TessBaseAPIDelete(api)\n",
300-
"api = tesseract.TessBaseAPICreate()"
301-
]
302-
},
303-
{
304-
"cell_type": "code",
305-
"execution_count": 8,
319+
"execution_count": 9,
306320
"metadata": {
307321
"ExecuteTime": {
308-
"end_time": "2019-03-23T12:57:33.956304Z",
309-
"start_time": "2019-03-23T12:57:33.902158Z"
322+
"end_time": "2019-03-24T19:20:51.661233Z",
323+
"start_time": "2019-03-24T19:20:51.292247Z"
310324
}
311325
},
312326
"outputs": [
@@ -331,6 +345,24 @@
331345
"print(f\"Image {filename} has {x_dpi[0]}x{y_dpi[0]} DPI.\")"
332346
]
333347
},
348+
{
349+
"cell_type": "code",
350+
"execution_count": 7,
351+
"metadata": {
352+
"ExecuteTime": {
353+
"end_time": "2019-03-23T12:57:33.259451Z",
354+
"start_time": "2019-03-23T12:57:33.254435Z"
355+
}
356+
},
357+
"outputs": [],
358+
"source": [
359+
"# Create tesseract API\n",
360+
"if api:\n",
361+
" tesseract.TessBaseAPIEnd(api)\n",
362+
" tesseract.TessBaseAPIDelete(api)\n",
363+
"api = tesseract.TessBaseAPICreate()"
364+
]
365+
},
334366
{
335367
"cell_type": "code",
336368
"execution_count": 9,

0 commit comments

Comments
 (0)