• Python调用Tesseract-OCR和Zxing完成图片OCR识别和二维码解码


    [硬件环境]

    Win10 64位

    [软件环境]

    Python版本:2.7.3

    Python库:

    1.1) Pillow

    1.2) Pytesseract

    2.1) Jpype

    其他:

    1.1) Tesseract-OCR的可执行文件

    2.1) JDK 1.6

    2.2) ZXing Jar包:javase-2.2.jar和core-2.2.jar

    [搭建过程]

    Tesseract-OCR:

    1. 安装Tesseract-OCR的可执行文件

    2. 安装Pillow库

    3. 安装Pytesseract库

    ZXing:

    1. 安装JDK 1.6

    2. 下载ZXing Jar包:javase-2.2.jar和core-2.2.jar供使用

    3. 安装Jpype库以供Python调用Jar包

    [相关代码]

    # 1.1.Install tesseract-ocr*.exe from http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe
    # 1.2.Install pillow as "pip install form *.whl" in the zip file directory in powershell
    # 1.3.Install pytesseract as "pip install form *.whl" in the zip file directory in powershell
    # 2.1.Install jpype as "python setup.py install" in the zip file directory in powershell
    import os.path
    import pytesseract
    from PIL import Image
    from jpype import *
    
    ##################################################### Tesserace-ocr ####################################################
    image = Image.open(r"D:ProjectFirstPythonOCR2.png")
    code = pytesseract.image_to_string(image)
    code = code.replace(' ', '');
    code = code.replace('-', '');
    code = code.replace('
    ', '');
    code = code.replace('
    ', '');
    print(code)
    
    ##################################################### Zxing Decode #####################################################
    # Start jvm
    jarpath = os.path.join(os.path.abspath('.'), 'D:/Project/FirstPython/')
    startJVM("C:/Program Files/Java/jre6/bin/server/jvm.dll", "-ea", ("-Djava.class.path=%s" % (jarpath + "javase-2.2.jar" + ";" + jarpath + "core-2.2.jar")))
    # Load the useful library classes
    File = JClass("java.io.File")
    BufferedImage = JClass("java.awt.image.BufferedImage")
    ImageIO = JClass("javax.imageio.ImageIO")
    BinaryBitmap = JClass("com.google.zxing.BinaryBitmap")
    DecodeHintType = JClass("com.google.zxing.DecodeHintType")
    LuminanceSource = JClass("com.google.zxing.LuminanceSource")
    BufferedImageLuminanceSource = JClass("com.google.zxing.client.j2se.BufferedImageLuminanceSource")
    MultiFormatReader = JClass("com.google.zxing.MultiFormatReader")
    NotFoundException = JClass("com.google.zxing.NotFoundException")
    Result = JClass("com.google.zxing.Result")
    HybridBinarizer = JClass("com.google.zxing.common.HybridBinarizer")
    Hashtable = JClass("java.util.Hashtable")
    # Do the qrcode decode
    image = ImageIO.read(File("D:/Project/FirstPython/Qrcode1.jpg"))
    source = BufferedImageLuminanceSource(image)
    bitmap = BinaryBitmap(HybridBinarizer(source))
    hints = Hashtable()
    hints.put(DecodeHintType.CHARACTER_SET, "GBK")
    result = MultiFormatReader().decode(bitmap, hints)
    print(result)
    # Shutdown JVM
    shutdownJVM()
  • 相关阅读:
    SpringBoot 整合Redis
    IDEA 修改之前保存的git地址的账号和密码
    SpringBoot 上传文件功能
    JAVA结合 JSON Web Token(JWT) 工具类
    SpringBoot 整合Spring Security框架
    Sublime Text3 设置
    Python 正则表达式
    Django+uWSGI+Nginx 部署网站
    web 设计中引入字体
    HTML 超链接返回上一级
  • 原文地址:https://www.cnblogs.com/jayhust/p/6398318.html
Copyright © 2020-2023  润新知