You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

EasyOCR识别葡萄牙语单独字母'e'异常的解决方法咨询

解决EasyOCR无法稳定识别葡萄牙语单独字母"e"的问题

问题概述

使用Python结合EasyOCR识别葡萄牙语文本时,无法稳定识别大词之间作为连接词的单独字母"e":部分区域完全检测不到该字母,部分区域仅能识别部分位置的"e"。

原代码

def getOCRResults(imgFP)->list[tuple[tuple[int,int,int,int]|None,str|None,float|None]]:

    reader = easyocr.Reader(['pt'], gpu=False)
    result = reader.readtext(imgFP, width_ths=0.7, add_margin=0.2, height_ths=0.8)
    return result


def draw_bounding_boxes(image, detections, threshold=0.25):
    for bbox, text, score in detections:
        if score > threshold:
            cv2.rectangle(image, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (0, 255, 0), 1)
            cv2.putText(image, text, tuple(map(int, bbox[0])), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 0, 0), 1)

OCRResults = getOCRResults(image_path)
img = cv2.imread(image_path)
draw_bounding_boxes(img,OCRResults,0.25)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGBA))
plt.show()

解决方案

1. 调整EasyOCR检测参数

EasyOCR默认参数对单个窄小字母的检测不够友好,可通过调整以下参数提升"e"的识别率:

  • width_ths:降低阈值,允许更窄的文本块被检测(原0.7改为0.3)
  • add_margin:增大边距,让单个字母更容易被纳入检测范围(原0.2改为0.3)
  • height_ths:降低高度阈值,适配不同行高的单个字母(原0.8改为0.5)
  • min_size:设置为1,避免忽略极小的文本块

修改后的getOCRResults函数:

def getOCRResults(imgFP)->list[tuple[tuple[int,int,int,int]|None,str|None,float|None]]:
    reader = easyocr.Reader(['pt'], gpu=False)
    # 适配单个字母检测的参数调整
    result = reader.readtext(
        imgFP,
        width_ths=0.3,
        add_margin=0.3,
        height_ths=0.5,
        min_size=1
    )
    return result

2. 图像预处理增强辨识度

如果图像存在模糊、光照不均或噪声,会影响单个字母的检测。可通过预处理提升图像清晰度:

def preprocess_image(img_path):
    img = cv2.imread(img_path)
    # 转灰度图
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # 自适应二值化,适配光照不均场景
    thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2)
    # 降噪处理
    denoised = cv2.medianBlur(thresh, 1)
    return denoised

调用时传入预处理后的图像:

preprocessed_img = preprocess_image(image_path)
OCRResults = getOCRResults(preprocessed_img)

3. 后处理上下文补全

如果仍有漏检,可基于葡萄牙语的语言规律(常见"X e Y"结构),通过后处理补全缺失的"e":

def post_process_results(results):
    texts = []
    bboxes = []
    scores = []
    for bbox, text, score in results:
        if score > 0.25:
            texts.append(text)
            bboxes.append(bbox)
            scores.append(score)
    
    processed_results = []
    for i in range(len(texts)):
        processed_results.append((bboxes[i], texts[i], scores[i]))
        # 检查相邻文本块的间距,判断是否可能缺失"e"
        if i < len(texts) - 1:
            current_right = bboxes[i][2][0]
            next_left = bboxes[i+1][0][0]
            gap = next_left - current_right
            # 间距小于20像素时,补全"e"(可根据实际图像调整阈值)
            if gap < 20:
                # 估算"e"的 bounding box
                e_bbox = (
                    (current_right + 5, bboxes[i][0][1]),
                    (next_left - 5, bboxes[i][0][1]),
                    (next_left - 5, bboxes[i][2][1]),
                    (current_right + 5, bboxes[i][2][1])
                )
                processed_results.append((e_bbox, "e", 0.9))
    return processed_results

使用后处理结果绘制框:

OCRResults = getOCRResults(image_path)
processed_results = post_process_results(OCRResults)
draw_bounding_boxes(img, processed_results, 0.25)

内容的提问来源于stack exchange,提问作者Anika

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 09:58:14