Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

human.match.find function allways return similarity 0 #395

Closed
dongphuongman opened this issue Sep 28, 2023 · 2 comments
Closed

human.match.find function allways return similarity 0 #395

dongphuongman opened this issue Sep 28, 2023 · 2 comments
Assignees

Comments

@dongphuongman
Copy link

My code i check image upload and compare it with embeding in database but after call function human.match.find function it allways return similarity 0. Althought i up load same image with image save embedding in data base, below my code

export const recognition = async (req, res) => {
try {
// Access headers
const customHeader = req.headers['api-Key'];
if (!req.file) {
return res.status(400).json({ message: 'No file uploaded' });
}
// Do something with the header parameters
console.log('Custom Header:', customHeader);
const uploadedFile = req.file;
const input = uploadedFile.path;

  // Send a response
  if (!fs.existsSync(input)) {
    throw new Error('Cannot load image:', input);
  }
  
  await init();
  const buffer = fs.readFileSync(input);
  const tensor = human.tf.node.decodeImage(buffer, 3);
  log.state('Loaded image:', input, tensor.shape);
  const result = await human.detect(tensor, myConfig);
  
  const sql = "SELECT name, embedding FROM employee";
  const matchOptions = {order: 2 }; // for faceres model
  pool.query(sql)
    .then(([rows]) => {
      const embeddingArray = rows.map((rec) => rec.embedding); // build array with just embeddings           
      const best =  human.match.find(result.face[0].embedding, embeddingArray,matchOptions); // return is object: { index: number, similarity: number, distance: number }            console.log(best.index);
      console.log({similarity: best.similarity });
      human.tf.dispose(tensor);
      log.state('Detected faces:', result.face.length);
      res.json(result.face[0].embedding);          
    })
    .catch((error) => {
      console.error('Error selecting data:', error);
      res.status(500).json({ error: 'Internal server error' });
    });

} catch (error) {
  console.error(error);
  res.status(500).json({ error: 'Internal server error' });
}

};

Environment

  • Human library version 3.12
  • Built-in custom code?
  • Type of module used (e.g. js, esm, esm-nobundle)?
  • TensorFlow/JS version (if not using bundled module)?
  • Browser or NodeJS and version (e.g. NodeJS 14.15 or Chrome 89)?
  • OS and Hardware platform (e.g. *Windows 11)
@vladmandic
Copy link
Owner

and what is the type of rec.embedding?
you said you're using a database, but no note on which one? based on SELECT name, embedding FROM employee, its some sort of sql database.
note that most sql databases don't natively store float arrays, so i'd assume you'd need to do some serialization when storing and deserialize when reading.

if you can reproduce the problem with simple store, then its an issue, but i cannot troubleshoot your sql interface.
once there is a reproduction that can be used, i can reopen.

@dongphuongman
Copy link
Author

and what is the type of rec.embedding? you said you're using a database, but no note on which one? based on SELECT name, embedding FROM employee, its some sort of sql database. note that most sql databases don't natively store float arrays, so i'd assume you'd need to do some serialization when storing and deserialize when reading.

if you can reproduce the problem with simple store, then its an issue, but i cannot troubleshoot your sql interface. once there is a reproduction that can be used, i can reopen.

Thank for quick reply, i just solved by update
const embeddingArray = rows.map((rec) => rec.embedding); // build array with just embeddings
to
const embeddingArray = rows.map((rec) => JSON.parse(rec.embedding));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants