forked from justadudewhohacks/opencv4nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocrHMMWords.js
37 lines (28 loc) · 1.34 KB
/
ocrHMMWords.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const cv = require('../');
const path = require('path');
if (!cv.xmodules.text) {
throw new Error('exiting: opencv4nodejs compiled without text module');
}
const dataPath = path.resolve('../data/text-data/');
const modelsPath = path.resolve('../data/text-models');
const beamSearchModel = path.resolve(modelsPath, 'OCRBeamSearch_CNN_model_data.xml.gz');
const vocabulary = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const lexicon = [
'abb', 'riser', 'CHINA', 'HERE', 'HERO', 'President', 'smash', 'KUALA', 'Produkt', 'NINTENDO',
'foo', 'asdf', 'BAR', 'this', 'makes', 'no', 'sense', 'at', 'all'
];
const transitionP = cv.createOCRHMMTransitionsTable(vocabulary, lexicon);
const emissionP = cv.Mat.eye(62, 62, cv.CV_64FC1);
const hmmClassifier = cv.loadOCRHMMClassifierCNN(beamSearchModel);
const hmmDecoder = new cv.OCRHMMDecoder(hmmClassifier, vocabulary, transitionP, emissionP);
const wordImages = ['scenetext_word01.jpg', 'scenetext_word02.jpg']
.map(file => path.resolve(dataPath, file))
.map(cv.imread);
wordImages.forEach((img) => {
const grayImg = img.type === cv.CV_8U ? img : img.bgrToGray();
const mask = grayImg.threshold(100, 255, cv.THRESH_BINARY_INV);
const ret = hmmDecoder.runWithInfo(grayImg, mask);
console.log('outputText:', ret.outputText);
cv.imshow('mask', mask);
cv.imshowWait('img', img);
});