Skip to content

Commit

Permalink
constraining recognition to numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
antimatter15 committed Jan 12, 2015
1 parent 4f1e5a4 commit 843ab6c
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions examples/numbers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!doctype html>
<html>
<head>
</head>
<body>
<script src="../ocrad.js"></script>
<style>
.highlight {
position: absolute;
background: yellow;
opacity: 0.5;
width: 10px;
height: 10px;
}
</style>
<script>

function OCRImage(image){
var canvas = document.createElement('canvas')
canvas.width = image.naturalWidth;
canvas.height = image.naturalHeight;
canvas.getContext('2d').drawImage(image, 0, 0)
var raw = []
var text = OCRAD(canvas,
false, // don't invert the colors (i.e. set this to true if you have white on black text)
function(line){ // this is for giving the Ocrad raw format which includes letter bounding boxes
raw.push(line); // its a function that gets called for each line of stdout
});

var letters = parseOcrad(raw);
console.log(letters) // here's a list of recognized letters and their corresponding coordinates and confidences

letters.map(function(letter){
// select the letters which have vanna white's approval
return letter.matches.filter(function(match){
return /\d/.test(match.letter)
})
}).forEach(function(candidates){
if(candidates.length > 0){
console.log(candidates[0].letter)
}else{
console.log('_')
}
})
}


function parseOcrad(raw){
var raw = raw.map(function(e){
return e.match(/^\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*;\s*(\d+)(\,?.+)?$/)
}).filter(function(e){
return e
}).map(function(e){
var x = parseInt(e[1]),
y = parseInt(e[2]),
w = parseInt(e[3]),
h = parseInt(e[4]),
g = parseInt(e[5]);

var matches = [];
if(g > 0){
var etc = e[6].trim();
while(etc[0] == ',' && etc[1] == ' '){
etc = etc.slice(2)
var m = etc.match(/^\'(.+?)\'(\d+)/)
matches.push({
letter: m[1],
confidence: parseInt(m[2])
})
etc = etc.slice(m[0].length)
}
}
console.log(matches)
if(matches.length != g) console.error('recognition count mismatch', g, matches);
return {
x: x,
y: y,
width: w,
height: h,
matches: matches
}
});

return raw;
}
</script>
<img src="numbers.png" onload="OCRImage(this)">
</body>
</html>


Binary file added examples/numbers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 843ab6c

Please sign in to comment.