Restructure prototype into the rowing_stats package

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]>
This commit is contained in:
2026-10-09 11:46:19 +01:00
co-authored by Claude Opus 5.5
parent 9b1f7e5898
commit 9bfdc1330f
15 changed files with 658 additions and 407 deletions
+121
View File
@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "dc4ef0c3",
"metadata": {},
"source": [
"# Pipeline walkthrough\n",
"\n",
"Step through the extraction pipeline on a single photo to see what each stage produces.\n",
"Requires `pip install -e \".[notebook]\"` from the repository root."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "221851b3",
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"from pathlib import Path\n",
"\n",
"import cv2 as cv\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"\n",
"from rowing_stats.display import crop_display\n",
"from rowing_stats.image_io import get_datetime_taken, load_image, pil_to_cv\n",
"from rowing_stats.ocr import ocr_image, preprocess_for_ocr\n",
"from rowing_stats.parsing import parse_stats"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "438ecbd5",
"metadata": {},
"outputs": [],
"source": [
"def plot_images(imgs: list[np.ndarray]):\n",
" \"\"\"\n",
" Plot a list of images in a single matplotlib figure.\n",
"\n",
" Parameters\n",
" ----------\n",
" imgs : list[np.ndarray]\n",
" List of images (H×W greyscale or H×W×3 BGR arrays).\n",
" \"\"\"\n",
" if not imgs:\n",
" return\n",
"\n",
" n = len(imgs)\n",
" cols = math.ceil(math.sqrt(n))\n",
" rows = math.ceil(n / cols)\n",
"\n",
" fig, axes = plt.subplots(rows, cols, figsize=(cols * 5, rows * 5))\n",
" axes = np.atleast_1d(axes).ravel()\n",
"\n",
" for ax, img in zip(axes, imgs):\n",
" if img.ndim == 2:\n",
" ax.imshow(img, cmap=\"gray\")\n",
" else:\n",
" ax.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))\n",
" ax.axis(\"off\")\n",
"\n",
" # Hide any unused subplots\n",
" for ax in axes[len(imgs):]:\n",
" ax.axis(\"off\")\n",
"\n",
" plt.tight_layout()\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da8bada5",
"metadata": {},
"outputs": [],
"source": [
"photos_path = Path(\"../photos\")\n",
"img = load_image(photos_path / \"IMG_5413.JPEG\")\n",
"arr = pil_to_cv(img)\n",
"print(get_datetime_taken(img))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cb01649f",
"metadata": {},
"outputs": [],
"source": [
"warped = crop_display(arr)\n",
"plot_images([arr, warped, preprocess_for_ocr(warped)])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5dd910f8",
"metadata": {},
"outputs": [],
"source": [
"text = ocr_image(warped)\n",
"print(text)\n",
"parse_stats(text)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}