• 图片验证码识别技术——Tesseraact


    将图片翻译成文字一般被称为光学文字识别(Optical  Character  Recognition),简称为OCR。

    实现ORC的库不是很多,特别是开源的,比较优秀的开源图像识别库——Tesseract。

    Tesseract:

    tesseract是一个OCR库。Tesseract是目前公认最优秀、最准确的开源OCR库。Tesseract具有

    很高的识别度,也具有很高的灵活性,可以通过训练识别任何字体。

    安装:

    windows系统:

    在以下链接下载可执行文件,然后一顿点击下一步安装即可(纯英文路径)。具体安装方法参考:https://blog.csdn.net/lianxiaobao/article/details/82465010(仅供参考,自行百度)。

    https://github.com/tesseract-ocr/

    linux系统:

    可以在以下链接下载源码自行编译。

    https://github.com/tesseract-ocr/tesseract/wiki/Compiling

    或者在Ubuntu下通过以下命令进行安装:

    sudo apt install tesseract-acr

    Mac系统:

    用Homebrew即可方便安装:

    brew install tesseract

    设置环境变量:

    安装完成后,如果想要在命令中使用Tesseract,那么应该设置环境变量。mac和Linux安装时默认设置好了。 

    在Windows下把tesseract.exe所在的路径添加到PATH环境变量中。还需要把训练的数据文件路径放到环境变量中,

    在环境变量中,添加一个TESSDATA_PREFIX=C:path_to_tesseractdata eseractdata。

    在命令行中使用tesseract识别图像:

    如果想要cmd下能够使用tesseract命令,那么需要把tesseract.exe所在的目录放到PATH环境变量中。

    然后使用命令:tesseract  图片路径  文件路径。识别不同的语言要下载不同的语言训练库。

    实例(注意:路径切换):

    tesseract a.png a

    那么就会识别出 a.png 中的图片,并且把文字写到 a.txt中。如果不想要写入文件直接想显示在终端,那么不要加文件名就可以了。

    查看安装的语言库:

    在代码中使用tesseract识别图像

    在python代码中操作tesseract,需要安装一个库,叫做pytesseract。通过pip的方式即可安装:

    pip install pytesseract

    并且需要读取图片,需要借助一个第三方库叫做PIL。通过pip list看下是否安装,如果没有安装,通过pip的方式安装:

    pip install PIL

    使用pytesseract将图片上的文字转换为文本文字的示例代码如下:

    #导入pytesseract库
    import pytesseract
    #导入Image库
    from PIL import Image
    # 指定tesseract.exe所有的路径
    pytesseract.pytesseract.tesseract_cmd = r'D:ProgramAppTesseractOCR	esseract.exe'
    # 打开图片
    image = Image.open('a.png')
    # 调用image_to_string将图片转为文字
    text = pytesseract.image_to_string(image)
    print(text)
    import pytesseract
    from urllib3 import request
    from PIL import Image
    import time
    
    def main():
        pytesseract.pytesseract.tesseract_cmd = r'D:	esseract-master	esseract.exe'
        # 图片的地址
        url = ' '
        while True:
            # 下载图片
            request.urlretrieve(url,'captcha.png')
            image = Image.open('captcha.png')
            # 识别图片
            text = pytesseract.image_to_string(image)
            print(text)
            time.sleep(2)
    
    
    if __name__ == '__main__':
        main()
    demo
  • 相关阅读:
    SQL Server 查看正在运行的事务信息的 2 种方法。
    SQL Server 查看正在运行的事务信息的 2 种方法。
    js防抖和限流
    js防抖和限流
    CSS cursor 属性
    CSS cursor 属性
    JS-中使用Math.round(x)保留1位小数点
    I/O系列教材 (一)- Java 的File类,以及常用方法
    异常处理系列教材 (五)- Java 自定义异常
    异常处理系列教材 (四)- java Throwable接口
  • 原文地址:https://www.cnblogs.com/-hao-/p/14191073.html
Copyright © 2020-2023  润新知