人脸检测源码facedetection

简介: 人脸检测源码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();
        }
    }
}
相关文章
|
6月前
|
算法 计算机视觉 开发者
OpenCV中使用Eigenfaces人脸识别器识别人脸实战(附Python源码)
OpenCV中使用Eigenfaces人脸识别器识别人脸实战(附Python源码)
335 0
|
计算机视觉 C++ Python
OpenCV人脸检测打卡系统
OpenCV人脸检测打卡系统
95 0
|
5月前
|
监控 安全 计算机视觉
实战 | 18行代码轻松实现人脸实时检测【附完整代码与源码详解】Opencv、人脸检测
实战 | 18行代码轻松实现人脸实时检测【附完整代码与源码详解】Opencv、人脸检测
|
6月前
|
计算机视觉 开发者 Python
OpenCV中Fisherfaces人脸识别器识别人脸实战(附Python源码)
OpenCV中Fisherfaces人脸识别器识别人脸实战(附Python源码)
242 0
|
6月前
|
机器学习/深度学习 编解码 算法
Python实现视频人脸检测识别功能
Python实现视频人脸检测识别功能
127 0
|
6月前
|
机器学习/深度学习 计算机视觉
人脸检测
人脸检测
199 2
|
6月前
|
算法 计算机视觉 开发者
OpenCV中LBPH人脸识别器识别人脸实战(附Python源码)
OpenCV中LBPH人脸识别器识别人脸实战(附Python源码)
393 0
|
存储 算法 数据可视化
|
机器学习/深度学习 人工智能 算法
LabVIEW快速实现OpenCV DNN(YunNet)的人脸检测(含源码)
LabVIEW快速实现OpenCV DNN(YunNet)的人脸检测
587 0
LabVIEW快速实现OpenCV DNN(YunNet)的人脸检测(含源码)
|
人工智能 Java 计算机视觉
Java实现人脸检测
Java实现人脸检测
272 0