人脸检测源码facedetection

人脸检测源码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Controls;
using AForge.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using face;



namespace Camtest
{
    public partial class facedetection : Form
    {
        /// <summary>
        /// 人脸检测
        /// </summary>
        public facedetection()
        {
            InitializeComponent();
            //启动默认在屏幕中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }
        FilterInfoCollection videoDevices;
        VideoCaptureDevice videoSource;
        public int selectedDeviceIndex = 0;
        public int selectedPICIndex = 0;
        /// <summary>
        /// 加载窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {


            // 刷新可用相机的列表
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
            comboBoxCameras.Items.Clear();

            for (int i = 0; i < videoDevices.Count; i++)
            {
                comboBoxCameras.Items.Add(videoDevices[i].Name.ToString());
            }


            if (comboBoxCameras.Items.Count > 0)
                comboBoxCameras.SelectedIndex = 0;
            picsize.SelectedIndex = 0;
            this.label4.Text = this.label5.Text = this.label7.Text = this.label9.Text = this.label11.Text = this.label13.Text = "正在识别";
            this.label4.ForeColor = Color.Red;
            this.label5.ForeColor = Color.Red;
            this.label7.ForeColor = Color.Red;
            this.label9.ForeColor = Color.Red;
            this.label11.ForeColor = Color.Red;
            this.label13.ForeColor = Color.Red;
            openCan();

        }
        //关闭窗体
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {


            DialogResult r = MessageBox.Show("确定要退出程序?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (r != DialogResult.OK)
            {

                e.Cancel = true;
            }
            videoSourcePlayer1.Stop();//停止摄像头
            videoSourcePlayer1.Dispose();


        }

        //实时显示照片
        private void videoSourcePlayer1_Click(object sender, EventArgs e)
        {

        }



        /// <summary>
        /// 打开摄像头
        /// </summary>
        public void openCan()
        {
            selectedPICIndex = picsize.SelectedIndex;

            selectedDeviceIndex = comboBoxCameras.SelectedIndex;
            //连接摄像头。
            videoSource = new VideoCaptureDevice(videoDevices[selectedDeviceIndex].MonikerString);
            videoSource.VideoResolution = videoSource.VideoCapabilities[selectedDeviceIndex];
            // 枚举所有摄像头支持的像素,设置拍照为1920*1080
            foreach (VideoCapabilities capab in videoSource.VideoCapabilities)
            {
                if (selectedPICIndex == 0)
                {
                    if (capab.FrameSize.Width == 1920 && capab.FrameSize.Height == 1080)
                    {
                        videoSource.VideoResolution = capab;
                        break;
                    }
                    if (capab.FrameSize.Width == 1280 && capab.FrameSize.Height == 720)
                    {
                        videoSource.VideoResolution = capab;
                        break;
                    }
                }
                else
                {
                    if (capab.FrameSize.Width == 1280 && capab.FrameSize.Height == 720)
                    {
                        videoSource.VideoResolution = capab;
                        break;
                    }
                }
            }
            videoSourcePlayer1.VideoSource = videoSource;

            // set NewFrame event handler
            videoSourcePlayer1.Start();
        }



        //保存图片
        private void button2_Click(object sender, EventArgs e)
        {
            if (videoSource == null)
                return;

            Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
            //图片名称,年月日时分秒毫秒.jpg
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";

            //获取项目的根目录
            String path = AppDomain.CurrentDomain.BaseDirectory;

            //将图片保存在服务器里面
            bitmap.Save(path + "\picture\" + fileName, ImageFormat.Jpeg);
            bitmap.Dispose();

            //进行面部特征识别
            facemodel facem = face_test.FaceDetect(path + "\picture\" + fileName);
            this.label4.Text = facem.age;      //年龄
            this.label5.Text = facem.beauty;  //漂亮度
            string expression = facem.expression;//表情
            if (expression.Equals("0"))
            {
                this.label7.Text = "不笑";
            }
            else if (expression.Equals("1"))
            {
                this.label7.Text = "微笑";
            }
            else if (expression.Equals("2"))
            {
                this.label7.Text = "大笑";
            }
            string gender = facem.gender;//性别
            if (gender.Equals("male"))
            {
                this.label9.Text = "男";
            }
            else
            {
                this.label9.Text = "女";
            }
            string glasses = facem.glasses;//是否戴眼镜
            if (glasses.Equals("0"))
            {
                this.label11.Text = "无眼镜";
            }
            else if (glasses.Equals("1"))
            {
                this.label11.Text = "普通眼镜";
            }
            else
            {
                this.label11.Text = "墨镜";
            }
            string race = facem.race;//人种
            if (race.Equals("yellow"))
            {
                this.label13.Text = "黄人";
            }
            else if (race.Equals("white"))
            {
                this.label13.Text = "白人";
            }
            else if (race.Equals("black"))
            {
                this.label13.Text = "黑人";
            }
            else if (race.Equals("arabs"))
            {
                this.label13.Text = "棕人";
            }

        }

        //取消的按钮
        private void close_Click(object sender, EventArgs e)
        {
            //停止摄像头
            videoSourcePlayer1.Stop();
            this.Close();
            welcome we = new welcome();
            we.Show();
        }



    }
}
原文地址:https://www.cnblogs.com/a1111/p/12816124.html