forked from antimatter15/ocrad.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f1e5a4
commit 843ab6c
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.