This commit is contained in:
HuangHai
2026-01-14 10:40:29 +08:00
parent d4a0065f8a
commit 068cb63274
6 changed files with 151 additions and 46 deletions

View File

@@ -229,36 +229,48 @@ def detect_cards_cv(image_path, top_ratio=0.40, bottom_ratio=0.12):
return cards
def draw_rectangles(image_path, points, output_path=None):
def draw_rectangles(image_path, bboxes=None, click_points=None):
"""
使用 OpenCV 在图片上绘制矩形框
使用 OpenCV 在图片上绘制矩形框和点击点,生成 _vl.jpg 和 _flag.jpg
- _vl.jpg: 仅包含矩形框,供视觉模型参考
- _flag.jpg: 包含矩形框和点击点,供人工调试
"""
try:
from Apps.TeLaiDian.Config.Setting import DRAW_DEBUG_BOXES
# 这里暂时写死颜色和粗细,或者从 Setting 读取
DEBUG_BOX_COLOR = (0, 255, 0)
DEBUG_BOX_COLOR = (0, 255, 0) # 绿色矩形
DEBUG_POINT_COLOR = (0, 0, 255) # 红色点击点
DEBUG_BOX_THICKNESS = 3
# if not DRAW_DEBUG_BOXES: # 暂时强制开启以方便调试
# return image_path
img = read_image(image_path)
if img is None:
return image_path
for p in points:
if len(p) == 4:
cv2.rectangle(img, (int(p[0]), int(p[1])), (int(p[2]), int(p[3])), DEBUG_BOX_COLOR, DEBUG_BOX_THICKNESS)
elif len(p) == 2:
center = (int(p[0]), int(p[1]))
cv2.circle(img, center, 10, DEBUG_BOX_COLOR, -1)
save_path = output_path if output_path else image_path
save_image(save_path, img)
return save_path
# 1. 先绘制矩形框 (VL 版)
vl_img = img.copy()
if bboxes:
for box in bboxes:
if len(box) == 4:
cv2.rectangle(vl_img, (int(box[0]), int(box[1])), (int(box[2]), int(box[3])), DEBUG_BOX_COLOR, DEBUG_BOX_THICKNESS)
vl_path = image_path.replace(".jpg", "_vl.jpg")
save_image(vl_path, vl_img)
# 2. 在 VL 版基础上绘制点击点 (Flag 版)
flag_img = vl_img.copy()
if click_points:
for p in click_points:
if len(p) == 2:
center = (int(p[0]), int(p[1]))
# 绘制一个实心圆和中心点
cv2.circle(flag_img, center, 12, DEBUG_POINT_COLOR, -1)
cv2.circle(flag_img, center, 2, (255, 255, 255), -1)
flag_path = image_path.replace(".jpg", "_flag.jpg")
save_image(flag_path, flag_img)
return vl_path, flag_path
except Exception as e:
logger.error(f"绘制矩形框失败: {e}")
return image_path
logger.error(f"绘制诊断图片失败: {e}")
return image_path, image_path
def clear_temp_dir(save_dir=None):
"""清空临时目录中的所有文件"""