python 識別圖形驗證碼

pip 安裝 pytesseract

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

pip install pytesseract   

安裝tesseract-ocr

jTessBoxEditor用于訓(xùn)練識別庫中包含tesseract

https://github.com/tesseract-ocr/tesseract/wiki
https://pan.baidu.com/s/1FPd8O03pus98fNGA5RzVGQ
https://download.csdn.net/download/whatday/7740469
安裝完成后配置環(huán)境變量
使用
https://github.com/tesseract-ocr/tesseract/wiki
語音包
https://github.com/tesseract-ocr/tesseract/wiki/Data-Files

pytesseract配置

1、若tesseract未加入系統(tǒng)變量則修改
找到 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、指定訓(xùn)練識別庫

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能夠優(yōu)化識別

輸出結(jié)果

378m

自制訓(xùn)練庫

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選擇對應(yīng)的版本
在Python中執(zhí)行

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

執(zhí)行
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)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1.需要導(dǎo)入以下包(pytesseract.src里面應(yīng)該要用notepad++編輯Tesseract-OCR路徑...
    大樹_20e9閱讀 374評論 0 0
  • # Awesome Python [![Awesome](https://cdn.rawgit.com/sindr...
    emily_007閱讀 2,345評論 0 3
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    小邁克閱讀 3,129評論 1 3
  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,894評論 2 45
  • 用知識指導(dǎo)生活,全民健康的時代,不只是醫(yī)生一個行業(yè)的事,需要全民學習健康知識的時代! 有些人把自己的專業(yè)分的很細,...
    正心正德閱讀 198評論 0 0

友情鏈接更多精彩內(nèi)容