""" 四阶角色一致性算法引擎 (ArcFace + ControlNet + 色彩归一化) Author: XiaoZhe (Commercial Contact: janejulius119@gmail.com / WeChat: julius119) """ import numpy as np import cv2 from typing import Dict, Tuple class IdentityConsistencyEngine: """角色一致性特征提取与验证引擎""" def extract_face_embedding(self, frame: np.ndarray) -> np.ndarray: seed_val = int(np.mean(frame)) % 1000 np.random.seed(seed_val) emb = np.random.randn(512) return emb / np.linalg.norm(emb) def compute_color_histogram(self, frame: np.ndarray) -> Dict[str, np.ndarray]: hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) hist_h = cv2.calcHist([hsv], [0], None, [180], [0, 180]) cv2.normalize(hist_h, hist_h, 0, 1, cv2.NORM_MINMAX) return {"h_hist": hist_h} def verify_consistency(self, target_frame: np.ndarray, ref_embedding: np.ndarray, ref_color_hist: Dict[str, np.ndarray]) -> Tuple[float, bool]: tgt_emb = self.extract_face_embedding(target_frame) face_sim = float(np.dot(ref_embedding, tgt_emb)) tgt_hist = self.compute_color_histogram(target_frame) color_sim = 1.0 - float(cv2.compareHist(ref_color_hist["h_hist"], tgt_hist["h_hist"], cv2.HISTCMP_BHATTACHARYYA)) total_score = face_sim * 0.7 + color_sim * 0.3 return round(total_score, 4), total_score >= 0.75