javascriptnode.jstensorflowface-recognitionface-api

problem with importing @tensorflow/tfjs-node while working with face-api.js package (node.js)


i use @tensorflow/tfjs-node package for face-api.js package to speed up things (as they said ) that is my code :

    // import nodejs bindings to native tensorflow,
// not required, but will speed up things drastically (python required)

require('@tensorflow/tfjs-node');


// implements nodejs wrappers for HTMLCanvasElement, HTMLImageElement, ImageData
const { loadImage,Canvas, Image, ImageData } = require('canvas')

const faceapi = require('face-api.js');

// patch nodejs environment, we need to provide an implementation of
// HTMLCanvasElement and HTMLImageElement
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })


// patch nodejs environment, we need to provide an implementation of
// HTMLCanvasElement and HTMLImageElement
faceapi.env.monkeyPatch({ Canvas, Image, ImageData })

Promise.all([
 faceapi.nets.ssdMobilenetv1.loadFromDisk('./models'),
 faceapi.nets.faceRecognitionNet.loadFromDisk('./models'),
 faceapi.nets.faceLandmark68Net.loadFromDisk('./models')
])
.then(async () => {
    const image1= await loadImage("https://enigmatic-waters-76106.herokuapp.com/1.jpeg")
    const image2= await loadImage("https://enigmatic-waters-76106.herokuapp.com/8.jpeg")
    const result = await faceapi.detectSingleFace(image1).withFaceLandmarks()
   .withFaceDescriptor()

    const singleResult = await faceapi
    .detectSingleFace(image2)
    .withFaceLandmarks()
    .withFaceDescriptor()
       const labeledDescriptors = [
          new faceapi.LabeledFaceDescriptors(
                'saied',
            [result.descriptor]
          )
        ]
            const faceMatcher = new faceapi.FaceMatcher(labeledDescriptors)
    const bestMatch = faceMatcher.findBestMatch(singleResult.descriptor)
    console.log(labeledDescriptors[0].descriptors)

})

and when i run the code i get this error

TypeError: forwardFunc_1 is not a function at G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3166:55 at G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2989:22 at Engine.scopedRun (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2999:23) at Engine.tidy (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2988:21) at kernelFunc (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3166:29) at G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3187:27 at Engine.scopedRun (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2999:23) at Engine.runKernelFunc (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3183:14) at mul_ (G:\test\node_modules\face-api.js\node_modules@tensorflow\tfjs-core\dist\ops\binary_ops.js:327:28) at Object.mul (G:\test\node_modules\face-api.js\node_modules@tensorflow\tfjs-core\dist\ops\operation.js:46: 29) (Use node --trace-warnings ... to show where the warning was created) (node:3496) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throw ing inside of an async function without a catch block, or by rejecting a promise which was not handled with .cat ch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1) (node:3496) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code

when i delete " require('@tensorflow/tfjs-node'); " the code run prefectly but i need to import @tensorflow/tfjs-node to make the proccess faster

node: v14.15.4

npm: 6.14.10

@tensorflow/tfjs-node: v3.0.0 Python 2.7.15 (required for @tensorflow/tfjs-node)

face-api.js: v0.22.2

thanks in advance for :)


Solution

  • As explained in this github issue

    The version of face-api.js you are using is not compatible with tfjs 2.0+ or 3.0+, only obsolete 1.x. Why it worked before you added tfjs-node? because face-api.js actually includes bundled version of tfjs-core 1.x. Once you added tfjs-node, it overrode global tf namespace, but its a much newer version and not compatible.

    You must install obsolete tfjs-node 1.x OR follow the pointers they give to use a newer port of face-api.js that supports TF 2.0.