File size: 22,521 Bytes
2c67080 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 |
# coding: utf-8
# [Pix2Text](https://github.com/breezedeus/pix2text): an Open-Source Alternative to Mathpix.
# Copyright (C) 2022-2024, [Breezedeus](https://www.breezedeus.com).
import os
import logging
import glob
import json
from multiprocessing import Process
from pprint import pformat
import click
from pix2text import set_logger, Pix2Text
_CONTEXT_SETTINGS = {"help_option_names": ['-h', '--help']}
logger = set_logger(log_level=logging.INFO)
@click.group(context_settings=_CONTEXT_SETTINGS)
def cli():
pass
@cli.command('predict')
@click.option(
"-l",
"--languages",
type=str,
default='en,ch_sim',
help="Language Codes for Text-OCR to recognize, separated by commas",
show_default=True,
)
@click.option(
"--layout-config",
type=str,
default=None,
help="Configuration information for the layout parser model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--mfd-config",
type=str,
default=None,
help="Configuration information for the MFD model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--formula-ocr-config",
type=str,
default=None,
help="Configuration information for the Latex-OCR mathematical formula recognition model. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--text-ocr-config",
type=str,
default=None,
help="Configuration information for Text-OCR recognition, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--enable-formula/--disable-formula",
default=True,
help="Whether to enable formula recognition",
show_default=True,
)
@click.option(
"--enable-table/--disable-table",
default=True,
help="Whether to enable table recognition",
show_default=True,
)
@click.option(
"-d",
"--device",
help="Choose to run the code using `cpu`, `gpu`, or a specific GPU like `cuda:0`",
type=str,
default='cpu',
show_default=True,
)
@click.option(
"--file-type",
type=click.Choice(['pdf', 'page', 'text_formula', 'formula', 'text']),
default='text_formula',
help="Which file type to process, 'pdf', 'page', 'text_formula', 'formula', or 'text'",
show_default=True,
)
@click.option(
"--resized-shape",
help="Resize the image width to this size before processing",
type=int,
default=768,
show_default=True,
)
@click.option(
"-i",
"--img-file-or-dir",
required=True,
help="File path of the input image/pdf or the specified directory",
)
@click.option(
"--save-debug-res",
default=None,
help="If `save_debug_res` is set, the directory to save the debug results; default value is `None`, which means not to save",
show_default=True,
)
@click.option(
"--rec-kwargs",
type=str,
default=None,
help="kwargs for calling .recognize(), in JSON string format",
show_default=True,
)
@click.option(
"--return-text/--no-return-text",
default=True,
help="Whether to return only the text result",
show_default=True,
)
@click.option(
"--auto-line-break/--no-auto-line-break",
default=True,
help="Whether to automatically determine to merge adjacent line results into a single line result",
show_default=True,
)
@click.option(
"-o",
"--output-dir",
default='output-md',
help="Output directory for the recognized text results. Only effective when `file-type` is `pdf` or `page`",
show_default=True,
)
@click.option(
"--log-level",
default='INFO',
help="Log Level, such as `INFO`, `DEBUG`",
show_default=True,
)
def predict(
languages,
layout_config,
mfd_config,
formula_ocr_config,
text_ocr_config,
enable_formula,
enable_table,
device,
file_type,
resized_shape,
img_file_or_dir,
save_debug_res,
rec_kwargs,
return_text,
auto_line_break,
output_dir,
log_level,
):
"""Use Pix2Text (P2T) to predict the text information in an image or PDF."""
logger = set_logger(log_level=log_level)
mfd_config = json.loads(mfd_config) if mfd_config else {}
formula_ocr_config = json.loads(formula_ocr_config) if formula_ocr_config else {}
languages = [lang.strip() for lang in languages.split(',') if lang.strip()]
text_ocr_config = json.loads(text_ocr_config) if text_ocr_config else {}
layout_config = json.loads(layout_config) if layout_config else {}
text_formula_config = {
'languages': languages, # 'en,ch_sim
'mfd': mfd_config,
'formula': formula_ocr_config,
'text': text_ocr_config,
}
total_config = {
'layout': layout_config,
'text_formula': text_formula_config,
}
p2t = Pix2Text.from_config(
total_configs=total_config,
enable_formula=enable_formula,
enable_table=enable_table,
device=device,
)
fp_list = []
if os.path.isfile(img_file_or_dir):
fp_list.append(img_file_or_dir)
if save_debug_res:
save_debug_res = [save_debug_res]
elif os.path.isdir(img_file_or_dir):
fn_list = glob.glob1(img_file_or_dir, '*g')
fp_list = [os.path.join(img_file_or_dir, fn) for fn in fn_list]
if save_debug_res:
os.makedirs(save_debug_res, exist_ok=True)
save_debug_res = [
os.path.join(save_debug_res, 'output-debugs-' + fn) for fn in fn_list
]
else:
raise ValueError(f'{img_file_or_dir} is not a valid file or directory')
rec_kwargs = json.loads(rec_kwargs) if rec_kwargs else {}
rec_kwargs['resized_shape'] = resized_shape
rec_kwargs['return_text'] = return_text
rec_kwargs['auto_line_break'] = auto_line_break
for idx, fp in enumerate(fp_list):
if file_type in ('pdf', 'page'):
rec_kwargs['save_debug_res'] = (
save_debug_res[idx] if save_debug_res is not None else None
)
else:
rec_kwargs['save_analysis_res'] = (
save_debug_res[idx] if save_debug_res is not None else None
)
out = p2t.recognize(fp, file_type=file_type, **rec_kwargs)
if file_type in ('pdf', 'page'):
out = out.to_markdown(output_dir)
logger.info(
f'In image: {fp}\nOuts: \n{out if isinstance(out, str) else pformat(out)}\n'
)
@cli.command('evaluate')
@click.option(
"-l",
"--languages",
type=str,
default='en,ch_sim',
help="Language Codes for Text-OCR to recognize, separated by commas",
show_default=True,
)
@click.option(
"--layout-config",
type=str,
default=None,
help="Configuration information for the layout parser model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--mfd-config",
type=str,
default=None,
help="Configuration information for the MFD model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--formula-ocr-config",
type=str,
default=None,
help="Configuration information for the Latex-OCR mathematical formula recognition model. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--text-ocr-config",
type=str,
default=None,
help="Configuration information for Text-OCR recognition, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--enable-formula/--disable-formula",
default=True,
help="Whether to enable formula recognition",
show_default=True,
)
@click.option(
"--enable-table/--disable-table",
default=True,
help="Whether to enable table recognition",
show_default=True,
)
@click.option(
"-d",
"--device",
help="Choose to run the code using `cpu`, `gpu`, or a specific GPU like `cuda:0`",
type=str,
default='cpu',
show_default=True,
)
@click.option(
"--file-type",
type=click.Choice(['pdf', 'page', 'text_formula', 'formula', 'text']),
default='text_formula',
help="Which file type to process, 'pdf', 'page', 'text_formula', 'formula', or 'text'",
show_default=True,
)
@click.option(
"--resized-shape",
help="Resize the image width to this size before processing",
type=int,
default=768,
show_default=True,
)
@click.option(
"-i",
"--input-json",
required=True,
help="JSON file containing evaluation data with image paths and ground truth",
)
@click.option(
"--gt-key",
default="model_result",
help="Key name for ground truth text in the JSON data",
show_default=True,
)
@click.option(
"--prefix-img-dir",
default="data",
help="Root directory for image files, will be prepended to img_path in JSON",
show_default=True,
)
@click.option(
"--rec-kwargs",
type=str,
default=None,
help="kwargs for calling .recognize(), in JSON string format",
show_default=True,
)
@click.option(
"--auto-line-break/--no-auto-line-break",
default=True,
help="Whether to automatically determine to merge adjacent line results into a single line result",
show_default=True,
)
@click.option(
"-o",
"--output-json",
default='evaluation_results.json',
help="Output JSON file for evaluation results",
show_default=True,
)
@click.option(
"--output-excel",
default=None,
help="Output Excel file with embedded images (optional)",
show_default=True,
)
@click.option(
"--output-html",
default=None,
help="Output HTML report with embedded images (optional)",
show_default=True,
)
@click.option(
"--max-img-width",
default=400,
help="Maximum width for embedded images in pixels",
show_default=True,
)
@click.option(
"--max-img-height",
default=300,
help="Maximum height for embedded images in pixels",
show_default=True,
)
@click.option(
"--max-samples",
default=-1,
help="Maximum number of samples to process (-1 for all samples)",
show_default=True,
)
@click.option(
"--log-level",
default='INFO',
help="Log Level, such as `INFO`, `DEBUG`",
show_default=True,
)
def evaluate(
languages,
layout_config,
mfd_config,
formula_ocr_config,
text_ocr_config,
enable_formula,
enable_table,
device,
file_type,
resized_shape,
input_json,
gt_key,
prefix_img_dir,
rec_kwargs,
auto_line_break,
output_json,
output_excel,
output_html,
max_img_width,
max_img_height,
max_samples,
log_level,
):
"""Evaluate Pix2Text (P2T) performance using a JSON file with image paths and ground truth."""
from pix2text.utils import (
calculate_cer_batch,
calculate_cer,
save_evaluation_results_to_excel_with_images,
create_html_report_with_images
)
logger = set_logger(log_level=log_level)
# Load evaluation data
try:
with open(input_json, 'r', encoding='utf-8') as f:
eval_data = json.load(f)
except Exception as e:
logger.error(f"Failed to load evaluation data from {input_json}: {e}")
return
if not isinstance(eval_data, list):
logger.error("Evaluation data must be a list of dictionaries")
return
# Validate data format
for i, item in enumerate(eval_data):
if not isinstance(item, dict):
logger.error(f"Item {i} is not a dictionary")
return
if 'img_path' not in item or gt_key not in item:
logger.error(f"Item {i} missing required keys 'img_path' or '{gt_key}'")
return
# Initialize Pix2Text
mfd_config = json.loads(mfd_config) if mfd_config else {}
formula_ocr_config = json.loads(formula_ocr_config) if formula_ocr_config else {}
languages = [lang.strip() for lang in languages.split(',') if lang.strip()]
text_ocr_config = json.loads(text_ocr_config) if text_ocr_config else {}
layout_config = json.loads(layout_config) if layout_config else {}
text_formula_config = {
'languages': languages,
'mfd': mfd_config,
'formula': formula_ocr_config,
'text': text_ocr_config,
}
total_config = {
'layout': layout_config,
'text_formula': text_formula_config,
}
p2t = Pix2Text.from_config(
total_configs=total_config,
enable_formula=enable_formula,
enable_table=enable_table,
device=device,
)
# Prepare recognition kwargs
rec_kwargs = json.loads(rec_kwargs) if rec_kwargs else {}
rec_kwargs['resized_shape'] = resized_shape
rec_kwargs['return_text'] = True
rec_kwargs['auto_line_break'] = auto_line_break
def filter_and_clean_gt(gt):
# 只针对部分的图片进行识别
# 去掉收尾的'"'
if not gt:
return False, gt
if gt.startswith(r'$$') and gt.endswith(r'$$'):
gt = gt[2:-2]
if '$$' not in gt:
return True, gt.strip()
return False, gt
# Process each image and collect results
predictions = []
ground_truths = []
results = []
# Apply max_samples limit
if max_samples > 0:
import random
random.seed(42)
random.shuffle(eval_data)
logger.info(f"Limited to {max_samples} samples for evaluation")
logger.info(f"Starting evaluation on {len(eval_data)} images...")
for i, item in enumerate(eval_data):
if len(results) >= max_samples:
break
img_path = item['new_img_path']
ground_truth = item[gt_key]
# Handle ground truth that might be a JSON string
if isinstance(ground_truth, str):
try:
ground_truth = json.loads(ground_truth)
except json.JSONDecodeError:
# If it's not valid JSON, use as is
pass
# Apply formula filtering if needed
is_formula, ground_truth = filter_and_clean_gt(ground_truth)
if not is_formula:
continue
# Prepend prefix_img_dir to img_path if it's not an absolute path
if not os.path.isabs(img_path):
img_path = os.path.join(prefix_img_dir, img_path)
logger.info(f"Processing image {i+1}/{len(eval_data)}: {img_path}")
try:
# Check if image file exists
if not os.path.exists(img_path):
logger.warning(f"Image file not found: {img_path}")
continue
# Recognize text
prediction = p2t.recognize(img_path, file_type=file_type, **rec_kwargs)
# Convert to string if needed
if not isinstance(prediction, str):
if hasattr(prediction, 'to_markdown'):
prediction = prediction.to_markdown()
else:
prediction = str(prediction)
predictions.append(prediction)
ground_truths.append(ground_truth)
# Calculate individual CER
cer = calculate_cer(prediction, ground_truth)
result = {
'img_path': img_path,
'ground_truth': ground_truth,
'prediction': prediction,
'cer': cer
}
results.append(result)
logger.info(f"Image {img_path} CER: {cer:.4f}")
except Exception as e:
logger.error(f"Error processing image {img_path}: {e}")
continue
# resort results by cer
# results.sort(key=lambda x: x['cer'], reverse=True)
# Calculate overall CER
if predictions and ground_truths:
cer_stats = calculate_cer_batch(predictions, ground_truths)
# Prepare final results
evaluation_results = {
'summary': {
'total_samples': len(results),
'average_cer': cer_stats['average_cer'],
'individual_cers': cer_stats['individual_cers']
},
'detailed_results': results
}
# Save results
try:
with open(output_json, 'w', encoding='utf-8') as f:
json.dump(evaluation_results, f, ensure_ascii=False, indent=2)
logger.info(f"Evaluation results saved to: {output_json}")
except Exception as e:
logger.error(f"Failed to save evaluation results: {e}")
# Print summary
logger.info("=" * 50)
logger.info("EVALUATION SUMMARY")
logger.info("=" * 50)
logger.info(f"Total samples processed: {len(results)}")
logger.info(f"Average CER: {cer_stats['average_cer']:.4f}")
logger.info(f"Best CER: {min(cer_stats['individual_cers']):.4f}")
logger.info(f"Worst CER: {max(cer_stats['individual_cers']):.4f}")
logger.info("=" * 50)
else:
logger.error("No valid predictions generated")
# Save results to Excel with embedded images (if requested)
if output_excel and results:
excel_success = save_evaluation_results_to_excel_with_images(
results=results,
output_file=output_excel,
img_path_key='img_path',
gt_key='ground_truth',
pred_key='prediction',
cer_key='cer',
max_img_width=max_img_width,
max_img_height=max_img_height
)
if excel_success:
logger.info(f"Excel file with embedded images saved to: {output_excel}")
else:
logger.warning("Failed to save Excel file with embedded images")
# Save results to HTML report with embedded images (if requested)
if output_html and results:
html_success = create_html_report_with_images(
results=results,
output_file=output_html,
img_path_key='img_path',
gt_key='ground_truth',
pred_key='prediction',
cer_key='cer',
max_img_width=max_img_width,
max_img_height=max_img_height
)
if html_success:
logger.info(f"HTML report with embedded images saved to: {output_html}")
else:
logger.warning("Failed to save HTML report with embedded images")
@cli.command('serve')
@click.option(
"-l",
"--languages",
type=str,
default='en,ch_sim',
help="Language Codes for Text-OCR to recognize, separated by commas",
show_default=True,
)
@click.option(
"--layout-config",
type=str,
default=None,
help="Configuration information for the layout parser model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--mfd-config",
type=str,
default=None,
help="Configuration information for the MFD model, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--formula-ocr-config",
type=str,
default=None,
help="Configuration information for the Latex-OCR mathematical formula recognition model. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--text-ocr-config",
type=str,
default=None,
help="Configuration information for Text-OCR recognition, in JSON string format. Default: `None`, meaning using the default configuration",
show_default=True,
)
@click.option(
"--enable-formula/--disable-formula",
default=True,
help="Whether to enable formula recognition",
show_default=True,
)
@click.option(
"--enable-table/--disable-table",
default=True,
help="Whether to enable table recognition",
show_default=True,
)
@click.option(
"-d",
"--device",
help="Choose to run the code using `cpu`, `gpu`, or a specific GPU like `cuda:0`",
type=str,
default='cpu',
show_default=True,
)
@click.option(
"-o",
"--output-md-root-dir",
default='output-md-root',
help="Markdown output root directory for the recognized text results. Only effective when `file-type` is `pdf` or `page`",
show_default=True,
)
@click.option(
'-H', '--host', type=str, default='0.0.0.0', help='server host', show_default=True,
)
@click.option(
'-p', '--port', type=int, default=8503, help='server port', show_default=True,
)
@click.option(
'--reload',
is_flag=True,
help='whether to reload the server when the codes have been changed',
show_default=True,
)
@click.option(
"--log-level",
default='INFO',
help="Log Level, such as `INFO`, `DEBUG`",
show_default=True,
)
def serve(
languages,
layout_config,
mfd_config,
formula_ocr_config,
text_ocr_config,
enable_formula,
enable_table,
device,
output_md_root_dir,
host,
port,
reload,
log_level,
):
"""Start the HTTP service."""
from pix2text.serve import start_server
logger = set_logger(log_level=log_level)
analyzer_config = json.loads(mfd_config) if mfd_config else {}
formula_ocr_config = json.loads(formula_ocr_config) if formula_ocr_config else {}
languages = [lang.strip() for lang in languages.split(',') if lang.strip()]
text_ocr_config = json.loads(text_ocr_config) if text_ocr_config else {}
layout_config = json.loads(layout_config) if layout_config else {}
text_formula_config = {
'languages': languages, # 'en,ch_sim
'mfd': analyzer_config,
'formula': formula_ocr_config,
'text': text_ocr_config,
}
total_config = {
'layout': layout_config,
'text_formula': text_formula_config,
}
p2t_config = dict(
total_configs=total_config,
enable_formula=enable_formula,
enable_table=enable_table,
device=device,
)
api = Process(
target=start_server,
kwargs={
'p2t_config': p2t_config,
'output_md_root_dir': output_md_root_dir,
'host': host,
'port': port,
'reload': reload,
},
)
api.start()
api.join()
if __name__ == "__main__":
cli()
|