验证码识别前期准备:
1.安装:tesseract-oc,直接安装就行,要配环境,下载地址点这里
参考资料:点这里,再说一下,实测一定要配环境!!!
2.安装pytesseract:pip install pytesseract
3.之前pil,Pillow已经装过,如果没有安装PIL的:请看这里
4.我的环境:python2.7&opencv3.4
一般如果是按照我之前的博客来装的,应该没问题,如果有,请自行百度!我也是百度好久才调试成功的~
代码如下:
-
# -*- coding=GBK -*-
-
import cv2
as cv
-
import Image
-
import pytesseract
-
-
-
def recognize_text():
-
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
-
ret, binary = cv.threshold(gray,
0,
255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
-
kernel = cv.getStructuringElement(cv.MORPH_RECT, (
1,
6))
-
binl = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel)
-
kernel = cv.getStructuringElement(cv.MORPH_RECT, (
5,
1))
-
open_out = cv.morphologyEx(binl, cv.MORPH_OPEN, kernel)
-
cv.bitwise_not(open_out, open_out)
# 背景变为白色
-
cv.imshow(
"转换", open_out)
-
textImage = Image.fromarray(open_out)
-
text = pytesseract.image_to_string(textImage)
-
print(
"This OK:%s"%text)
-
-
-
src = cv.imread(
"C://yzm.jpg")
-
cv.imshow(
"原来", src)
-
recognize_text()
-
cv.waitKey(
0)
-
cv.destroyAllWindows()
运行结果: