Peach: Local-first video metadata scraper and media manager.
fix(metadata): 挑主角的脸先卡分数,别只按面积
longmeidaofbaacc47a3b1
diff --git a/scripts/detect_avatar_faces.py b/scripts/detect_avatar_faces.pyindex 60d47731c..3a716f00b 100644--- a/scripts/detect_avatar_faces.py+++ b/scripts/detect_avatar_faces.py@@ -26,7 +26,7 @@ if str(SRC_DIR) not in sys.path:sys.path.insert(0, str(SRC_DIR))from peach.config import GENERATED_DIR-from peach.face_detect import FaceDetector, FaceModelUnavailable+from peach.face_detect import FaceDetector, FaceModelUnavailable, main_facefrom peach.web_contract import face_focus@@ -40,8 +40,8 @@ def detect(path: Path, detector: FaceDetector) -> dict | None:record: dict = {"ratio": ratio, "face": None}if not faces:return record- # 多张脸时取最大的:实体图的主角通常占画面最大,其余是拼贴或背景人物。- best = faces[0]+ # 多张脸时挑主角:先卡分数再取最大,判据在 peach.face_detect.main_face。+ best = main_face(faces)cx, cy = best.cx, best.cyrecord["face"] = {"cx": cx, "cy": cy, "score": best.score}focus = face_focus(ratio, cx, cy)diff --git a/scripts/detect_cover_faces.py b/scripts/detect_cover_faces.pyindex d15ebea5c..1635651b8 100644--- a/scripts/detect_cover_faces.py+++ b/scripts/detect_cover_faces.py@@ -33,7 +33,7 @@ if str(SRC_DIR) not in sys.path:sys.path.insert(0, str(SRC_DIR))from peach.config import COVER_DIR-from peach.face_detect import FaceDetector, FaceModelUnavailable+from peach.face_detect import FaceDetector, FaceModelUnavailable, main_face#: 长封套的宽高比下限。低于它按竖版正封处理(整张就是正封,没有剧照区)。SLEEVE_RATIO_MIN = 1.2@@ -61,7 +61,7 @@ def detect(path: Path, detector: FaceDetector) -> dict | None:faces = [face for face in faces if MIN_Y <= face.cy <= MAX_Y]if not faces:return {"ratio": round(ratio, 3), "face": None}- best = max(faces, key=lambda face: face.area)+ best = main_face(faces)return {"ratio": round(ratio, 3),"face": {"cx": best.cx, "cy": best.cy, "score": best.score}}diff --git a/src/peach/face_detect.py b/src/peach/face_detect.pyindex 3bb8373e4..0fb6243ed 100644--- a/src/peach/face_detect.py+++ b/src/peach/face_detect.py@@ -46,6 +46,8 @@ DEFAULT_NMS = 0.3#: 送进网络前的长边上限。YuNet 是定尺寸输入,超大图先缩再检更快且不掉召回;#: 坐标按缩放比还原回原图。MAX_SIDE = 1280+#: 挑主角时,分数落后最好那张这么多的框不参与「取最大」。+SCORE_MARGIN = 0.1class FaceModelUnavailable(RuntimeError):@@ -67,6 +69,21 @@ class Face:return self.width * self.height+def main_face(faces: list[Face], margin: float = SCORE_MARGIN) -> Face | None:+ """主角那张脸:先按分数筛掉明显不如最好那张的框,再在剩下的里取最大。++ 只按面积挑会被「大而勉强」的误检抢走。实测 `performer-8218`(jae 名录的+ 600×1000 人像):YuNet 给了两个框,脸是 0.202×0.170 分 0.928,另一个大一倍多、+ 罩在胸口,分 0.798——圆头像于是取景在胸口。只按分数挑则会被背景里那张小而清晰的+ 脸抢走。两个都用一次,各挡住对方的失败例。+ """+ if not faces:+ return None+ best_score = max(face.score for face in faces)+ strong = [face for face in faces if face.score >= best_score - margin]+ return max(strong, key=lambda face: face.area)++def ensure_model(path: Path | None = None, *, allow_download: bool = True) -> Path:"""返回可用的模型路径;本地没有且允许联网时取一次。diff --git a/tests/test_face_detect.py b/tests/test_face_detect.pyindex 572238455..75250a174 100644--- a/tests/test_face_detect.py+++ b/tests/test_face_detect.py@@ -88,5 +88,34 @@ class DetectorContractTests(unittest.TestCase):self.assertIsInstance(detector, face_detect.FaceDetector)+class MainFaceTests(unittest.TestCase):+ """挑主角那张脸:分数先卡一道,再在剩下的里取最大。"""++ @staticmethod+ def face(cx, cy, width, height, score):+ return face_detect.Face(cx=cx, cy=cy, width=width, height=height, score=score)++ def test_a_big_but_weak_box_loses_to_the_real_face(self):+ """实测 `performer-8218`:大出一倍多的那个框罩在胸口,分只有 0.798。++ 只按面积挑,圆头像就取景在胸口——脸在画面上四分之一处。+ """+ chest = self.face(0.389, 0.482, 0.427, 0.313, 0.798)+ head = self.face(0.389, 0.262, 0.202, 0.170, 0.928)+ self.assertEqual(face_detect.main_face([chest, head]), head)++ def test_a_small_sharp_bystander_does_not_beat_the_subject(self):+ """实测 `performer-8540`:右上角还检出一个 0.066×0.052 的小框。++ 分数相当时仍按面积挑,否则背景里那张小而清晰的脸会抢走取景。+ """+ subject = self.face(0.608, 0.207, 0.215, 0.152, 0.930)+ bystander = self.face(0.694, 0.050, 0.066, 0.052, 0.925)+ self.assertEqual(face_detect.main_face([subject, bystander]), subject)++ def test_no_face_is_no_face(self):+ self.assertIsNone(face_detect.main_face([]))++if __name__ == "__main__":unittest.main()