You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
296 lines
12 KiB
296 lines
12 KiB
using Emgu.CV;
|
|
using Emgu.CV.Structure;
|
|
using Emgu.CV.Util;
|
|
using IOTContainer.Common;
|
|
using IOTContainer.Model;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Shapes;
|
|
using static IOTContainer.Common.FaceMethod;
|
|
|
|
namespace IOTContainer.View
|
|
{
|
|
/// <summary>
|
|
/// FaceWindow.xaml 的交互逻辑
|
|
/// </summary>
|
|
public partial class FaceWindow : Window
|
|
{
|
|
private Capture m_capture = null; // 摄像头操作对象
|
|
//private Capture m_capture1 = null; // 摄像头操作对象
|
|
private Thread m_threadFaceDet = null; // 检测线程
|
|
private Object m_lock = null; // 锁
|
|
//private Mat m_matFrame = null; // 当前帧
|
|
private Mat m_matFrame = new Mat(); // 当前帧
|
|
//private Mat m_matFrame1 = null; // 当前帧
|
|
private bool m_bStartThread = false;
|
|
private GCHandle m_gc;
|
|
[DllImport("gdi32")]
|
|
private static extern int DeleteObject(IntPtr o);
|
|
public static BitmapSource ToBitmapSource(Mat mat)
|
|
{
|
|
using (System.Drawing.Bitmap source = mat.ToImage<Bgr, byte>().Bitmap)
|
|
{
|
|
IntPtr ptr = source.GetHbitmap();
|
|
|
|
BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
|
|
ptr,
|
|
IntPtr.Zero,
|
|
Int32Rect.Empty,
|
|
System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
|
|
|
|
DeleteObject(ptr);// 释放内存,否则内存泄露
|
|
|
|
return bs;
|
|
}
|
|
}
|
|
private bool IsInit = true;
|
|
bool isSuccess = true;
|
|
int i = 0;
|
|
public FaceWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
try
|
|
{
|
|
int init = initGetFace(640, 480, 10);
|
|
if (init != 1)
|
|
{
|
|
IsInit = false;
|
|
}
|
|
}
|
|
catch (SEHException seh)
|
|
{
|
|
IsInit = false;
|
|
Log.MyLog.WriteLogFile("FaceWindow:"+ seh.ToString(), "FaceWindow");
|
|
}
|
|
|
|
}
|
|
|
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
{
|
|
m_lock = new object();
|
|
OnBtnOpenUsb();
|
|
}
|
|
// 开启USB摄像头
|
|
private void OnBtnOpenUsb()
|
|
{
|
|
if (m_bStartThread)
|
|
{
|
|
Log.MyLog.WriteLogFile("OnBtnOpenUsb:摄像头已打开", "FaceWindow");
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
// 调用emgu打开摄像头,需先将emgu文件夹中的"cvextern.dll"拷贝到编译好的EXE程序目录"build_32/bin"中
|
|
m_capture = new Capture(FaceParameters.Parameters._cameraIndex);
|
|
|
|
m_capture.ImageGrabbed += ProcessFrame;//图像捕捉事件
|
|
m_capture.Start();
|
|
}
|
|
catch (Exception exec)
|
|
{
|
|
Log.MyLog.WriteLogFile(exec.Message + "\r\n请将当前目录emgu下的cvextern.dll拷贝到执行程序目录", "FaceWindow");
|
|
}
|
|
}
|
|
private void ProcessFrame(object sender, EventArgs arg)
|
|
{
|
|
Mat mat = null;
|
|
lock (m_lock)
|
|
{
|
|
m_capture.Retrieve(m_matFrame, 0);
|
|
if (null != m_matFrame)
|
|
{
|
|
mat = m_matFrame.Clone();
|
|
}
|
|
this.Dispatcher.BeginInvoke(new Action(() =>
|
|
{
|
|
if (mat != null && !mat.IsEmpty)
|
|
{
|
|
var imageSource = ToBitmapSource(mat);
|
|
if (imageSource != null)
|
|
{
|
|
video.Source = imageSource; // 显示到界面上
|
|
}
|
|
}
|
|
}));
|
|
if (FaceParameters.Parameters._isOpenLive && m_matFrame != null)
|
|
{
|
|
try
|
|
{
|
|
using (Mat newFrame = new Mat(m_matFrame, new System.Drawing.Rectangle(new System.Drawing.Point(250, 140), new System.Drawing.Size(260, 260))))
|
|
{
|
|
try
|
|
{
|
|
VectorOfByte buff = new VectorOfByte();
|
|
CvInvoke.Imencode(".jpg", newFrame, buff);
|
|
byte[] encodeBuff = new byte[buff.Size];
|
|
Marshal.Copy(buff.StartAddress, encodeBuff, 0, buff.Size);
|
|
string base64Str = Convert.ToBase64String(encodeBuff);
|
|
Mat rmat = new Mat();
|
|
CvInvoke.Resize(newFrame,rmat,new System.Drawing.Size(640, 480));
|
|
Bitmap bm = rmat.Clone().Bitmap;
|
|
Image<Bgr, byte> image = new Image<Bgr, byte>(bm);
|
|
bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
|
|
Mat newFrame2 = image.Mat;
|
|
VectorOfByte buff2 = new VectorOfByte();
|
|
CvInvoke.Imencode(".jpg", newFrame2, buff2);
|
|
var faceData = new FaceSendModel
|
|
{
|
|
age = 28,
|
|
genderMale = "男",
|
|
vipId = "",
|
|
faceID = "",
|
|
carNo = "",
|
|
isOK = "0",
|
|
faceImage = base64Str
|
|
};
|
|
try
|
|
{
|
|
if(IsInit)
|
|
{
|
|
var resultinfo = getFaceInfo(getFace(rmat.Ptr), rmat);
|
|
|
|
if (resultinfo.faceSize > 0)
|
|
{
|
|
foreach (var facepos in resultinfo.Faceobj)
|
|
{
|
|
var quality = new faceProperty();
|
|
quality = getProperty(rmat.Ptr, facepos);
|
|
if (quality.Sex == -1 || quality.Old == -1)
|
|
{
|
|
continue;
|
|
}
|
|
faceData.age = quality.Old;
|
|
faceData.genderMale = quality.Sex == 0 ? "男" : "女";
|
|
faceData.isOK = "1";
|
|
Log.MyLog.WriteLogFile($"发送视频流:{JsonConvert.SerializeObject(faceData)}", "EdgeFunction");
|
|
ComParameters.Parameters.EdgeWindow.pushFaceBase(JsonConvert.SerializeObject(faceData));
|
|
}
|
|
}
|
|
//推送视频流
|
|
//Log.MyLog.WriteLogFile($"发送视频流:{JsonConvert.SerializeObject(faceData)}", "EdgeFunction");
|
|
}
|
|
else
|
|
{
|
|
|
|
FaceParameters.Parameters.count ++;
|
|
if (FaceParameters.Parameters.count == 100)
|
|
{
|
|
try
|
|
{
|
|
faceData.age = 28;
|
|
faceData.genderMale = "男";
|
|
faceData.isOK = "1";
|
|
Log.MyLog.WriteLogFile($"发送视频流:{JsonConvert.SerializeObject(faceData)}", "EdgeFunction");
|
|
ComParameters.Parameters.EdgeWindow.pushFaceBase(JsonConvert.SerializeObject(faceData));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.Message, "FaceWindow");
|
|
}
|
|
}
|
|
}
|
|
ComParameters.Parameters.EdgeWindow.pushFaceBase(JsonConvert.SerializeObject(faceData));
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.Message, "FaceWindow");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.Message, "FaceWindow");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.MyLog.WriteLogFile(ex.Message, "FaceWindow");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
public void ClosedWin()
|
|
{
|
|
m_bStartThread = false;
|
|
try
|
|
{
|
|
if (null != m_capture)
|
|
{
|
|
m_capture.Stop();
|
|
m_capture.Dispose();
|
|
}
|
|
}
|
|
catch (Exception exec)
|
|
{
|
|
|
|
}
|
|
|
|
if (m_gc.IsAllocated)
|
|
{
|
|
m_gc.Free();
|
|
}
|
|
}
|
|
|
|
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
{
|
|
m_bStartThread = false;
|
|
try
|
|
{
|
|
if (null != m_capture)
|
|
{
|
|
m_capture.Stop();
|
|
m_capture.Dispose();
|
|
}
|
|
}
|
|
catch (Exception exec)
|
|
{
|
|
|
|
}
|
|
|
|
if (m_gc.IsAllocated)
|
|
{
|
|
m_gc.Free();
|
|
}
|
|
}
|
|
|
|
//private void Window_Closed(object sender, EventArgs e)
|
|
//{
|
|
// m_bStartThread = false;
|
|
// try
|
|
// {
|
|
// if (null != m_capture)
|
|
// {
|
|
// m_capture.Stop();
|
|
// m_capture.Dispose();
|
|
// }
|
|
// }
|
|
// catch (Exception exec)
|
|
// {
|
|
|
|
// }
|
|
|
|
// if (m_gc.IsAllocated)
|
|
// {
|
|
// m_gc.Free();
|
|
// }
|
|
//}
|
|
}
|
|
}
|
|
|