Files
aiData/Test/Test_PriceBox_Detection.py
HuangHai fdd5532b33 'commit'
2026-01-15 10:16:26 +08:00

68 lines
2.3 KiB
Python

# coding=utf-8
import os
import sys
import cv2
import numpy as np
# 设置项目根目录
project_root = 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.Kit import detect_price_info_container_cv, read_image, save_image
def test_price_box_detection():
# 待测试的图片路径
test_image_path = os.path.join(project_root, "Output", "Screenshot_20260115_083521.jpg")
output_image_path = os.path.join(project_root, "Output", "Test_PriceBox_Result_V2.jpg")
print(f"--- 开始测试价格容器识别 ---")
print(f"输入图片: {test_image_path}")
if not os.path.exists(test_image_path):
print(f"错误: 测试图片不存在!")
return
# 1. 调用刚才实现的 CV 识别函数
container_norm = detect_price_info_container_cv(test_image_path)
if not container_norm:
print("识别失败: 未能检测到‘价格信息’容器矩形。")
return
print(f"识别成功! 归一化坐标: {container_norm}")
# 2. 读取图片并绘制绿框进行可视化验证
img = read_image(test_image_path)
if img is None:
print("错误: 无法读取图片。")
return
h, w = img.shape[:2]
x1 = int(container_norm[0] * w / 1000)
y1 = int(container_norm[1] * h / 1000)
x2 = int(container_norm[2] * w / 1000)
y2 = int(container_norm[3] * h / 1000)
# 绘制外框 (绿框)
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 4)
# 绘制中间分割线 (区分左侧当前价和右侧会员价)
mid_x = x1 + (x2 - x1) // 2
cv2.line(img, (mid_x, y1), (mid_x, y2), (0, 255, 0), 2)
# 加上文字标注
cv2.putText(img, "Target Area (Left Half)", (x1 + 10, y1 + 40),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
# 3. 保存结果图
if save_image(output_image_path, img):
print(f"--- 测试完成 ---")
print(f"结果已保存至: {output_image_path}")
print(f"请检查该图片中的绿框是否准确圈定了‘价格信息’板块及其左侧区域。")
else:
print("错误: 结果图保存失败。")
if __name__ == "__main__":
test_price_box_detection()