├── 1.jpg ├── 2.jpg ├── 3.jpg ├── test_1.jpg ├── test_2.jpg ├── test_3.jpg ├── package.json ├── README.md └── index.js /1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/1.jpg -------------------------------------------------------------------------------- /2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/2.jpg -------------------------------------------------------------------------------- /3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/3.jpg -------------------------------------------------------------------------------- /test_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/test_1.jpg -------------------------------------------------------------------------------- /test_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/test_2.jpg -------------------------------------------------------------------------------- /test_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/think2011/node-ocr-demo/HEAD/test_3.jpg -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "demo", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "gm": "^1.21.1", 13 | "node-tesseract": "^0.2.7" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # node-ocr-demo 2 | 用 node.js 配合 tesseract 和 graphicsmagick 实现验证码识别 3 | 4 | > 这是 [用node.js实现验证码简单识别](http://think2011.net/2016/01/31/node-ocr/) 的demo。 5 | 6 | ## 安装 7 | 需要先安装 [node-tesseract](https://github.com/desmondmorris/node-tesseract) 和 [gm](https://github.com/aheckmann/gm),然后还要安装他们各自的依赖(对,就是这么麻烦 ╮(╯_╰)╭) 8 | 9 | ## 执行 10 | 在 node.js v4+ 环境下,执行 `node index.js` 即可看到结果。 11 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var tesseract = require('node-tesseract'); 3 | var gm = require('gm'); 4 | 5 | processImg('1.jpg', 'test_1.jpg') 6 | .then(recognizer) 7 | .then(text => { 8 | console.log(`识别结果:${text}`); 9 | }) 10 | .catch((err)=> { 11 | console.error(`识别失败:${err}`); 12 | }); 13 | 14 | /** 15 | * 处理图片为阈值图片 16 | * @param imgPath 17 | * @param newPath 18 | * @param [thresholdVal=55] 默认阈值 19 | * @returns {Promise} 20 | */ 21 | function processImg (imgPath, newPath, thresholdVal) { 22 | return new Promise((resolve, reject) => { 23 | gm(imgPath) 24 | .threshold(thresholdVal || 55) 25 | .write(newPath, (err)=> { 26 | if (err) return reject(err); 27 | 28 | resolve(newPath); 29 | }); 30 | }); 31 | } 32 | 33 | /** 34 | * 识别图片 35 | * @param imgPath 36 | * @param options tesseract options 37 | * @returns {Promise} 38 | */ 39 | function recognizer (imgPath, options) { 40 | options = Object.assign({psm: 7}, options); 41 | 42 | return new Promise((resolve, reject) => { 43 | tesseract 44 | .process(imgPath, options, (err, text) => { 45 | if (err) return reject(err); 46 | resolve(text.replace(/[\r\n\s]/gm, '')); 47 | }); 48 | }); 49 | } --------------------------------------------------------------------------------