This commit is contained in:
HuangHai
2026-01-15 11:46:59 +08:00
parent f038929687
commit 8cbcca472b
11 changed files with 178 additions and 41 deletions

View File

@@ -270,6 +270,45 @@ def draw_rectangles(image_path, points, output_path=None):
logger.error(f"绘制矩形框失败: {e}")
return image_path
from Util.EasyOcrKit import get_easyocr_reader
# 预加载 EasyOCR Reader (单例模式)
def get_ocr_reader():
return get_easyocr_reader(gpu=True)
def detect_price_info_container_cv(image_path):
"""
使用 OCR 精准定位详情页中的价格入口文本(“阶段性电价”)。
返回: [x1, y1, x2, y2] 绝对像素坐标,如果未找到则返回 None
"""
img = read_image(image_path)
if img is None:
return None
h, w = img.shape[:2]
keywords = ['阶段性电价']
try:
reader = get_ocr_reader()
results = reader.read_text(img)
for (quad, text, prob) in results:
if any(kw in text for kw in keywords) and prob >= 0.5:
pts = np.array(quad).astype(int)
x_min = np.min(pts[:, 0])
y_min = np.min(pts[:, 1])
x_max = np.max(pts[:, 0])
y_max = np.max(pts[:, 1])
center_x = (x_min + x_max) // 2
center_y = (y_min + y_max) // 2
logger.info(f"[OCR识别] 找到文本: '{text}', 置信度: {prob:.4f}, 中心坐标: ({center_x}, {center_y})")
return (center_x, center_y)
except Exception as e:
logger.error(f"OCR 识别发生异常: {e}")
return None
def setup_logger(name, log_file=None, clear_old_log=False):
"""
配置日志,支持同时输出到控制台和文件。