python训练好的图片验证_python 识别图形验证码

pip 安装 pytesseract

pip install pytesseract

安装tesseract-ocr

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''

# 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 ""'

# 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

自制训练库

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

Pillow

pip install Pillow

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值