Azurespot
Azurespot

Reputation: 3152

Convert image into Readable stream

I have an image and it needs to go into an API call as a stream.Readable as per the API reference:

function addFaceFromStream(personGroupId: string, personId: string, image: stream.Readable, options?: Object)

I have been trying this for hours, but can't seem to get it. I am currently trying this, but get an error saying the _read() method is not implemented:

let childImages = new FileSet("c*.jpg").files // returns a string[]
// Add images to the person group person "child"
for (let chImage of childImages) {
    // Returns a Promise<PersistedFace>
    await client.personGroupPerson.addFaceFromStream(PERSON_GROUP_ID, 
        child.personId, new stream.Readable().push(chImage).push(null))
        .then((face) => {
            console.log('ID ' + face.persistedFaceId + ' was added to a person group person called Child.')
        })
}

Error:

events.js:187
      throw er; // Unhandled 'error' event
      ^

Error [ERR_METHOD_NOT_IMPLEMENTED]: The _read() method is not implemented
    at Readable._read (_stream_readable.js:647:24)
    at Readable.read (_stream_readable.js:490:10)
    at maybeReadMore_ (_stream_readable.js:634:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21)
Emitted 'error' event on Readable instance at:
    at errorOrDestroy (internal/streams/destroy.js:108:12)
    at Readable._read (_stream_readable.js:647:3)
    at Readable.read (_stream_readable.js:490:10)
    at maybeReadMore_ (_stream_readable.js:634:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'ERR_METHOD_NOT_IMPLEMENTED'

I keep seeing this similar way where people are using pipe() after they create a Readable stream, but I don't need to prepare it for write-to (if I understand pipe correctly). It just needs to be a Readable stream.

Upvotes: 0

Views: 2076

Answers (1)

Azurespot
Azurespot

Reputation: 3152

I found it in an obscure file buried away. This works:

const createReadStream = require('fs').createReadStream

    let childImages = new FileSet("c*.jpg").files

    // Add images to the person group person "child"
    for (let chImage of childImages) {
        // Returns a Promise<PersistedFace>
        await client.personGroupPerson.addFaceFromStream(PERSON_GROUP_ID, 
            child.personId, () => createReadStream(chImage))
            .then((face) => {
                console.log('ID ' + face.persistedFaceId + ' was added to a person group person called Child.')
            })
    }

Upvotes: 1

Related Questions