question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fetch implementation is missing

See original GitHub issue

Like the title says, fetch implemtation is missing.

face-api version: 0.17.1

import * as faceapi from 'face-api.js';
class App {

    private MODEL_URL: string;
    constructor() {
        console.log('starting app...');
        console.log(process.env.MODELS);
        this.MODEL_URL = process.env.MODELS;
    }
    start() {
        try {
            this.loadModels();
            console.log(`Models loadedd successfully....`);
        } catch (error) {
            console.error(error);
        }
    }

    private async loadModels() {
        await faceapi.loadSsdMobilenetv1Model(this.MODEL_URL)
    }

}

(node:25181) UnhandledPromiseRejectionWarning: Error: fetch - missing fetch implementation for nodejs environment at fetch (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tfjs-image-recognition-base/src/env/createNodejsEnv.ts:24:11) at Object.<anonymous> (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tfjs-image-recognition-base/src/dom/fetchOrThrow.ts:9:21) at step (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tslib/tslib.js:133:27) at Object.next (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tslib/tslib.js:114:57) at /home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tslib/tslib.js:107:75 at new Promise (<anonymous>) at Object.__awaiter (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tslib/tslib.js:103:16) at Object.fetchOrThrow (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tfjs-image-recognition-base/build/commonjs/dom/fetchOrThrow.js:6:20) at Object.<anonymous> (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tfjs-image-recognition-base/src/dom/fetchJson.ts:4:17) at step (/home/Faiz/Organization/Repos/Image_Analysis/using_FaceAPI/node_modules/tslib/tslib.js:133:27) (node:25181) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of anasync function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 4) (node:25181) [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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
zavalyshyncommented, Oct 22, 2019

You don’t need fetch nor monkeypatch. Just use the special API calls to load the model files from local storage (the tutorials don’t mention those, but README at npm does), like this:

const faceapi = require('face-api.js');
const tf = require('@tensorflow/tfjs-node');
const Canvas = require('canvas');
const Image = require('canvas');
const ImageData = require('canvas');

const MODEL_URL = `${__dirname}/face-models/`;

faceapi.nets.ssdMobilenetv1.loadFromDisk(MODEL_URL)
    .then(faceapi.nets.faceLandmark68Net.loadFromDisk(MODEL_URL))
    .then(faceapi.nets.faceRecognitionNet.loadFromDisk(MODEL_URL))
    .catch(error => {
        error.log(error);
    });

5reactions
cefjoeiicommented, Mar 2, 2019

Browser

Loading the models on the browser was working fine.

const MODELS_URL = 'path/to/models';

// Load the face detection models
await faceapi.loadSsdMobilenetv1Model(MODELS_URL);

// Load the face landmark models
await faceapi.loadFaceLandmarkModel(MODELS_URL);

// Load the face recognition models
await faceapi.loadFaceRecognitionModel(MODELS_URL);       

However, I faced the same issue on Node.js.

Node.js

I found the solution by looking through the example code in the repository.

import path from 'path';

// Import a fetch implementation for Node.js
import fetch from 'node-fetch';

// Make face-api.js use that fetch implementation
faceapi.env.monkeyPatch({ fetch: fetch });

// Try experimenting on wherever your models are located. Mine are from up one folder
// You will get 'Error: Only absolute urls are supported' if you don't specify the absolute path
const MODELS_URL = path.join(__dirname, '/../models/face-api');

async function doSomething() {
  // Load the face detection models
  await faceapi.nets.ssdMobilenetv1.loadFromDisk(MODELS_URL);

  // Load the face landmark models
  await faceapi.nets.faceLandmark68Net.loadFromDisk(MODELS_URL);

  // Load the face recognition models
  await faceapi.nets.faceRecognitionNet.loadFromDisk(MODELS_URL);

  // Do something else
  ...
}


Read more comments on GitHub >

github_iconTop Results From Across the Web

That error is due to fetch not being implemented in a nodejs ...
That error is due to fetch not being implemented in a nodejs environment, which is used to load the models from an URL....
Read more >
face-api.js load image file from disk - Stack Overflow
FetchImage should be used when you are trying to retrieve the image from online. If you are using the image from local disk,...
Read more >
Solved: Error: requires a fetch implementation - Esri Community
Error: `arcgis-rest-request` requires a `fetch` implementation and global variables for `Promise` ... You are missing `fetch`, `FormData`.
Read more >
The Fetch API is finally coming to Node.js - LogRocket Blog
It's been a long time coming, but the Fetch API is now available in Node.js core. Learn why it took so long and...
Read more >
ReferenceError: fetch is not defined in NodeJs | bobbyhadz
To solve the error, install and import the node-fetch package, which provides a fetch() compatible API in the NodeJs runtime. referenceerror-fetch-is-not- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found