Split the script into one module per pipeline stage (image_io, display, ocr, parsing, pipeline, cli), with type hints throughout, and add packaging metadata and pinned dependencies. Failures are now typed exceptions instead of None returns and prints: unreadable, corrupt or missing images raise ImageLoadError, a photo with no detectable display raises DisplayNotFoundError, and a missing Tesseract binary raises TesseractNotInstalledError. The CLI reports a bad file and carries on, and exits non-zero if any file failed. It also gains JSON and CSV output. Behaviour changes: - OCR runs in sparse-text mode (--psm 11) without a character whitelist. The previous single-word mode (--psm 8) parsed no fields from any of the sample photos, and the whitelist made Tesseract drop the spaces the parser relies on to separate fields. - A 'Cal/hr' reading is no longer also reported as total calories. - The '500m' in a '/500m' pace label is no longer reported as distance. - A pace is no longer reported as elapsed time. - The distance is no longer also reported as projected distance. - Debug contour images are actually written (the float boxes were rejected by drawContours and the error swallowed). The unused matplotlib/imshow display helpers are dropped; plotting lives in the walkthrough notebook, which replaces the scratch one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
3.0 KiB
3.0 KiB
In [ ]:
import math
from pathlib import Path
import cv2 as cv
import matplotlib.pyplot as plt
import numpy as np
from rowing_stats.display import crop_display
from rowing_stats.image_io import get_datetime_taken, load_image, pil_to_cv
from rowing_stats.ocr import ocr_image, preprocess_for_ocr
from rowing_stats.parsing import parse_statsIn [ ]:
def plot_images(imgs: list[np.ndarray]):
"""
Plot a list of images in a single matplotlib figure.
Parameters
----------
imgs : list[np.ndarray]
List of images (H×W greyscale or H×W×3 BGR arrays).
"""
if not imgs:
return
n = len(imgs)
cols = math.ceil(math.sqrt(n))
rows = math.ceil(n / cols)
fig, axes = plt.subplots(rows, cols, figsize=(cols * 5, rows * 5))
axes = np.atleast_1d(axes).ravel()
for ax, img in zip(axes, imgs):
if img.ndim == 2:
ax.imshow(img, cmap="gray")
else:
ax.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
ax.axis("off")
# Hide any unused subplots
for ax in axes[len(imgs):]:
ax.axis("off")
plt.tight_layout()
plt.show()In [ ]:
photos_path = Path("../photos")
img = load_image(photos_path / "IMG_5413.JPEG")
arr = pil_to_cv(img)
print(get_datetime_taken(img))In [ ]:
warped = crop_display(arr)
plot_images([arr, warped, preprocess_for_ocr(warped)])In [ ]:
text = ocr_image(warped)
print(text)
parse_stats(text)