EasyOCR,OCR识别

pip install easyocr

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
EasyOCR OCR demo
用法: python ocr_easyocr_demo.py /path/to/image.jpg
"""

import sys
import easyocr
import cv2

def preprocess_cv(img_path):
    # 这里做轻度预处理:缩放 + 灰度
    img = cv2.imread(img_path)
    if img is None:
        raise FileNotFoundError(f"无法读取图片: {img_path}")
    h, w = img.shape[:2]
    if max(h, w) < 1000:
        img = cv2.resize(img, (int(w*2), int(h*2)), interpolation=cv2.INTER_LINEAR)
    # EasyOCR 接受彩色或灰度,传递原始 BGR 即可(库会做内部处理)
    return img

def ocr_with_easyocr(img_path, langs=['ch_sim','en']):
    img = preprocess_cv(img_path)
    # 创建 reader,指定语言。第一次创建会下载模型(若没缓存)
    reader = easyocr.Reader(langs, gpu=False)  # gpu=True 在有GPU时更快
    # 返回值为每个检测到的文本框: (bounds, text, confidence)
    results = reader.readtext(img)
    return results

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("用法: python ocr_easyocr_demo.py /path/to/image.jpg")
        sys.exit(1)
    path = sys.argv[1]
    try:
        res = ocr_with_easyocr(path)
        print("识别结果(按检测顺序):\n" + "="*30)
        for bbox, text, prob in res:
            print(f"[{prob:.3f}] {text}  bbox:{bbox}")
        print("="*30)
    except Exception as e:
        print("出错:", e)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值