- Tesseract安裝
- 使用
- 本地安裝
Tesseract安裝
- 直接在需要使用的頁面中引入
或者
-
下載下傳 https://github.com/naptha/tesseract.js zip檔案并
解壓,将dist目錄中的
拷貝到項目中,然後通過tesseract.js
标簽引入script
使用
Tesseract.recognize("http://xxx/xxx.jpg", {
classify_bln_numeric_mode:
}).then(function(result){
$("#code").val(result.text);
});
recognize
方法第一個參數是類圖檔對象,可以是
img
元素,也可以是一個圖檔連結。第二個參數是配置選項,例如本例中的
classify_bln_numeric_mode
假定輸入是數字;更多的配置選項參考 https://github.com/naptha/tesseract.js/blob/master/docs/tesseract_parameters.md
then
方法傳回處理結果
本地安裝
tesseract.js
隻是一個簡單的包裝器,實際識别工作依賴于
WebWorker
。而
WebWorker
以及其需要的其它資源都來自與CDN。如果我們想将
WebWorker
以及其它資源儲存在本地提高加載速度,我們可以按如下方法設定:
Tesseract.create({
workerPath: '/path/to/worker.js',
langPath: 'https://localhost/',
corePath: 'https://localhost/index.js',
}).recognize("http://xxx/xxx.jpg", {
lang: 'eng',
classify_bln_numeric_mode:
}).then(function(result){
$("#code").val(result.text);
});
-
:workerPath
路徑,worker.js
可以從第一步中下載下傳的worker.js
目錄中擷取dist
-
: 語言資源包請求位址,預設 https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/。語言包url可以通過langPath
計算得到,例如英語的語言包位址在上例中就是langPath + langCode + '.traineddata.gz'
。這裡說的語言是指圖檔中待識别的内容是何種語言,可以在https://localhost/eng.traineddata.gz
方法中通過第二個參數設定,上例中設定語言為recognize
,完整的可取語言清單參見 https://github.com/naptha/tesseract.js/blob/master/docs/tesseract_lang_list.mdeng
-
:corePath
路徑,預設 https://cdn.rawgit.com/naptha/tesseract.js-core/master/index.jsindex.js