ZHJA_HJGL/HJGL_ZH/BLL/libFace.cs

156 lines
6.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
namespace RLSB
{
/// <summary>
/// err = 0 正常非0表示错误
/// </summary>
/// <param name="err"></param>
/// <param name="content"></param>
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void NameCallBack(int err, string content );
public class libFace
{
/// <summary>
/// 传图片文件路径返回识别的标签top决定返回前几个。
/// 一张图片可能识别出来与多个标签都匹配而top是决定返回匹配最高的几个标签
/// </summary>
/// <param name="filename"></param>
/// <param name="top"></param>
/// <returns></returns>
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.Cdecl)]
public extern static int GetNameByFile(string filename, int top, [MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
/// <summary>
/// 根据特证码返回识别的标签top决定返回前几个。
/// </summary>
/// <param name="strfeature"></param>
/// <param name="top"></param>
/// <returns></returns>
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.Cdecl)]
public extern static int GetNameByFeature(string strfeature, int top, [MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
/// <summary>
/// 将图片训练成人脸库与label对应
/// </summary>
/// <param name="filename">图片路径</param>
/// <param name="label">标签名</param>
/// <returns></returns>
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.StdCall)]
public extern static bool AddFileLabel(string filename, string label);
//public extern static bool AddFileLabel(IntPtr filename, IntPtr label);
/// <summary>
/// 返回所标签,通过“;”分割ZZZ;CCC;DDD
/// </summary>
/// <returns></returns>
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int GetAllLabel([MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
/// <summary>
/// 返回所标签与对应的图片文件名,通过“;”分割标签与文件名用“”割开ZZZ,333.jpg;CCC,444.jpg
/// 文件在FaceRecognition.dll所在路径下的modelimages目录下面
/// </summary>
/// <returns></returns>
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static int GetAllLabelAndImage([MarshalAs(UnmanagedType.FunctionPtr)] NameCallBack cb);
/// <summary>
/// 删除图片库中的素材,按标签名(会删除所有符合标签的素才)
/// </summary>
/// <param name="label"></param>
/// <returns></returns>
[DllImport("FaceRecognition.dll", CallingConvention = CallingConvention.StdCall)]
public extern static bool DelLibByLabel( string label );
/// <summary>
/// 删除图片库中的素材按图片文件名文件名是库自动生成的可以通过GetAllLabelAndImage返回
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
[DllImport("FaceRecognition.dll",CallingConvention=CallingConvention.StdCall)]
public extern static bool DelLibByFileName( string filename );
public static bool TrainFileLabel(string filen, string label)
{
return AddFileLabel(filen, label);
//IntPtr ptrPicPath = mallocIntptr(filen);
//IntPtr ptrLabel = mallocIntptr(label);
//// ret = TrainImage(ptrPicPath, ptrLabel); ;// Marshal.GetFunctionPointerForDelegate(ccb));
//bool ret=AddFileLabel(ptrPicPath, ptrLabel);
//Marshal.FreeHGlobal(ptrPicPath);
//Marshal.FreeHGlobal(ptrLabel);
//return ret;
}
//public delegate int CheckImageDG(IntPtr imagePath, CheckCallBack ccb);
//public static bool DeleteLabel(string label)
//{
// IntPtr ptrLabel = mallocIntptr(label);
// int n = DeleteLabelByName(ptrLabel);
// Marshal.FreeHGlobal(ptrLabel);
// return 1 == n;
//}
//public static int DoCheckImage(string picpath, CheckCallBack ccb)
//{
// IntPtr ptrPicPath = mallocIntptr(picpath);
// int r = CheckImage(ptrPicPath,ccb);;// Marshal.GetFunctionPointerForDelegate(ccb));
// Marshal.FreeHGlobal(ptrPicPath);
// return r;
//}
//public static int DoTrainImage(string picpath, string label)
//{
// int ret = 0;
// IntPtr ptrPicPath = mallocIntptr(picpath);
// IntPtr ptrLabel = mallocIntptr(label);
// ret = TrainImage(ptrPicPath, ptrLabel); ;// Marshal.GetFunctionPointerForDelegate(ccb));
// Marshal.FreeHGlobal(ptrPicPath);
// Marshal.FreeHGlobal(ptrLabel);
// return ret;
//}
/// <summary>
/// 根据数据的长度申请非托管空间
/// </summary>
/// <param name="strData">要申请非托管空间的数据</param>
/// <returns>指向非拖管空间的指针</returns>
private static IntPtr mallocIntptr(string strData)
{
//先将字符串转化成字节方式
Byte[] btData = System.Text.Encoding.Default.GetBytes(strData);
//申请非拖管空间
IntPtr m_ptr = Marshal.AllocHGlobal(btData.Length);
//给非拖管空间清0
Byte[] btZero = new Byte[btData.Length + 1]; //一定要加1,否则后面是乱码,原因未找到
Marshal.Copy(btZero, 0, m_ptr, btZero.Length);
//给指针指向的空间赋值
Marshal.Copy(btData, 0, m_ptr, btData.Length);
return m_ptr;
}
/// <summary>
/// 根据长度申请非托管空间
/// </summary>
/// <param name="strData">要申请非托管空间的大小</param>
/// <returns>指向非拖管空间的指针</returns>
private static IntPtr mallocIntptr(int length)
{
//申请非拖管空间
IntPtr m_ptr = Marshal.AllocHGlobal(length);
//给非拖管空间清0
Byte[] btZero = new Byte[length + 1]; //一定要加1,否则后面是乱码,原因未找到
Marshal.Copy(btZero, 0, m_ptr, btZero.Length);
//给指针指向的空间赋值
Marshal.Copy(btZero, 0, m_ptr, length);
return m_ptr;
}
}
}