使用 face-api.js 包 (node.js) 时导入 @tensorflow/tfjs-node 的问题

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

我使用@tensorflow/tfjs-node face-api.js 包来加快速度(正如他们所说) 那是我的代码:

    // 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)

})

当我 运行 代码时,我得到这个错误

TypeError: forwardFunc_1 不是函数 在 G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3166:55 在 G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2989:22 在 Engine.scopedRun (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2999:23) 在 Engine.tidy (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2988:21) 在 kernelFunc (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3166:29) 在 G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3187:27 在 Engine.scopedRun (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:2999:23) 在 Engine.runKernelFunc (G:\test\node_modules@tensorflow\tfjs-core\dist\tf-core.node.js:3183:14) 在 mul_ (G:\test\node_modules\face-api.js\node_modules@tensorflow\tfjs-core\dist\ops\binary_ops.js:327:28) 在 Object.mul (G:\test\node_modules\face-api.js\node_modules@tensorflow\tfjs-core\dist\ops\operation.js:46: 29) (使用 node --trace-warnings ... 显示创建警告的位置) (节点:3496)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。此错误源自 throw 在没有 catch 块的异步函数内部,或者通过拒绝未使用 .cat 处理的承诺 通道()。要在未处理的承诺拒绝时终止节点进程,请使用 CLI 标志 --unhandled-rejections=strict(请参阅 https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝编号:1) (节点:3496)[DEP0018] DeprecationWarning:未处理的承诺拒绝已弃用。将来,未处理的承诺拒绝将以非零退出代码终止 Node.js 进程

当我删除“require('@tensorflow/tfjs-node');”代码运行时,我需要导入@tensorflow/tfjs-node以使过程更快

节点: v14.15.4

npm: 6.14.10

@tensorflow/tfjs-node: v3.0.0 Python 2.7.15(@tensorflow/tfjs-node 需要)

脸-api.js: v0.22.2

在此先感谢您:)

正如解释的那样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.

您必须安装过时的 tfjs-node 1.x 或按照他们给出的指示使用 newer port of face-api.js that supports TF 2.0.