首页 > 代码库 > Python调用Tesseract-OCR和Zxing完成图片OCR识别和二维码解码

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识别和二维码解码