{"record":{"id":"c4b1d86048b31a3f","repo":"PaddlePaddle/PaddleOCR","slug":"compare-ocr-json-py-requires-pip-install-shapely","errorCode":null,"errorMessage":"compare_ocr_json.py requires `pip install shapely`","messagePattern":"compare_ocr_json\\.py requires `pip install shapely`","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"deploy/ios_demo/scripts/compare_ocr_json.py","lineNumber":69,"sourceCode":"        prev = cur\n    return prev[-1]\n\n\ndef _cer(ref: str, hyp: str) -> float:\n    if not ref and not hyp:\n        return 0.0\n    if not ref:\n        return 1.0\n    return _levenshtein(ref, hyp) / max(len(ref), 1)\n\n\ndef _polygon_iou(\n    poly_a: Sequence[Sequence[float]], poly_b: Sequence[Sequence[float]]\n) -> float:\n    try:\n        from shapely.geometry import Polygon\n    except ImportError as exc:\n        raise RuntimeError(\n            \"compare_ocr_json.py requires `pip install shapely`\"\n        ) from exc\n\n    def _to_poly(p: Sequence[Sequence[float]]) -> Polygon:\n        pts = [(float(x), float(y)) for x, y in p]\n        if len(pts) < 3:\n            return Polygon()\n        if pts[0] != pts[-1]:\n            pts = pts + [pts[0]]\n        return Polygon(pts)\n\n    p1 = _to_poly(poly_a)\n    p2 = _to_poly(poly_b)\n    if p1.is_empty or p2.is_empty or not p1.is_valid or not p2.is_valid:\n        return 0.0\n    inter = p1.intersection(p2).area\n    union = p1.union(p2).area\n    if union <= 0:","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/PaddlePaddle/PaddleOCR/blob/2661c7c0ef5c613e8f93c6e93b2e052399f0f854/deploy/ios_demo/scripts/compare_ocr_json.py#L51-L87","documentation":"Raised lazily by compare_ocr_json.py the first time it needs to compute polygon IoU: the shapely geometry library is not importable. The script only imports shapely inside _polygon_iou, so the failure appears at comparison time rather than at startup, and it chains the original ImportError with an install hint.","triggerScenarios":"Running compare_ocr_json.py on a JSON pair whose items contain polygons (triggering _polygon_iou) in a Python environment where shapely is not installed or is broken.","commonSituations":"Fresh venv/CI image with only core PaddleOCR deps; shapely installed for a different interpreter than the one running the script; a corrupted shapely install that fails on import.","solutions":["pip install shapely in the same interpreter/environment that runs the script.","If it should already be installed, verify with `python -c \"import shapely; print(shapely.__version__)\" using that exact interpreter.","Add shapely to the project's requirements file so CI picks it up."],"exampleFix":"# before\nRuntimeError: compare_ocr_json.py requires `pip install shapely`\n\n# after\npip install shapely\npython deploy/ios_demo/scripts/compare_ocr_json.py ref.json hyp.json","handlingStrategy":"validation","validationCode":"import importlib.util, sys\n\nif importlib.util.find_spec(\"shapely\") is None:\n    sys.exit(\"shapely missing — run: pip install shapely\")\n\n# only now invoke the comparison\nsubprocess.run([sys.executable, \"deploy/ios_demo/scripts/compare_ocr_json.py\", ref, hyp])","typeGuard":null,"tryCatchPattern":"try:\n    from shapely.geometry import Polygon  # early, explicit import\nexcept ImportError:\n    Polygon = None  # or exit with install instructions\n\nif Polygon is None:\n    raise SystemExit(\"pip install shapely before running comparisons\")","preventionTips":["Add shapely to requirements/dev-requirements for any repo containing compare_ocr_json.py.","Import heavy optional deps at startup, not lazily, so failures are immediate and obvious.","Pin the interpreter: run scripts with the same python that has the deps installed."],"tags":["paddleocr","ios","dependency","shapely","ocr-eval"],"backgroundTag":null,"analyzedSha":"2661c7c0ef5c613e8f93c6e93b2e052399f0f854","analyzedAt":"2026-08-14T20:17:30.180Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}