Tuesday, 6 June 2017

Image Process about nodejs

I have a problem that I want to get the image number.

But I have tried many method to process image (Like edge, channel, colorize) but I still can't get the answer.

Here is my node.js code. Please give me a direction, thanks.

const fs = require('fs');
const tesseract = require('node-tesseract');
const gm = require('gm');

const superagent = require('superagent');
const cheerio = require('cheerio');
const request = require('request');

const info = fs.createWriteStream(__dirname + '/../logs/info.log', { flags: 'a', mode: '0666' });
const error = fs.createWriteStream(__dirname + '/../logs/error.log', { flags: 'a', mode: '0666' });
const logger = new console.Console(info, error);


process.on('uncaughtException', function (err) {
    'use strict';
    logger.error(err);
});

var processImg = function (imgPath) {
    var newPath = 'process.jpg';
    return new Promise((resolve, reject) => {
        gm(imgPath)
            //.compose(Over)
            .channel("Gray")
            //.edge(2)
            //.colorize(128,155,155)
            .write(newPath, (err) => {
            if (err) return reject(err);
                resolve(newPath);
            }); 
    });
}

var recognizer = function (imgPath, options) {
    options = Object.assign({
        psm: 8,
        config: "digits",
   }, options);

    return new Promise((resolve, reject) => {
        tesseract 
            .process(imgPath, options, (err, text) => {
                console.log(text);
                if (err) return reject(err);
               resolve(text.replace(/[\r\n\s]/gm, ''));
            });
    });
}


processImg('1.jpg')
.then(recognizer)
.then((text) => {
    console.log(text);
})
.catch((err) => {
    console.error(err);
});

This is my Image



via 王佳勝

No comments:

Post a Comment