Skip to content

Commit 90b2299

Browse files
committed
ruff lint
1 parent 721c1a5 commit 90b2299

11 files changed

+42
-15
lines changed

OCR/ocr/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for FastAPI interface functions."""
2+
23
import base64
34

45
import uvicorn

OCR/ocr/services/alignment/backends/four_point_transform.py

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class FourPointTransform:
1616
Attributes:
1717
image (np.ndarray): The input image as a NumPy array.
1818
"""
19+
1920
def __init__(self, image: Path | np.ndarray):
2021
"""Initializes the FourPointTransform object with an image.
2122

OCR/ocr/services/alignment/backends/image_homography.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Aligns two images using image homography algorithms."""
2+
23
from pathlib import Path
34

45
import numpy as np
@@ -17,6 +18,7 @@ class ImageHomography:
1718
match_ratio (float): The ratio used for Lowe's ratio test to filter good matches.
1819
_sift (cv.SIFT): The SIFT detector used to find keypoints and descriptors.
1920
"""
21+
2022
def __init__(self, template: Path | np.ndarray, match_ratio=0.3):
2123
"""Initializes the ImageHomography object with a template image.
2224

OCR/ocr/services/alignment/backends/random_perspective_transform.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class RandomPerspectiveTransform:
1111
"""Class to generates a random perspective transform based on a template image.
12-
12+
1313
This class allows you to apply random distortions to an image by computing
1414
a perspective transformation matrix and warping the image accordingly.
1515
@@ -85,7 +85,7 @@ def transform(self, transformer: np.ndarray) -> np.ndarray:
8585
def random_transform(self, distortion_scale: float) -> np.ndarray:
8686
"""Warp the template image with specified distortion_scale.
8787
88-
This method internally calls `make_transform` to generate the transformation matrix
88+
This method internally calls `make_transform` to generate the transformation matrix
8989
and applies it using `transform`.
9090
9191
Args:

OCR/ocr/services/alignment/image_alignment.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for aligning images using a specified image alignment backend."""
2+
23
import numpy as np
34

45
from ocr.services.alignment.backends import ImageHomography
@@ -8,9 +9,10 @@ class ImageAligner:
89
"""Class for aligning images using a specified image alignment backend.
910
1011
Attributes:
11-
aligner: An alignment backend class or instance that provides an `align` method.
12+
aligner: An alignment backend class or instance that provides an `align` method.
1213
Default is the ImageHomography backend from the ocr.services.alignment module.
1314
"""
15+
1416
def __init__(self, aligner=ImageHomography):
1517
"""Initializes an ImageAligner instance with the specified image alignment backend.
1618

OCR/ocr/services/batch_metrics.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for batch processing OCR and ground truth files and calculating metrics."""
2+
23
from ocr.services.metrics_analysis import OCRMetrics
34
import os
45
import csv
@@ -16,6 +17,7 @@ class BatchMetricsAnalysis:
1617
ground_truth_folder (str): Path to the folder containing ground truth files.
1718
csv_output_folder (str): Path to the folder where CSV output files will be saved.
1819
"""
20+
1921
def __init__(self, ocr_folder: str, ground_truth_folder: str, csv_output_folder: str) -> None:
2022
"""Initializes the BatchMetricsAnalysis class with paths to OCR, ground truth, and output folders.
2123

OCR/ocr/services/batch_segmentation.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Process and segment a batch of images and perform OCR on the results."""
2+
23
import os
34
import json
45
import time
@@ -18,7 +19,10 @@ class BatchSegmentationOCR:
1819
output_folder (str): Path to the folder where OCR results and timing information will be saved.
1920
model (ImageOCR): An optional pre-defined OCR model; if None, a default instance of ImageOCR is used.
2021
"""
21-
def __init__(self, image_folder: str, segmentation_template: str, labels_path: str, output_folder: str, model=None) -> None:
22+
23+
def __init__(
24+
self, image_folder: str, segmentation_template: str, labels_path: str, output_folder: str, model=None
25+
) -> None:
2226
"""Initializes the BatchSegmentationOCR instance with the specified paths and an optional OCR model.
2327
2428
Args:
@@ -76,7 +80,9 @@ def process_images(self) -> list[dict]:
7680
print("Processing complete.")
7781
return results
7882

79-
def segment_ocr_image(self, segmenter: ImageSegmenter, ocr, image_path: str, image_file: str) -> tuple[dict[str, tuple[str, float]], float]:
83+
def segment_ocr_image(
84+
self, segmenter: ImageSegmenter, ocr, image_path: str, image_file: str
85+
) -> tuple[dict[str, tuple[str, float]], float]:
8086
"""Segments the image and runs OCR, returning results and time taken.
8187
8288
Args:

OCR/ocr/services/image_ocr.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module for OCR using a transformers-based OCR model."""
2+
23
from collections.abc import Iterator
34

45
from transformers import TrOCRProcessor, VisionEncoderDecoderModel
@@ -16,6 +17,7 @@ class ImageOCR:
1617
processor (TrOCRProcessor): Processor for TrOCR model that prepares images for OCR.
1718
model (VisionEncoderDecoderModel): Pre-trained TrOCR model for extracting text from images.
1819
"""
20+
1921
def __init__(self, model="microsoft/trocr-large-printed"):
2022
"""Initializes the ImageOCR class with the specified OCR model.
2123
@@ -32,7 +34,7 @@ def __init__(self, model="microsoft/trocr-large-printed"):
3234
def compute_line_angle(lines: list) -> Iterator[float]:
3335
"""Computes the angle in degrees of the lines detected by the Hough transform, based on their endpoints.
3436
35-
This method processes the output of `cv.HoughLinesP` (lines in (x1, y1, x2, y2) format) and computes the angle
37+
This method processes the output of `cv.HoughLinesP` (lines in (x1, y1, x2, y2) format) and computes the angle
3638
between each line and the horizontal axis.
3739
3840
Args:
@@ -186,12 +188,12 @@ def image_to_text(self, segments: dict[str, np.ndarray]) -> dict[str, tuple[str,
186188
For each segment, it extracts the text and the average confidence score.
187189
188190
Args:
189-
segments (dict[str, np.ndarray]): A dictionary where keys are segment labels (e.g., 'header', 'body'),
191+
segments (dict[str, np.ndarray]): A dictionary where keys are segment labels (e.g., 'header', 'body'),
190192
and values are NumPy arrays representing the corresponding image segments.
191193
192194
Returns:
193-
dict[str, tuple[str, float]]: A dictionary where each key corresponds to a segment label, and each value is
194-
a tuple containing the recognized text (as a string) and the confidence score
195+
dict[str, tuple[str, float]]: A dictionary where each key corresponds to a segment label, and each value is
196+
a tuple containing the recognized text (as a string) and the confidence score
195197
(as a float) for the recognition.
196198
"""
197199
digitized: dict[str, tuple[str, float]] = {}

OCR/ocr/services/image_segmenter.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module to segment images based on a segmentation template and a set of labels."""
2+
23
import cv2 as cv
34
import numpy as np
45
import json
@@ -31,7 +32,9 @@ def crop_zeros(image: np.ndarray) -> np.ndarray:
3132
] # inclusive
3233

3334

34-
def segment_by_mask_then_crop(raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool) -> dict[str, np.ndarray]:
35+
def segment_by_mask_then_crop(
36+
raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool
37+
) -> dict[str, np.ndarray]:
3538
"""Segments a raw image based on a color mask in the segmentation template, and then crops the resulting regions to remove zero (black) areas.
3639
3740
Args:
@@ -75,7 +78,9 @@ def segment_by_mask_then_crop(raw_image: np.ndarray, segmentation_template: np.n
7578
return segments
7679

7780

78-
def segment_by_color_bounding_box(raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool) -> dict[str, np.ndarray]:
81+
def segment_by_color_bounding_box(
82+
raw_image: np.ndarray, segmentation_template: np.ndarray, labels: list[dict[str, str]], debug: bool
83+
) -> dict[str, np.ndarray]:
7984
"""Segments a raw image by detecting colored boundary boxes in the segmentation template.
8085
8186
Args:
@@ -119,6 +124,7 @@ class ImageSegmenter:
119124
`segment_by_mask_then_crop` or `segment_by_color_bounding_box`.
120125
debug (bool): If `True`, saves debug images and prints additional information.
121126
"""
127+
122128
def __init__(
123129
self,
124130
segmentation_function=segment_by_mask_then_crop,
@@ -152,7 +158,9 @@ def segment(
152158
"""
153159
return self.segmentation_function(raw_image, segmentation_template, labels, self.debug)
154160

155-
def load_and_segment(self, raw_image_path: str, segmentation_template_path: str, labels_path: str) -> dict[str, np.ndarray]:
161+
def load_and_segment(
162+
self, raw_image_path: str, segmentation_template_path: str, labels_path: str
163+
) -> dict[str, np.ndarray]:
156164
"""Loads image files and labels from specified paths, and then segments the image.
157165
158166
Args:

OCR/ocr/services/metrics_analysis.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Module to calculate OCR metrics and compare results to ground truth data."""
2+
23
import json
34
import csv
45
import Levenshtein
@@ -15,6 +16,7 @@ class OCRMetrics:
1516
ocr_json (dict): A dict from JSON data containing OCR results.
1617
ground_truth_json (dict): A dict from JSON data containing the ground truth values.
1718
"""
19+
1820
def __init__(self, ocr_json_path=None, ground_truth_json_path=None, ocr_json=None, ground_truth_json=None):
1921
"""Initializes the OCRMetrics object with OCR and ground truth data, either loaded from files or provided as dictionaries.
2022

OCR/ocr/services/tesseract_ocr.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class TesseractOCR:
2222
* https://github.com/sirfz/tesserocr/blob/bbe0fb8edabdcc990f1e6fa9334c0747c2ac76ee/tesserocr/__init__.pyi#L47
2323
* https://tesseract-ocr.github.io/tessdoc/tess3/ControlParams.html
2424
"""
25+
2526
def __init__(self, psm=PSM.AUTO, variables=dict()):
2627
"""Initializes the TesseractOCR object with the specified page segmentation mode and internal variables.
2728
@@ -88,12 +89,12 @@ def image_to_text(self, segments: dict[str, np.ndarray]) -> dict[str, tuple[str,
8889
For each segment, it extracts the text and the average confidence score returned from the Tesseract API.
8990
9091
Args:
91-
segments (dict[str, np.ndarray]): A dictionary where keys are segment labels (e.g., 'header', 'body'),
92+
segments (dict[str, np.ndarray]): A dictionary where keys are segment labels (e.g., 'header', 'body'),
9293
and values are NumPy arrays representing the corresponding image segments.
9394
9495
Returns:
95-
dict[str, tuple[str, float]]: A dictionary where each key corresponds to a segment label, and each value is
96-
a tuple containing the recognized text (as a string) and the confidence score
96+
dict[str, tuple[str, float]]: A dictionary where each key corresponds to a segment label, and each value is
97+
a tuple containing the recognized text (as a string) and the confidence score
9798
(as a float) for the recognition.
9899
"""
99100
digitized: dict[str, tuple[str, float]] = {}

0 commit comments

Comments
 (0)