基本信息
源码名称:C# 人脸识别
源码大小:2.50M
文件格式:.rar
开发语言:C#
更新时间:2019-01-10
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace FaceRecognition
{
    public partial class FrmMain : Form
    {
        public FrmMain() {
            InitializeComponent();
        }
        #region 结构体
        unsafe public struct DLL_1CN_RECOG_OUT_STRUCT
        {
            public byte address;                 //'无意义,传址用的
            public float VALUE;                  //'相似度值
            public fixed byte Template_ID[33];          //'相似的ID
            public fixed byte TemplateFileName[256];    //'这个ID对应的模板的图像文件名称
        };



        unsafe public struct DLL_OUT_FACE_STRUCT
        {
            public byte address;     //无意义,传址的
            public Int32 eye1_x;
            public Int32 eye1_y;

            public Int32 eye2_x;
            public Int32 eye2_y;       //两眼坐标

            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;       //人脸矩形

            public Int32 angle;        //人脸平面角度 (正面垂直时为90度)
            public float tally;       //得分 ,100分制,即人脸的置信度。

            public Int32 is_small_face;//    '是小脸还是大脸,1为小脸,是启用了小脸检测的结果,但小脸不能进行后续的人脸识别
            //'当是小脸时,只输出人脸矩形,即只有人脸矩形有效,其它值无效

            //'下面的值要进行了人眼定位与人脸检测的后期处理才有效
            public Int32 skin_color_R;     //'采样肤色COLORREF。(RGB)
            public Int32 skin_color_G;     //'采样肤色COLORREF。(RGB)
            public Int32 skin_color_B;     //'采样肤色COLORREF。(RGB)
            public Int32 skin_hd_bright;   //'采样肤色的灰度亮度。

            public Int32 left_face_len;        //'从左眼开始计算的左脸估计长度。
            public Int32 right_face_len;       //'从右眼开始计算的右脸估计长度。
            public float face_width_rely;   // '脸宽的信任度[0,1)。

            public Int32 nose_x;                // '在原图像中的鼻尖位置。
            public Int32 nose_y;                //'在原图像中的鼻尖位置。
            public float nose_rely;         //'鼻尖位置:可信任度[0,1)。

            public Int32 month_x;               // '在原图像中的嘴中心位置。
            public Int32 month_y;                // '在原图像中的嘴中心位置。
            public float month_rely;           // '嘴心位置:可信任度[0,1)。

            public float glass_rely;             //'可能眼镜的置信度[0,1)

            public Int32 eye1_w;
            public Int32 eye1_h;
            public Int32 eye2_w;
            public Int32 eye2_h;        //人眼球大小,要示姿势端正,眼距为100以上的人脸,且眼球无反光的情况下才正确

            public Int32 CloseEyeBelievable;           //闭眼的可能性系数输出,值域[0,1000]
        };//END STRUCT DEF
        #endregion

        #region 变量与API定义
        public bool is_usb_ok;
        public Int32 OID;

        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zInitialize(string UserName);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zUnInitialize();
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zCreateOneThreadObject(Int32 is_load_tzlib, string datafilepath);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zDeleteOneThreadObject(Int32 OID);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zGetLastError(Int32 OID, byte* outstr);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zFaceLocate(Int32 OID, string FileName, Int32 MAX_OUT_NUMS, float Threshold, DLL_OUT_FACE_STRUCT* pofs);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zFlagFace(Int32 OID, Int32 draw_window_hwnd, Int32 order, Int32 offset_x, Int32 offset_y);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zFaceLocate_FreeMemory(Int32 OID);

        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zRecog1C1(Int32 OID, string vid, Int32 order, float* VALUE, string TemplateFileName);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zRecog1C1_Fast(Int32 OID, string vid, Int32 order, float* VALUE, string TemplateFileName);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zRecog1CN(Int32 OID, Int32 order, Int32 max_out_num, DLL_1CN_RECOG_OUT_STRUCT* pout);

        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 UsbVideo_Init(Int32 DEVICE_ID, Int32 play_window_hwnd, Int32 Base_Height);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 UsbVideo_Get_Width();
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 UsbVideo_Get_Height();
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 UsbVideo_CapOneBmp(string bmpFileName);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 UsbVideo_EndAll();



        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zAddFaceTemplate(Int32 OID, string TID, Int32 order);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zDelTemplateA(Int32 OID, string TID, string template_filename);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zDelTemplateB(Int32 OID, string TID, Int32 BH);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zDelAllTemplate(Int32 OID);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zUpdateMemory(Int32 OID);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zCountMemoryTidTotaleNums(Int32 OID);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zDrawOneTemplatePhoto(Int32 OID, string TID, Int32 BH, Int32 object_window_hwnd, Int32 start_x, Int32 start_y, Int32 IS_FLAG_EYE);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zCountTemplateTotaleNums(Int32 OID, string TID);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zEnumMemoryTid(Int32 OID, byte* alltid);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zEnumTid(Int32 OID, byte* alltid);


        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zGetA(Int32 OID, Int32 PARA_NAME_ORDER);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zSetA(Int32 OID, Int32 PARA_NAME_ORDER, Int32 VALUE);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zSetB(Int32 OID, Int32 PARA_NAME_ORDER, float VALUE);


        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zInitCheck(Int32 OID, Int32 order);
        [DllImport("SunLightFace.dll")]
        public static unsafe extern Int32 zFrameCheck(Int32 OID, string bmpFileName);


        [DllImport("kernel32.dll")]
        public static unsafe extern Int32 Beep(Int32 dwFreq, Int32 dwDuration);
        #endregion

        #region 重写Dispose
        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing) {
            if (disposing && (components != null)) {
                components.Dispose();
            }
            base.Dispose(disposing);
            //当关窗口时,放出内存
            if (OID > 0) zDeleteOneThreadObject(OID); //回收对象内存
            zUnInitialize(); //反初始化
            if (is_usb_ok == true) UsbVideo_EndAll();
        }
        #endregion

        #region 窗体载入初始化时
        //载入初始化时
        private void FrmMain_Load(object sender, System.EventArgs e) {
            OID = -1;
            is_usb_ok = true;
            if (UsbVideo_Init(0, pictureBox1.Handle.ToInt32(), 240) != 1) {
                MessageBox.Show("本范例程序需要有USB摄像头的支持!");
                is_usb_ok = false;
                Dispose(true);
            }

            //int vw = UsbVideo_Get_Width();
            //int vh = UsbVideo_Get_Height();
            //pictureBox1.Width = vw;
            //pictureBox1.Height = vh;


            zInitialize("阳光人脸识别二次开发包<青铜版>:此版为免费正式版本,最大用户数1000,许可商用.");
            OID = zCreateOneThreadObject(1, Application.StartupPath   "\\zdata");
            if (OID < 1) MessageBox.Show("载入人脸数据失败!");
        }//END FUNC 
        #endregion

        #region 功能函数
        //功能函数,把VC的byte *转为C#的string
        unsafe private string byte2string(byte* p, Int32 len) {
            byte[] ba = new byte[len];
            for (int i = 0; i < len; i  ) ba[i] = p[i];
            string outr = System.Text.Encoding.Default.GetString(ba);
            int at = outr.IndexOf("\0");//去掉尾部的C语言结束符
            if (at == -1) return string.Empty;
            return outr.Substring(0, at);
        }//end func :len长度是空间长度,但空间的尾部可能并未填满,再之前则是C语言结束符 
        #endregion

        #region 控件事件
        #region 人脸检测 - button1_Click
        //人脸检测
        unsafe private void button1_Click(object sender, System.EventArgs e) {
            Int32 i;
            //1.先捉一张图						
            if (UsbVideo_CapOneBmp("temp.bmp") != 1) return;


            //2.人脸定位
            DLL_OUT_FACE_STRUCT* pofs = stackalloc DLL_OUT_FACE_STRUCT[32   1];
            Int32 ret = zFaceLocate(OID, "temp.bmp", 32, 0.4f, pofs);
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
                return;
            }
            //else if(ret>0)MessageBox.Show("OK");

            //3.标出人脸
            pictureBox2.Image = Image.FromFile("temp.bmp");
            pictureBox2.Refresh();
            pictureBox2.Image.Dispose();
            for (i = 0; i < ret; i  ) zFlagFace(OID, pictureBox2.Handle.ToInt32(), i, 0, 0);


            //4.清除人脸检测内存
            zFaceLocate_FreeMemory(OID);
        }//END FUNC 
        #endregion

        #region 人脸认证 - button2_Click
        //人脸认证
        unsafe private void button2_Click(object sender, System.EventArgs e) {
            Int32 i;
            //1.先捉一张图
            if (UsbVideo_CapOneBmp("temp.bmp") != 1) return;


            //2.人脸定位
            DLL_OUT_FACE_STRUCT* pofs = stackalloc DLL_OUT_FACE_STRUCT[32   1];
            Int32 ret = zFaceLocate(OID, "temp.bmp", 32, 0.5f, pofs);
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
                return;
            }

            //3.标出人脸
            pictureBox2.Image = Image.FromFile("temp.bmp");
            pictureBox2.Refresh();
            pictureBox2.Image.Dispose();


            for (i = 0; i < ret; i  ) zFlagFace(OID, pictureBox2.Handle.ToInt32(), i, 0, 0);

            //4.人脸认证
            float xsd;

            ret = zRecog1C1_Fast(OID, t1.Text, 0, &xsd, null);
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
            }

            label2.Text = xsd.ToString();

            if (zGetA(OID, 8) == 1) zUpdateMemory(OID);//让动态模板更新生效

            //5.清除人脸检测内存
            zFaceLocate_FreeMemory(OID);
        } 
        #endregion

        #region 人脸识别 - button3_Click
        //人脸识别
        unsafe private void button3_Click(object sender, System.EventArgs e) {
            Int32 i;
            //1.先捉一张图
            if (UsbVideo_CapOneBmp("temp.bmp") != 1) return;


            //2.人脸定位
            DLL_OUT_FACE_STRUCT* pofs = stackalloc DLL_OUT_FACE_STRUCT[32   1];
            Int32 ret = zFaceLocate(OID, "temp.bmp", 32, 0.5f, pofs);
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
                return;
            }

            //3.标出人脸
            pictureBox2.Image = Image.FromFile("temp.bmp");
            pictureBox2.Refresh();
            pictureBox2.Image.Dispose();


            for (i = 0; i < ret; i  ) zFlagFace(OID, pictureBox2.Handle.ToInt32(), i, 0, 0);

            //4.人脸识别


            DLL_1CN_RECOG_OUT_STRUCT rout;

            ret = zRecog1CN(OID, 0, 1, &rout);
            if (ret >= 1) {
                label2.Text = "  v:"   rout.VALUE.ToString();
                label2.Text  = " id:"   byte2string(rout.Template_ID, 33);
                label2.Text  = " fn:"   byte2string(rout.TemplateFileName, 256);
            }
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
            }

            if (zGetA(OID, 8) == 1) zUpdateMemory(OID);//让动态模板更新生效
            //5.清除人脸检测内存
            zFaceLocate_FreeMemory(OID);
        }//END FUNC 
        #endregion

        #region 添加一个模板 - button4_Click
        //添加一个模板
        unsafe private void button4_Click(object sender, EventArgs e) {
            Int32 i;
            //1.先捉一张图
            if (UsbVideo_CapOneBmp("tempAAA.bmp") != 1) return;

            //2.人脸定位
            DLL_OUT_FACE_STRUCT* pofs = stackalloc DLL_OUT_FACE_STRUCT[32   1];
            Int32 ret = zFaceLocate(OID, "tempAAA.bmp", 32, 0.5f, pofs);
            if (ret < 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
                return;
            }

            //3.标出人脸
            pictureBox2.Image = Image.FromFile("tempAAA.bmp");
            pictureBox2.Refresh();
            pictureBox2.Image.Dispose();


            for (i = 0; i < ret; i  ) zFlagFace(OID, pictureBox2.Handle.ToInt32(), i, 0, 0);

            //4.开始加模板,并让添加立即生效。

            if (zAddFaceTemplate(OID, t1.Text, 0) == 1) zUpdateMemory(OID);

            //5.清除人脸检测内存
            zFaceLocate_FreeMemory(OID);
        }//END FUNC 
        #endregion

        #region 删除指定ID的第1个模板 - button5_Click
        //删除指定ID的第1个模板
        private void button5_Click(object sender, EventArgs e) {
            if (t1.Text.Length > 0)//ID已输入
            {
                zDelTemplateB(OID, t1.Text, 0); //删除第1个模板,如果最后一个参数为2,就是删除第三个模板
            }
        }//END FUNC 
        #endregion

        #region 画出指定ID的模板照片 - button6_Click
        //画出指定ID的模板照片
        private void button6_Click(object sender, EventArgs e) {
            if (t1.Text.Length == 0) return;

            Int32 ret1 = zCountTemplateTotaleNums(OID, t1.Text);


            Int32 SY = 280;
            Int32 i, SX;
            for (i = 0; i < ret1; i  ) {
                SX = 10   i * 120;

                if (i >= 5) {
                    SY = 420;
                    SX = 10   (i - 5) * 120;
                }

                zDrawOneTemplatePhoto(OID, t1.Text, i, this.Handle.ToInt32(), SX, SY, 0);
            }
        }//end func 
        #endregion

        #region 是否选择了动态模板更新 - checkBox1_CheckedChanged
        //是否选择了动态模板更新
        private void checkBox1_CheckedChanged(object sender, EventArgs e) {
            if (checkBox1.Checked == true)
                zSetA(OID, 23, 1);
            else
                zSetA(OID, 23, 0);
        }
        #endregion

        #region 活体识别 - button7_Click
        //活体识别 
        unsafe private void button7_Click(object sender, EventArgs e) {
            label2.Text = "";

            Int32 i;
            //1.先捉一张图
            if (UsbVideo_CapOneBmp("temp.bmp") != 1) return;

            //2.人脸定位
            DLL_OUT_FACE_STRUCT* pofs = stackalloc DLL_OUT_FACE_STRUCT[32   1];
            Int32 ret = zFaceLocate(OID, "temp.bmp", 1, 0.5f, pofs);
            if (ret <= 0) {
                byte* ermsg = stackalloc byte[256];
                zGetLastError(OID, ermsg);
                MessageBox.Show(byte2string(ermsg, 256));
                return;
            }

            //3.标出人脸
            zFlagFace(OID, pictureBox1.Handle.ToInt32(), 0, 0, 0);


            //////////////////////////////////////
            //下面是插入的活体识别代码
            bool is_ht_ok = false;
            zInitCheck(OID, 0);  //初始化活体检测

            /////////////////////////////////////////////////////////////////////////////
            //注意:请在这个位置向被识别用户给出眨眼的提示---------------------<ADD CODE>
            /////////////////////////////////////////////////////////////////////////////
            //向用户提示闭眼,建议用图显提示法,比如语音提示或图形标志提示
            Beep(1000, 50);

            for (i = 0; i < 20; i  )// 这个20次循环,乘上下面每次循环的时间,就是活体识别的等待总时间,可由开发人员自行调节。
            {
                if (UsbVideo_CapOneBmp("temp.bmp") != 1) continue;
                int ret4 = zFrameCheck(OID, "temp.bmp");  //进行连续帧检测
                if (ret4 == 1) { is_ht_ok = true; break; }
                if (ret4 < 0) break; //发现"作假"
                // '如果返回值RET4=0,表示还没判断出来,继续循环...	
            }

            /////////////////////////////////////////////////////////////////////////////
            //注意:请在这个位置向被识别用户给出眨眼的提示---------------------<ADD CODE>
            /////////////////////////////////////////////////////////////////////////////
            Beep(1000, 50); //向用户提示可以睁眼了
            //////////////////////////////////////
            //////////////////////////////////////
            //上面是插入的活体识别代码

            //4.人脸认证
            if (is_ht_ok == true) {
                float xsd;

                ret = zRecog1C1_Fast(OID, t1.Text, 0, &xsd, null);
                if (ret < 0) {
                    byte* ermsg = stackalloc byte[256];
                    zGetLastError(OID, ermsg);
                    MessageBox.Show(byte2string(ermsg, 256));
                }

                label2.Text = xsd.ToString();

                if (zGetA(OID, 8) == 1) zUpdateMemory(OID);//让动态模板更新生效
            }

            //5.清除人脸检测内存
            zFaceLocate_FreeMemory(OID);
        }//end func 
        #endregion

        #region 枚举所有ID - button8_Click
        //枚举所有ID
        private void button8_Click(object sender, EventArgs e) {
            string[] tidarray = null;
            unsafe {
                Int32 num = zCountTemplateTotaleNums(OID, null);
                byte* outbuf = stackalloc byte[33 * num];
                zEnumTid(OID, outbuf);
                string alltid = byte2string(outbuf, 33 * num);
                tidarray = alltid.Split(',');
                MessageBox.Show(alltid);
            }

            if (tidarray != null) {
                for (int i = 0; i < tidarray.Length; i  ) Console.WriteLine(tidarray[i]);
            }
        }//END FUNC 
        #endregion
        #endregion
    }
}