人脸检测源码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();
        }
    }
}
相关文章
|
编解码 人工智能
全球二氧化碳排放数据1deg产品(ODIAC)数据
全球二氧化碳排放数据1deg产品(ODIAC)数据
396 0
|
Java API Maven
【现成工具】java获取国家法定节假日包含指定月份节假日和周末
【现成工具】java获取国家法定节假日包含指定月份节假日和周末
3222 0
|
存储 Oracle Java
Java调用存储过程小结
Java调用存储过程小结
140 0
|
数据库连接 开发者 Python
Python进阶宝典:十个实用技巧提升代码效率
Python进阶宝典:十个实用技巧提升代码效率
84 0
|
canal 关系型数据库 MySQL
Canal数据同步工具
Canal数据同步工具
281 2
|
网络协议 安全 Linux
Telnet协议:远程终端协议的基础知识
Telnet协议:远程终端协议的基础知识
1393 2
|
算法 NoSQL Redis
一文搞懂布隆过滤器(BloomFilter)
一文搞懂布隆过滤器(BloomFilter)
928 0
|
图形学
Unity【DateTime】- 如何为软件添加使用有效期
Unity【DateTime】- 如何为软件添加使用有效期
287 0
Unity【DateTime】- 如何为软件添加使用有效期
|
设计模式 测试技术 数据安全/隐私保护
Page Object设计模式
在之前的博客中,测试脚本是使用线性模式来编写的
Page Object设计模式
|
6天前
|
人工智能 运维 安全