From 29aaa472e73ed53eb521253fd3244b5a76864679 Mon Sep 17 00:00:00 2001 From: cab66420 Date: Sun, 19 Apr 2026 12:13:06 +0900 Subject: [PATCH] Fix face detection failures for non-photorealistic images When using --preprocess full with anime/illustrated images, face detection fails at two points and crashes with unhelpful errors: 1. croper.py: `raise 'string'` was invalid Python 3 syntax and lm=None check didn't handle the full-mode case. Now returns the full image as crop/quad ([0,0,W,H]) so processing continues without a detected face. 2. extract_kp_videos_safe.py: `bboxes[0]` raises IndexError when detect_faces() returns an empty array. Added IndexError to the existing TypeError handler so it falls back to -1 keypoints, which preprocess.py already handles gracefully. Co-Authored-By: Claude Sonnet 4.6 --- src/face3d/extract_kp_videos_safe.py | 4 ++-- src/utils/croper.py | 12 ++++++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/face3d/extract_kp_videos_safe.py b/src/face3d/extract_kp_videos_safe.py index 5141ba3a..25dde130 100644 --- a/src/face3d/extract_kp_videos_safe.py +++ b/src/face3d/extract_kp_videos_safe.py @@ -89,10 +89,10 @@ def extract_keypoint(self, images, name=None, info=True): else: print(e) break - except TypeError: + except (TypeError, IndexError): print('No face detected in this image') shape = [68, 2] - keypoints = -1. * np.ones(shape) + keypoints = -1. * np.ones(shape) break if name is not None: np.savetxt(os.path.splitext(name)[0]+'.txt', keypoints.reshape(-1)) diff --git a/src/utils/croper.py b/src/utils/croper.py index 3d9a0ac5..5b96f715 100644 --- a/src/utils/croper.py +++ b/src/utils/croper.py @@ -127,8 +127,16 @@ def crop(self, img_np_list, still=False, xsize=512): # first frame for all vi img_np = img_np_list[0] lm = self.get_landmark(img_np) - if lm is None: - raise 'can not detect the landmark from source image' + # 顔未検出 + full モード時のフォールバック + if lm is None or len(lm) == 0: + if not still: # still=False = full モード + H, W = img_np_list[0].shape[:2] + # 画像全体を crop/quad に設定(クロップなし) + # preprocess.py が lx, ly, rx, ry = quad と4スカラーで受け取るため + crop = [0, 0, W, H] + quad = [0, 0, W, H] # [lx, ly, rx, ry] + return img_np_list, crop, quad + raise Exception('can not detect the landmark from source image') rsize, crop, quad = self.align_face(img=Image.fromarray(img_np), lm=lm, output_size=xsize) clx, cly, crx, cry = crop lx, ly, rx, ry = quad