人脸认证源码faceIdentify

简介: 人脸认证源码faceIdentify

人脸认证:

using AForge.Video.DirectShow;
using face;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Camtest
{
    public partial class faceIdentify : Form
    {
        public faceIdentify()
        {
            InitializeComponent();
            //启动默认在屏幕中间
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        }
        //Api_Key
        public static string Api_Key = "OVYw5Ok0y9U8n6CfVPYt0wfZ";
        //Secret_Key
        public static string Secret_Key = "aCN3lupCarq3rC9G8Rylqz1d36Towp8G";
        FilterInfoCollection videoDevices;
        VideoCaptureDevice videoSource;
        public int selectedDeviceIndex = 0;
        public int selectedPICIndex = 0;
        //窗体加载
        private void faceIdentify_Load(object sender, EventArgs e)
        {
           //显示为正在检测
            this.label1.Text = this.label2.Text = this.label6.Text = this.label9.Text = "正在识别";
            // 刷新可用相机的列表
            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;
            //打开摄像头
            openCamera();
        }
        //打开摄像头
        public void openCamera()
        {
            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();
        }
        /// <summary>
        /// 签到的按钮
        /// 先保存图片,然后进行比较,获取的id,查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void qiandao_Click(object sender, EventArgs e)
        {
            Users users = FaceIdentifys(SavePicture());
            this.label1.Text = users.age.ToString();
            this.label2.Text = users.name;
            this.label6.Text = users.phone;
            this.label9.Text = users.address;
            if (users.picture != null)
            {
                this.pictureBox1.Image = Image.FromFile(users.picture, false);
            }
        }
        //关闭窗口
        private void faceIdentify_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult r = MessageBox.Show("确定要退出程序?", "操作提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (r != DialogResult.OK)
            {
                e.Cancel = true;
            }
            videoSourcePlayer1.Stop();//停止摄像头
            videoSourcePlayer1.Dispose();
        }
        /// <summary>
        /// 人脸识别
        /// </summary>
        /// <param name="filename"></param>
        public static Users FaceIdentifys(string filename)
        {
            long id = 0;
            string ids = "";
            double scores_num = 0;
            Users user = new Users();
            var client = new Baidu.Aip.Face.Face(Api_Key, Secret_Key);
            var image1 = File.ReadAllBytes(filename);
            var result = client.User.Identify(image1, new[] { "gr_test" }, 1, 1);
            //先判断脸是不是在上面,在继续看有匹配的没,否则提示放上脸
            //得到根节点
            JObject jo_result = (JObject)JsonConvert.DeserializeObject(result.ToString());
            if ((string)jo_result["error_msg"] != null)
            {
                MessageBox.Show("对不起,请把脸放上!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
            else
            {
                //检测到脸
                //得到result节点
                JArray jo_age = (JArray)JsonConvert.DeserializeObject(jo_result["result"].ToString());
                foreach (var val in jo_age)
                {
                    id = long.Parse(((JObject)val)["uid"].ToString());  //获取uid
                    string scores = ((JObject)val)["scores"].ToString();//获取scores
                    int num1 = scores.IndexOf("\n") + 2;
                    int num2 = scores.LastIndexOf("]")-8;
                    ids = scores.Substring(num1, num2);
                    scores_num =double.Parse(ids);
                }
                if (scores_num > 80)
                {
                    user = QueryUsersById(id);
                    if (user.id != 0)
                    {
                        MessageBox.Show("签到成功,已检测到您的信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("对不起,系统根据您的脸未检测到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                }
                else {
                    MessageBox.Show("对不起,系统根据您的脸未检测到信息", "操作提示", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                }
            }
            return user;
        }
        /// <summary>
        /// 保存图片
        /// </summary>
        public string SavePicture()
        {
            if (videoSource == null)
            {
                return null;
            }
            Bitmap bitmap = videoSourcePlayer1.GetCurrentVideoFrame();
            //图片名称,年月日时分秒毫秒.jpg
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff") + ".jpg";
            //获取项目的根目录
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string picture = path + "\\picture\\" + fileName;
            //将图片保存在服务器里面
            bitmap.Save(picture, ImageFormat.Jpeg);
            bitmap.Dispose();
            return picture;
        }
        /// <summary>
        /// 根据编号查询用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Users QueryUsersById(long id)
        {
            Users user = new Users();
            string sql = "select * from users where id = @id";
            using (SqlDataReader reader = SqlHelper.ExcuteReader(sql, CommandType.Text, new SqlParameter("@id", id)))
            {
                if (reader.Read())
                {
                    user.id = long.Parse(reader[0].ToString());
                    user.name = reader[1].ToString();
                    user.age = Convert.ToInt32(reader[2]);
                    user.phone = reader[3].ToString();
                    user.password = reader[4].ToString();
                    user.address = reader[5].ToString();
                    user.picture = reader[6].ToString();
                }
            }
            return user;
        }
        //取消的按钮
        private void close_Click(object sender, EventArgs e)
        {
            //停止摄像头
            videoSourcePlayer1.Stop();
            this.Close();
            welcome we = new welcome();
            we.Show();
        }
    }
}
相关文章
|
4月前
|
机器学习/深度学习 敏捷开发 人工智能
阿里云人工智能平台PAI和百炼有什么区别?PAI和百炼定位、功能及使用方法对比
阿里云PAI是“造模型”平台,面向算法工程师,支持从训练到部署的全周期AI开发;百炼是“用模型”平台,聚焦大模型快速应用,助力业务团队低门槛构建智能体。两者互补协同,覆盖AI开发全流程。
1214 5
|
4月前
|
人工智能 前端开发 API
Google发布50页AI Agent白皮书,老金帮你提炼10个核心要点
老金分享Google最新AI Agent指南:让AI从“动嘴”到“动手”。Agent=大脑(模型)+手(工具)+协调系统,可自主完成任务。通过ReAct模式、多Agent协作与RAG等技术,实现真正自动化。入门推荐LangChain,文末附开源知识库链接。
2458 119
|
8天前
|
人工智能 自然语言处理 文字识别
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
Qwen3.7-Max是阿里云百炼面向智能体时代推出的新一代旗舰模型,对标GPT-5.5、Claude Opus 4.7等闭源旗舰。该模型支持百万级token上下文窗口,具备顶级推理能力、多模态搜索与视觉理解增强、流式输出低延迟响应等核心优势,覆盖编程、办公、长周期自主执行等复杂场景。同时支持OpenAI接口兼容,便于系统快速迁移。用户可通过Token Plan团队或节省计划等订阅方式灵活调用,适合企业级高要求场景使用。
3517 15
阿里云百炼Qwen3.7-Max简介:能力、优势、支持订阅计划参考
|
15天前
|
人工智能 开发工具 iOS开发
Claude Code 新手完全上手指南:安装、国产模型配置与常用命令全解
Claude Code 是一款运行在终端环境中的 AI 编程助手,能够直接在命令行中完成代码生成、项目分析、文件修改、命令执行、Git 管理等开发全流程工作。它最大的特点是**任务驱动、终端原生、轻量高效、多模型兼容**,无需图形界面、不依赖 IDE 插件,能够深度融入开发者日常工作流。
3572 12
|
9天前
|
人工智能 自然语言处理 供应链
|
12天前
|
人工智能 Linux BI
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
JeecgBoot AI专题研究 一键脚本:Claude Code + JeecgBoot Skills + DeepSeek 全平台接入 一行命令装好 Claude Code + JeecgBoot Skills + DeepSeek 接入,无需翻墙使用 Claude Code,支持 Wind
2930 7
国内用 Claude Code 终于不用翻墙了:一行命令搞定,自动接 DeepSeek
|
18天前
|
Shell API 开发工具
Claude Code 快速上手指南(新手友好版)
AI编程工具卷疯啦!Claude Code凭借任务驱动+终端原生的特性,成了开发者的效率搭子。本文从安装、登录、切换国产模型到常用命令,手把手带新手快速上手,全程避坑,30分钟独立用起来。
3692 25
|
9天前
|
人工智能 自然语言处理 安全
Claude Code 全攻略:命令大全+三种模式+记忆体系+实战工作流完整手册
Claude Code 是当前最流行的终端级 AI 编程助手,能够直接在命令行中完成代码生成、项目理解、文件修改、命令执行、错误修复等全流程开发工作。它不依赖图形界面、不占用额外资源,却能深度理解项目结构,自动生成规范代码,大幅提升研发效率。
1426 3