move logging to CLI and make initialization optional

This commit is contained in:
kba 2025-09-18 13:07:41 +02:00
parent 8ebba5ac04
commit c64d102613
3 changed files with 52 additions and 16 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ models_eynollah*
output.html output.html
/build /build
/dist /dist
*.tif

View file

@ -1,5 +1,6 @@
import sys import sys
import click import click
import logging
from ocrd_utils import initLogging, getLevelName, getLogger from ocrd_utils import initLogging, getLevelName, getLogger
from eynollah.eynollah import Eynollah, Eynollah_ocr from eynollah.eynollah import Eynollah, Eynollah_ocr
from eynollah.sbb_binarize import SbbBinarizer from eynollah.sbb_binarize import SbbBinarizer
@ -241,14 +242,60 @@ def binarization(patches, model_dir, input_image, output_image, dir_in, dir_out)
is_flag=True, is_flag=True,
help="if this parameter set to true, this tool will ignore layout detection and reading order. It means that textline detection will be done within printspace and contours of textline will be written in xml output file.", help="if this parameter set to true, this tool will ignore layout detection and reading order. It means that textline detection will be done within printspace and contours of textline will be written in xml output file.",
) )
# TODO move to top-level CLI context
@click.option( @click.option(
"--log_level", "--log_level",
"-l", "-l",
type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']), type=click.Choice(['OFF', 'DEBUG', 'INFO', 'WARN', 'ERROR']),
help="Override log level globally to this", help="Override 'eynollah' log level globally to this",
)
#
@click.option(
"--setup-logging",
is_flag=True,
help="Setup a basic console logger",
) )
def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_deskewed, save_all, extract_only_images, save_page, enable_plotting, allow_enhancement, curved_line, textline_light, full_layout, tables, right2left, input_binary, allow_scaling, headers_off, light_version, reading_order_machine_based, do_ocr, num_col_upper, num_col_lower, skip_layout_and_reading_order, ignore_page_extraction, log_level): def layout(
image,
out,
overwrite,
dir_in,
model,
save_images,
save_layout,
save_deskewed,
save_all,
extract_only_images,
save_page,
enable_plotting,
allow_enhancement,
curved_line,
textline_light,
full_layout,
tables,
right2left,
input_binary,
allow_scaling,
headers_off,
light_version,
reading_order_machine_based,
do_ocr,
num_col_upper,
num_col_lower,
skip_layout_and_reading_order,
ignore_page_extraction,
log_level,
setup_logging
):
if setup_logging:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(formatter)
getLogger('eynollah').addHandler(console_handler)
getLogger('eynollah').setLevel(logging.INFO)
else:
initLogging() initLogging()
if log_level: if log_level:
getLogger('eynollah').setLevel(getLevelName(log_level)) getLogger('eynollah').setLevel(getLevelName(log_level))
@ -273,7 +320,6 @@ def layout(image, out, overwrite, dir_in, model, save_images, save_layout, save_
assert image or dir_in, "Either a single image -i or a dir_in -di is required" assert image or dir_in, "Either a single image -i or a dir_in -di is required"
eynollah = Eynollah( eynollah = Eynollah(
model, model,
logger=getLogger('eynollah'),
dir_out=out, dir_out=out,
dir_of_cropped_images=save_images, dir_of_cropped_images=save_images,
extract_only_images=extract_only_images, extract_only_images=extract_only_images,

View file

@ -6,7 +6,6 @@
document layout analysis (segmentation) with output in PAGE-XML document layout analysis (segmentation) with output in PAGE-XML
""" """
from logging import Logger
from difflib import SequenceMatcher as sq from difflib import SequenceMatcher as sq
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
import math import math
@ -201,18 +200,8 @@ class Eynollah:
num_col_upper : Optional[int] = None, num_col_upper : Optional[int] = None,
num_col_lower : Optional[int] = None, num_col_lower : Optional[int] = None,
skip_layout_and_reading_order : bool = False, skip_layout_and_reading_order : bool = False,
logger : Optional[Logger] = None,
): ):
if logger:
self.logger = logger
else:
self.logger = getLogger('eynollah') self.logger = getLogger('eynollah')
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(message)s')
console_handler.setFormatter(formatter)
self.logger.addHandler(console_handler)
self.logger.setLevel(logging.INFO)
if skip_layout_and_reading_order: if skip_layout_and_reading_order:
textline_light = True textline_light = True