1
|
|
|
""" |
2
|
|
|
Utility functions and constants usable in various circumstances. |
3
|
|
|
|
4
|
|
|
* ``coordinates_of_segment``, ``coordinates_for_segment`` |
5
|
|
|
|
6
|
|
|
These functions convert polygon outlines for PAGE elements on all hierarchy |
7
|
|
|
levels below page (i.e. region, line, word, glyph) between relative coordinates |
8
|
|
|
w.r.t. a corresponding image and absolute coordinates w.r.t. the top-level image. |
9
|
|
|
This includes rotation and offset correction, based on affine transformations. |
10
|
|
|
(Used by ``Workspace`` methods ``image_from_page`` and ``image_from_segment``) |
11
|
|
|
|
12
|
|
|
* ``rotate_coordinates``, ``shift_coordinates``, ``transpose_coordinates``, ``transform_coordinates`` |
13
|
|
|
|
14
|
|
|
These backend functions compose affine transformations for reflection, rotation |
15
|
|
|
and offset correction of coordinates, or apply them to a set of points. They can be |
16
|
|
|
used to pass down the coordinate system along with images (both invariably sharing |
17
|
|
|
the same operations context) when traversing the element hierarchy top to bottom. |
18
|
|
|
(Used by ``Workspace`` methods ``image_from_page`` and ``image_from_segment``). |
19
|
|
|
|
20
|
|
|
* ``rotate_image``, ``crop_image``, ``transpose_image`` |
21
|
|
|
|
22
|
|
|
These PIL.Image functions are safe replacements for the ``rotate``, ``crop``, and |
23
|
|
|
``transpose`` methods. |
24
|
|
|
|
25
|
|
|
* ``image_from_polygon``, ``polygon_mask`` |
26
|
|
|
|
27
|
|
|
These functions apply polygon masks to PIL.Image objects. |
28
|
|
|
|
29
|
|
|
* ``xywh_from_points``, ``points_from_xywh``, ``polygon_from_points`` etc. |
30
|
|
|
|
31
|
|
|
These functions have the syntax ``X_from_Y``, where ``X``/``Y`` can be |
32
|
|
|
|
33
|
|
|
* ``bbox`` is a 4-tuple of integers x0, y0, x1, y1 of the bounding box (rectangle) |
34
|
|
|
|
35
|
|
|
(used by PIL.Image) |
36
|
|
|
* ``points`` a string encoding a polygon: ``"0,0 100,0 100,100, 0,100"`` |
37
|
|
|
|
38
|
|
|
(used by PAGE-XML) |
39
|
|
|
* ``polygon`` is a list of 2-lists of integers x, y of points forming an (implicitly closed) polygon path: ``[[0,0], [100,0], [100,100], [0,100]]`` |
40
|
|
|
|
41
|
|
|
(used by opencv2 and higher-level coordinate functions in ocrd_utils) |
42
|
|
|
* ``xywh`` a dict with keys for x, y, width and height: ``{'x': 0, 'y': 0, 'w': 100, 'h': 100}`` |
43
|
|
|
|
44
|
|
|
(produced by tesserocr and image/coordinate recursion methods in ocrd.workspace) |
45
|
|
|
* ``x0y0x1y1`` is a 4-list of strings ``x0``, ``y0``, ``x1``, ``y1`` of the bounding box (rectangle) |
46
|
|
|
|
47
|
|
|
(produced by tesserocr) |
48
|
|
|
* ``y0x0y1x1`` is the same as ``x0y0x1y1`` with positions of ``x`` and ``y`` in the list swapped |
49
|
|
|
|
50
|
|
|
* ``is_local_filename``, ``safe_filename``, ``abspath``, ``get_local_filename`` |
51
|
|
|
|
52
|
|
|
FS-related utilities |
53
|
|
|
|
54
|
|
|
* ``is_string``, ``membername``, ``concat_padded``, ``nth_url_segment``, ``remove_non_path_from_url``, ``parse_json_string_with_comments``, ``parse_json_string_or_file``, ``set_json_key_value_overrides``, ``assert_file_grp_cardinality``, ``make_file_id`` |
55
|
|
|
|
56
|
|
|
String and OOP utilities |
57
|
|
|
|
58
|
|
|
* ``MIMETYPE_PAGE``, ``EXT_TO_MIME``, ``MIME_TO_EXT``, ``VERSION`` |
59
|
|
|
|
60
|
|
|
Constants |
61
|
|
|
|
62
|
|
|
* ``logging``, ``setOverrideLogLevel``, ``getLevelName``, ``getLogger``, ``initLogging`` |
63
|
|
|
|
64
|
|
|
Exports of ocrd_utils.logging |
65
|
|
|
|
66
|
|
|
* ``deprecated_alias`` |
67
|
|
|
|
68
|
|
|
Decorator to mark a kwarg as deprecated |
69
|
|
|
""" |
70
|
|
|
|
71
|
|
|
from .constants import ( |
72
|
|
|
VERSION, |
73
|
|
|
MIMETYPE_PAGE, |
74
|
|
|
EXT_TO_MIME, |
75
|
|
|
MIME_TO_EXT, |
76
|
|
|
PIL_TO_MIME, |
77
|
|
|
MIME_TO_PIL, |
78
|
|
|
REGEX_PREFIX, |
79
|
|
|
LOG_FORMAT, |
80
|
|
|
LOG_TIMEFMT) |
81
|
|
|
|
82
|
|
|
from .deprecate import ( |
83
|
|
|
deprecated_alias) |
84
|
|
|
|
85
|
|
|
from .image import ( |
86
|
|
|
adjust_canvas_to_rotation, |
87
|
|
|
adjust_canvas_to_transposition, |
88
|
|
|
bbox_from_points, |
89
|
|
|
bbox_from_polygon, |
90
|
|
|
bbox_from_xywh, |
91
|
|
|
coordinates_for_segment, |
92
|
|
|
coordinates_of_segment, |
93
|
|
|
crop_image, |
94
|
|
|
image_from_polygon, |
95
|
|
|
points_from_bbox, |
96
|
|
|
points_from_polygon, |
97
|
|
|
points_from_x0y0x1y1, |
98
|
|
|
points_from_xywh, |
99
|
|
|
points_from_y0x0y1x1, |
100
|
|
|
polygon_from_bbox, |
101
|
|
|
polygon_from_points, |
102
|
|
|
polygon_from_x0y0x1y1, |
103
|
|
|
polygon_from_xywh, |
104
|
|
|
polygon_mask, |
105
|
|
|
rotate_coordinates, |
106
|
|
|
rotate_image, |
107
|
|
|
shift_coordinates, |
108
|
|
|
transform_coordinates, |
109
|
|
|
transpose_coordinates, |
110
|
|
|
transpose_image, |
111
|
|
|
xywh_from_bbox, |
112
|
|
|
xywh_from_points, |
113
|
|
|
xywh_from_polygon) |
114
|
|
|
|
115
|
|
|
from .introspect import ( |
116
|
|
|
set_json_key_value_overrides, |
117
|
|
|
membername) |
118
|
|
|
|
119
|
|
|
from .logging import ( |
120
|
|
|
logging, |
121
|
|
|
getLogger, |
122
|
|
|
getLevelName, |
123
|
|
|
initLogging, |
124
|
|
|
setOverrideLogLevel) |
125
|
|
|
|
126
|
|
|
from .os import ( |
127
|
|
|
abspath, |
128
|
|
|
pushd_popd, |
129
|
|
|
unzip_file_to_dir) |
130
|
|
|
|
131
|
|
|
from .str import ( |
132
|
|
|
assert_file_grp_cardinality, |
133
|
|
|
concat_padded, |
134
|
|
|
get_local_filename, |
135
|
|
|
is_local_filename, |
136
|
|
|
is_string, |
137
|
|
|
make_file_id, |
138
|
|
|
nth_url_segment, |
139
|
|
|
parse_json_string_or_file, |
140
|
|
|
parse_json_string_with_comments, |
141
|
|
|
remove_non_path_from_url, |
142
|
|
|
safe_filename) |
143
|
|
|
|