creatale / node-dv

A node.js library for processing and understanding scanned documents
Other
340 stars 72 forks source link

Assertion failed! and invalid Buffer length when creating new Image from sharp buffer #26

Closed eloops closed 9 years ago

eloops commented 9 years ago

I'm trying to pass a TIFF file through sharp, outputting to a buffer and then creating a new Image with the output.

node 0.12.7 x86, on Windows 8.1 64-bit. DV compiled with vs2013 (through node-gyp install dv)

When I do a conversion to PNG and pass it through as a buffer:

var fpath = __dirname + '\\in\\dd-000.tif'
sharp(fs.readFileSync(fpath)).png().toBuffer(function(err, data, info) {
  if (err) {
    throw err
  } else {
    console.log(data.length)
    var image = dv.Image('png', data)
  }
})

This results in this error popping up on screen: assertion

Similarly, if I instead convert it to a raw and pass it through along with the width/height:

var fpath = __dirname + '\\in\\dd-000.tif'
sharp(fs.readFileSync(fpath)).raw().toBuffer(function(err, data, info) {
  if (err) {
    throw err
  } else {
    console.log(data.length)
    var image = dv.Image('gray', data, info.width, info.height)
  }
})

It errors with:

E:\node\rb\index.js:74
    var image = dv.Image('gray', data, info.width, info.height)
                   ^
Error: invalid Buffer length
    at Error (native)
    at E:\node\rb\index.js:74:20

I feel like I'm missing something here?

eloops commented 9 years ago

I seem to have fixed this issue, I'm not entirely sure how I've managed to though. I tried to run npm install --arch=ia32 --msvs_version=2013 (from the installation instructions for sharp/Windows) and node-gyp recompiled dv.

I had also used bluebird to promisify fs, although I wasn't using it.

I can't really point to either of these as solid 'fixes' (after re-compiling I saw the error at least a few more times).

Here's what my code looks like now:

      sharp(fs.readFileSync(fpath)).png().toBuffer(function(err, data, info) {
        if (err) {
          throw err
        } else {
          var image = new dv.Image('png', data)
          var tesseract = new dv.Tesseract('eng', image)
          tesseract.pageSegMode = 'auto_only'
          var word_bbox = tesseract.findWords()
          var para_bbox = tesseract.findParagraphs()

        }
      })

The only change that I can see is using the new word to force the constructor.

sourcehunter commented 9 years ago

Yes, you must initialize a dv.Image with new, otherwise you will get the "Assertion failed!" error message. Also see #22.

We are aware, that this is an issue, especially for new dv users.