easyocr 本地部署模型 识别图像 ocr - python 实现

发布于:2024-10-17 ⋅ 阅读:(13) ⋅ 点赞:(0)

使用 easyocr 本地部署识别图像 ocr ,可以满足简单图像场景的ocr识别。

可以进行 中文、英文 ocr 识别。

安装 python 库

pip install easyocr

识别本地模型下载地址:easyocr本地部署模型识别图像ocr-python实现资源-CSDN文库

也可通过程序直接下载官方链接

识别示例代码具体如下:

#-*-coding:utf-8-*-
# date:2021-03-21
# Author: DataBall - XIAN
# Function: 实现图像 OCR 定位和识别

import os
os.environ['EASYOCR_DATA_DIR'] = './ckpt'
import easyocr
import cv2
import numpy as np

if __name__ == "__main__":
    reader = easyocr.Reader(['ch_sim','en'], gpu=True,model_storage_directory = "./ckpt") # 加载本地模型
    path_img= r"examples\s1.jpg"
    # path_img= r"examples\s2.jpg"
    print("path_img:",path_img)
    result = reader.readtext(path_img)
    # print("result:",result)
    print("len:",len(result))
    img = cv2.imread(path_img)

    idx = 0
    for m_ in result:
        idx += 1
        print(" [{}] {} ".format(idx,m_)) # 打印 ocr 识别结果
        pts = np.array(m_[0])
        pts = pts.reshape((-1,1,2))  #reshape为10x1x2的numpy
        # print(pts.shape)
        try:
            # 绘制区域
            cv2.polylines(img,[pts],True,(255,0,0),5)
            cv2.polylines(img,[pts],True,(0,02,550),2)
        except:
            continue
    cv2.namedWindow("img",0)
    cv2.imshow("img",img)
    cv2.waitKey(0)

【英文】识别ocr的示例 log如下:

path_img: examples\s1.jpg
len: 6
 [1] ([[215, 220], [372, 220], [372, 269], [215, 269]], 'Mairie du Il', 0.7160473332813915)
 [2] ([[256, 321], [477, 321], [477, 383], [256, 383]], 'IPalais du LOUVRE', 0.4025377946164086)
 [3] ([[242, 418], [515, 418], [515, 474], [242, 474]], 'LES ARTS DECORATIFS', 0.9520467181101142)
 [4] ([[260, 538], [481, 538], [481, 590], [260, 590]], 'IMusee du LOUVRE', 0.6906741106430193)
 [5] ([[197, 637], [281, 637], [281, 675], [197, 675]], 'Theatre', 0.9687920530479167)
 [6] ([[195, 668], [378, 668], [378, 713], [195, 713]], 'du PALAIS-ROYAII', 0.38048889529807867)

【英文】识别示例显示结果如下图:

【中文】 ocr识别示例 log如下:

path_img: examples\s2.jpg
len: 18
 [1] ([[68, 4], [411, 4], [411, 120], [68, 120]], 'T', 0.02513536960325169)
 [2] ([[53, 199], [151, 199], [151, 219], [53, 219]], '小科(', 0.005845191785346051)
 [3] ([[51, 227], [121, 227], [121, 267], [51, 267]], '雷军', 0.9930638791278288)
 [4] ([[312, 302], [346, 302], [346, 328], [312, 328]], '你', 0.9963706343826892)
 [5] ([[315, 329], [409, 329], [409, 349], [315, 349]], '不愿薏听我的', 0.12065996670191718)
 [6] ([[319, 357], [385, 357], [385, 377], [319, 377]], '我就闭嘴', 0.3216131031513214)
 [7] ([[330, 414], [376, 414], [376, 438], [330, 438]], '加旦', 0.00018517122243834601)
 [8] ([[334, 442], [442, 442], [442, 466], [334, 466]], '以后证明你错了', 0.9192290431047331)
 [9] ([[333, 473], [397, 473], [397, 493], [333, 493]], '而我了', 0.7705055472317546)
 [10] ([[333, 501], [443, 501], [443, 521], [333, 521]], '那你要向我道歉', 0.82041872865308)
 [11] ([[361, 625], [443, 625], [443, 645], [361, 645]], '本刊记者 :花月', 0.5882038669206735)
 [12] ([[47, 653], [69, 653], [69, 665], [47, 665]], '20元', 0.1347271710802868)
 [13] ([[93, 653], [125, 653], [125, 665], [93, 665]], '塑36?}', 0.0013240344658845757)
 [14] ([[131, 653], [189, 653], [189, 665], [131, 665]], '201841月288', 0.615746162991523)
 [15] ([[200, 654], [224, 654], [224, 662], [200, 662]], 'SO', 0.10028489515834976)
 [16] ([[231, 653], [259, 653], [259, 665], [231, 665]], '0', 0.0001401376108900185)
 [17] ([[262, 654], [326, 654], [326, 662], [262, 662]], 'IT6ArC', 0.0034239475033886247)
 [18] ([[346, 654], [422, 654], [422, 662], [346, 662]], 'SONo', 0.014922617934644222)

【中文】识别示例显示结果如下图:

​​

助力快速掌握数据集的信息和使用方式。

数据可以如此美好