Add verbosity option, fix regression with separation of lines/paragraphs in terminal text output with coordinate-enabled engines

This commit is contained in:
AuroraWright
2025-09-20 11:52:26 +02:00
parent bf9debd479
commit de760458a9
4 changed files with 23 additions and 13 deletions

View File

@@ -50,8 +50,10 @@ parser.add_argument('-sc', '--screen_capture_combo', type=str, default=argparse.
help='When reading with screen capture, combo to wait on for taking a screenshot instead of using the delay. As an example: "<ctrl>+<shift>+s". The list of keys can be found here: https://pynput.readthedocs.io/en/latest/keyboard.html#pynput.keyboard.Key')
parser.add_argument('-l', '--language', type=str, default=argparse.SUPPRESS,
help='Two letter language code for filtering screencapture OCR results. Ex. "ja" for Japanese, "zh" for Chinese, "ko" for Korean, "ar" for Arabic, "ru" for Russian, "el" for Greek, "he" for Hebrew, "th" for Thai. Any other value will use Latin Extended (for most European languages and English).')
parser.add_argument('-of', '--output_format', type=str, default=argparse.SUPPRESS, choices=['text', 'json'],
parser.add_argument('-of', '--output_format', type=str, default=argparse.SUPPRESS,
help='The output format for OCR results. Can be "text" (default) or "json" (to include coordinates).')
parser.add_argument('-v', '--verbosity', type=int, default=argparse.SUPPRESS,
help='Terminal window verbosity. Can be -2 (all recognized text is showed whole, default), -1 (only timestamps are shown), 0 (nothing is shown but errors), or larger than 0 to cut displayed text to that amount of characters.')
class Config:
has_config = False
@@ -82,7 +84,8 @@ class Config:
'screen_capture_combo': '',
'screen_capture_old_macos_api': False,
'language': 'ja',
'output_format': 'text'
'output_format': 'text',
'verbosity': -2
}
def __parse(self, value):

View File

@@ -85,6 +85,7 @@ try:
except:
optimized_png_encode = False
@dataclass
class BoundingBox:
"""
@@ -133,7 +134,6 @@ class OcrResult:
def empty_post_process(text):
return text
def post_process(text):
text = ' '.join([''.join(i.split()) for i in text.splitlines()])
text = text.replace('', '...')
@@ -141,7 +141,6 @@ def post_process(text):
text = jaconv.h2z(text, ascii=True, digit=True)
return text
def input_to_pil_image(img):
is_path = False
if isinstance(img, Image.Image):
@@ -159,7 +158,6 @@ def input_to_pil_image(img):
raise ValueError(f'img must be a path, PIL.Image or bytes object, instead got: {img}')
return pil_image, is_path
def pil_image_to_bytes(img, img_format='png', png_compression=6, jpeg_quality=80, optimize=False):
if img_format == 'png' and optimized_png_encode and not optimize:
raw_data = img.convert('RGBA').tobytes()
@@ -172,11 +170,9 @@ def pil_image_to_bytes(img, img_format='png', png_compression=6, jpeg_quality=80
image_bytes = image_bytes.getvalue()
return image_bytes
def pil_image_to_numpy_array(img):
return np.array(img.convert('RGBA'))
def limit_image_size(img, max_size):
img_bytes = pil_image_to_bytes(img)
if len(img_bytes) <= max_size:
@@ -745,7 +741,6 @@ class AppleVision:
def _preprocess(self, img):
return pil_image_to_bytes(img, 'tiff')
class AppleLiveText:
name = 'alivetext'
readable_name = 'Apple Live Text'
@@ -883,7 +878,6 @@ class AppleLiveText:
ns_image = NSImage.alloc().initWithData_(ns_data)
return ns_image
class WinRTOCR:
name = 'winrtocr'
readable_name = 'WinRT OCR'

View File

@@ -825,6 +825,7 @@ def process_and_write_results(img_or_path, last_result, filtering, notify):
return orig_text
output_format = config.get_general('output_format')
verbosity = config.get_general('verbosity')
output_string = ''
log_message = ''
@@ -838,6 +839,7 @@ def process_and_write_results(img_or_path, last_result, filtering, notify):
full_text_parts.append(w.text)
if w.separator:
full_text_parts.append(w.separator)
full_text_parts.append('\n')
unprocessed_text = "".join(full_text_parts)
if output_format == 'json':
@@ -862,7 +864,15 @@ def process_and_write_results(img_or_path, last_result, filtering, notify):
output_string = post_process(unprocessed_text)
log_message = output_string
logger.opt(ansi=True).info(f'Text recognized in {end_time - start_time:0.03f}s using <{engine_color}>{engine_instance.readable_name}</{engine_color}>: {log_message}')
if verbosity != 0:
if verbosity < -1:
log_message_terminal = ': ' + log_message
elif verbosity == -1:
log_message_terminal = ''
else:
log_message_terminal = ': ' + (log_message if len(log_message) <= verbosity else log_message[:verbosity] + '[...]')
logger.opt(ansi=True).info(f'Text recognized in {end_time - start_time:0.03f}s using <{engine_color}>{engine_instance.readable_name}</{engine_color}>{log_message_terminal}')
if notify and config.get_general('notifications'):
notifier.send(title='owocr', message='Text recognized: ' + log_message, urgency=get_notification_urgency())