UNPKG

nfv

Version:

[Updated to use ndv package instead dv] FormVision is a node.js library for extracting data from scanned forms

84 lines (77 loc) 2.89 kB
// Generated by CoffeeScript 1.12.7 var QUIETZONE_WIDTH, boundingBox, cloneWithQuietZone, detectCandidates, dv; dv = require('ndv'); boundingBox = require('./math').boundingBox; QUIETZONE_WIDTH = 35; detectCandidates = function(image) { var blobImage, blobMap, blobMask; blobImage = image.thin('bg', 8, 5).dilate(5, 3); blobMap = blobImage.distanceFunction(8); blobMask = blobMap.threshold(10).invert().dilate(42, 22); return blobMask.connectedComponents(8); }; cloneWithQuietZone = function(image, rect) { var clone, cropped; cropped = image.crop(rect); clone = new dv.Image(cropped.width + QUIETZONE_WIDTH * 2, cropped.height + QUIETZONE_WIDTH * 2, cropped.depth); clone.clearBox(0, 0, clone.width, clone.height); clone.drawImage(cropped, QUIETZONE_WIDTH, QUIETZONE_WIDTH, cropped.width, cropped.height); return clone; }; module.exports.findBarcodes = function(image, zxing) { var candidate, clearedImage, code, codes, couldBeRotated, grayImage, i, len, point, ref; clearedImage = new dv.Image(image); grayImage = image.toGray(); codes = []; ref = detectCandidates(grayImage); for (i = 0, len = ref.length; i < len; i++) { candidate = ref[i]; try { couldBeRotated = candidate.height * 1.75 > candidate.width; zxing.image = cloneWithQuietZone(grayImage, candidate); zxing.tryHarder = couldBeRotated; code = zxing.findCode(); if ((code == null) && candidate.width < 0.3 * grayImage.width) { zxing.image = zxing.image.scale(2).open(5, 3).scale(0.5).otsuAdaptiveThreshold(400, 400, 0, 0, 0.1).image; code = zxing.findCode(); } if (code != null) { if (candidate.width < 0.3 * grayImage.width) { clearedImage.clearBox(candidate); } code.box = boundingBox((function() { var j, len1, ref1, results; ref1 = code.points; results = []; for (j = 0, len1 = ref1.length; j < len1; j++) { point = ref1[j]; results.push({ x: point.x, y: point.y, width: 1, height: 1 }); } return results; })()); if (code.box.width < 20) { code.box.y -= QUIETZONE_WIDTH; code.box.x -= Math.round(candidate.width / 2); code.box.width += candidate.width; code.box.height += QUIETZONE_WIDTH * 2; } if (code.box.height < 20) { code.box.x -= QUIETZONE_WIDTH; code.box.y -= Math.round(candidate.height / 2); code.box.height += candidate.height; code.box.width += QUIETZONE_WIDTH * 2; } code.box.x += candidate.x - QUIETZONE_WIDTH; code.box.y += candidate.y - QUIETZONE_WIDTH; delete code.points; codes.push(code); } } catch (error) {} } return [codes, clearedImage]; };