Python调用Tesseract-OCR和Zxing完成图片OCR识别和二维码解码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python调用Tesseract-OCR和Zxing完成图片OCR识别和二维码解码相关的知识,希望对你有一定的参考价值。
先贴代码:
# 1.Install tesseract-ocr*.exe from http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe # 2.Install pillow as"pip install form *.whl" # 3.Install pytesseract as"pip install form *.whl" import os.path import pytesseract from PIL import Image from jpype import *
##################################################### Tesserace-ocr #################################################### #image = Image.open(r"D:\Project\AI\1.MachineLearning\A.Tensorflow\2.Python_Prj\FirstPython\processed_7039.png") image = Image.open(r"D:\Project\AI\1.MachineLearning\A.Tensorflow\2.Python_Prj\FirstPython\OCR2.png") code = pytesseract.image_to_string(image) code = code.replace(‘ ‘, ‘‘); code = code.replace(‘-‘, ‘‘); code = code.replace(‘\r‘, ‘‘); code = code.replace(‘\n‘, ‘‘); print(code) ##################################################### Zxing Decode ##################################################### # Start jvm jarpath = os.path.join(os.path.abspath(‘.‘), ‘D:/Project/AI/1.MachineLearning/A.Tensorflow/2.Python_Prj/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/AI/1.MachineLearning/A.Tensorflow/2.Python_Prj/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()
以上是关于Python调用Tesseract-OCR和Zxing完成图片OCR识别和二维码解码的主要内容,如果未能解决你的问题,请参考以下文章
Python图片文字识别——Windows下Tesseract-OCR的安装与使用
使用Python,OpenCV进行Tesseract-OCR绑定及识别