Commit 771b1494 authored by Jerome Flesch's avatar Jerome Flesch

Tesseract C-API: Fix (again) detection_orientation(), by using TessBaseAPIDetectOS()

Signed-off-by: Jerome Flesch's avatarJerome Flesch <jflesch@gmail.com>
parent 06d1b6e9
......@@ -35,17 +35,6 @@ __all__ = [
]
class TesseractError(Exception):
"""
Exception raised when Tesseract fails.
"""
def __init__(self, status, message):
Exception.__init__(self, message)
self.status = status
self.message = message
self.args = (status, message)
def can_detect_orientation():
return True
......@@ -53,30 +42,21 @@ def can_detect_orientation():
def detect_orientation(image, lang=None):
handle = tesseract_raw.init(lang=lang)
try:
#tesseract_raw.set_page_seg_mode(
# handle, tesseract_raw.PageSegMode.OSD_ONLY
#)
tesseract_raw.set_page_seg_mode(
handle, tesseract_raw.PageSegMode.OSD_ONLY
)
tesseract_raw.set_image(handle, image)
tesseract_raw.recognize(handle)
page_iterator = tesseract_raw.analyse_layout(handle)
if page_iterator is None:
raise TesseractError(
"failed", "Orientation detection failed. No script ?"
)
try:
orientation = tesseract_raw.page_iterator_orientation(page_iterator)
angle = {
tesseract_raw.Orientation.PAGE_UP: 0,
tesseract_raw.Orientation.PAGE_RIGHT: 90,
tesseract_raw.Orientation.PAGE_DOWN: 180,
tesseract_raw.Orientation.PAGE_LEFT: 270
}[orientation['orientation']]
return {
"angle": angle,
# TODO: confidence
}
finally:
tesseract_raw.page_iterator_delete(page_iterator)
os = tesseract_raw.detect_os(handle)
orientation = {
tesseract_raw.Orientation.PAGE_UP: 0,
tesseract_raw.Orientation.PAGE_RIGHT: 90,
tesseract_raw.Orientation.PAGE_DOWN: 180,
tesseract_raw.Orientation.PAGE_LEFT: 270,
}[os['orientation']]
return {
'angle': orientation,
'confidence': os['confidence']
}
finally:
tesseract_raw.cleanup(handle)
......
......@@ -2,6 +2,7 @@ import ctypes
import os
import sys
TESSDATA_PREFIX = os.getenv('TESSDATA_PREFIX', None)
if sys.platform[:3] == "win":
......@@ -25,6 +26,56 @@ for libname in libnames:
except OSError:
pass
class TesseractError(Exception):
"""
Exception raised when Tesseract fails.
"""
def __init__(self, status, message):
Exception.__init__(self, message)
self.status = status
self.message = message
self.args = (status, message)
class PageSegMode(object):
OSD_ONLY = 0
AUTO_OSD = 1
AUTO_ONLY = 2
AUTO = 3
SINGLE_COLUMN = 4
SINGLE_BLOCK_VERT_TEXT = 5
SINGLE_BLOCK = 6
SINGLE_LINE = 7
SINGLE_WORD = 8
CIRCLE_WORD = 9
SINGLE_CHAR = 10
SPARSE_TEXT = 11
SPARSE_TEXT_OSD = 12
COUNT = 13
class Orientation(object):
PAGE_UP = 0
PAGE_RIGHT = 1
PAGE_DOWN = 2
PAGE_LEFT = 3
class OSResults(ctypes.Structure):
_fields_ = [
("orientations", ctypes.c_float * 4),
("scripts_na", ctypes.c_float * 4 * (116 + 1 + 2 + 1)),
("unicharset", ctypes.c_void_p),
("best_orientation_id", ctypes.c_int),
("best_script_id", ctypes.c_int),
("best_sconfidence", ctypes.c_float),
("best_oconfidence", ctypes.c_float),
# extra padding in case the structure is extended later
("padding", ctypes.c_char * 512),
]
if g_libtesseract:
g_libtesseract.TessVersion.argtypes = []
g_libtesseract.TessVersion.restype = ctypes.c_char_p
......@@ -101,29 +152,11 @@ if g_libtesseract:
]
g_libtesseract.TessPageIteratorOrientation.restype = None
class PageSegMode(object):
OSD_ONLY = 0
AUTO_OSD = 1
AUTO_ONLY = 2
AUTO = 3
SINGLE_COLUMN = 4
SINGLE_BLOCK_VERT_TEXT = 5
SINGLE_BLOCK = 6
SINGLE_LINE = 7
SINGLE_WORD = 8
CIRCLE_WORD = 9
SINGLE_CHAR = 10
SPARSE_TEXT = 11
SPARSE_TEXT_OSD = 12
COUNT = 13
class Orientation(object):
PAGE_UP = 0
PAGE_RIGHT = 1
PAGE_DOWN = 2
PAGE_LEFT = 3
g_libtesseract.TessBaseAPIDetectOS.argstype = [
ctypes.c_void_p, # TessBaseAPI*
ctypes.POINTER(OSResults),
]
g_libtesseract.TessBaseAPIDetectOS.restype = ctypes.c_bool
def init(lang=None):
......@@ -262,3 +295,17 @@ def page_iterator_orientation(iterator):
"textline_order": textline_order.value,
"deskew_angle": deskew_angle.value,
}
def detect_os(handle):
global g_libtesseract
assert(g_libtesseract)
results = OSResults()
r = g_libtesseract.TessBaseAPIDetectOS(handle, ctypes.pointer(results))
if not r:
raise TesseractError("TessBaseAPIDetectOS() failed")
return {
"orientation": results.best_orientation_id,
"confidence": results.best_oconfidence,
}
......@@ -264,8 +264,8 @@ def get_all_tests():
'test_european',
'test_french',
]
#tests = unittest.TestSuite(map(TestTxt, test_names))
#all_tests.addTest(tests)
tests = unittest.TestSuite(map(TestTxt, test_names))
all_tests.addTest(tests)
test_names = [
'test_basic',
......@@ -274,10 +274,10 @@ def get_all_tests():
'test_japanese',
'test_write_read',
]
#tests = unittest.TestSuite(map(TestWordBox, test_names))
#all_tests.addTest(tests)
#tests = unittest.TestSuite(map(TestLineBox, test_names))
#all_tests.addTest(tests)
tests = unittest.TestSuite(map(TestWordBox, test_names))
all_tests.addTest(tests)
tests = unittest.TestSuite(map(TestLineBox, test_names))
all_tests.addTest(tests)
test_names = [
'test_can_detect_orientation',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment