Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/simple-example.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ formSchema =
height: 42

,
path: 'anchors.gender'
path: 'gender'
type: 'text'
box:
x: 1525
Expand All @@ -48,6 +48,6 @@ formSchema =
formReader = new fv.FormReader 'eng'
formReader.image = image
form = formReader.find()
form.match formSchema, (err, formData) =>
form.match formSchema, (err, formData, projections) =>
console.log formData
fs.writeFile 'example.log.png', form.toImage().toBuffer('png')
fs.writeFile 'example.log.png', form.toImage(projections).toBuffer('png')
33 changes: 20 additions & 13 deletions src/form_reader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Form
schemaToPage = estimateTransform formSchema.words, @data[2], fallbackScale

# Match text and verify cleanliness of empty text fields.
{anchors} = matchText formData, formSchema, @data[2], schemaToPage, @images[0]
@anchors = anchors
matchText formData, formSchema, @data[2], schemaToPage, @images[0]

# Estimate schema to fields transform and match checkboxes.
schemaToFields = estimateTransform formSchema.fields, formData, 1, 1, matchByPath
matchCheckboxes formData, formSchema, @data[3], @data[2], schemaToPage, schemaToFields
Expand All @@ -45,6 +45,7 @@ class Form
for field in formSchema.fields
unpack formData, field.path

#XXX: remove fromValidators, as post-processing is out of scope.
# Call form validators.
async.forEach formSchema.fields, (field, nextField) ->
if field.formValidator?
Expand All @@ -53,11 +54,11 @@ class Form
nextField()
, (err) ->
return cb err if err?
cb null, formData
cb null, formData, [schemaToPage, schemaToFields]

return

toImage: =>
toImage: (projections) =>
resultImage = new dv.Image @images[0].width * @images.length, @images[0].height, 32
imageOffset = (box, index) =>
return {
Expand All @@ -76,14 +77,20 @@ class Form
resultImage.drawBox(imageOffset(boxed.box, index), 2, 0, 0, 255, 0.5)
resultImage.drawBox(imageOffset(boxed.candidate, index), 2, 255, 0, 0)

for anchor, index in @anchors
box =
x: anchor.word.box.x + anchor.offset.x
y: anchor.word.box.y + anchor.offset.y
width: anchor.word.box.width
height: anchor.word.box.height
resultImage.drawBox(imageOffset(box, 1), 4, 0, 255, 50, 0.5)
resultImage.drawLine(imageOffset(box, 1), imageOffset(anchor.word.box, 1), 4, 0, 255, 255, 0.5)
for projection in projections ? []
box = projection
x: 0
y: 0
width: 100
height: 100
p0 = {x: 0, y: 0}
p1 = {x: box.x, y: box.y}
p2 = {x: box.x + box.width, y: box.y}
p3 = {x: box.x, y: box.y + box.height}
for image, index in @images when image?
resultImage.drawLine(imageOffset(p0, index), imageOffset(p1, index), 4, 0, 255, 255, 0.5)
resultImage.drawLine(imageOffset(p1, index), imageOffset(p2, index), 4, 0, 255, 255, 0.5)
resultImage.drawLine(imageOffset(p1, index), imageOffset(p3, index), 4, 0, 255, 255, 0.5)

return resultImage

Expand All @@ -109,4 +116,4 @@ module.exports = class FormReader
[data[2], images[2]] = findText images[1], @tesseract
[data[3], images[3]] = findCheckboxes images[2]
return new Form data, images


Loading