15
15
)
16
16
from ocrd_modelfactory import page_from_file
17
17
from ocrd_models .ocrd_page import (
18
- MetadataItemType ,
19
- LabelsType , LabelType ,
20
18
to_xml , AlternativeImageType
21
19
)
22
20
from ocrd import Processor
@@ -76,12 +74,16 @@ def __init__(self, *args, **kwargs):
76
74
kwargs ['ocrd_tool' ] = self .ocrd_tool ['tools' ][TOOL ]
77
75
kwargs ['version' ] = self .ocrd_tool ['version' ]
78
76
super (OcropyBinarize , self ).__init__ (* args , ** kwargs )
79
- LOG = getLogger ('processor.OcropyBinarize' )
80
77
if hasattr (self , 'output_file_grp' ):
81
- if self .parameter ['grayscale' ] and self .parameter ['method' ] != 'ocropy' :
82
- LOG .critical ('requested method %s does not support grayscale normalized output' ,
83
- self .parameter ['method' ])
84
- raise Exception ('only method=ocropy allows grayscale=true' )
78
+ # processing context
79
+ self .setup ()
80
+
81
+ def setup (self ):
82
+ self .logger = getLogger ('processor.OcropyBinarize' )
83
+ if self .parameter ['grayscale' ] and self .parameter ['method' ] != 'ocropy' :
84
+ self .logger .critical ('requested method %s does not support grayscale normalized output' ,
85
+ self .parameter ['method' ])
86
+ raise Exception ('only method=ocropy allows grayscale=true' )
85
87
86
88
def process (self ):
87
89
"""Binarize (and optionally deskew/despeckle) the pages/regions/lines of the workspace.
@@ -105,31 +107,18 @@ def process(self):
105
107
106
108
Produce a new output file by serialising the resulting hierarchy.
107
109
"""
108
- LOG = getLogger ('processor.OcropyBinarize' )
109
110
level = self .parameter ['level-of-operation' ]
110
111
assert_file_grp_cardinality (self .input_file_grp , 1 )
111
112
assert_file_grp_cardinality (self .output_file_grp , 1 )
112
113
113
114
for (n , input_file ) in enumerate (self .input_files ):
114
- LOG .info ("INPUT FILE %i / %s" , n , input_file .pageId or input_file .ID )
115
+ self . logger .info ("INPUT FILE %i / %s" , n , input_file .pageId or input_file .ID )
115
116
file_id = make_file_id (input_file , self .output_file_grp )
116
117
117
118
pcgts = page_from_file (self .workspace .download_file (input_file ))
119
+ self .add_metadata (pcgts )
118
120
page_id = pcgts .pcGtsId or input_file .pageId or input_file .ID # (PageType has no id)
119
121
page = pcgts .get_Page ()
120
-
121
- # add metadata about this operation and its runtime parameters:
122
- metadata = pcgts .get_Metadata () # ensured by from_file()
123
- metadata .add_MetadataItem (
124
- MetadataItemType (type_ = "processingStep" ,
125
- name = self .ocrd_tool ['steps' ][0 ],
126
- value = TOOL ,
127
- Labels = [LabelsType (
128
- externalModel = "ocrd-tool" ,
129
- externalId = "parameters" ,
130
- Label = [LabelType (type_ = name ,
131
- value = self .parameter [name ])
132
- for name in self .parameter .keys ()])]))
133
122
134
123
page_image , page_xywh , page_image_info = self .workspace .image_from_page (
135
124
page , page_id , feature_filter = 'binarized' )
@@ -139,7 +128,7 @@ def process(self):
139
128
dpi = page_image_info .resolution
140
129
if page_image_info .resolutionUnit == 'cm' :
141
130
dpi *= 2.54
142
- LOG .info ('Page "%s" uses %f DPI' , page_id , dpi )
131
+ self . logger .info ('Page "%s" uses %f DPI' , page_id , dpi )
143
132
zoom = 300.0 / dpi
144
133
else :
145
134
zoom = 1
@@ -148,10 +137,12 @@ def process(self):
148
137
self .process_page (page , page_image , page_xywh , zoom ,
149
138
input_file .pageId , file_id )
150
139
else :
151
- regions = page .get_TextRegion () + (
152
- page .get_TableRegion () if level == 'region' else [])
140
+ if level == 'table' :
141
+ regions = page .get_TableRegion ()
142
+ else : # region
143
+ regions = page .get_AllRegions (classes = ['Text' ])
153
144
if not regions :
154
- LOG .warning ('Page "%s" contains no text regions' , page_id )
145
+ self . logger .warning ('Page "%s" contains no text regions' , page_id )
155
146
for region in regions :
156
147
region_image , region_xywh = self .workspace .image_from_segment (
157
148
region , page_image , page_xywh , feature_filter = 'binarized' )
@@ -161,7 +152,8 @@ def process(self):
161
152
continue
162
153
lines = region .get_TextLine ()
163
154
if not lines :
164
- LOG .warning ('Page "%s" region "%s" contains no text lines' , page_id , region .id )
155
+ self .logger .warning ('Page "%s" region "%s" contains no text lines' ,
156
+ page_id , region .id )
165
157
for line in lines :
166
158
line_image , line_xywh = self .workspace .image_from_segment (
167
159
line , region_image , region_xywh , feature_filter = 'binarized' )
@@ -179,12 +171,11 @@ def process(self):
179
171
local_filename = file_path ,
180
172
mimetype = MIMETYPE_PAGE ,
181
173
content = to_xml (pcgts ))
182
- LOG .info ('created file ID: %s, file_grp: %s, path: %s' ,
183
- file_id , self .output_file_grp , out .local_filename )
174
+ self . logger .info ('created file ID: %s, file_grp: %s, path: %s' ,
175
+ file_id , self .output_file_grp , out .local_filename )
184
176
185
177
def process_page (self , page , page_image , page_xywh , zoom , page_id , file_id ):
186
- LOG = getLogger ('processor.OcropyBinarize' )
187
- LOG .info ("About to binarize page '%s'" , page_id )
178
+ self .logger .info ("About to binarize page '%s'" , page_id )
188
179
features = page_xywh ['features' ]
189
180
if 'angle' in page_xywh and page_xywh ['angle' ]:
190
181
# orientation has already been annotated (by previous deskewing),
@@ -229,8 +220,7 @@ def process_page(self, page, page_image, page_xywh, zoom, page_id, file_id):
229
220
comments = features ))
230
221
231
222
def process_region (self , region , region_image , region_xywh , zoom , page_id , file_id ):
232
- LOG = getLogger ('processor.OcropyBinarize' )
233
- LOG .info ("About to binarize page '%s' region '%s'" , page_id , region .id )
223
+ self .logger .info ("About to binarize page '%s' region '%s'" , page_id , region .id )
234
224
features = region_xywh ['features' ]
235
225
if 'angle' in region_xywh and region_xywh ['angle' ]:
236
226
# orientation has already been annotated (by previous deskewing),
@@ -277,9 +267,8 @@ def process_region(self, region, region_image, region_xywh, zoom, page_id, file_
277
267
comments = features ))
278
268
279
269
def process_line (self , line , line_image , line_xywh , zoom , page_id , region_id , file_id ):
280
- LOG = getLogger ('processor.OcropyBinarize' )
281
- LOG .info ("About to binarize page '%s' region '%s' line '%s'" ,
282
- page_id , region_id , line .id )
270
+ self .logger .info ("About to binarize page '%s' region '%s' line '%s'" ,
271
+ page_id , region_id , line .id )
283
272
features = line_xywh ['features' ]
284
273
bin_image , angle = binarize (line_image ,
285
274
method = self .parameter ['method' ],
@@ -294,8 +283,8 @@ def process_line(self, line, line_image, line_xywh, zoom, page_id, region_id, fi
294
283
#orientation = -angle
295
284
#orientation = 180 - (180 - orientation) % 360 # map to [-179.999,180]
296
285
#line.set_orientation(orientation) # does not exist on line level!
297
- LOG .warning ("cannot add orientation %.2f to page '%s' region '%s' line '%s'" ,
298
- - angle , page_id , region_id , line .id )
286
+ self . logger .warning ("cannot add orientation %.2f to page '%s' region '%s' line '%s'" ,
287
+ - angle , page_id , region_id , line .id )
299
288
bin_image = remove_noise (bin_image ,
300
289
maxsize = self .parameter ['noise_maxsize' ])
301
290
if self .parameter ['noise_maxsize' ]:
0 commit comments