目前OCR主要依賴幾個低層庫,本博客採用Tesseract,Tesseract是由Google維護的開源OCR。本博客在windows環境進行,linux環境同理。
1、安裝Tesseract:
windows下Tesseract的安裝比較簡單,下載exe然後一直next下去就行了。但是由於要做中文的識別,所以一定在安裝的時候要勾選相應的中文庫,這個安裝時是默認不下載的。
2、安裝pytesseract
pytesseract是對tesseract做的一層Python API封裝。
pip3 install pytesseract
3、編寫py代碼:
import pytesseract
from PIL import Image
pytesseract.pytesseract.tesseract_cmd = 'D://ocr/Tesseract-OCR/tesseract.exe'
text = pytesseract.image_to_string(Image.open('D://ocr/qximg/test1.png'),lang='chi_sim')
首先導入相應的庫。第一行代碼從正確的位置引入tesseract運行文件。第二行代碼選擇要識別的圖片,設置簡體中文識別。text即爲圖片中中文的內容。