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.
203 lines
5.9 KiB
203 lines
5.9 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Drawing.Imaging;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Emgu.CV;
|
|
using Emgu.CV.Structure;
|
|
|
|
namespace DemoUI
|
|
{
|
|
public class CommonMethod
|
|
{
|
|
|
|
// 前端
|
|
[DllImport("FaceApi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
|
|
//public extern static int initGetFace(int imgWidht, int imgHeight, float resolutionF, double clarityF, double poseExF, double brigthF, double integF, double poseF);
|
|
public extern static int initGetFace(int imgWidht, int imgHeight, int maxFace);
|
|
[DllImport("FaceApi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
|
|
public extern static IntPtr getFace(IntPtr src);
|
|
|
|
[DllImport("FaceApi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
|
|
private extern static IntPtr getProperty(IntPtr src, int x, int y, int widht, int height);
|
|
|
|
[DllImport("FaceApi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
|
|
public extern static void delPtr(IntPtr src);
|
|
|
|
[StructLayoutAttribute(LayoutKind.Sequential)]
|
|
|
|
//前端结构体
|
|
public struct faceInfo
|
|
{
|
|
public int faceSize;
|
|
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
|
|
faceObj[] faceobj;
|
|
public int FaceSize { get => faceSize; set => faceSize = value; }
|
|
public faceObj[] Faceobj { get => faceobj; set => faceobj = value; }
|
|
}
|
|
public static bool pulse = false;
|
|
|
|
//前端结构体
|
|
public struct faceObj
|
|
{
|
|
int pid;
|
|
int x;
|
|
int y;
|
|
int width;
|
|
int height;
|
|
float resolution_score;
|
|
float clarity_score;
|
|
float poseEx_score;
|
|
float brigth_score;
|
|
float integ_score;
|
|
float pose_score;
|
|
public int Pid { get => pid; set => pid = value; }
|
|
public int X { get => x; set => x = value; }
|
|
public int Y { get => y; set => y = value; }
|
|
public int Width { get => width; set => width = value; }
|
|
public int Height { get => height; set => height = value; }
|
|
public float Resolution_score { get => resolution_score; set => resolution_score = value; }
|
|
public float Clarity_score { get => clarity_score; set => clarity_score = value; }
|
|
public float PoseEx_score { get => poseEx_score; set => poseEx_score = value; }
|
|
public float Brigth_score { get => brigth_score; set => brigth_score = value; }
|
|
public float Integ_score { get => integ_score; set => integ_score = value; }
|
|
public float Pose_score { get => pose_score; set => pose_score = value; }
|
|
}
|
|
|
|
public struct faceProperty
|
|
{
|
|
int sex;
|
|
int old;
|
|
public int Sex { get => sex; set => sex = value; }
|
|
public int Old { get => old; set => old = value; }
|
|
}
|
|
public static faceProperty getProperty(IntPtr facePtr, faceObj obj)
|
|
{
|
|
IntPtr faceProperty = getProperty(facePtr, obj.X, obj.Y, obj.Width, obj.Height);
|
|
return (faceProperty)Marshal.PtrToStructure(faceProperty, typeof(faceProperty));
|
|
}
|
|
public struct faceQuality
|
|
{
|
|
|
|
int sex;
|
|
|
|
int old;
|
|
|
|
int left_eye_status;
|
|
|
|
int right_eye_status;
|
|
|
|
public int Sex { get => sex; set => sex = value; }
|
|
public int Old { get => old; set => old = value; }
|
|
public int Left_eye_status { get => left_eye_status; set => left_eye_status = value; }
|
|
public int Right_eye_status { get => right_eye_status; set => right_eye_status = value; }
|
|
}
|
|
public static faceQuality getQuality(IntPtr ptr)
|
|
{
|
|
var facequality = (faceQuality)Marshal.PtrToStructure(ptr, typeof(faceQuality));
|
|
return facequality;
|
|
}
|
|
public static faceInfo getFaceInfo(IntPtr ptr, Mat mat)
|
|
{
|
|
var faceinfo = (faceInfo)Marshal.PtrToStructure(ptr, typeof(faceInfo));
|
|
if (faceinfo.faceSize == 0)
|
|
{
|
|
delPtr(ptr);
|
|
}
|
|
return faceinfo;
|
|
}
|
|
|
|
|
|
//阈值结构体(可重写)
|
|
public struct thresholdFp
|
|
{
|
|
float resolutionF;
|
|
double clarityF;
|
|
double poseExF;
|
|
double brigthF;
|
|
double integF;
|
|
double poseF;
|
|
|
|
public float ResolutionF { get => resolutionF; set => resolutionF = value; }
|
|
public double ClarityF { get => clarityF; set => clarityF = value; }
|
|
public double PoseExF { get => poseExF; set => poseExF = value; }
|
|
public double BrigthF { get => brigthF; set => brigthF = value; }
|
|
public double IntegF { get => integF; set => integF = value; }
|
|
public double PoseF { get => poseF; set => poseF = value; }
|
|
}
|
|
//图片转为base64编码的字符串
|
|
public static string ImgToBase64String(Bitmap bmp)
|
|
{
|
|
try
|
|
{
|
|
MemoryStream ms = new MemoryStream();
|
|
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
byte[] arr = new byte[ms.Length];
|
|
ms.Position = 0;
|
|
ms.Read(arr, 0, (int)ms.Length);
|
|
ms.Close();
|
|
return Convert.ToBase64String(arr);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
public static Bitmap Base64StringToImage(string strbase64)
|
|
{
|
|
try
|
|
{
|
|
byte[] arr = Convert.FromBase64String(strbase64);
|
|
MemoryStream ms = new MemoryStream(arr);
|
|
Bitmap bmp = new Bitmap(ms);
|
|
ms.Close();
|
|
return bmp;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
public static IntPtr BitPtr(Bitmap image)
|
|
{
|
|
|
|
int width = image.Width;
|
|
|
|
int height = image.Height;
|
|
|
|
int length = height * 3 * width;
|
|
|
|
var RGB = new byte[length];
|
|
|
|
BitmapData data = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
|
|
|
|
System.IntPtr Scan0 = data.Scan0;
|
|
|
|
System.Runtime.InteropServices.Marshal.Copy(Scan0, RGB, 0, length);
|
|
|
|
double gray = 0;
|
|
|
|
for (int i = 0; i < RGB.Length; i = i + 3)
|
|
{
|
|
|
|
gray = RGB[i + 2] * 0.3 + RGB[i + 1] * 0.59 + RGB[i] * 0.11;
|
|
|
|
RGB[i + 2] = RGB[i + 1] = RGB[i] = (byte)gray;
|
|
}
|
|
System.Runtime.InteropServices.Marshal.Copy(RGB, 0, Scan0, length);
|
|
image.UnlockBits(data);
|
|
return Scan0;
|
|
}
|
|
public Mat Bitmap2Mat(Bitmap bitmap)
|
|
{
|
|
Emgu.CV.Image<Bgr, Byte> currentFrame = new Emgu.CV.Image<Bgr, Byte>(bitmap); //只能这么转
|
|
Mat invert = new Mat();
|
|
CvInvoke.BitwiseAnd(currentFrame, currentFrame, invert);
|
|
return invert;
|
|
}
|
|
}
|
|
}
|
|
|