|
| 1 | +# Copyright (C) 2021-2026, Mindee. |
| 2 | + |
| 3 | +# This program is licensed under the Apache License 2.0. |
| 4 | +# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details. |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import logging |
| 9 | +import sys |
| 10 | + |
| 11 | +from doctr.io import DocumentFile |
| 12 | +from doctr.models import ocr_predictor |
| 13 | + |
| 14 | +logging.basicConfig(format="%(levelname)s: %(message)s", level=logging.INFO) |
| 15 | + |
| 16 | + |
| 17 | +def main(argv=None): |
| 18 | + """Main function for the docTR CLI tool""" |
| 19 | + # parse command-line arguments and set up the model |
| 20 | + args = _parse_args(argv) |
| 21 | + model = ocr_predictor( |
| 22 | + det_arch=args.det_arch, |
| 23 | + reco_arch=args.reco_arch, |
| 24 | + pretrained=True, |
| 25 | + assume_straight_pages=args.assume_straight_pages, |
| 26 | + preserve_aspect_ratio=args.preserve_aspect_ratio, |
| 27 | + symmetric_pad=args.symmetric_pad, |
| 28 | + detect_orientation=args.detect_orientation, |
| 29 | + straighten_pages=args.straighten_pages, |
| 30 | + detect_language=args.detect_language, |
| 31 | + det_bs=args.det_bs, |
| 32 | + reco_bs=args.reco_bs, |
| 33 | + ) |
| 34 | + |
| 35 | + # load the document |
| 36 | + try: |
| 37 | + if args.input_path.lower().endswith(".pdf"): |
| 38 | + doc = DocumentFile.from_pdf(args.input_path) |
| 39 | + else: |
| 40 | + doc = DocumentFile.from_images(args.input_path) |
| 41 | + logging.info(f"Document loaded successfully from {args.input_path}") |
| 42 | + except FileNotFoundError: |
| 43 | + logging.error(f"File not found: {args.input_path}") |
| 44 | + sys.exit(1) |
| 45 | + except ValueError: |
| 46 | + logging.error(f"File could not be read as a valid image or PDF: {args.input_path}") |
| 47 | + sys.exit(1) |
| 48 | + except Exception as e: |
| 49 | + logging.error(f"Error occurred while loading the document: {e}") |
| 50 | + sys.exit(1) |
| 51 | + |
| 52 | + # perform OCR |
| 53 | + logging.info("Performing OCR...") |
| 54 | + result = model(doc) |
| 55 | + |
| 56 | + # save results to JSON file |
| 57 | + try: |
| 58 | + with open(args.output, "w", encoding="utf-8") as f: |
| 59 | + json.dump(result.export(), f, indent=4, ensure_ascii=False) |
| 60 | + logging.info(f"Results saved to {args.output}") |
| 61 | + except FileNotFoundError: |
| 62 | + logging.error(f"Could not write output file at given path: {args.output}") |
| 63 | + sys.exit(1) |
| 64 | + except Exception as e: |
| 65 | + logging.error(f"Results could not be saved: {e}") |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | + |
| 69 | +def _parse_args(argv=None): |
| 70 | + parser = argparse.ArgumentParser( |
| 71 | + description="docTR CLI tool for OCR prediction on images and PDFs", |
| 72 | + formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
| 73 | + ) |
| 74 | + |
| 75 | + # required input path |
| 76 | + parser.add_argument("--input_path", type=str, required=True, help="path to input image or PDF file") |
| 77 | + |
| 78 | + # architecture selection |
| 79 | + parser.add_argument( |
| 80 | + "--det_arch", |
| 81 | + type=str, |
| 82 | + default="db_resnet50", |
| 83 | + help="name of the detection architecture or the model itself to use", |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--reco_arch", |
| 87 | + type=str, |
| 88 | + default="crnn_vgg16_bn", |
| 89 | + help="name of the recognition architecture or the model itself to use", |
| 90 | + ) |
| 91 | + |
| 92 | + # processing options |
| 93 | + parser.add_argument( |
| 94 | + "--assume_straight_pages", |
| 95 | + action=argparse.BooleanOptionalAction, |
| 96 | + default=True, |
| 97 | + help="assume only straight pages without rotated textual elements", |
| 98 | + ) |
| 99 | + parser.add_argument( |
| 100 | + "--straighten_pages", action="store_true", help="attempt to straighten skewed pages before analysis" |
| 101 | + ) |
| 102 | + parser.add_argument( |
| 103 | + "--preserve_aspect_ratio", |
| 104 | + action=argparse.BooleanOptionalAction, |
| 105 | + default=True, |
| 106 | + help="preserve aspect ratio when resizing pages", |
| 107 | + ) |
| 108 | + parser.add_argument("--symmetric_pad", action="store_true", help="apply symmetric padding") |
| 109 | + parser.add_argument("--det_bs", type=int, default=2, help="batch size for detection") |
| 110 | + parser.add_argument("--reco_bs", type=int, default=128, help="batch size for recognition") |
| 111 | + parser.add_argument("--detect_orientation", action="store_true", help="automatically detect page orientation") |
| 112 | + parser.add_argument("--detect_language", action="store_true", help="detect language of the text") |
| 113 | + |
| 114 | + # output options |
| 115 | + parser.add_argument("--output", type=str, default="results.json", help="path to output results in JSON format") |
| 116 | + |
| 117 | + return parser.parse_args(argv) |
0 commit comments