Files
aiData/Apps/TeLaiDian/TestCV.py
HuangHai 9e610b1906 'commit'
2026-01-14 13:14:05 +08:00

78 lines
3.0 KiB
Python

# coding=utf-8
import os
import sys
import cv2
import numpy as np
# 确保项目根目录在 sys.path 中
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if project_root not in sys.path:
sys.path.append(project_root)
from Apps.TeLaiDian import Kit
from Apps.TeLaiDian.Config.Setting import SAFE_EXCLUDE_RATIO, BOTTOM_SAFE_EXCLUDE_RATIO
def test_cv_detection(image_path):
print(f"开始测试 CV 检测: {image_path}")
if not os.path.exists(image_path):
print(f"错误: 文件不存在 {image_path}")
return
img = Kit.read_image(image_path)
h, w = img.shape[:2]
print(f"图片尺寸: {w}x{h}")
print(f"当前过滤阈值: top_limit={int(h*SAFE_EXCLUDE_RATIO)}, bottom_limit={int(h*(1-BOTTOM_SAFE_EXCLUDE_RATIO))}")
# 1. 检测卡片
bboxes = Kit.detect_cards_cv(image_path, top_ratio=SAFE_EXCLUDE_RATIO, bottom_ratio=BOTTOM_SAFE_EXCLUDE_RATIO)
print(f"检测到 {len(bboxes)} 个卡片区域")
for i, box in enumerate(bboxes):
print(f" 卡片 {i+1}: {box}")
# 1.5 检测红色价格块并打印
try:
price_blocks = Kit.detect_list_price_blocks_cv(image_path, top_ratio=SAFE_EXCLUDE_RATIO, bottom_ratio=BOTTOM_SAFE_EXCLUDE_RATIO)
except AttributeError:
price_blocks = []
print(f"检测到 {len(price_blocks)} 个红色价格块")
for i, pb in enumerate(price_blocks):
print(f" 价格块 {i+1}: {pb}")
# 2. 生成 _vl.jpg (绿框 + 蓝点标记红色价格行)
vl_path = image_path.replace(".jpg", "_vl.jpg")
img_vl = Kit.read_image(image_path)
for box in bboxes:
cv2.rectangle(img_vl, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 3)
for pb in price_blocks:
px1, py1, px2, py2 = pb
pcx = (px1 + px2) // 2
pcy = (py1 + py2) // 2
cv2.circle(img_vl, (pcx, pcy), 10, (255, 0, 0), -1)
Kit.save_image(vl_path, img_vl)
print(f"已生成 VLM 标注图: {vl_path}")
# 3. 生成 _flag.jpg (绿框 + 点击点)
flag_path = image_path.replace(".jpg", "_flag.jpg")
img_flag = img_vl.copy()
for box in bboxes:
# 计算点击点:卡片中心
center_x = (box[0] + box[2]) // 2
center_y = (box[1] + box[3]) // 2
# 绘制红色点击点 (实心圆 + 十字)
cv2.circle(img_flag, (center_x, center_y), 15, (0, 0, 255), -1)
cv2.line(img_flag, (center_x - 30, center_y), (center_x + 30, center_y), (255, 255, 255), 3)
cv2.line(img_flag, (center_x, center_y - 30), (center_x, center_y + 30), (255, 255, 255), 3)
# 标注序号
idx = bboxes.index(box) + 1
cv2.putText(img_flag, str(idx), (box[0] + 10, box[1] + 40), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 3)
Kit.save_image(flag_path, img_flag)
print(f"已生成人工核对图: {flag_path}")
if __name__ == "__main__":
target_image = r"d:\dsWork\aiData\Output\tld_list_1768359492.jpg"
test_cv_detection(target_image)