pref: 新增编号绘制功能

This commit is contained in:
2025-10-27 22:17:50 +08:00
parent b0b8dc9b8f
commit 011ff0b54e

View File

@@ -87,10 +87,8 @@ image_client = ImageClient("tcp://175.24.228.220:7701", client_id="local")
# 初始化数据库 # 初始化数据库
init_db() init_db()
# --- 辅助函数 --- def draw_detections_on_image(image: np.ndarray, detections: list, left_position: int = None, right_position: int = None) -> np.ndarray:
"""在图像上绘制检测框和位置信息"""
def draw_detections_on_image(image: np.ndarray, detections: list) -> np.ndarray:
"""在图像上绘制检测框"""
# 复制原图以避免修改原始图像 # 复制原图以避免修改原始图像
marked_image = image.copy() 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] 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: for detection in detections:
# 获取检测信息 # 获取检测信息
@@ -602,13 +628,12 @@ def update_manual_detections():
except Exception as e: 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 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): def regenerate_marked_images(image_id, detections, side):
"""重新生成标注图片,支持左右图像分别处理""" """重新生成标注图片,支持左右图像分别处理"""
conn = sqlite3.connect(DATABASE_PATH) conn = sqlite3.connect(DATABASE_PATH)
cursor = conn.cursor() cursor = conn.cursor()
cursor.execute(""" 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 FROM images
WHERE id = ? WHERE id = ?
""", (image_id,)) """, (image_id,))
@@ -618,7 +643,7 @@ def regenerate_marked_images(image_id, detections, side):
if not row: if not row:
raise Exception("Image not found") 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 重新生成对应的标注图片 # 根据指定的 side 重新生成对应的标注图片
if side == 'left' and left_marked_filename: 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): if os.path.exists(left_path):
img_left = cv2.imread(left_path) img_left = cv2.imread(left_path)
if img_left is not None: 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) cv2.imwrite(left_marked_path, marked_left_img)
elif side == 'right' and right_marked_filename: 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): if os.path.exists(right_path):
img_right = cv2.imread(right_path) img_right = cv2.imread(right_path)
if img_right is not None: 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) cv2.imwrite(right_marked_path, marked_right_img)
# 添加人工标注页面路由 # 添加人工标注页面路由