采用EasyOCR将单个图像文件中的文本自动识别(OCR)

单个图像文件文本识别

运行方式:python ocrimg.py 图像文件名;
返回结果:文本识别结果保存在文件 图像文件名前缀.md 中;
可以在支持Markdown的编辑器中查看;
需要安装 easyocr 软件包和中文支持库。

TODO:1)截屏并识别,可复制。2)支持数学公式等符号识别

CF: https://www.jaided.ai/easyocr/modelhub/
Model Directory: C:\Users\Administrator.EasyOCR\model

代码

import easyocr
import os
import time
from sys import argv


def print_ocr(result):
    """将识别出来的文本分行打印
    1. 需要按照 box 的y轴坐标升序排列;
    2. 根据文本框的坐标来判断分行行数时。每行的像素超过多少合适? 30 pixels?
    """

    def newlines(delta, pixels, flag=True):
        nline = int(delta//pixels)
        if nline < 5:
            return '\n'
        elif nline > 10:
            return '$\\\\[{0}em]$\n'.format(10) if flag else '\n'*11
        else:
            return '$\\\\[{0}em]$\n'.format(nline-1) if flag else '\n'*nline

    # 此处需要测试范例文件来决定 每行所占像素值是否符合源文件。
    px_line = 40
    rtxt = result[0][1]  # 第一个识别结果
    prestart = result[0][0][0]  # 第一个识别结果的坐标起点
    for txt in result[1:]:  
        #print(str(txt[1]))  ##是否顺序显示,确实按照 y 轴升序显示的。
        curstart = txt[0][0]
        deltax = curstart[1] - prestart[1]
        if deltax > px_line:
            #rtxt += '\n'*int(deltax//px_line)  #用空行
            rtxt += newlines(deltax, px_line)
            #flag=True 转化为Markdown的换行符, flag=False 还是采用换行符'\n'
        else:
            rtxt += ' '
        rtxt += txt[1]
        prestart = curstart

    return rtxt



if __name__ == '__main__':
    if len(argv) > 1:
        start = time.time()
        input_img = argv[1]
        
        if os.path.isfile(input_img): 
            infile = input_img
        else:
            print("输入的文件{0}不存在!".format(input_img))
            exit()

        sep = ' '
        enter = '<div STYLE="page-break-after: always;"></div>\n\n'
        outfile = argv[1].split(sep='.')[0] 
        if len(outfile) ==0:
            outfile = "temp_eocr"
        outfile = outfile + ".md"

        with open(outfile, encoding='utf-8', mode='a') as output:
            print("正处理 {0} ......".format(infile))
            output.write('## ' + infile + '\n\n')
            reader = easyocr.Reader(['ch_sim','en'], gpu = False)
            #,'la','ro'-->ValueError: Chinese_sim is only compatible with English, try lang_list=["ch_sim","en"]
            result = reader.readtext(infile, detail=1)  #detail=0:简化版,detail=1:详细版
            myLst = print_ocr(result)
            output.write(myLst+'\n\n')  #将本文件内容写入文件。
            output.write(enter)  #写入分页符 Markdown 格式中用的HTML 分页符

        print("运行耗时{0}秒!".format(time.time()-start))
    else:
        print("""
        使用说明:python ocrimg.py 文件名
        使用举例:python ocrimg.py imgfile.png
        """)

失败的识别举例

已–>巳, 时–>肘, x–>工, 大–>犬,n --> 兀, - --> 一, | -->1, 50–>S0, 距–>眍,8–>&, X --> 义丫

小时–>小肘,时间–>时闻; 笫一–>第一,笫二–>第二,笫三-第三,笫四-第四,笫五-第五,蔬菜-蔬莱,填入–>填人. 100–>IO0, 逗号,–>小数点

数学公式基本识别不了。

没有考虑使用频率,以及和词组关联。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java OCR Framework An Optical Character Recognition Framework written purely in Java. Installation Build the project and add the jar for the project along with all the jars in the jar directory to your compile-time libraries. Usage There are 4 main parts to OCR: Normalization Segmentation Feature Extraction Classification Feature Extraction and Classification are the only required parts. For Feature Extraction there are 5 algorithms at your disposal Horizontal Celled Projection Vertical Celled Projection Horizontal Projection Histogram Vertical Projection Histogram Local Line Fitting This framework loosely uses a Fluent Interface Builder syntax. Example: OCR ocr = OCRBuilder .create() .normalization(new Normalization()) .segmentation(new Segmentation()) .featureExtraction( FeatureExtractionBuilder .create() .children( new HorizontalCelledProjection(5), new VerticalCelledProjection(5), new HorizontalProjectionHistogram(), new VerticalProjectionHistogram(), new LocalLineFitting(49)) .build()) .neuralNetwork( NeuralNetworkBuilder .create() .fromFile("neural_network.eg") .build()) .build(); Contributing Want to help out? Feel free to share your ideas. Fork it. Create a branch (git checkout -b my_fancy_feature) Commit your changes (git commit -am "Added amazing feature") Push to the branch (git push origin my_fancy_feature) Open a Pull Request References Arora, Sandhya (2008). “Combining Multiple Feature Extraction Techniques for Handwritten Devnagari Character Recognition”, IEEE Region 10 Colloquium. pp. 342-348 Haykin, Simon (1999). “Neural Networks A Comprehensive Foundation”, 2nd Edition. Pearson Education. Perez, Juan-Carlos ; Vidal, Enrique ; Sanchez, Lourdes (1994). “Simple and Effective Feature Extraction for Optical Character Recognition”, Selected Paper From the 5th Spanish Symposium on Pattern Recognition and Image Analysis. Zahid Hossain, M. ; Ashraful Amin, M. ; Yan, Hong (2012). “Rapid Feature Extraction for Optical Character Recognition”, IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol. 24, No. 6. pp. 801-813 Thanks Thanks to Heaton Research for providing an amazing Neural Network framework. Also thanks to Apache Math Commons for doing all the math without the mess.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值