Skip to content

Instantly share code, notes, and snippets.

@yaronn
Created June 4, 2012 14:40
Show Gist options
  • Save yaronn/2868796 to your computer and use it in GitHub Desktop.
Save yaronn/2868796 to your computer and use it in GitHub Desktop.
var http = require('http')
var fs = require('fs')
require('bufferjs');
var spawn = require('child_process').spawn
var PicFile ='c:\\ocr\\ocr.bin'
var ResFilePrefix = 'c:\\ocr\\output'
var ResFile = ResFilePrefix + ".txt"
http.createServer(function (req, res) {
var body = new Buffer(0);
req.on('data', function (chunk) {
body = Buffer.concat(body, chunk)
});
req.on('end', function () {
fs.writeFileSync(PicFile, body)
var ocr = spawn('tesseract', [PicFile, ResFilePrefix, '-l', 'yan']);
ocr.on('exit', function (code) {
console.log('child process exited with code ' + code);
if (code!=0) {
res.end("ocr returned 0")
return
}
var str = fs.readFileSync(ResFile).toString().trim()
res.writeHead(200);
if (str=="") {
res.end("ocr returned an empty string")
return
}
try {
var ev = eval(str)
var output = "output: " + ev
res.end(output)
}
catch (ex) {
res.end("exception during evaluation: " + ex)
}
});
});
}).listen(8080);
console.log('Server running at http://127.0.0.1:8080/');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment