python 识别图形验证码

2018-08-13  本文已影响31人  proud2008

pip 安装 pytesseract

Pytesseract地址:https://github.com/madmaze/pytesseract

pip install pytesseract   

安装tesseract-ocr

jTessBoxEditor用于训练识别库中包含tesseract

https://github.com/tesseract-ocr/tesseract/wiki
https://pan.baidu.com/s/1FPd8O03pus98fNGA5RzVGQ
https://download.csdn.net/download/whatday/7740469
安装完成后配置环境变量
使用
https://github.com/tesseract-ocr/tesseract/wiki
语音包
https://github.com/tesseract-ocr/tesseract/wiki/Data-Files

pytesseract配置

1、若tesseract未加入系统变量则修改
找到 D:\Python27\Lib\site-packages\pytesseract\pytesseract.py
python安装第三方包的路径 修改

# CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY
#tesseract_cmd = 'tesseract'
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'

或者在代码中指定:

# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
# Example  pytesseract.pytesseract.tesseract_cmd = r'D:\tesseract\tesseract.exe'

2、指定训练识别库

tessdata_dir_config = r'--tessdata-dir "<replace_with_your_tessdata_dir_path>"'
# Example config: r'--tessdata-dir "C:\Program Files (x86)\Tesseract-OCR\tessdata"'
# It's important to add double quotes around the dir path.

pytesseract.image_to_string(image, lang='chi_sim', config=tessdata_dir_config)

识别测试

test.png
try:
    import Image
except ImportError:
    from PIL import Image #Pillow模块
import pytesseract
print pytesseract.image_to_string(Image.open("test.png").convert('L')) #Convert能够优化识别

输出结果

378m

自制训练库

https://blog.csdn.net/lilin020401/article/details/39340533
下载jTessBoxEditor

图片处理模块 以提高识别成功率

Pillow

pip install Pillow

Pillow 使用 https://blog.csdn.net/wfy2695766757/article/details/81193370
https://blog.csdn.net/claroja/article/details/79084747
https://python-pillow.org/

opencv

安装 pip install opencv-python不是pip install cv2(会报错)
也可下载手动安装到这儿http://www.lfd.uci.edu/~gohlke/pythonlibs/去下载opencv选择对应的版本
在Python中执行

import pip._internal
print(pip._internal.pep425tags.get_supported())

[('cp36', 'cp36m', 'win32'), ('cp36', 'none', 'win32'), ('py3', 'none', 'win32'), ('cp36', 'none', 'any'), ('cp3', 'none', 'any'), ('py36', 'none', 'any'), ('py3', 'none', 'any'), ('py35', 'none', 'any'), ('py34', 'none', 'any'), ('py33', 'none', 'any'), ('py32', 'none', 'any'), ('py31', 'none', 'any'), ('py30', 'none', 'any')]

从列表中选择上面返回中有的

image.png
执行
pip install opencv_python-***.whl
在python下运行import cv2不报错就是安装成功了
顺利地安装完了
文档 https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_intro/py_intro.html
# !/usr/bin/python
# -*- coding: utf-8 -*-
# import cv2
from __future__ import print_function
import cv2
import numpy as np

# help(cv)
img = cv2.imread("lhcheng2/img/%1.jpg")
cv2.imshow("aa", img)
# 灰度
imger = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("imger", imger)
# 二值化
ret, imgere = cv2.threshold(imger, 127, 255, cv2.THRESH_BINARY)
cv2.imshow("imgere", imgere)
cv2.imwrite("imgere.png",imgere)
# 腐蚀
kernel = np.ones((2, 2), np.uint8)
erode = cv2.erode(imgere, kernel)
cv2.imshow("erode", erode)
cv2.imwrite("erode.png",erode)
# 边缘检测
canny = cv2.Canny(erode, 30, 100)
cv2.imshow("canny", canny)
cv2.imwrite("canny.png",canny)
# 霍夫变换找直线
lines = cv2.HoughLinesP(canny, 1, np.pi / 180, 30, minLineLength=60, maxLineGap=10)
lines1 = lines[:, 0, :]  # 提取为二维
# 画线
for x1, y1, x2, y2 in lines1[:]:
    cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("img-END", img)
上一篇下一篇

猜你喜欢

热点阅读