Skip to content

Fix numpy 2.x compatibility in face3d utilities#1040

Open
AbdullahKhan123-tis wants to merge 1 commit into
OpenTalker:mainfrom
AbdullahKhan123-tis:fix/numpy2-compatibility
Open

Fix numpy 2.x compatibility in face3d utilities#1040
AbdullahKhan123-tis wants to merge 1 commit into
OpenTalker:mainfrom
AbdullahKhan123-tis:fix/numpy2-compatibility

Conversation

@AbdullahKhan123-tis
Copy link
Copy Markdown

Summary

Three files in src/face3d/ crash or raise warnings when running with numpy 2.x. This PR fixes all of them.

src/face3d/util/preprocess.py

  • Wrap
    p.VisibleDeprecationWarning in ry/except AttributeError - this warning class was removed in numpy 2.0 and raises AttributeError on import
  • Replace loat((t[0] - w0/2)*s) with
    p.array(...).item() to avoid the numpy 2.0 error "only size-1 arrays can be converted to Python scalars" when [0] is a 1-element array
  • Explicitly extract scalar values from [0] and [1] via .flat[0] before passing to
    p.array([w0, h0, s, t[0], t[1]]), fixing the "setting an array element with a sequence" inhomogeneous shape error

src/face3d/util/my_awing_arch.py

  • Replace preds.astype(np.float, copy=False) with preds.astype(float, copy=False) -
    p.float was deprecated in numpy 1.20 and removed in numpy 2.0

src/face3d/extract_kp_videos_safe.py

  • Add progressive fallback confidence thresholds for detect_faces (0.97 ? 0.5 ? 0.2). The default 0.97 threshold can miss valid faces in portrait photos where the face is slightly off-center or has non-standard framing, causing an IndexError on �boxes[0]. The fallbacks ensure detection succeeds without compromising quality for well-framed inputs.

Test plan

  • Tested end-to-end on PyTorch 2.5.1+cu121, numpy 1.26.4, RTX 4050 (6 GB VRAM)
  • Landmark detection, 3DMM extraction, and face rendering all complete without error
  • No regressions on standard portrait inputs

- preprocess.py: wrap VisibleDeprecationWarning filter in try/except
  (np.VisibleDeprecationWarning was removed in numpy 2.0); use .item()
  instead of float() on multi-element arrays; explicitly cast t[0]/t[1]
  to scalar floats to avoid inhomogeneous array shape error in
  np.array([w0, h0, s, t[0], t[1]])
- my_awing_arch.py: replace removed np.float alias with built-in float
- extract_kp_videos_safe.py: add fallback confidence thresholds (0.5,
  0.2) when RetinaFace finds no faces at the default 0.97 threshold,
  preventing IndexError on portraits with less-than-ideal framing

Tested with numpy 1.26.4 and numpy 2.x on PyTorch 2.5.1+cu121.

Co-Authored-By: Claude Sonnet 4.6 <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.

2 participants