Skip to content

Commit bdde6fd

Browse files
bertskyRobert Sachunsky
andauthored
place long running API calls in cysignals blocks (#384)
* place long running API calls in cysignals blocks * add cysignals runtime dependency --------- Co-authored-by: Robert Sachunsky <sachunsky@informatik.uni-leipzig.de>
1 parent 924478e commit bdde6fd

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
cysignals
12
Pillow

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ def finalize_options(self):
318318
cmdclass={"build_ext": my_build_ext},
319319
ext_modules=[make_extension()],
320320
test_suite="tests",
321-
setup_requires=["Cython>=3.0.0,<3.2.0"],
321+
setup_requires=["Cython>=3.0.0,<3.2.0", "cysignals"],
322+
install_requires=["cysignals"],
322323
packages=["tesserocr"],
323324
package_dir={"tesserocr": "tesserocr"},
324325
)

tesserocr/tesserocr.pyx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ from libcpp.pair cimport pair
3939
from libcpp.vector cimport vector
4040
from cython.operator cimport preincrement as inc, dereference as deref
4141
from cpython.version cimport PY_MAJOR_VERSION
42+
from cysignals.signals cimport sig_on, sig_off
4243

4344

4445
_LOGGER = logging.getLogger("tesserocr")
@@ -2073,8 +2074,11 @@ cdef class PyTessBaseAPI:
20732074
blockids_addr = NULL
20742075
if not paraids:
20752076
paraids_addr = NULL
2076-
boxa = self._baseapi.GetComponentImages(level, text_only, raw_image, raw_padding,
2077-
&pixa, blockids_addr, paraids_addr)
2077+
with nogil:
2078+
sig_on()
2079+
boxa = self._baseapi.GetComponentImages(level, text_only, raw_image, raw_padding,
2080+
&pixa, blockids_addr, paraids_addr)
2081+
sig_off()
20782082
if boxa == NULL:
20792083
# no components found
20802084
return []
@@ -2122,7 +2126,9 @@ cdef class PyTessBaseAPI:
21222126
"""
21232127
cdef PageIterator *piter
21242128
with nogil:
2129+
sig_on()
21252130
piter = self._baseapi.AnalyseLayout(merge_similar_words)
2131+
sig_off()
21262132
if piter == NULL:
21272133
return None
21282134
return PyPageIterator.createPageIterator(piter)
@@ -2145,13 +2151,15 @@ cdef class PyTessBaseAPI:
21452151
ETEXT_DESC monitor
21462152
int res
21472153
with nogil:
2154+
sig_on()
21482155
if timeout > 0:
21492156
monitor.cancel = NULL
21502157
monitor.cancel_this = NULL
21512158
monitor.set_deadline_msecs(timeout)
21522159
res = self._baseapi.Recognize(&monitor)
21532160
else:
21542161
res = self._baseapi.Recognize(NULL)
2162+
sig_off()
21552163
return res == 0
21562164

21572165
"""Methods to retrieve information after :meth:`SetImage`,
@@ -2164,13 +2172,15 @@ cdef class PyTessBaseAPI:
21642172
ETEXT_DESC monitor
21652173
int res
21662174
with nogil:
2175+
sig_on()
21672176
if timeout > 0:
21682177
monitor.cancel = NULL
21692178
monitor.cancel_this = NULL
21702179
monitor.set_deadline_msecs(timeout)
21712180
res = self._baseapi.RecognizeForChopTest(&monitor)
21722181
else:
21732182
res = self._baseapi.RecognizeForChopTest(NULL)
2183+
sig_off()
21742184
return res == 0
21752185

21762186
cdef TessResultRenderer *_get_renderer(self, cchar_t *outputbase):
@@ -2370,7 +2380,9 @@ cdef class PyTessBaseAPI:
23702380
"""Return the recognized text coded as UTF-8 from the image."""
23712381
cdef char *text
23722382
with nogil:
2383+
sig_on()
23732384
text = self._baseapi.GetUTF8Text()
2385+
sig_off()
23742386
self._destroy_pix()
23752387
if text == NULL:
23762388
with gil:
@@ -2710,7 +2722,9 @@ def image_to_text(image, lang=_DEFAULT_LANG, PageSegMode psm=PSM_AUTO,
27102722
if pix == NULL:
27112723
with gil:
27122724
raise RuntimeError('Failed to read picture')
2725+
sig_on()
27132726
text = _image_to_text(pix, clang, psm, cpath, oem)
2727+
sig_off()
27142728
if text == NULL:
27152729
with gil:
27162730
raise RuntimeError(f'Failed to init API, possibly an invalid tessdata path: {path}')
@@ -2755,7 +2769,9 @@ def file_to_text(filename, lang=_DEFAULT_LANG, PageSegMode psm=PSM_AUTO,
27552769
if pix == NULL:
27562770
with gil:
27572771
raise RuntimeError('Failed to read picture')
2772+
sig_on()
27582773
text = _image_to_text(pix, clang, psm, cpath, oem)
2774+
sig_off()
27592775
if text == NULL:
27602776
with gil:
27612777
raise RuntimeError(f'Failed to init API, possibly an invalid tessdata path: {path}')

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ envlist = py39, py310, py311, py312, py313
44
setenv = LC_ALL = C
55
passenv = TESSDATA_PREFIX
66
deps =
7+
cysignals
78
Pillow
89
commands = {envpython} setup.py test

0 commit comments

Comments
 (0)