face-api.js 从磁盘加载图像文件

face-api.js load image file from disk

我尝试了以下方法来使用 face-api.js 从磁盘加载图像:

faceapi.fetchImage(path.resolve(INPUT_DIR, 'input1.jpg');

它抛出以下错误:Error: fetch - missing fetch implementation for nodejs environment

有没有其他方法可以从磁盘加载图像并使用 nodejs 显示?

它失败的原因是你需要实现获取功能才能工作。 FetchImage 应在您尝试从网上检索图像时使用。如果您使用的是本地磁盘中的图像,请执行以下操作:

  1. 从本地磁盘加载模块。

    await faceapi.nets.ssdMobilenetv1.loadFromDisk(path.join(__dirname, 'models'));
    

    // 对我来说,我把所有的模块都放在了src/models/

  2. 加载canvas

  3. 传递 canvas 和模块选项(如果有)

    let detectionresult = awaitfaceapi.detectAllFaces(canvas, this.getSSNMobileOptions())
    

documentation recommends you use the canvas做这样的事情:

const canvas = require('canvas');
faceapi.env.monkeyPatch({ Canvas, Image })
const img = await canvas.loadImage('./img.jpg');
const detections = await faceapi.detectSingleFace(img);

fetchImage will not work with local files:

faceapi.fetchImage as the name implies uses fetch under the hood, thus it doesn't work with filepaths to local files.