英文:
Face Recognition Login - C# with OpenCV
问题
这段代码中有一个明显的问题,每个图像都返回100%的相似度。有人可以指出我做错了什么吗?
我是新手使用OpenCV,我将会有一张上传的照片,需要与实时登录网站中拍摄的另一张照片进行比较。
以下是代码部分的翻译:
[HttpPost]
public IActionResult ReconhecerImagem([FromBody] ImagemDTO imagemDTO)
{
// Convert the base64 image to bytes
string base64String = imagemDTO.ImagemBase64.Replace(""", "+");
byte[] imageData = System.Convert.FromBase64String(base64String);
// Load the image using OpenCV
Mat imagem = Cv2.ImDecode(imageData, ImreadModes.Color);
// Perform facial recognition with OpenCV
// Here, you need to implement the facial recognition logic with OpenCV,
// such as loading reference images, extracting facial features, etc.
// Assuming you have a previously registered reference image named "imagemReferencia":
// Load the reference image using OpenCV
Mat imagemReferencia = Cv2.ImRead("C:\\Users\\wrwon\\Downloads\\Foto.jpg", ImreadModes.Color);
// Resize the images to have the same size
Size imageSize = new Size(150, 150);
Cv2.Resize(imagem, imagem, imageSize);
Cv2.Resize(imagemReferencia, imagemReferencia, imageSize);
// Convert the images to grayscale
Mat grayImagem = new Mat();
Cv2.CvtColor(imagem, grayImagem, ColorConversionCodes.BGR2GRAY);
Mat grayImagemReferencia = new Mat();
Cv2.CvtColor(imagemReferencia, grayImagemReferencia, ColorConversionCodes.BGR2GRAY);
// Create an EigenFaceRecognizer object
var recognizer = EigenFaceRecognizer.Create();
// Train the model with the reference image
recognizer.Train(new[] { grayImagemReferencia }, new[] { 1 });
// Perform facial recognition on the captured image
var result = recognizer.Predict(grayImagem);
// Normalize the distance to a scale of 0 to 100
double normalizedDistance = (result - 0) / (10000 - 0) * 100;
// Calculate similarity as 100 - normalized distance
double similarity = 100 - normalizedDistance;
// Check if the similarity is greater than or equal to 95%
if (similarity >= 95)
{
// Facial recognition successful
return Ok(new { success = true });
}
else
{
// Facial recognition failed
return Ok(new { success = false });
}
}
}
public class ImagemDTO
{
public string ImagemBase64 { get; set; }
}
}
英文:
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; }
}
}
答案1
得分: 0
你需要计算测试人脸图像和目标图像的特征向量,然后使用阈值来检查这两张人脸是否相似。有一个名为Emgu CV的库,它是一个C#包装器,您可以使用它来计算测试图像的特征向量。您将需要一个用于人脸特征提取的分类器,其中包括特征值和特征向量。
如果您希望基于单个图像检查图像的相似性,那么您不需要一个经过训练的模型,但如果您希望软件识别人脸图像,那么您需要使用神经网络训练一个模型,然后使用该模型来识别样本测试人脸以及在训练过程中使用的相应正确标签。
有一个名为Emgutf(Emgu Cv Tensor Flow)的库,它允许您构建、训练和测试神经网络,以满足图像处理和人脸识别的需求。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论