zoukankan      html  css  js  c++  java
  • 人脸检测源码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();
            }
    
    
    
        }
    }
    
  • 相关阅读:
    由wifi吞吐量问题联想到的分治思维
    总结----调试问题套路(经验)
    常用指令备忘录----持续更新
    【mark】OS是否使用svc方式分开系统空间和用户空间的优劣
    转载----五种开源协议(GPL,LGPL,BSD,MIT,Apache)
    rt-thread 动态装载实现、优化
    转:嵌入式: jffs2,yaffs2,logfs,ubifs文件系统性能分析
    gcc ld 链接器相关知识,调试指令(程序员的自我修养----链接、装载与库)
    HTML5与CSS3经典代码
    jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
  • 原文地址:https://www.cnblogs.com/a1111/p/12816124.html
Copyright © 2011-2022 走看看