C#Face Comparison

C#Face Comparison

Optical Character Recognition and face detection have been written before. They are all using ready-made wheels (directly called Baidu SDK). In fact, after taking a closer look at the documentation, you will know how to write it. Moreover, Baidu also provides code examples in multiple languages.

最后更新 1/20/2022 2:12 PM
Csharp小记
预计阅读 3 分钟
分类
.NET
标签
.NET C# face comparison

preface

Optical Character Recognition and face detection have been written before. They are all using ready-made wheels (directly called Baidu SDK). In fact, after looking carefully at the documentation, they all know how to write it. Moreover, Baidu also provides code examples in multiple languages;

So if there are no special requirements later, the code for calling Baidu API to implement some AI functions ends here; the reason why these three scenarios are written is because these three scenarios are used frequently in daily life and work.

The face comparison function is generally used when comparing and verifying the face with the person's ID card.

achieve functional

  • Verify that the two faces are the same person

development environment

  • Development tool: Visual Studio 2013

  • NET Framework version: 4.5

implementation code

//从官网下载AipSdk.dll引用到自己项目
//API文档地址:https://cloud.baidu.com/doc/FACE/s/Lk37c1tpf

//填写自己账号的api_key和secret_key
string api_key = "", secret_key = "";

 private void btnCompare_Click(object sender, EventArgs e)
{
    if (pictureBox1.Image == null || pictureBox2.Image == null)
    {
        MessageBox.Show("请先复制图片到图片框");
        return;
    }

    Baidu.Aip.Face.Face client = new Baidu.Aip.Face.Face(api_key, secret_key);
    List<byte[]> list = new List<byte[]>();
    list.Add(ImageToByte((Bitmap)pictureBox1.Image));
    list.Add(ImageToByte((Bitmap)pictureBox2.Image));
    JObject result = client.Match(list);
    if ((int)result["result_num"]==0)
    {
        textBox1.Text = "匹配失败";
    }
    else
    {
        JArray jarr = (JArray)result["result"];
        string score = jarr[0]["score"].ToString();
        textBox1.Text = "匹配度:" + score;
    }
}

//复制图片方法
private Image CopyImage()
{
    try
    {
        Image image = null;
        IDataObject iData = Clipboard.GetDataObject();
        if (iData.GetDataPresent(DataFormats.FileDrop))
        {
            object obj = iData.GetData(DataFormats.FileDrop);
            image = Image.FromFile((obj as string[])[0].ToString());
        }
        else if (iData.GetDataPresent(DataFormats.Bitmap))
        {
            object obj = iData.GetData(DataFormats.Bitmap);
            image = obj as Image;
        }
        return image;
    }
    catch { return null; }

}

//图片转byte[]
public byte[] ImageToByte(Bitmap inImg)
{
    MemoryStream mstream = new MemoryStream();
    inImg.Save(mstream, ImageFormat.Bmp);
    byte[] bytes = new Byte[mstream.Length];
    mstream.Position = 0;
    mstream.Read(bytes, 0, bytes.Length);
    mstream.Close();
    return bytes;
}

private void pictureBox1_Click(object sender, EventArgs e)
{
    //pictureBox1获得焦点
    pictureBox1.Focus();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
     //pictureBox2获得焦点
    pictureBox2.Focus();
}
private void pictureBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    //粘贴图片到pictureBox1
    if (e.Control && e.KeyCode == Keys.V)
    {
        pictureBox1.Image = CopyImage();
    }
}
private void pictureBox2_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    //粘贴图片到pictureBox2
    if (e.Control && e.KeyCode == Keys.V)
    {
        pictureBox2.Image = CopyImage();
    }
}

achieve effects

According to Baidu, if a similarity is greater than 80, it will generally be considered the same person.

When you look at the documentation, you may find that the parameters provided by the official website are inconsistent with what I wrote because the official website provides two APIs: V2 and V3. Just choose according to your needs.

** From simple to complex, use it immediately **

** Wonderful follow-up, keep paying attention **

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读