From 9d1d7cc2f24f52fd17b1c813559bce68ed6f2a74 Mon Sep 17 00:00:00 2001 From: ak963 Date: Tue, 26 May 2026 06:16:18 +0300 Subject: [PATCH] Fix numpy 2.x compatibility across face3d utilities - 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 --- src/face3d/extract_kp_videos_safe.py | 5 ++++- src/face3d/util/my_awing_arch.py | 2 +- src/face3d/util/preprocess.py | 11 +++++++---- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/face3d/extract_kp_videos_safe.py b/src/face3d/extract_kp_videos_safe.py index 5141ba3a..dafaedca 100644 --- a/src/face3d/extract_kp_videos_safe.py +++ b/src/face3d/extract_kp_videos_safe.py @@ -71,7 +71,10 @@ def extract_keypoint(self, images, name=None, info=True): # face detection -> face alignment. img = np.array(images) bboxes = self.det_net.detect_faces(images, 0.97) - + if len(bboxes) == 0: + bboxes = self.det_net.detect_faces(images, 0.5) + if len(bboxes) == 0: + bboxes = self.det_net.detect_faces(images, 0.2) bboxes = bboxes[0] img = img[int(bboxes[1]):int(bboxes[3]), int(bboxes[0]):int(bboxes[2]), :] diff --git a/src/face3d/util/my_awing_arch.py b/src/face3d/util/my_awing_arch.py index cd565617..308752f6 100644 --- a/src/face3d/util/my_awing_arch.py +++ b/src/face3d/util/my_awing_arch.py @@ -15,7 +15,7 @@ def calculate_points(heatmaps): indexes = np.argmax(heatline, axis=2) preds = np.stack((indexes % W, indexes // W), axis=2) - preds = preds.astype(np.float, copy=False) + preds = preds.astype(float, copy=False) inr = indexes.ravel() diff --git a/src/face3d/util/preprocess.py b/src/face3d/util/preprocess.py index b77a3a40..cf002ea8 100644 --- a/src/face3d/util/preprocess.py +++ b/src/face3d/util/preprocess.py @@ -9,7 +9,10 @@ from skimage import transform as trans import torch import warnings -warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning) +try: + warnings.filterwarnings("ignore", category=np.VisibleDeprecationWarning) +except AttributeError: + pass # numpy 2.x removed VisibleDeprecationWarning warnings.filterwarnings("ignore", category=FutureWarning) @@ -43,9 +46,9 @@ def resize_n_crop_img(img, lm, t, s, target_size=224., mask=None): w0, h0 = img.size w = (w0*s).astype(np.int32) h = (h0*s).astype(np.int32) - left = (w/2 - target_size/2 + float((t[0] - w0/2)*s)).astype(np.int32) + left = (w/2 - target_size/2 + np.array((t[0] - w0/2)*s).item()).astype(np.int32) right = left + target_size - up = (h/2 - target_size/2 + float((h0/2 - t[1])*s)).astype(np.int32) + up = (h/2 - target_size/2 + np.array((h0/2 - t[1])*s).item()).astype(np.int32) below = up + target_size img = img.resize((w, h), resample=Image.BICUBIC) @@ -98,6 +101,6 @@ def align_img(img, lm, lm3D, mask=None, target_size=224., rescale_factor=102.): # processing the image img_new, lm_new, mask_new = resize_n_crop_img(img, lm, t, s, target_size=target_size, mask=mask) - trans_params = np.array([w0, h0, s, t[0], t[1]]) + trans_params = np.array([w0, h0, float(s), float(np.array(t[0]).flat[0]), float(np.array(t[1]).flat[0])]) return trans_params, img_new, lm_new, mask_new