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)
識別測試

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')]
從列表中選擇上面返回中有的

執(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)