Tesseract.js和ocrad.js兩個js庫
Tesseract.js
引入Tesseract.js
Tesseract.recognize的識別方法有兩個配置參數
第一個參數可以是本地的一個圖片,也可以是網絡上的一個圖片地址,還可以是一個base64格式的二進制圖片格式;
第二個參數就是配置信息,包括識別的語種等等。這里classify_bln_numeric_mode表示假定圖片中只有數字,大家可以根據自己需要去設置不同的值
function recognize_image(){ document.getElementById('transcription').innerText = "(等待中...)" Tesseract.recognize("./顯示底部桌面.png", { lang: 'chi_sim', classify_bln_numeric_mode: 1 }).then(function(result){ console.log('哈哈',result.text); //識別出的文字 document.getElementById('transcription').className = "done" document.getElementById('transcription').innerText = result.text; }); } </script> <div id="main"> <!-- CODE大全:www.codedq.net --> <img id="pic" src="./顯示底部桌面.png" "recognize_image()"> <div id="transcription"></div>
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: 1 }).then(function(result){ $("#code").val(result.text); });
workerPath: worker.js路徑,worker.js可以從第一步中下載的dist目錄中獲取
langPath: 語言資源包請求地址,默認 https://cdn.rawgit.com/naptha/tessdata/gh-pages/3.02/。
語言包url可以通過 langPath + langCode + '.traineddata.gz’計算得到,例如英語的語言包地址在上例中就是 https://localhost/eng.traineddata.gz。這里說的語言是指圖片中待識別的內容是何種語言,可以在 recognize 方法中通過第二個參數設置,上例中設置語言為 eng,完整的可取語言列表參見 https://github.com/naptha/tesseract.js/blob/master/docs/tesseract_lang_list.md
corePath: index.js 路徑,默認 https://cdn.rawgit.com/naptha/tesseract.js-core/master/index.js
ocrad.js 和Tesseract.js用法一樣
缺點: 比如車牌,身份證,這些可做到零誤差,其余的只能做通用的文字識別,獲取模版識別 但是也有部分限制。