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

82
Util/EasyOcrKit.py Normal file
View File

@@ -0,0 +1,82 @@
# coding=utf-8
import easyocr
import numpy as np
import cv2
import logging
logger = logging.getLogger(__name__)
class EasyOcrKit:
_instance = None
_reader = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(EasyOcrKit, cls).__new__(cls)
return cls._instance
def __init__(self, langs=['ch_sim', 'en'], gpu=True):
"""
初始化 EasyOCR Reader
:param langs: 识别语言列表
:param gpu: 是否使用 GPU
"""
if self._reader is None:
try:
self._reader = easyocr.Reader(langs, gpu=gpu)
logger.info(f"EasyOCR Reader 初始化成功 (gpu={gpu})")
except Exception as e:
logger.error(f"EasyOCR Reader 初始化失败: {e}")
# 如果 GPU 失败,尝试回退到 CPU
if gpu:
logger.warning("尝试回退到 CPU 模式...")
self._reader = easyocr.Reader(langs, gpu=False)
def read_text(self, image):
"""
识别图片中的文字
:param image: 图片路径或 OpenCV 图像对象
:return: EasyOCR 识别结果列表
"""
if self._reader is None:
return []
return self._reader.readtext(image)
def find_text_position(self, image, target_text, threshold=0.5):
"""
在图片中查找特定文本的位置
:param image: 图片路径或 OpenCV 图像对象
:param target_text: 目标文本
:param threshold: 置信度阈值
:return: (found_text, quad, probability) 如果没找到则返回 None
"""
results = self.read_text(image)
for (quad, text, prob) in results:
if target_text in text and prob >= threshold:
return text, quad, prob
return None
def get_normalized_rect(self, quad, width, height):
"""
获取归一化的矩形坐标 [x1, y1, x2, y2] (0-1000)
:param quad: EasyOCR 返回的四个顶点坐标
:param width: 图片宽度
:param height: 图片高度
:return: [x1, y1, x2, y2]
"""
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])
return [
int(max(0, x_min) * 1000 / width),
int(max(0, y_min) * 1000 / height),
int(min(width, x_max) * 1000 / width),
int(min(height, y_max) * 1000 / height)
]
# 便捷函数
def get_easyocr_reader(gpu=True):
return EasyOcrKit(gpu=gpu)

Binary file not shown.