NodeJS 搭建图形识别功能解析

无情 阅读:228 2021-03-31 20:59:38 评论:0

第一步:创建项目存储路径(C:\node_workspace),创建NodeJs 项目(image_reco) 并初始化

cd C:\node_workspace

mkdir image_reco

cnpm init

第二步:图形识别依赖模块介绍和本地安装

node-images: Node.js 轻量级跨平台图像编解码库
tesseract.js: 纯 JS 的 OCR 库支持 62 种语言。

本地安装:node-images 和 tesseract.js模块

C:\node_workspace\image_reco>npm install tesseract.js 
npm notice created a lockfile as package-lock.json. You should commit this file. 
npm WARN [email protected] No description 
npm WARN [email protected] No repository field. 
 
+ [email protected] 
added 23 packages in 9.246s 
 
C:\node_workspace\image_reco>npm install images 
 
> [email protected] install C:\node_workspace\image_reco\node_modules\images 
> node ./scripts/install.js 
 
Downloading binary from https://github.com/zhangyuanwei/node-images/releases/download/v3.0.2/win32-x64-57_binding.node 
Download complete..] - : 
Binary saved to C:\node_workspace\image_reco\node_modules\images\vendor\win32-x64-57\binding.node 
Caching binary to C:\Users\zzg\AppData\Roaming\npm-cache\images\3.0.2\win32-x64-57_binding.node 
npm WARN [email protected] No description 
npm WARN [email protected] No repository field. 
 
+ [email protected] 
added 97 packages in 15.86s

编辑package.json 文件,添加第三方模块

{ 
  "name": "image_reco", 
  "version": "1.0.0", 
  "description": "", 
  "main": "app.js", 
  "scripts": { 
    "test": "echo \"Error: no test specified\" && exit 1" 
  }, 
  "author": "", 
  "license": "ISC", 
  "dependencies": { 
    "images": "^3.0.2", 
    "tesseract.js": "^1.0.14" 
  } 
}

第三步:项目根路径编写app.js 文件,编辑内容如下:

"use strict" 
var images = require('images') 
var Tesseract = require('tesseract.js'); 
var request = require('request'); 
var fs = require('fs'); 
// 将图片下载到本地 
function downloadFile(uri, filename, callback) { 
    var stream = fs.createWriteStream(filename); 
    request(uri).pipe(stream) 
        .on('close', function () { 
            callback(); 
        }); 
} 
// 识别图片 
function recognize(filePath, callback) { 
    // 图片放大 
    images(filePath) 
        .size(90) 
        .save(filePath); 
    // 识别 
    Tesseract 
        .recognize(filePath, { 
            lang: 'eng', // 语言选英文 
            tessedit_char_blacklist: 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' 
                //因为是数字验证码,排除字母 
        }) 
        .then((result) => { 
            callback(result.text); 
        }); 
} 
function getVcode() { 
    var url = 'https://ww1.sinaimg.cn/large/8c9b876fly1fe0bvsibibj201a00p07l.jpg'; 
    var filename = 'vcode.png'; 
    // 先下载下来,再识别 
    downloadFile(url, filename, function () { 
        recognize(filename, function (txt) { 
            console.log('识别结果: ' + txt); 
        }); 
    }); 
} 
getVcode();

第四步:安装项目依赖,执行图片识别app.js 代码

C:\node_workspace\image_reco>cnpm install 
√ Installed 2 packages 
√ Linked 0 latest versions 
√ Run 0 scripts 
√ All packages installed (used 34ms(network 22ms), speed 0B/s, json 0(0B), tarball 0B) 
 
C:\node_workspace\image_reco>node app.js 
pre-main prep time: 37 ms 
识别结果: 3128 

项目结构:

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
排行榜
KIKK导航

KIKK导航

关注我们