微信扫码
添加专属顾问
 
                        我要投稿
快速搭建高精度OCR识别服务,轻松应对多语言文字识别需求。 核心内容: 1. EasyOCR特点:轻量、识别率高、支持多语言 2. 环境准备与安装步骤 3. 基础使用与高级参数配置指南
 
                                最近接到一个需求:需要从邮件中自动下载某些邮件的附件,而这些附件是经过加密的压缩包,解压码在对应的图片中,所以需要自动识别图片中的解压码,然后去解压文件。
经过各种调研,对比了几种OCR,最后选定了EasyOCR,轻量、识别率高、开源免费!
EasyOCR 是一个用 Python 编写的 OCR(光学字符识别)库,支持 80+ 种语言的文字识别。它具有以下特点:
# 使用 pip 安装
pip install easyocr
# 如果需要支持 GPU,请确保已安装 CUDA
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
import easyocr
# 初始化读取器
reader = easyocr.Reader(['ch_sim','en'])  # 这里选择中文简体和英文
# 读取图像
result = reader.readtext('image.jpg')
# 输出结果
for (bbox, text, prob) in result:
    print(f'识别文本: {text}')
    print(f'置信度: {prob}')
result = reader.readtext(
    'image.jpg',
    detail = 0,               # 设置为 0 只返回文本,坐标和置信度会隐藏
    paragraph = True,         # 将临近文本合并为段落
    min_size = 10,           # 最小文本框大小
    contrast_ths = 0.15,     # 对比度阈值
    adjust_contrast = 0.5,    # 对比度调整
    text_threshold = 0.7,     # 文本检测阈值
    low_text = 0.4,          # 文本检测低阈值
    link_threshold = 0.4,     # 文本行连接阈值
)
对外提供API:
部分代码如下:
from flask import Flask, request, jsonify, render_template, json
import easyocr
import numpy as np
from PIL import Image
app = Flask(__name__)
reader = easyocr.Reader(['ch_sim','en'])
@app.route('/', methods=['GET'])
def index():
    pass
    
@app.route('/ocr', methods=['POST'])
def ocr_endpoint():
    try:
        if'image'notin request.files:
            return jsonify({'error': 'No image provided'}), 400
            
        image = request.files['image']
        img = Image.open(image)
        result = reader.readtext(np.array(img))
        
        processed_result = []
        current_line = []
        current_line_boxes = []
        
        sorted_result = sorted(result, key=lambda x: (x[0][0][1] + x[0][2][1]) / 2)
        
        y_threshold = 10
        
        for i, (bbox, text, prob) in enumerate(sorted_result):
            current_y = (bbox[0][1] + bbox[2][1]) / 2
            
            ifnot current_line:
                current_line.append((bbox, text))
                current_line_boxes.append(bbox)
            else:
                prev_y = (current_line[0][0][0][1] + current_line[0][0][2][1]) / 2
                
                if abs(current_y - prev_y) <= y_threshold:
                    current_line.append((bbox, text))
                    current_line_boxes.append(bbox)
                else:
                    current_line.sort(key=lambda x: x[0][0][0])
                    processed_result.append({
                        'text': ' '.join(item[1] for item in current_line)
                    })
                    current_line = [(bbox, text)]
                    current_line_boxes = [bbox]
        
        if current_line:
            current_line.sort(key=lambda x: x[0][0][0])
            processed_result.append({
                'text': ' '.join(item[1] for item in current_line)
            })
        
        response = app.response_class(
            response=json.dumps(
                {'result': processed_result}, 
                ensure_ascii=False,
                indent=2
            ),
            status=200,
            mimetype='application/json'
        )
        return response
        
    except Exception as e:
        return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8080, debug=True)  
GPU 加速
内存优化
图像预处理
内存不足
识别准确率不高
个人觉得EasyOCR 是一个功能强大且易用的 OCR 工具,适合各种文字识别场景,如果你也遇到需要和我一样的需求场景,也可以试试。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-29
初创公司的增长之道:如何让AI主动推荐你的产品?(下)
2025-10-29
AI将所有生意都卷成了创意产业
2025-10-28
ubuntu 本地部署MinerU完成文档解析
2025-10-27
AI浏览器的正确使用姿势是什么?我从Dia的这90+个Skills里找到了一些好场景
2025-10-27
魔笔 AI Chat Builder:让 AI 对话秒变可交互界面
2025-10-20
天猫行业中后台前端研发Agent设计
2025-10-20
告别错别字和退格键!这款AI语音输入法,让你的打字效率倍增(含实测体验)
2025-10-19
AI自动生成工作流,n8n官方出品
 
            2025-08-06
2025-09-17
2025-09-04
2025-09-02
2025-09-15
2025-09-05
2025-08-22
2025-09-18
2025-08-20
2025-10-10