python opencv pytesseract 验证码识别的实现
程序员文章站
2022-03-08 12:47:45
一、环境配置需要 pillow 和 pytesseract 这两个库,pip install 安装就好了。install pillow -i http://pypi.douban.com/simple...
一、环境配置
需要 pillow 和 pytesseract 这两个库,pip install 安装就好了。
install pillow -i http://pypi.douban.com/simple --trusted-host pypi.douban.com pip install pytesseract -i http://pypi.douban.com/simple --trusted-host pypi.douban.com
安装好tesseract-ocr.exe
pytesseract 库的配置:搜索找到pytesseract.py,打开该.py文件,找到 tesseract_cmd,改变它的值为刚才安装 tesseract.exe 的路径。
二、验证码识别
识别验证码,需要先对图像进行预处理,去除会影响识别准确度的线条或噪点,提高识别准确度。
实例1
import cv2 as cv import pytesseract from pil import image def recognize_text(image): # 边缘保留滤波 去噪 dst = cv.pyrmeanshiftfiltering(image, sp=10, sr=150) # 灰度图像 gray = cv.cvtcolor(dst, cv.color_bgr2gray) # 二值化 ret, binary = cv.threshold(gray, 0, 255, cv.thresh_binary_inv | cv.thresh_otsu) # 形态学操作 腐蚀 膨胀 erode = cv.erode(binary, none, iterations=2) dilate = cv.dilate(erode, none, iterations=1) cv.imshow('dilate', dilate) # 逻辑运算 让背景为白色 字体为黑 便于识别 cv.bitwise_not(dilate, dilate) cv.imshow('binary-image', dilate) # 识别 test_message = image.fromarray(dilate) text = pytesseract.image_to_string(test_message) print(f'识别结果:{text}') src = cv.imread(r'./test/044.png') cv.imshow('input image', src) recognize_text(src) cv.waitkey(0) cv.destroyallwindows()
运行效果如下:
识别结果:3n3d
process finished with exit code 0
实例2
import cv2 as cv import pytesseract from pil import image def recognize_text(image): # 边缘保留滤波 去噪 blur =cv.pyrmeanshiftfiltering(image, sp=8, sr=60) cv.imshow('dst', blur) # 灰度图像 gray = cv.cvtcolor(blur, cv.color_bgr2gray) # 二值化 ret, binary = cv.threshold(gray, 0, 255, cv.thresh_binary_inv | cv.thresh_otsu) print(f'二值化自适应阈值:{ret}') cv.imshow('binary', binary) # 形态学操作 获取结构元素 开操作 kernel = cv.getstructuringelement(cv.morph_rect, (3, 2)) bin1 = cv.morphologyex(binary, cv.morph_open, kernel) cv.imshow('bin1', bin1) kernel = cv.getstructuringelement(cv.morph_open, (2, 3)) bin2 = cv.morphologyex(bin1, cv.morph_open, kernel) cv.imshow('bin2', bin2) # 逻辑运算 让背景为白色 字体为黑 便于识别 cv.bitwise_not(bin2, bin2) cv.imshow('binary-image', bin2) # 识别 test_message = image.fromarray(bin2) text = pytesseract.image_to_string(test_message) print(f'识别结果:{text}') src = cv.imread(r'./test/045.png') cv.imshow('input image', src) recognize_text(src) cv.waitkey(0) cv.destroyallwindows()
运行效果如下:
二值化自适应阈值:181.0
识别结果:8a62n1process finished with exit code 0
实例3
import cv2 as cv import pytesseract from pil import image def recognize_text(image): # 边缘保留滤波 去噪 blur = cv.pyrmeanshiftfiltering(image, sp=8, sr=60) cv.imshow('dst', blur) # 灰度图像 gray = cv.cvtcolor(blur, cv.color_bgr2gray) # 二值化 设置阈值 自适应阈值的话 黄色的4会提取不出来 ret, binary = cv.threshold(gray, 185, 255, cv.thresh_binary_inv) print(f'二值化设置的阈值:{ret}') cv.imshow('binary', binary) # 逻辑运算 让背景为白色 字体为黑 便于识别 cv.bitwise_not(binary, binary) cv.imshow('bg_image', binary) # 识别 test_message = image.fromarray(binary) text = pytesseract.image_to_string(test_message) print(f'识别结果:{text}') src = cv.imread(r'./test/045.jpg') cv.imshow('input image', src) recognize_text(src) cv.waitkey(0) cv.destroyallwindows()
运行效果如下:
二值化设置的阈值:185.0
识别结果:7364process finished with exit code 0
到此这篇关于python opencv pytesseract 验证码识别的实现的文章就介绍到这了,更多相关opencv pytesseract 验证码识别内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
上一篇: Linux date命令的使用