• 使用python的pytesseract调用谷歌tesseract-ocr识别中英文字符


    tesseract-ocr简介

    一款免费的开源图像OCR文字识别引擎,初期Tesseract引擎由HP实验室研发,后来贡献给了开源软件业,后由Google进行改进、修改bug、优化,重新发布。它就能根据你的命令将你想要识别的图片中的文字转换成文本的形式,或者转换成能被常规文本编辑器编辑的文本如pdf。到目前为止,它已经支持简体中文、繁体中文、英文、日文、韩文等等60多种语言的识别。并随着大家对它功能上的要求在不断改进、不断消除bug、优化功能。

    Pytesseract简介

    Pytesseract是python的光学字符识别(OCR)工具。也就是说,它将识别并读取嵌入图像中的文本。 Pytesseract是Google的Tesseract-OCR引擎的包装器。它作为独立的调用脚本也很有用,因为它可以读取Python Imaging Library支持的所有图像类型,包括jpeg,png,gif,bmp,tiff等,而tesseract-ocr默认只支持tiff和bmp。

    安装

    安装tesseract-ocr

    sudo apt-get install tesseract-ocr               
    

    安装语言库

    tesseract-ocr-eng是英文库,tesseract-ocr-chi-sim是中文库

    sudo apt-get install tesseract-ocr-eng tesseract-ocr-chi-sim            
    

    安装依赖及pytesseract

    pytesseract是python调用谷歌tesseract-ocr工具的一个库,用于识别图片中的信息

    # 安装Pillow
    sudo pip3 install Pillow
    # 安装pytesseract
    sudo pip3 install pytesseract           
    

    使用

    try:
        from PIL import Image
    except ImportError:
        import Image
    import pytesseract
    
    # 如果PATH中没有tesseract可执行文件,请指定tesseract路径
    pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
    # Example tesseract_cmd = r'/usr/share/tesseract'
    
    # 识别的图像的字符串
    print(pytesseract.image_to_string(Image.open('test.png')))
    
    # 指定语言识别图像字符串,eng为英语
    print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
    
    # In order to bypass the image conversions of pytesseract, just use relative or absolute image path
    # NOTE: In this case you should provide tesseract supported images or tesseract will return error
    print(pytesseract.image_to_string('test.png'))
    
    # Batch processing with a single file containing the list of multiple image file paths
    print(pytesseract.image_to_string('images.txt'))
    
    # Timeout/terminate the tesseract job after a period of time
    try:
        print(pytesseract.image_to_string('test.jpg', timeout=2)) # Timeout after 2 seconds
        print(pytesseract.image_to_string('test.jpg', timeout=0.5)) # Timeout after half a second
    except RuntimeError as timeout_error:
        # Tesseract processing is terminated
        pass
    
    # 获取图像边界框
    print(pytesseract.image_to_boxes(Image.open('test.png')))
    
    # 获取包含边界框,置信度,行和页码的详细数据
    print(pytesseract.image_to_data(Image.open('test.png')))
    
    # 获取方向和脚本检测
    print(pytesseract.image_to_osd(Image.open('test.png')))
    
    # Get a searchable PDF
    pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf')
    with open('test.pdf', 'w+b') as f:
        f.write(pdf) # pdf type is bytes by default
    
    # Get HOCR output
    hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr')
    

    示例

    提取本地图片上的文字为一个整体的字符串

    def get_image_string(path, filename):
        '''使用谷歌开源框架ocr技术提取图片上的信息为字符串
    
        :Param path: <str> 图片的位置
    
        :Param filename: <str> 图片的名称
    
        :Return : <string> 从图片中提取的字符串
    
        '''
        username = getpass.getuser()
        path_base = '/home/' + str(username) + '/' + str(path) + '/' + str(filename) + '.png'
        text = pytesseract.image_to_string(Image.open(path_base), lang="chi_sim").replace(" ", "").replace("
    ", "")
        print(text)
        return text
  • 相关阅读:
    在peoplecode中直接调用SQR
    想起了李雷和韩梅梅
    结婚两周年纪念
    Unix Command Summary
    在PeopleSoft中如何隐藏菜单,导航栏,以及其他定制化链接
    那些朋友们
    整天工作的人为何当不了富翁
    ActiveX简单介绍
    SQL UNION
    Java程序设计问答大全(一)
  • 原文地址:https://www.cnblogs.com/qinlangsky/p/13491528.html
Copyright © 2020-2023  润新知