Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract-c_api-demo.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 # Copyright 2012 Zdenko Podobný
5 # Author: Zdenko Podobný
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # http://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 
19 """
20 Simple python demo script of tesseract-ocr 3.02 c-api
21 """
22 
23 import os
24 import sys
25 import ctypes
26 
27 # Demo variables
28 lang = "eng"
29 filename = "../phototest.tif"
30 libpath = "/usr/local/lib64/"
31 libpath_w = "../vs2008/DLL_Release/"
32 
33 if sys.platform == "win32":
34  libname = libpath_w + "libtesseract302.dll"
35  libname_alt = "libtesseract302.dll"
36  os.environ["PATH"] += os.pathsep + libpath_w
37 else:
38  libname = libpath + "libtesseract.so.3.0.2"
39  libname_alt = "libtesseract.so.3"
40 
41 try:
42  tesseract = ctypes.cdll.LoadLibrary(libname)
43 except:
44  try:
45  tesseract = ctypes.cdll.LoadLibrary(libname_alt)
46  except WindowsError, err:
47  print("Trying to load '%s'..." % libname)
48  print("Trying to load '%s'..." % libname_alt)
49  print(err)
50  exit(1)
51 
52 tesseract.TessVersion.restype = ctypes.c_char_p
53 tesseract_version = tesseract.TessVersion()
54 
55 # We need to check library version because libtesseract.so.3 is symlink
56 # and can point to other version than 3.02
57 if float(tesseract_version) < 3.02:
58  print("Found tesseract-ocr library version %s." % tesseract_version)
59  print("C-API is present only in version 3.02!")
60  exit(2)
61 
62 api = tesseract.TessBaseAPICreate()
63 rc = tesseract.TessBaseAPIInit3(api, "", lang);
64 if (rc):
65  tesseract.TessBaseAPIDelete(api)
66  print("Could not initialize tesseract.\n")
67  exit(3)
68 
69 text_out = tesseract.TessBaseAPIProcessPages(api, filename, None , 0);
70 result_text = ctypes.string_at(text_out)
71 print result_text