摘要:通过人脸识别与管理系统界面的开发(WinForm界面增强,OpenCV-Python智能识别),我将以一个用户登录窗体的两种不同登录验证方式向各位同学介绍如何利用WinForm开发出漂亮的应用程序窗体,为我们后面的各个章节中的窗体开发打下基础。另外也会向各位同学介绍WinForrm窗体如何调用Python程序让OpenCV-Python程序嵌入到我们的登录窗体中,从而实现人脸识别的。博文提供了完整的Python代码和使用教程,适合新入门的朋友参考,完整代码资源文件请转至文末的下载链接。本博文目录如下:
本章节主要介绍的内容如下:
一,如何利用WinForm开发出非常漂亮的窗体应用程序
二,WinForm如何调用OpenCV-Python实现人脸扫描
三,如何让Python程序嵌入WinForm窗体并传参
四,源码分享,教学ppt分享
========================================================================
系列教程开发环境如下:
1,用户界面开发采用VS Professional 2013 (12.0.21005.1)
2,用户界面美化插件采用CSkin(16.1.143)
3,Python开发IDE采用 Pycharm(3.10)
4,人脸识别组件采用 opencv-python==4.5.4.60
5,人脸录入组件采用opencv-contrib-python==4.5.4.60
=========================================================================
一,如何利用WinForm开发出非常漂亮的窗体应用程序
1,打开VS创建一个 Windows窗体应用程序
2,应用第三方库CSkin
a,需要从以下官网获取动态库 CSkin - 官方网站 C#/.Net Winform界面库
b,项目中添加引用此dll库
c,FrmLogin窗体添加引用并修改窗体父类
d,修改此窗体属性,完成界面设计
窗体属性设置代码如下:
//
// FrmLogin
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(84)))), ((int)(((byte)(77)))), ((int)(((byte)(167)))));
this.BackgroundImage = global::_01FaceDetection.Properties.Resources.back;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.CancelButton = this.btn_Close;
this.CanResize = false;
this.CaptionFont = new System.Drawing.Font("Arial", 12F);
this.CaptionHeight = 32;
this.ClientSize = new System.Drawing.Size(961, 577);
this.ControlBox = false;
this.Controls.Add(this.spback01);
this.Controls.Add(this.skinPanel1);
this.Controls.Add(this.skinLabel2);
this.Controls.Add(this.skinLabel1);
this.EffectBack = System.Drawing.Color.Transparent;
this.EffectCaption = CCWin.TitleType.Title;
this.ForeColor = System.Drawing.Color.Black;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "FrmLogin";
this.Radius = 10;
this.ShadowWidth = 10;
this.ShowBorder = false;
this.ShowDrawIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "";
this.TitleCenter = true;
this.TitleColor = System.Drawing.Color.FromArgb(((int)(((byte)(130)))), ((int)(((byte)(179)))), ((int)(((byte)(255)))));
this.TitleOffset = new System.Drawing.Point(20, 0);
this.Load += new System.EventHandler(this.FrmLogin_Load);
this.SizeChanged += new System.EventHandler(this.FrmLogin_SizeChanged);
this.skinPanel1.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.skinPictureBox1)).EndInit();
this.spback01.ResumeLayout(false);
this.spback02.ResumeLayout(false);
this.skinTabControl1.ResumeLayout(false);
this.skinTabPage1.ResumeLayout(false);
this.skinPanel2.ResumeLayout(false);
this.skinPanel2.PerformLayout();
this.skinPanel3.ResumeLayout(false);
this.sp_txtboxusername.ResumeLayout(false);
this.skinTabPage2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
二,WinForm如何调用OpenCV-Python实现人脸扫描
1,使用ProcessStartInfo调用本机Python.exe 执行OpenCV-Python实现人脸扫描
/// <summary>
/// py程序调用
/// </summary>
/// <param name="pathAlg">py文件路径</param>
/// <param name="pytype">python方法名称</param>
/// <param name="strjsons">参数信息</param>
/// <returns></returns>
public bool StartTest(string pathAlg, string pytype, string strjsons)
{
bool state = true;
if (!File.Exists(pathAlg))
{
throw new Exception("The file was not found.");
return false;
}
string sArguments = pathAlg;
//解析参数字符串获取参数;
if (strjsons != "" || strjson != null)
{
string[] argresult = strjsons.Split('|');
for (int i = 0; i < argresult.Length; i++)
{
sArguments += " " + argresult[i];//Python文件的路径用“/”划分比较常见
}
sArguments += " -u";//Python文件的路径用“/”划分比较常见
}
else
{
sArguments += "";
}
switch (pytype)
{
case "getlocalImg":
break;
case "getlocalcarme": //读取本地摄像头
break;
}
//sArguments += " " + fileimgpath + " " + userid + " " + username + " -u";//Python文件的路径用“/”划分比较常见
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = @"python.exe";//环境路径需要配置好
start.Arguments = sArguments;
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.RedirectStandardError = true;
start.CreateNoWindow = true;
using (progressTest = Process.Start(start))
{
// 异步获取命令行内容
progressTest.BeginOutputReadLine();
// 为异步获取订阅事件
progressTest.OutputDataReceived += new DataReceivedEventHandler(outputDataReceived);
}
return state;
}
public void outputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
this.Invoke(new Action(() =>
{
//this.txtadd_result.Text = e.Data;
}));
}
}
三,如何让Python程序嵌入WinForm窗体并传参
1,先通过FindWindow查找Python窗体句柄,再通过SetParent将Python窗体嵌入到WinForm窗体中的Panle中。
//先启动第三方应用
//开线程来查找窗体,不然UI线程会卡死
Task.Run(() =>
{
//CheckFaceWindow是要查找的窗体名称,自行替换
if (SetWindow.FindWindow("CheckFaceWindow"))
{
this.Invoke(new Action(() =>
{
SetWindow.SetParent(sp_Face.Handle, "CheckFaceWindow"); //设置父容器
}));
}
else
{
MessageBoxEx.Show("未能查找到人脸窗体!", "人脸检测", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
});
四,源码分享,教学ppt分享
人脸识别与管理系统界面开发源码(WinForm界面增强,OpenCV-Python智能识别)(第一章)-Python文档类资源-CSDN下载
解压密码:luojiageo@1111
五,写在最后
你是不是总羡慕别人的幸福,却常常忽略了自己生活中的美好。其实,幸福的人并非拥有了世界上最好的东西,而是珍惜了生命中的点点滴滴。学会用感恩的心面对生活,你就可以体味到更多的幸福。