"""Evaluate ocr_extractor.py on the repo's sample images and on synthetic images with known ground truth. Samples (all in samples/): * text_styles.png — the image from the original repo (static/uploads/img.png): five lines of normal / bold / italic / outlined text. Ground truth is the text the notebook itself printed for this image (cell 6). * text_styles_small.png — the same image resized to 300x150, exactly the input the notebook ran (cell 5). Reproducing the notebook's printed output on this file is the headline check. * dickens.png — the other image from the repo (static/uploads/figure-65.png): the opening of "A Tale of Two Cities". Ground truth is the visible text. * synthetic_clean.png — rendered here with DejaVu Sans; ground truth is the text that was rendered, so it is known exactly. * synthetic_rotated.png — the same render rotated 3 degrees, to test the deskew step. Metrics are character error rate (CER) and word error rate (WER), computed with whitespace collapsed to single spaces on both sides (case-sensitive, punctuation kept). Levenshtein distance is implemented here, no extra dependency. Nothing is copied from the notebook except the ground-truth text it printed for text_styles. """ import json import os import sys sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) from PIL import Image, ImageDraw, ImageFont # noqa: E402 import ocr_extractor # noqa: E402 HERE = os.path.dirname(os.path.abspath(__file__)) SAMPLES = os.path.join(HERE, "samples") WORK = os.path.join(HERE, "..", "work") TEXT_STYLES_GT = ( "Normal text and bold text " "Italic text and bold italic text " "Normal text and artificially bold text " "Artificially outlined text " "Artificially italic text and bold italic text" ) DICKENS_GT = ( "It was the best of times, it was the worst of times, " "it was the age of wisdom, it was the age of foolishness" ) SYNTHETIC_GT = ( "Machine learning is the science of getting computers " "to act without being explicitly programmed. " "The quick brown fox jumps over the lazy dog 1234567890" ) # --------------------------------------------------------------------------- # Levenshtein / error rates # --------------------------------------------------------------------------- def levenshtein(a, b): if a == b: return 0 if not a: return len(b) if not b: return len(a) prev = list(range(len(b) + 1)) for i, ca in enumerate(a, 1): cur = [i] for j, cb in enumerate(b, 1): cur.append(min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (ca != cb))) prev = cur return prev[-1] def normalise(text): return " ".join(text.replace("\x0c", " ").split()) def cer(reference, hypothesis): ref = normalise(reference) hyp = normalise(hypothesis) if not ref: return 0.0 if not hyp else 1.0 return levenshtein(ref, hyp) / len(ref) def wer(reference, hypothesis): ref = normalise(reference).split() hyp = normalise(hypothesis).split() if not ref: return 0.0 if not hyp else 1.0 return levenshtein(ref, hyp) / len(ref) # --------------------------------------------------------------------------- # Synthetic samples (ground truth known exactly, because we render it) # --------------------------------------------------------------------------- def make_synthetic(): os.makedirs(SAMPLES, exist_ok=True) font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" font = ImageFont.truetype(font_path, 30) img = Image.new("RGB", (1000, 170), "white") draw = ImageDraw.Draw(img) draw.text((20, 20), "Machine learning is the science of getting computers", fill="black", font=font) draw.text((20, 65), "to act without being explicitly programmed.", fill="black", font=font) draw.text((20, 110), "The quick brown fox jumps over the lazy dog 1234567890", fill="black", font=font) clean = os.path.join(SAMPLES, "synthetic_clean.png") img.save(clean) rotated = img.rotate(3, expand=False, fillcolor="white") rotated.save(os.path.join(SAMPLES, "synthetic_rotated.png")) return clean # --------------------------------------------------------------------------- # Evaluation # --------------------------------------------------------------------------- def evaluate_one(path, ground_truth): out = {} raw = ocr_extractor.extract_text(path) out["raw"] = {"text": normalise(raw), "cer": cer(ground_truth, raw), "wer": wer(ground_truth, raw)} pre = ocr_extractor.extract_text_preprocessed(path) out["preprocessed"] = {"text": normalise(pre), "cer": cer(ground_truth, pre), "wer": wer(ground_truth, pre)} deskewed = ocr_extractor.extract_text_preprocessed( path, steps=("grayscale", "denoise", "threshold", "deskew")) out["preprocessed_deskew"] = { "text": normalise(deskewed), "cer": cer(ground_truth, deskewed), "wer": wer(ground_truth, deskewed)} return out def main(): os.makedirs(SAMPLES, exist_ok=True) make_synthetic() cases = [ ("text_styles", os.path.join(SAMPLES, "text_styles.png"), TEXT_STYLES_GT), ("text_styles_small_300x150", os.path.join(SAMPLES, "text_styles_small.png"), TEXT_STYLES_GT), ("dickens", os.path.join(SAMPLES, "dickens.png"), DICKENS_GT), ("synthetic_clean", os.path.join(SAMPLES, "synthetic_clean.png"), SYNTHETIC_GT), ("synthetic_rotated_3deg", os.path.join(SAMPLES, "synthetic_rotated.png"), SYNTHETIC_GT), ] results = {} for name, path, gt in cases: if not os.path.exists(path): print(f"SKIP {name}: missing {path}") continue r = evaluate_one(path, gt) results[name] = r print(f"\n== {name} ==") print(f" ground truth: {normalise(gt)}") for variant in ("raw", "preprocessed", "preprocessed_deskew"): v = r[variant] print(f" {variant:20s} CER {v['cer']:.4f} WER {v['wer']:.4f}") print(f" -> {v['text'][:200]}") # Headline check: does the module reproduce the notebook's printed # output on the notebook's exact input (300x150)? small = results.get("text_styles_small_300x150", {}).get("raw", {}) notebook_reproduced = ( small.get("text", "") == normalise(TEXT_STYLES_GT)) print(f"\nNotebook cell-6 output reproduced exactly: {notebook_reproduced}") metrics = { "tesseract_version": str(ocr_extractor.pytesseract.get_tesseract_version()), "default_config": ocr_extractor.DEFAULT_CONFIG, "notebook_output_reproduced_exactly": notebook_reproduced, "cases": { name: { variant: {"cer": round(v["cer"], 4), "wer": round(v["wer"], 4), "text": v["text"]} for variant, v in r.items() } for name, r in results.items() }, } with open(os.path.join(HERE, "metrics.json"), "w") as f: json.dump(metrics, f, indent=2) print("\nwrote metrics.json") return 0 if __name__ == "__main__": raise SystemExit(main())