人脸识别开源SDK源码

代码是调用开源SDk的FaceCore关键代码。附件中有详细的接口调用说明
    FaceCore人脸识别开放平台 (SERVICE INTERFACE PLATFORM)是基于人脸检测、比对核心业务技术的服务平台。平台可为外部合作伙伴提供基于高精度人脸识别技术为基础的相关服务,例如Api、人脸识别、数据安全等。作为人脸识别的重要开发途径,FaceCore平台将推动各行各业定制、创新、进化,并最终促成新商业文明生态圈的建立。我们的使命是把人脸识别技术、规范等一系列核心技术基础服务,像水、电、煤一样输送给所有需要的合作伙伴、开发者、社区媒体、安全机构和各行各业。帮助社会各界通过使用此平台获得更丰厚的商业价值。

关键代码段:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using FaceCore.OpenPlatform.DTO;

namespace Com.Facecore.platform
{
    public class FaceClient
    {
        private string faceKey = string.Empty;
        private string faceUrlbase = string.Empty;
        public FaceClient(string key)
        {
            faceKey = key;
        }

        #region 特征值检测
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="filePath">文件地址</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetect(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))//空
                return null;
            if (!File.Exists(Path.GetFullPath(filePath))) //文件不存在
                return null;
            FileStream fs = new FileStream(Path.GetFullPath(filePath), FileMode.Open);
            byte[] bs = new byte[fs.Length];
            fs.Read(bs, 0, bs.Length);
            fs.Close();
            fs.Dispose();
            return FaceDetect(bs);
        }
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="img">包含人脸的Image对象</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetect(Image img)
        {
            if (img == null)
                return null;
            var newImage = new Bitmap(img);
            MemoryStream ms = new MemoryStream();
            newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            newImage.Dispose();

            return FaceDetect(ms.ToArray());

        }
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="bts">图片对象的byte数组</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetect(byte[] bts)
        {
            FaceHttp http = new FaceHttp();
            string image = Convert.ToBase64String(bts);          
            FaceDetectRequest request = new FaceDetectRequest() { faceimage = image };
            string url = ConstUrl.URL_CN + ConstUrl.FaceDeteiveResouse + "?appkey=" + faceKey;
            var value = http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request));
            return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceDetectResponse>(value);
        }
        #endregion

        #region 特征值检测包含年龄性别
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="filePath">文件地址</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetectAgeAndSex(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))//空
                return null;
            if (!File.Exists(Path.GetFullPath(filePath))) //文件不存在
                return null;
            FileStream fs = new FileStream(Path.GetFullPath(filePath), FileMode.Open);
            byte[] bs = new byte[fs.Length];
            fs.Read(bs, 0, bs.Length);
            fs.Close();
            fs.Dispose();
            return FaceDetectAgeAndSex(bs);
        }
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="img">包含人脸的Image对象</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetectAgeAndSex(Image img)
        {
            if (img == null)
                return null;
            var newImage = new Bitmap(img);
            MemoryStream ms = new MemoryStream();
            newImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            newImage.Dispose();

            return FaceDetectAgeAndSex(ms.ToArray());

        }
        /// <summary>
        /// 特征值检测
        /// </summary>
        /// <param name="bts">图片对象的byte数组</param>
        /// <returns></returns>
        public FaceDetectResponse FaceDetectAgeAndSex(byte[] bts)
        {
            FaceHttp http = new FaceHttp();
            string image = Convert.ToBase64String(bts);
            //http.HttpPost("", "");
            FaceDetectRequest request = new FaceDetectRequest() { faceimage = image };
            string url = ConstUrl.URL_CN + ConstUrl.FaceDeteiveAgeAndSexResouse + "?appkey=" + faceKey;
            var value = http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request));
            return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceDetectResponse>(value);
        }
        #endregion

        #region 特征值比较
        /// <summary>
        /// 特征值比较
        /// </summary>
        /// <param name="facetrue1">特征值返回串</param>
        /// <param name="facetrue2">特征值返回串</param>
        public FaceCompareResponse FaceContrast(string facetrue1, string facetrue2)
        {
            FaceCompareRequest request = new FaceCompareRequest();
            request.facefeature1 = facetrue1;
            request.facefeature2 = facetrue2;
            string url = ConstUrl.URL_CN + ConstUrl.FaceCompareResouse + "?appkey=" + faceKey;
            FaceHttp http = new FaceHttp();
            return Newtonsoft.Json.JsonConvert.DeserializeObject<FaceCompareResponse>(http.HttpPost(url, Newtonsoft.Json.JsonConvert.SerializeObject(request)));
        }
        #endregion
    }
}

人脸比对客户端代码:

using System;
using System.Drawing;
using System.Windows.Forms;
using Com.Facecore.platform;
using System.IO;
using Com.Facecore.platform.Common;

namespace faceView.UcControl
{
    public partial class UcCompar : UserControl
    {
        public UcCompar()
        {
            InitializeComponent();
            ImgSelect = Common.ConvertImage(Path.GetFullPath("Images\\button\\选择照片-人脸比对认证.png"));
            ImgSelect_Click = Common.ConvertImage(Path.GetFullPath("Images\\button\\选择照片-点击-人脸比对认证.png"));
            ImgCompar = Common.ConvertImage(Path.GetFullPath("Images\\button\\开始比对-人脸比对认证.png"));
            ImgCompar_Click = Common.ConvertImage(Path.GetFullPath("Images\\button\\开始比对-点击-人脸比对认证.png"));
            pic1Select.Image = ImgSelect;
            pic2Select.Image = ImgSelect;
            picCompar.Image = ImgCompar;
        }
        private Image ImgSelect { get; set; }
        private Image ImgSelect_Click { get; set; }
        private Image ImgCompar { get; set; }
        private Image ImgCompar_Click { get; set; }
        string face1true = string.Empty;
        string face2true = string.Empty;
        private string SelectPicture()
        {
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter =
                "JPEG File Interchange Format (*.jpg)|*.jpg;*.jpeg|" +
                "Windows Bitmap(*.bmp)|*.bmp|" +
                "Graphics Interchange Format (*.gif)|(*.gif)|" +
                "Portable Network Graphics (*.png)|*.png|" +
                "Tag Image File Format (*.tif)|*.tif;*.tiff";
            openFileDialog.ShowDialog();
            return openFileDialog.FileName;
        }
        private void btn1Select_Click(object sender, EventArgs e)
        {
            string filename = SelectPicture();
            if (!string.IsNullOrEmpty(filename))
            {
                //8c1be718fa976083a6940009f36c056e
                FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e");
                byte[] bt = GetFilebyte(filename);
                var face = client.FaceDetect(filename);//获取特征值
                pic1.Image = Image.FromStream(new MemoryStream(bt));
                if (face != null && face.facemodels.Count > 0)
                {
                    face1true = face.facemodels[0].base64feature;
                }
                else
                {
                    MessageBox.Show("未读取到人脸信息");
                    pic1.Image = null;
                    face1true = string.Empty;
                }
            }
            else
            {
                pic1.Image = null;
                face1true = string.Empty;
            }

        }
        private byte[] GetFilebyte(string filename)
        {
            if (File.Exists(filename))
            {
                FileStream stream = new FileStream(filename, FileMode.Open);
                byte[] bts = new byte[stream.Length];
                stream.Read(bts, 0, bts.Length);
                stream.Close();
                stream.Dispose();
                return bts;
            }
            return null;
        }
        private void btn2Select_Click(object sender, EventArgs e)
        {
            string filename = SelectPicture();
            if (!string.IsNullOrEmpty(filename))
            {
                FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e");
                byte[] bt = GetFilebyte(filename);
                var face = client.FaceDetect(bt);//获取特征值
                pic2.Image = Image.FromStream(new MemoryStream(bt));
                if (face != null && face.facemodels.Count > 0)
                {
                    face2true = face.facemodels[0].base64feature;
                }
                else
                {
                    MessageBox.Show("未读取到人脸信息");
                    pic2.Image = null;
                    face2true = string.Empty;
                }

            }
            else
            {
                pic2.Image = null;
                face2true = string.Empty;
            }
        }
        private void btnCompar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(face1true) || string.IsNullOrEmpty(face2true))
            {
                MessageBox.Show("缺少人脸信息");
                return;
            }
            FaceClient client = new FaceClient("8c1be718fa976083a6940009f36c056e");
            var contrast = client.FaceContrast(face1true, face2true);
            if (contrast != null)
            {
                lblCompar.Text = "相似度" + (contrast.similar * 100).ToString("0.00") + "%";
            }
        }

        private void pic2Select_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.Image = ImgSelect_Click;
        }

        private void pic2Select_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pic = (PictureBox)sender;
            pic.Image = ImgSelect;

        }

        private void picCompar_MouseEnter(object sender, EventArgs e)
        {
            picCompar.Image = ImgCompar_Click;
        }

        private void picCompar_MouseLeave(object sender, EventArgs e)
        {
            picCompar.Image = ImgCompar;
        }
    }
}


 源程序下载地址1:http://download.csdn.net/detail/lovegonghui/9003385

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值