This commit is contained in:
HuangHai
2026-01-15 09:06:27 +08:00
parent 9cc78ff125
commit f8af4dd9ae
13 changed files with 404 additions and 103 deletions

View File

@@ -0,0 +1,71 @@
# coding=utf-8
# pip install easyocr
# python -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121 --force-reinstall
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 Util.EasyOcrKit import get_easyocr_reader
def test_easyocr_price():
# 待测试的图片路径
test_image_path = os.path.join(project_root, "Output", "Screenshot_20260115_083521.jpg")
output_image_path = os.path.join(project_root, "Output", "Test_EasyOCR_Result.jpg")
print(f"--- 开始测试 EasyOCR 识别 ---")
print(f"输入图片: {test_image_path}")
if not os.path.exists(test_image_path):
print(f"错误: 测试图片不存在!")
return
# 1. 初始化 OCR (使用封装类)
reader = get_easyocr_reader(gpu=True)
# 2. 读取图片
img = cv2.imread(test_image_path)
if img is None:
print("错误: 无法读取图片。")
return
# 3. 识别
target = '全部时段'
found = reader.find_text_position(img, target)
h, w = img.shape[:2]
if found:
text, quad, prob = found
pts = np.array(quad).astype(int)
cv2.polylines(img, [pts], isClosed=True, color=(0, 255, 0), thickness=2)
# 使用封装后的方法获取归一化中心点 (演示 get_normalized_rect)
rect = reader.get_normalized_rect(quad, w, h)
norm_x = (rect[0] + rect[2]) // 2
norm_y = (rect[1] + rect[3]) // 2
print(f'找到“{text}” (目标: {target}) 置信度={prob:.2f}')
print(f'归一化坐标: [{norm_x}, {norm_y}]')
cv2.putText(img, f"OCR Found: {text}", (pts[0][0], pts[0][1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 2)
else:
print(f"未能在图片中找到包含“{target}”的文本。")
# 打印出所有识别到的文本,方便调试
print("识别到的所有文本:")
results = reader.read_text(img)
for (_, text, _) in results:
print(f" - {text}")
# 4. 保存结果图
cv2.imwrite(output_image_path, img)
print(f"--- 测试完成 ---")
print(f"结果已保存至: {output_image_path}")
if __name__ == "__main__":
test_easyocr_price()

View File

@@ -0,0 +1,67 @@
# 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()