vladmandic / human

Human: AI-powered 3D Face Detection & Rotation Tracking, Face Description & Recognition, Body Pose Tracking, 3D Hand & Finger Tracking, Iris Analysis, Age & Gender & Emotion Prediction, Gaze Tracking, Gesture Recognition

Home Page:https://vladmandic.github.io/human/demo/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

human.match.find function allways return similarity 0

dongphuongman opened this issue · comments

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)

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.

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));