AI两张图片人脸比对

代码实现的功能:

通过两张照片进行比对得出相似度,来判断两张照片是否为同一人。此功能可以用在多种场景,例如学校的门禁系统。

代码实现:

private string APP_ID = "33711276";
private string API_KEY = "1nhS4AcrLFffxqDKB73bb1U7";
private string SECRET_KEY = "rtnRFv5RcE0dMxBWqmL6PX3LYwVAdETW";
private Face client = null;

    本段代码定义了三个字符串变量 APP_IDAPI_KEY 和 SECRET_KEY,以及一个 Face 类型的私有变量 client,它被初始化为 null。本段代码的功能为初始化一个与某种人脸识别服务交互的客户端。

private bool IsStart = false;

该行代码的作用是判断是否可以检测人脸

public Form1()
{
    InitializeComponent();
    client = new Face(API_KEY, SECRET_KEY);
}

本段代码初始化了窗体组件 InitializeComponent(),并随后创建了一个 Face 类的实例,将 API_KEY 和 SECRET_KEY 作为参数传递给该实例的构造函数。

private void button2_Click(object sender, EventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog();
    dialog.InitialDirectory = "F:\\";
    dialog.Filter = "所有文件|*.*";
    dialog.RestoreDirectory = true;
    dialog.FilterIndex = 2;
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        if (string.IsNullOrEmpty(textBox2.Text))
        {
            textBox2.Text = dialog.FileName;
        }
        else
        {
            textBox3.Text = dialog.FileName;
        }
    }

}

本段代码实现了一个文件选择对话框(OpenFileDialog),该对话框允许用户从指定的初始目录(D盘)中选择一个文件。然后,根据 textBox2 是否为空,将所选文件的路径赋值给 textBox2 或 textBox3

public string ReadImg(string img)
{
    return Convert.ToBase64String(File.ReadAllBytes(img));
}

该段代码实现了将读取两个图片文件的内容转换为Base64编码的字符串,

private void button1_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
    {
        MessageBox.Show("请选择要对比的人脸图片");
        return;
    }
    try
    {
        string path1 = textBox2.Text;
        string path2 = textBox3.Text;

        var faces = new JArray
        {
            new JObject
            {
                {"image", ReadImg(path1)},
                {"image_type", "BASE64"},
                {"face_type", "LIVE"},
                {"quality_control", "LOW"},
                {"liveness_control", "NONE"},
            },
            new JObject
            {
                {"image", ReadImg(path2)},
                {"image_type", "BASE64"},
                {"face_type", "LIVE"},
                {"quality_control", "LOW"},
                {"liveness_control", "NONE"},
            }
         };

        // 带参数调用人脸比对
        var result = client.Match(faces);
        textBox1.Text = result.ToString();
    }
    catch (Exception ex)
    { }

}

该段代码的作用是当text.box2或text.box3中没有图片时,输出“请选择要对比的人脸图片”。然后构建一个JSON数组,该数组包含两个对象,每个对象代表一个要用于人脸比对的图片。接着调用一个名为 client.Match 的方法来执行人脸比对,并将结果输出到 textBox1

代码结果:

完整代码:

using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using Baidu.Aip.Face;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Forms;



namespace 作业3
{
    public partial class Form1 : Form
    {
        private string APP_ID = "33711276";
        private string API_KEY = "1nhS4AcrLFffxqDKB73bb1U7";
        private string SECRET_KEY = "rtnRFv5RcE0dMxBWqmL6PX3LYwVAdETW";
        private Face client = null;
        /// <summary>
        /// 是否可以检测人脸
        /// </summary>
        private bool IsStart = false;
        
        
        public Form1()
        {
            InitializeComponent();
            client = new Face(API_KEY, SECRET_KEY);
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.InitialDirectory = "F:\\";
            dialog.Filter = "所有文件|*.*";
            dialog.RestoreDirectory = true;
            dialog.FilterIndex = 2;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (string.IsNullOrEmpty(textBox2.Text))
                {
                    textBox2.Text = dialog.FileName;
                }
                else
                {
                    textBox3.Text = dialog.FileName;
                }
            }

        }
        public string ReadImg(string img)
        {
            return Convert.ToBase64String(File.ReadAllBytes(img));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("请选择要对比的人脸图片");
                return;
            }
            try
            {
                string path1 = textBox2.Text;
                string path2 = textBox3.Text;

                var faces = new JArray
                {
                    new JObject
                    {
                        {"image", ReadImg(path1)},
                        {"image_type", "BASE64"},
                        {"face_type", "LIVE"},
                        {"quality_control", "LOW"},
                        {"liveness_control", "NONE"},
                    },
                    new JObject
                    {
                        {"image", ReadImg(path2)},
                        {"image_type", "BASE64"},
                        {"face_type", "LIVE"},
                        {"quality_control", "LOW"},
                        {"liveness_control", "NONE"},
                    }
                 };

                // 带参数调用人脸比对
                var result = client.Match(faces);
                textBox1.Text = result.ToString();
            }
            catch (Exception ex)
            { }

        }
    }
}

实验小结:

        通过本次实验我学习到了人脸比对的相关代码知识,通过此代码我们可以实现人脸对比功能,可运用于多个场景,例如校园录入人脸信息,通过人脸比对实现刷脸进门进门功能。

源码:

AI人脸比对 - 代码片段 - Gitee.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值