diff --git a/cam_web.py b/cam_web.py index b76301e..8532ffc 100644 --- a/cam_web.py +++ b/cam_web.py @@ -87,10 +87,8 @@ image_client = ImageClient("tcp://175.24.228.220:7701", client_id="local") # 初始化数据库 init_db() -# --- 辅助函数 --- - -def draw_detections_on_image(image: np.ndarray, detections: list) -> np.ndarray: - """在图像上绘制检测框""" +def draw_detections_on_image(image: np.ndarray, detections: list, left_position: int = None, right_position: int = None) -> np.ndarray: + """在图像上绘制检测框和位置信息""" # 复制原图以避免修改原始图像 marked_image = image.copy() @@ -105,6 +103,34 @@ def draw_detections_on_image(image: np.ndarray, detections: list) -> np.ndarray: # 获取图像尺寸 h, w = image.shape[:2] + if left_position is not None: + position_text = f"POS/CAM_L: {left_position if left_position != 0 else 'NAN'}" + elif right_position is not None: + position_text = f"POS/CAM_R: {right_position if right_position != 0 else 'NAN'}" + else: + position_text = "" + + if position_text: + # 设置字体参数 + font = cv2.FONT_HERSHEY_SIMPLEX + font_scale = 1.0 + font_thickness = 2 + text_size = cv2.getTextSize(position_text, font, font_scale, font_thickness)[0] + + # 计算文本背景位置 + text_x = 10 + text_y = 30 + + # 绘制白色背景矩形 + cv2.rectangle(marked_image, + (text_x - 5, text_y - text_size[1] - 5), + (text_x + text_size[0] + 5, text_y + 5), + (255, 255, 255), -1) + + # 绘制黑色文字 + cv2.putText(marked_image, position_text, (text_x, text_y), + font, font_scale, (0, 0, 0), font_thickness) + # 绘制每个检测框 for detection in detections: # 获取检测信息 @@ -602,13 +628,12 @@ def update_manual_detections(): except Exception as e: return jsonify({"message": f"Manual detections for image {image_id} ({side}) updated successfully but failed to regenerate marked images: {str(e)}"}), 500 -# 添加重新生成标注图片的函数 def regenerate_marked_images(image_id, detections, side): """重新生成标注图片,支持左右图像分别处理""" conn = sqlite3.connect(DATABASE_PATH) cursor = conn.cursor() cursor.execute(""" - SELECT left_filename, right_filename, left_marked_filename, right_marked_filename + SELECT left_filename, right_filename, left_marked_filename, right_marked_filename, left_position, right_position FROM images WHERE id = ? """, (image_id,)) @@ -618,7 +643,7 @@ def regenerate_marked_images(image_id, detections, side): if not row: raise Exception("Image not found") - left_filename, right_filename, left_marked_filename, right_marked_filename = row + left_filename, right_filename, left_marked_filename, right_marked_filename, left_position, right_position = row # 根据指定的 side 重新生成对应的标注图片 if side == 'left' and left_marked_filename: @@ -628,7 +653,7 @@ def regenerate_marked_images(image_id, detections, side): if os.path.exists(left_path): img_left = cv2.imread(left_path) if img_left is not None: - marked_left_img = draw_detections_on_image(img_left, detections) + marked_left_img = draw_detections_on_image(img_left, detections, left_position=left_position, right_position=None) cv2.imwrite(left_marked_path, marked_left_img) elif side == 'right' and right_marked_filename: @@ -638,7 +663,7 @@ def regenerate_marked_images(image_id, detections, side): if os.path.exists(right_path): img_right = cv2.imread(right_path) if img_right is not None: - marked_right_img = draw_detections_on_image(img_right, detections) + marked_right_img = draw_detections_on_image(img_right, detections, left_position=None, right_position=right_position) cv2.imwrite(right_marked_path, marked_right_img) # 添加人工标注页面路由