Mac OCR 圖像文字識別調研(tesseract & baidu clound)

OCR (Optical Character Recognition,光學字符識別)是指電子設備(例如掃描儀或數碼相機)檢查紙上打印的字符,通過檢測暗、亮的模式確定其形狀,然後用字符識別方法將形狀翻譯成計算機文字的過程。

本文記錄使用python進行圖片識別,嘗試了兩種辦法,在此記錄!

Tesseract


安裝

Mac直接使用brew進行安裝,未安裝brew請點擊!

# 安裝tesseract的同時安裝訓練工具
brew install --with-training-tools tesseract

# 安裝tesseract的同時安裝所有語言,語言包比較大,建議不安裝
brew install  --all-languages tesseract

# 安裝tesseract,並安裝訓練工具和語言
brew install --all-languages --with-training-tools tesseract 

# 只安裝tesseract,不安裝訓練工具,推薦安裝!(我安裝的這個!^_^)
brew install  tesseract

安裝完成後,驗證是否成功

tesseract -v

使用

命令行調用

# 獲取幫助
tesseract --help
# imgNmae: 圖片路徑
# result: 識別結果,存放於result.txt文件
tesseract imgName result

Python調用

使用python調用,還需要安裝兩個包。

pip install pillow
pip insstall pytesseract

測試圖片:
在這裏插入圖片描述
實例:

from PIL import Image
import pytesseract

image = Image.open('test.png')
text = pytesseract.image_to_string(image, lang='eng')
print(text)

輸出結果:

/usr/local/bin/python3.7 /Users/test.py
The official site of the NBA | NBA.com

Process finished with exit code 0

如需識別中文或者其他語言,下載對應 語言庫 即可。

百度雲 OCR

安裝

  • 首先需要註冊一個百度賬號,登錄 後 在產品欄搜索 “文字識別”,點擊 “立即使用” 跳轉到概覽頁面。

  • 創建應用,拿到AppID、API Key、Secret Key
    在這裏插入圖片描述

  • 安裝python庫

pip install baidu-aip

使用

官方文檔

from aip import AipOcr

config = {
    'appId': '',
    'apiKey': '',
    'secretKey': ''
}

client = AipOcr(**config)

def get_file_content(file):
    with open(file, 'rb') as fp:
        return fp.read()
        
image = get_file_content('test.png')
result = client.basicAccurate(image)

text = '\n'.join([w['words'] for w in result['words_result']])
print(text)

輸出結果:

/usr/local/bin/python3.7 /Users/test.py
 The official site of the NBA NBA. come

Process finished with exit code 0

總結

  • 準確率
    經過多張圖片的測試,發現百度AI的識別結果更加準確一些,也有可能是因爲我對tesseract的瞭解不夠全面。

  • 對新手友好度
    百度更好上手~接口文檔全面滴很~功能也很多~主要是不用裝很多依賴庫~

  • 是否收費
    tesseract完全免費!
    百度 - 通用文字識別,一天可調用500次,不同功能的API有不同的次數限制,個人玩玩還是夠用的!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章