Skip to content

fix: degrade gracefully when OpenCV is present but unusable - #2

Open
hiroki-abe-58 wants to merge 1 commit into
miyang-ai:mainfrom
hiroki-abe-58:fix/detect-faces-best-effort
Open

hiroki-abe-58 wants to merge 1 commit into
miyang-ai:mainfrom
hiroki-abe-58:fix/detect-faces-best-effort

Conversation

@hiroki-abe-58

Copy link
Copy Markdown

Summary

Related to #1. Thanks to @greatweir-star for the detailed report and reproduction.

detect_faces only caught ImportError. An OpenCV that imports but cannot be used raised into pipeline.act() and ended an otherwise deterministic lattice/granule run with outcome: "failed". Examples are OpenCV 5.x without a top-level CascadeClassifier, a build without cv2.data or cascade files, or a cv2.error at run time.

This PR keeps face protection best-effort and keeps the degraded state diagnosable:

OpenCV state before after
not installed [], cv2_unavailable unchanged
import fails inside cv2 (e.g. DLL / ABI error) [], cv2_unavailable [], cv2_error:ImportError
no CascadeClassifier / no cv2.data AttributeError, run failed [], cv2_error:AttributeError
no cascade file can be loaded [], haar_cascade_no_face [], cv2_error:FileNotFoundError
error at run time (detectMultiScale, ...) exception, run failed [], cv2_error:<type> (cv2_error:error for cv2.error)
working OpenCV boxes, haar_cascade / haar_cascade_no_face unchanged

Implementation notes:

  • The OpenCV calls move unchanged into _haar_cascade_boxes().
  • detect_faces() catches Exception (not BaseException) only around the cv2 import and that helper. Box merging stays outside the try.
  • The check is not limited to CascadeClassifier: any failure in the OpenCV part degrades.
  • No change to pipeline.py, thresholds or signal-processing behaviour.

Tests

tests/test_face_detection.py covers:

  • each broken case, using small fake cv2 modules (via monkeypatch);
  • a fake cv2 package whose import raises;
  • a working fake, to check that merged boxes are unchanged;
  • a lattice pipeline.run with a broken cv2, which must still finish with diagnose → notch → verify → finish and verify passed.

Verification

Local only: Windows 11, Python 3.12.13, numpy 2.5.3.

  • New tests on unmodified main (865a148): 6 failed, 2 passed. The 2 that pass are the unchanged paths.
  • This branch, without OpenCV: pytest 23 passed.
  • This branch, with opencv-python-headless 4.14.0.94 (.[face]): pytest 23 passed.
  • ruff check . is clean.
  • With real OpenCV 4.14, detect_faces returns identical boxes and status on unmodified main and on this branch for 7 local test images (one with a detected box).
  • I did not install a real OpenCV 5.x. The incompatible builds are reproduced with fake modules only.
  • Not run locally: macOS, Python 3.11 / 3.13 (left to CI).

🤖 Generated with Claude Code

detect_faces only caught ImportError. An importable OpenCV without
CascadeClassifier (e.g. OpenCV 5.x), without cv2.data or cascade files,
or one that fails at run time raised into pipeline.act() and turned a
deterministic lattice/granule run into outcome "failed".

Face protection is optional, so the OpenCV calls now run in a helper and
any failure there degrades to no boxes:
- OpenCV not installed: "cv2_unavailable" (unchanged)
- present but unusable (failed import, missing API, no cascade loaded,
  runtime error): "cv2_error:<exception type>"
Previously "no cascade could be loaded" was reported as
haar_cascade_no_face. Box merging and the working OpenCV path are
unchanged.

Regression tests use small fake cv2 modules for each broken case and
check that a lattice run still finishes with notch, verify and finish.

Related to miyang-ai#1 (reported by greatweir-star).

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant