From cefc5d733cdf7fa09e5b6479886d3a8f08c958d9 Mon Sep 17 00:00:00 2001 From: Tian jianyong <11429339@qq.com> Date: Sun, 28 Dec 2025 17:53:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=A4=AD=E5=9C=86=E5=A4=A7?= =?UTF-8?q?=E5=B0=8F=EF=BC=8C=E4=BF=AE=E6=AD=A3=E6=8D=95=E6=8D=89=E6=A1=86?= =?UTF-8?q?=20bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- face_rec.py | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/face_rec.py b/face_rec.py index 2c4f37a..729cb31 100644 --- a/face_rec.py +++ b/face_rec.py @@ -1279,7 +1279,7 @@ class FaceRecognitionSystem: img = cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) return img - def draw_info_on_frame(self, frame: np.ndarray) -> np.ndarray: + def draw_info_on_frame(self, frame: np.ndarray, mirrored: bool = False) -> np.ndarray: """在帧上绘制检测和识别信息""" display_frame = frame.copy() h, w = display_frame.shape[:2] @@ -1292,6 +1292,11 @@ class FaceRecognitionSystem: x_max = int(box['x_max']) y_max = int(box['y_max']) + # 镜像翻转时调整坐标 + if mirrored: + x_min = w - 1 - x_max + x_max = w - 1 - x_min + # 根据识别状态选择颜色 if self.display_info['person_name']: # 已识别 - 绿色 @@ -1815,17 +1820,8 @@ class FaceRecognitionSystem: # 镜像翻转:使画面与实际场景一致(在绘制信息之前) frame = cv2.flip(frame, 1) - # 调整人脸框坐标以适应镜像翻转 - if self.display_info['face_box']: - h, w = frame.shape[:2] - box = self.display_info['face_box'] - x_min = int(box['x_min']) - x_max = int(box['x_max']) - # 水平翻转坐标:new_x = width - 1 - old_x - self.display_info['face_box']['x_min'] = w - 1 - x_max - self.display_info['face_box']['x_max'] = w - 1 - x_min - - # 绘制信息 + # 绘制信息(传入镜像状态,由 draw_info_on_frame 内部处理坐标翻转) + display_frame = self.draw_info_on_frame(frame, mirrored=True) display_frame = self.draw_info_on_frame(frame) # 绘制人脸轮廓引导框(屏幕中央) @@ -1833,11 +1829,15 @@ class FaceRecognitionSystem: center_x = w // 2 center_y = h // 2 # 椭圆参数:中心点、长轴、短轴、旋转角度、起始角度、结束角度 - cv2.ellipse(display_frame, (center_x, center_y), (120, 160), 0, 0, 360, (0, 200, 0), 2) + cv2.ellipse(display_frame, (center_x, center_y), (75, 100), 0, 0, 360, (0, 200, 0), 2) - # 添加提示文字 - cv2.putText(display_frame, "请将脸部对准圆圈内", (center_x - 100, center_y + 200), - cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 200, 0), 2) + # 添加提示文字(使用中文支持函数) + guide_text = "请将脸部对准圆圈内" + # 计算文字居中位置 + text_size = cv2.getTextSize(guide_text, cv2.FONT_HERSHEY_SIMPLEX, 0.8, 2)[0] + text_x = center_x - text_size[0] // 2 + text_y = center_y + 150 + display_frame = self.cv2_add_chinese_text(display_frame, guide_text, (text_x, text_y), self.font_medium, (0, 200, 0)) # 使用缓存的屏幕尺寸拉伸视频帧到全屏 display_frame = cv2.resize(display_frame, (self.screen_width, self.screen_height), interpolation=cv2.INTER_LINEAR)