RecAPI
|
OmniPage CSDK v22.0 has introduced Python API to access KernelAPI, RecAPIPlus and RecPDF functions in Python. The mapping code is autogenerated via SWIG that relies on the C API. See PythonSamples bundled with this CSDK to see the example usages.
Installation
Accessing the Python API requires an extra step after CSDK install. Please locate the available *.whl files under the installation folder (default is shown in these examples).
Using a dos prompt:
Please find the wheel package as a file, e.g. omnipage-22.1-cp37-cp37m-win_amd64.whl that belongs exactly to this CSDK version, and install it manually.
Make sure you have removed any previous install:
Your python main version 3.7.* have to match in this case, according to 37 in the .whl name.
So far it was assumed that python and pip executables can be found in PATH - in case not, or have multiple python environments (picking the right one):
Separate available .whl package belongs to python-3.8.*, python-3.9.* etc...
Samples
After installing the CSDK, Python samples are available here:
Example - current working directory can be anywhere:
Its default input directory is located in the Samples\Inputs folder, and the output files are generated in
Note: color codes are being used by PythonSamples for dos prompt. In case you are not seeing the colors, then you need to open registry, and create in HKCU\Console a DWORD named VirtualTerminalLevel and set it to 0x1.
Notations
As function signatures for Python, we are using PEP 3107 and PEP 484 annotations. The CSDK-specific types (e.g. "RECT") are enclosed within double quotes. Modern Python IDEs are able to use these annotations for static code checks and hints.
Initialization
See kRecInit, RecInitPlus and rPdfInit. All these 3 functions have an optional 3rd parameter with None by default, that means the directory of an installed CSDK binary looked up automatically from the registry. In case you have multiple installations, you may need to specify this path. It is important to call the belonging kRecQuit / RecQuitPlus / rPdfQuit for freeing resources in the end, separately for each 3 type of Init call in reverse order.
Types
When operating on more complex types, SWIG offers special ways for handling them. For simplicity, the enum types are mapped here via int. For their names in Python, please refer to the C-style enum definitions of the C API. But, some of the types are int-like, e.g. REC_COLOR. You can access int-like values via the int function:
There is one special type that needs to be used in a with statement:
This usage is intended to avoid running the garbage collector too early.
Arrays
For CSDK arrays, the mappings between C and Python are possible via SWIG wrappers: "RectArray", "IntArray", ... so any type name having the "...Array" postfix in double quotes. Example:
Type names of arrays applying the same rule as above: RectArray, PointArray, VDICTDESCArray, CombCellInfoArray, LetterArray, UIntArray, IntArray, SizeTArray, IntArray, DoubleArray, ModuleInfoArray, ClassifyInfoArray.
Note that arrays of enums are accessible via IntArray, and other int-like array e.g. array of REC_COLOR is represented via UIntArray.
SizeTArray is the type name in Python API of the array of size_t in C.
Resource handling
Where not mentioned explicitly and no belonging C API function in this document, objects are freed automatically. E.g. the kRecFree function is available only in C, but doesn't exist in Python so no need to care. However, there are a few ones that exist on the Pythin API too, and the user has to call these explicitly:
It is important to call these functions before kRecQuit/RecQuitPlus/rPdfQuit, in case have any handle unfreed.
Numpy ndarray support
In case an input or output picture should be kept in the memory or passed by another module - typically numpy.ndarray -, we have introduced support for passing input/output via these functions:
These functions are not supported in Python at this moment:
Python is supported only with the 64-bit CSDK release, with some of the latest 64-bit Python versions available at CSDK release time. The 32-bit versions are not supported.