• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dynobo / normcap / 14280746188

05 Apr 2025 09:50AM UTC coverage: 84.841% (-2.2%) from 87.002%
14280746188

Pull #690

github

web-flow
Merge d28da9558 into c7b17ed4a
Pull Request #690: feature/add-qr-support

318 of 410 new or added lines in 35 files covered. (77.56%)

5 existing lines in 2 files now uncovered.

2569 of 3028 relevant lines covered (84.84%)

3.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.97
/normcap/detection/ocr/recognize.py
1
"""Detect OCR tool & language and perform OCR on selected part of image."""
2

3
import logging
4✔
4
import tempfile
4✔
5
import time
4✔
6
from collections.abc import Iterable
4✔
7
from os import PathLike
4✔
8
from pathlib import Path
4✔
9
from typing import Optional, Union
4✔
10

11
import cv2
4✔
12
import numpy as np
4✔
13

14
from normcap.detection.ocr import enhance, tesseract, transformer
4✔
15
from normcap.detection.ocr.models import OEM, PSM, OcrResult, TessArgs
4✔
16

17
logger = logging.getLogger(__name__)
4✔
18

19

20
def _save_image_in_temp_folder(image: np.ndarray, postfix: str = "") -> None:
4✔
21
    """For debugging it can be useful to store the cropped image."""
22
    if logger.getEffectiveLevel() != logging.DEBUG:
4✔
NEW
23
        return
×
24

25
    temp_dir = Path(tempfile.gettempdir()) / "normcap"
4✔
26
    temp_dir.mkdir(exist_ok=True)
4✔
27

28
    now_str = time.strftime("%Y-%m-%d_%H-%M-%S", time.gmtime())
4✔
29
    file_name = f"{now_str}{postfix}.png"
4✔
30

31
    logger.debug("Save debug image as %s", temp_dir / file_name)
4✔
32
    cv2.imwrite(str(temp_dir / file_name), image)
4✔
33

34

35
def get_text_from_image(  # noqa: PLR0913
4✔
36
    tesseract_cmd: PathLike,
37
    languages: Union[str, Iterable[str]],
38
    image: np.ndarray,
39
    tessdata_path: Optional[PathLike] = None,
40
    parse: bool = True,
41
    resize_factor: Optional[float] = None,
42
    padding_size: Optional[int] = None,
43
) -> OcrResult:
44
    """Apply OCR on selected image section."""
45
    image = enhance.preprocess(image, resize_factor=resize_factor, padding=padding_size)
4✔
46
    _save_image_in_temp_folder(image, postfix="_enhanced")
4✔
47

48
    tess_args = TessArgs(
4✔
49
        tessdata_path=tessdata_path,
50
        lang=languages if isinstance(languages, str) else "+".join(languages),
51
        oem=OEM.DEFAULT,
52
        psm=PSM.AUTO,
53
    )
54
    logger.debug(
4✔
55
        "Run Tesseract on image of size %s with args:\n%s",
56
        image.shape[:2],
57
        tess_args,
58
    )
59
    ocr_result_data = tesseract.perform_ocr(
4✔
60
        cmd=tesseract_cmd, image=image, args=tess_args.as_list()
61
    )
62
    result = OcrResult(tess_args=tess_args, words=ocr_result_data, image=image)
4✔
63
    logger.debug("OCR detections:\n%s", ",\n".join(str(w) for w in result.words))
4✔
64

65
    if parse:
4✔
66
        result = transformer.apply(result)
4✔
67
        logger.debug("Parsed text:\n%s", result.parsed)
4✔
68

69
    return result
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc