用cv2模块读取和显示模块
#导包cv2拓展模块import cv2#先给窗体起名字cv2.namedWindow("ShowImage1")cv2.namedWindow("ShowImage2")image1 = cv2.imread("img01.jpg",1)image2 = cv2.imread("img01.jpg",0) # 位置参数,第二个0代表灰度模式# 创建窗口句柄cv2.imshow("ShowImage1",image1)cv2.imshow("ShowImage2",image2)cv2.waitKey(0) # 0代表按任何键后继续执行程序# 释放窗口句柄cv2.destroyAllWindows()
OCR开源识别程序
Downloadfor old versions of Tesseract
Tesseract 3
https://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-3.05.02-0621.exe
调用tesseract程序识别图片
出处:毫无障碍学Python
10.2.1 简单的OCR-Tesseract包
图片
代码
import subprocess# 1、调用tesseract识别图片,并将结果保存至result.txt文件中ocr = subprocess.Popen("tesseract text1.jpg result")# 2、识别过程中插入程序等待时间ocr.wait()# 3、打开并读取文本文件,使用strip()去除头尾text1 = open("result.txt",mode ='r',encoding='utf-8').read().strip()print(text1)
结果
How to change background image without changing drawn lines
待识别的验证码
图像识别OpenCV处理
import cv2, subprocess# 已安装OpenCV-python模块img = cv2.imread("media\\bank.jpg") #讀圖cv2.namedWindow("Image") cv2.imshow("Image", img) #顯示圖形cv2.waitKey(0) #参数为0,则不会有返回值cv2.destroyWindow("Image")gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #轉為灰階_, inv = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV) #轉為反相黑白for i in range(len(inv)): #i為每一列for j in range(len(inv[i])): #j為每一行if inv[i][j] == 255: #顏色為白色count = 0 for k in range(-2, 3):for l in range(-2, 3):try:if inv[i + k][j + l] == 255: #若是白點就將count加1count += 1except IndexError:passif count <= 6: #週圍少於等於6個白點inv[i][j] = 0 #將白點去除dilation = cv2.dilate(inv, (8,8), iterations=1) #圖形加粗cv2.imwrite("media\\bank_t.jpg", dilation) #存檔文件# 已安装tesseract-ocr-setup-3.02.02.exechild = subprocess.Popen('tesseract media\\bank_t.jpg result') #OCR辨識child.wait() #等待text = open('result.txt').read().strip() #读取文本print("驗證碼為 " + text) #打印输出
结果:
Tesseract Open Source OCR Engine v3.02 with Leptonica
驗證碼為 698139
如果觉得《python实现OCR识别图片验证码》对你有帮助,请点赞、收藏,并留下你的观点哦!