justadudewhohacks / face-api.js

JavaScript API for face detection and face recognition in the browser and nodejs with tensorflow.js
MIT License
16.69k stars 3.71k forks source link

Documentation confusion: It's there a way to load the models from a variable instead that from the files and use it later #517

Open lagcamgc opened 4 years ago

lagcamgc commented 4 years ago

In the documentation looks like the only way to load the models and pass it to the instance is from the files on the static folder:

await faceapi.nets.ssdMobilenetv1.loadFromUri('/models')

But i have observed that we can use a Float32Array but i can't find a way to pass it to the faceapi instance, this is what i have tried:

let net = new faceapi.TinyFaceDetector() let weights = await faceapi.fetchNetWeights('/models/tiny_face_detector_model-shard1.weights') console.log('weights') console.log(weights) net.load(weights) console.log('net') console.log(net) await faceapi.nets.tinyFaceDetector.loadFromWeightMap(weights) -> In this line fails const detection = await faceapi.detectSingleFace(contextForFaceDetection.canvas, new faceapi.TinyFaceDetectorOptions({ scoreThreshold: configuration.FACE_SCORE_ADMISSION }))

But i get: image

Can someone help me to clarify this, in the documentation is not so clear how i can load the model without using the methods to access to the files?The model weights a lot and i wuold like so save it into a variable to reuse it using the localStorage or the IndexedDB

justadudewhohacks commented 4 years ago

await faceapi.nets.tinyFaceDetector.loadFromWeightMap(weights)

Why are you calling loadFromWeightMap after loading the weights into your local net instance? await faceapi.nets.tinyFaceDetector.load(weights) should do the job for you.

awdr74100 commented 3 years ago

@lagcamgc has your problem been solved? I found tiny_face_detector_model.bin and ssd_mobilenetv1_model.bin seems to have some problems:

import * as faceapi from "face-api.js";

const float32Array_1 = await faceapi.fetchNetWeights(
  "/weights/face_landmark_68_tiny_model.bin"
);
console.log(float32Array_1); // [6.35521664824198e+30, -1.8255705378378978e-13, ...]

const float32Array_2 = await faceapi.fetchNetWeights(
  "/weights/tiny_face_detector_model.bin"
);
console.log(float32Array_2); // Error: byte length of Float32Array should be a multiple of 4

1

and all models will fail to load:

const net = new faceapi.FaceLandmark68TinyNet();
const float32Array_1 = await faceapi.fetchNetWeights(
  "/weights/face_landmark_68_tiny_model.bin"
);
await net.load(float32Array_1); // Error: Based on the provided shape, [1,1,32,32], the tensor should have 1024 values but has 578

2

I am using the module provided by @vladmandic/face-api . Even though I am using the face-api.js module, I still make the above error. Am I missing something?

lagcamgc commented 3 years ago

Hello! @awdr74100 at the end of the day i end up using a library called picojs, so short answer... no, i was not able not make it work even with the help of the previous comment