There is something obvious that I'm not seeing in this code, but every image return 100% similarity. Anyone can point me what I'm doing wrong?
I'm new using OpenCV and I will have a uploaded photo and I will need to compare with another one taken from a website in real time login.
[HttpPost]
public IActionResult ReconhecerImagem([FromBody] ImagemDTO imagemDTO)
{
// Converter a imagem base64 em bytes
string base64String = imagemDTO.ImagemBase64.Replace(" ", "+");
byte[] imageData = System.Convert.FromBase64String(base64String);
// Carregar a imagem usando OpenCV
Mat imagem = Cv2.ImDecode(imageData, ImreadModes.Color);
// Realizar o reconhecimento facial com o OpenCV
// Aqui você precisa implementar a lógica de reconhecimento facial com OpenCV,
// como carregar imagens de referência, extrair características faciais etc.
// Supondo que você tenha uma imagem de referência previamente cadastrada, chamada "imagemReferencia":
// Carregar a imagem de referência usando OpenCV
Mat imagemReferencia = Cv2.ImRead("C:\\Users\\wrwon\\Downloads\\Foto.jpg", ImreadModes.Color);
// Redimensionar as imagens para terem o mesmo tamanho
Size imageSize = new Size(150, 150);
Cv2.Resize(imagem, imagem, imageSize);
Cv2.Resize(imagemReferencia, imagemReferencia, imageSize);
// Converter as imagens para escala de cinza
Mat grayImagem = new Mat();
Cv2.CvtColor(imagem, grayImagem, ColorConversionCodes.BGR2GRAY);
Mat grayImagemReferencia = new Mat();
Cv2.CvtColor(imagemReferencia, grayImagemReferencia, ColorConversionCodes.BGR2GRAY);
// Criar um objeto EigenFaceRecognizer
var recognizer = EigenFaceRecognizer.Create();
// Treinar o modelo com a imagem de referência
recognizer.Train(new[] { grayImagemReferencia }, new[] { 1 });
// Realizar o reconhecimento facial na imagem capturada
var result = recognizer.Predict(grayImagem);
// Normalizar a distância em uma escala de 0 a 100
double normalizedDistance = (result - 0) / (10000 - 0) * 100;
// Calcular a similaridade como 100 - distância normalizada
double similarity = 100 - normalizedDistance;
// Verificar se a similaridade é maior ou igual a 95%
if (similarity >= 95)
{
// Reconhecimento facial bem-sucedido
return Ok(new { success = true });
}
else
{
// Reconhecimento facial falhou
return Ok(new { success = false });
}
}
}
public class ImagemDTO
{
public string ImagemBase64 { get; set; }
}
}
You need to compute the eigen vector of the test face image and that of the target image and then use a threshold to check if the two faces are similar. There is a library called Emgu CV which is a C# wrapper you can use to compute the eigen vector of a test image. You will need a classifier for the face feature extraction including the eigen value and eigen vector.
If you desire to check for the similarity of images based on a single image then you don't need a trained model but if you want the Software to recognize face images then you need to train a model using a neural network and then use the model to recognize a sample test face with the corresponding correct label that was used during the training process. There is a library called Emgutf(Emgu Cv Tensor Flow ) which is a library that let's you build, train and test neural networks for the needs of image processing, face recognition included.