Сравнение лиц по фотографии C#

Добавлено 13 апр 2024 в 19:11
Using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

using FaceRecognitionDotNet;

using Emgu.CV;

using Emgu.CV.CvEnum;

using Emgu.CV.Structure;



namespace FaceDetection

{

public partial class Form1 : Form

{

public string file1, file2 = "";



private static readonlyCascadeClassifierEyeClassifier = new CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml");

private static readonlyCascadeClassifierFaceClassifier = new CascadeClassifier("haarcascade_frontalface_default.xml");

private static readonlyCascadeClassifierCatClassifier = new CascadeClassifier("haarcascade_frontalcatface_extended.xml");

private static readonlyCascadeClassifierSmileClassifier = new CascadeClassifier("haarcascade_smile.xml");



//переменныедлярасчетапараметровлиц

int eye_raz1, eye_raz2, nos1, nos2, rot1, rot2, face1, face2 = -1;



private void Button1_Click(object sender, EventArgs e)

{



eye_raz1 = 1; nos1 = 1; rot1 = 1; face1 = -1;

try

{

using (OpenFileDialogofd = new OpenFileDialog() { Multiselect = false, Filter = "Image Files (*.jpg;*.png;*.bmp;)|*.jpg;*.png;*.bmp;|All Files (*.*)|*.*;" })

{

if (ofd.ShowDialog() == DialogResult.OK)

{

Int i = 0;

Image<Bgr, byte>inputImage = new Image<Bgr, byte>(ofd.FileName);

Image<Gray, byte>grayImage = inputImage.Convert<Gray, byte>().Clone();



Rectangle[] faces = FaceClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle face in faces)

{

inputImage.Draw(face, new Bgr(113, 113, 245), 2);

CvInvoke.PutText(inputImage, "Face1", new System.Drawing.Point(face.X + 3, face.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

face1 = face.Bottom + face.X+face.Width;

}



Rectangle[] eyes = EyeClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle eye in eyes)

{

inputImage.Draw(eye, new Bgr(113, 245, 177), 2);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(eye.X + 3, eye.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

}

if (eyes.Length> 1) eye_raz1 = eyes[0].Location.X + eyes[0].Location.Y - eyes[1].Location.X + eyes[1].Location.Y;

i = 0;

Rectangle[] smiles = SmileClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle smile in smiles)

{

if (smile.Location.Y>inputImage.Height * 0.6)

{

CvInvoke.Rectangle(inputImage, smile, new Bgr(Color.Teal).MCvScalar, 2);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(smile.X + 3, smile.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

rot1 = smile.Location.X + smile.Location.Y;

}



}

i = 0;

Rectangle[] Nose = CatClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle nos in Nose)

{

CvInvoke.Rectangle(inputImage, nos, new Bgr(Color.Yellow).MCvScalar, 3);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(nos.X + 3, nos.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

nos1 = nos.Location.X + nos.Location.Y;

}

pictureBox1.Image = inputImage.AsBitmap();

file1 = ofd.FileName;

grayImage.Dispose();

inputImage.Dispose();

}

}

}

catch

{



}

}



public Form1()

{

InitializeComponent();

}



private void Button2_Click(object sender, EventArgs e)

{

eye_raz2 = 1; nos2 = 1; rot2 = 1; face2 = 1;

try

{

using (OpenFileDialogofd = new OpenFileDialog() { Multiselect = false, Filter = "Image Files (*.jpg;*.png;*.bmp;)|*.jpg;*.png;*.bmp;|All Files (*.*)|*.*;" })

{

if (ofd.ShowDialog() == DialogResult.OK)

{

inti = 0;

Image<Bgr, byte>inputImage = new Image<Bgr, byte>(ofd.FileName);

Image<Gray, byte>grayImage = inputImage.Convert<Gray, byte>().Clone();



Rectangle[] faces = FaceClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle face in faces)

{

inputImage.Draw(face, new Bgr(113, 113, 245), 2);

CvInvoke.PutText(inputImage, "Face2", new System.Drawing.Point(face.X + 3, face.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

face2 = face.Bottom + face.X+face.Width;

}



Rectangle[] eyes = EyeClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle eye in eyes)

{

inputImage.Draw(eye, new Bgr(113, 245, 177), 2);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(eye.X + 3, eye.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

}

i = 0;



if (eyes.Length> 1) eye_raz2 = eyes[0].Location.X + eyes[0].Location.Y - eyes[1].Location.X + eyes[1].Location.Y;



Rectangle[] smiles = SmileClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle smile in smiles)

{

if (smile.Location.Y>inputImage.Height * 0.6)

{

CvInvoke.Rectangle(inputImage, smile, new Bgr(Color.Teal).MCvScalar, 2);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(smile.X + 3, smile.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

rot2 = smile.Location.X + smile.Location.Y;

}

}

i = 0;



Rectangle[] Nose = CatClassifier.DetectMultiScale(grayImage, 1.05, 3);

foreach (Rectangle nos in Nose)

{

CvInvoke.Rectangle(inputImage, nos, new Bgr(Color.Yellow).MCvScalar, 3);

CvInvoke.PutText(inputImage, "" + i++, new System.Drawing.Point(nos.X + 3, nos.Y + 13), FontFace.HersheySimplex, 0.4, new Bgr(Color.Black).MCvScalar);

nos2 = nos.Location.X + nos.Location.Y;

}

pictureBox2.Image = inputImage.AsBitmap();

file2 = ofd.FileName;

grayImage.Dispose();

inputImage.Dispose();

}

}

}

catch

{

MessageBox.Show("Something went wrong.");

}

}



private void Button3_Click(object sender, EventArgs e)

{

try

{

string currentDirectory = Environment.CurrentDirectory + "\\models";

FaceRecognitionfr;

fr = FaceRecognition.Create(currentDirectory);





varrecog = FaceRecognition.LoadImageFile(file1);

varenToCompare = fr.FaceEncodings(recog).First();



var recog2 = FaceRecognition.LoadImageFile(file2);

var enToCompare2 = fr.FaceEncodings(recog2).First();





//MessageBox.Show("Похоже, что это один человек!", "Результат", MessageBoxButtons.OK);

Graf();

}

catch

{

// MessageBox.Show("Это два разных человека!", "Результат", MessageBoxButtons.OK, MessageBoxIcon.Error);

Graf();

}

}



privatevoidGraf()

{

//расчет главного процента совпадения

double FACE =

(((face1 > face2 ? (1 - Convert.ToDouble(face1 - face2) / face1) : (1 - Convert.ToDouble(face2 - face1) / face2)) * 100)

+

((eye_raz1 > eye_raz2 ? (1 - Convert.ToDouble(eye_raz1 - eye_raz2) / eye_raz1) : (1 - Convert.ToDouble(eye_raz2 - eye_raz1) / eye_raz2)) * 100)

+

((nos1 > nos2 ? (1 - Convert.ToDouble(nos1 - nos2) / nos1) : (1 - Convert.ToDouble(nos2 - nos1) / nos2)) * 100)

+

((rot1 > rot2 ? (1 - Convert.ToDouble(rot1 - rot2) / rot1) : (1 - Convert.ToDouble(rot2 - rot1) / rot2)) * 100)) /4;



pego1.Visible = true;

pego1.PeString.MainTitle = "Процентсовпадения " + FACE + " %";

pego1.PeString.SubTitle = "";



pego1.PeData.Subsets = 2;

pego1.PeData.Points = 4;

pego1.PeData.Y[0, 0] = face1;

pego1.PeData.Y[0, 1] = eye_raz1;

pego1.PeData.Y[0, 2] = nos1;

pego1.PeData.Y[0, 3] = rot1;





pego1.PeData.Y[1, 0] = face2;

pego1.PeData.Y[1, 1] = eye_raz2;

pego1.PeData.Y[1, 2] = nos2;

pego1.PeData.Y[1, 3] = rot2;



pego1.PeString.PointLabels[0] = "Лицо";

pego1.PeString.PointLabels[1] = "Глаза";

pego1.PeString.PointLabels[2] = "Нос";

pego1.PeString.PointLabels[3] = "Рот";



pego1.PeString.SubsetLabels[0] = "Человек #1";

pego1.PeString.SubsetLabels[1] = "Человек #2";

pego1.PeString.YAxisLabel = "Разницамеждуточкамилица";



pego1.PeColor.SubsetColors[0] = Color.FromArgb(60, 0, 180, 0);

pego1.PeColor.SubsetColors[1] = Color.FromArgb(180, 0, 0, 130);



pego1.PeColor.BitmapGradientMode = false;

pego1.PeColor.QuickStyle = Gigasoft.ProEssentials.Enums.QuickStyle.LightShadow;

pego1.PeTable.Show = Gigasoft.ProEssentials.Enums.GraphPlusTable.Both;

pego1.PeData.Precision = Gigasoft.ProEssentials.Enums.DataPrecision.NoDecimals;

pego1.PeFont.Label.Bold = true;

pego1.PePlot.Method = Gigasoft.ProEssentials.Enums.GraphPlottingMethod.Bar;

pego1.PePlot.Option.GradientBars = 8;

pego1.PePlot.Option.BarGlassEffect = true;

pego1.PeLegend.Location = Gigasoft.ProEssentials.Enums.LegendLocation.Left;

pego1.PePlot.DataShadows = Gigasoft.ProEssentials.Enums.DataShadows.ThreeDimensional;

pego1.PeFont.FontSize = Gigasoft.ProEssentials.Enums.FontSize.Large;

pego1.PePlot.SubsetLineTypes[0] = Gigasoft.ProEssentials.Enums.LineType.MediumSolid;

pego1.PePlot.SubsetLineTypes[1] = Gigasoft.ProEssentials.Enums.LineType.MediumDash;



pego1.PeUserInterface.HotSpot.Data = true;



pego1.PeConfigure.RenderEngine = Gigasoft.ProEssentials.Enums.RenderEngine.Direct2D;

pego1.PeConfigure.PrepareImages = true;

pego1.PeConfigure.CacheBmp = true;

pego1.PeConfigure.AntiAliasGraphics = true;

pego1.PeConfigure.AntiAliasText = true;



pego1.PeFunction.ReinitializeResetImage();



pego1.Refresh();

pego1.Visible = true;

}

}

}

3fd3458b54