Skip to content

Instantly share code, notes, and snippets.

@reecefenwick
Last active August 11, 2016 00:17
Show Gist options
  • Save reecefenwick/96fc9c229ca2d21a633c to your computer and use it in GitHub Desktop.
Save reecefenwick/96fc9c229ca2d21a633c to your computer and use it in GitHub Desktop.
tesseract example
var express = require('express');
var fs = require('fs');
var Busboy = require('busboy');
var mime = require('mime');
var tesseract = require('node-tesseract');
var app = express();
app.post('/upload', function(req, res) {
try {
var busboy = new Busboy({
headers: req.headers
});
} catch (err) {
return res.status(400).json({
message: 'Unable to parse incoming file.',
error: err
})
}
busboy.on('file', function(fieldname, filestream, filename, encoding, mimetype) {
console.log('incoming file');
var filepath = './files/' + _id + '.' + mime.extension(mimetype);
var ws = fs.createWriteStream(filepath);
ws.on('finish', function() {
console.log('file written to disk');
tesseract.process(filepath, function(err, text) {
console.log('tesseract process finished');
if (err) return callback(err);
callback(null, file, text)
})
});
filestream.pipe(ws);
});
busboy.on('error', function(err) {
res.status(500).json({
message: 'There was a problem parsing your file.',
error: err
})
});
});
var server = app.listen(3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
{
"name": "tesseract",
"dependencies": {
"busboy": "*",
"express": "*",
"mime": "*",
"node-tesseract": "*"
},
"scripts": {
"start": "node app"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment