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.

Saving and Loading Descriptors for future use

See original GitHub issue

With ref https://github.com/justadudewhohacks/face-api.js#face-recognition-by-matching-descriptors

I’ve managed to train the faces and and saving the dataset with JSON.stringify(labelledDescriptors) to a static JSON.

Is there a way to quickly load this dataset, or do I have to load the raw JSON and reinit the dataset with each new faceapi.LabeledFaceDescriptors(name, descriptors)?

Is there method such as

const labelledDescriptors = await faceapi.fetchDescriptors('/files/labeledDescriptors.json');
const faceMatcher = new faceapi.FaceMatcher(labelledDescriptors);

Pardon, if I didn’t explain myself well. But the objective is to quickly loaded a saved labeledDescriptors for usage.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
Mactacscommented, Jul 11, 2019

const labeledFaceDescriptors = await loadLabeledImages() var json_str = “{"parent":” + JSON.stringify(labeledFaceDescriptors) + “}” // save the json_str to json file

// Load json file and parse var content = JSON.parse(json_str)

for (var x = 0; x < Object.keys(content.parent).length; x++) { for (var y = 0; y < Object.keys(content.parent[x]._descriptors).length; y++) { var results = Object.values(content.parent[x]._descriptors[y]); content.parent[x]._descriptors[y] = new Float32Array(results); } } const faceMatcher = await createFaceMatcher(content);

function loadLabeledImages() { const labels = [‘Black Widow’, ‘Captain America’, ‘Captain Marvel’, ‘Hawkeye’, ‘Jim Rhodes’, ‘Thor’, ‘Tony Stark’] return Promise.all( labels.map(async label => { const descriptions = [] for (let i = 1; i <= 2; i++) { const img = await faceapi.fetchImage(https://raw.githubusercontent.com/WebDevSimplified/Face-Recognition-JavaScript/master/labeled_images/${label}/${i}.jpg) const detections = await faceapi.detectSingleFace(img).withFaceLandmarks().withFaceDescriptor() descriptions.push(detections.descriptor) } return new faceapi.LabeledFaceDescriptors(label, descriptions) }) ) }

// Create Face Matcher async function createFaceMatcher(data) { const labeledFaceDescriptors = await Promise.all(data.parent.map(className => { const descriptors = []; for (var i = 0; i < className._descriptors.length; i++) { descriptors.push(className._descriptors[i]); } return new faceapi.LabeledFaceDescriptors(className._label, descriptors); })) return new faceapi.FaceMatcher(labeledFaceDescriptors); }

6reactions
Bajajarcommented, Mar 3, 2019

This is not the bestway but it’s working. Set faceMatcher as global variable then you can call faceMatcher.findBestMatch(newdata) to compare new data with your saved dataset.

// Global variable
var faceMatcher;

// Create Face Matcher
async function createFaceMatcher(data) {
  const labeledFaceDescriptors = await Promise.all(data._labeledDescriptors.map(className => {
    const descriptors = [];
    for (var i = 0; i < className._descriptors.length; i++) {
      descriptors.push(className._descriptors[i]);
    }
    return new faceapi.LabeledFaceDescriptors(className._label, descriptors);
  }))
  return new faceapi.FaceMatcher(labeledFaceDescriptors);
}

// Load json to backend
  fs.readFile('demo.json', async function(err, data) {
    if (err) {
      console.log(err);
    }
    var content = JSON.parse(data);
    for (var x = 0; x < content['_labeledDescriptors'].length; x++) {
      for (var y = 0; y < content['_labeledDescriptors'][x]['_descriptors'].length; y++) {
        var results = Object.values(content['_labeledDescriptors'][x]['_descriptors'][y]);
        content._labeledDescriptors[x]._descriptors[y] = new Float32Array(results);
      }
    }
    faceMatcher = await createFaceMatcher(content);
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and loading image descriptors - python - Stack Overflow
Ok, after much searching I found the answer to my own question. The load and save of descriptors is working fine above.
Read more >
How to Save and Load Your Keras Deep Learning Model
In this post, you will look at three examples of saving and loading your model to a file: Save Model to JSON. Save...
Read more >
Lesson 19.4 – Saving and loading the player information
There are several different ways you could save data: in a database, a comma-separated value file, an XML file, etc. We're going to...
Read more >
GameMaker Studio 2 - Best Saving and Loading Tutorial (2.3 ...
Like the other tutorial I did, only better. A universal approach for when you need to save or load data in GameMaker, using...
Read more >
Saving and loading MOs and CI coefficients - BAGEL Manual
This file can be read back to provide a reference for a future calculation. ... Description: This option is only used with load_ref....
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