From ee5e3a086a5dc145e7f5df3de32e0e072c183949 Mon Sep 17 00:00:00 2001 From: z8018 <1282578289@qq.com> Date: 星期五, 06 六月 2025 15:19:40 +0800 Subject: [PATCH] 1 --- project/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Face/FaceHelper.cs | 314 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 314 insertions(+), 0 deletions(-) diff --git a/project/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Face/FaceHelper.cs b/project/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Face/FaceHelper.cs new file mode 100644 index 0000000..0654569 --- /dev/null +++ b/project/WCS/WIDESEAWCS_Server/WIDESEAWCS_Common/Face/FaceHelper.cs @@ -0,0 +1,314 @@ +锘縰sing System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace WIDESEAWCS_Common.Face +{ + public class FaceHelper + { + static string picturePath = ""; + static FileInfo[] fileInfos; + static string currentPath = ""; + + /// <summary> + /// 浜鸿劯璇嗗埆鏂规硶 + /// </summary> + /// <param name="sourcePath">宸插綍鍏ヤ汉鑴稿浘鐗囪矾寰�</param> + /// <param name="desPath">瑕佹槸琚殑浜鸿劯鍥剧墖璺緞</param> + /// <returns></returns> + public static (bool, string) FaceRecognition(string sourcePath, string desPath) + { + int ret = TH_Faces.IdFaceSdkInit(); + if (ret < 0) + { + IntPtr pRunCode = Marshal.AllocHGlobal(35 + 1); + TH_Faces.IdFaceSdkGetRunCode(pRunCode); + Marshal.FreeHGlobal(pRunCode); + } + + picturePath = sourcePath; + currentPath = desPath; + List<IntPtr> pFrame_Sources = new List<IntPtr>(); + List<IntPtr> pFeature_Sources = new List<IntPtr>(); + GetSourceFeature(ref pFrame_Sources, ref pFeature_Sources); + + IntPtr pFrame_Desc = IntPtr.Zero; + IntPtr pFeature_Desc = IntPtr.Zero; + GetDesFeature(ref pFrame_Desc, ref pFeature_Desc); + + List<byte> bytes = Check(ref pFrame_Desc, ref pFeature_Desc, ref pFrame_Sources, ref pFeature_Sources); + + if (bytes.Count == 0) + { + return (false, "浜鸿劯璇嗗埆澶辫触"); + } + + byte max = bytes.Max(); // 鑾峰彇鏈�澶у垎鏁� + + int index = bytes.IndexOf(max); // 鑾峰彇鏈�澶у垎鏁扮殑绱㈠紩 + + if (fileInfos.Length > index) + { + if (max < 80) // 濡傛灉鏈�澶у垎鏁板皬浜�80锛岃涓轰汉鑴歌瘑鍒け璐� + { + return (false, "浜鸿劯璇嗗埆澶辫触"); + } + + return (true, fileInfos[index].Name); // 杩斿洖瀵瑰簲鐨勬枃浠跺悕 + } + + return (false, "浜鸿劯璇嗗埆澶辫触"); + } + + public static (bool, string) FaceRecognitionOne(string sourcePath, string desPath) + { + int ret = TH_Faces.IdFaceSdkInit(); + if (ret < 0) + { + IntPtr pRunCode = Marshal.AllocHGlobal(35 + 1); + TH_Faces.IdFaceSdkGetRunCode(pRunCode); + Marshal.FreeHGlobal(pRunCode); + } + + picturePath = sourcePath; + currentPath = desPath; + IntPtr pFrame_Source = IntPtr.Zero; + IntPtr pFeature_Source = IntPtr.Zero; + GetSourceFeature(ref pFrame_Source, ref pFeature_Source); + + IntPtr pFrame_Desc = IntPtr.Zero; + IntPtr pFeature_Desc = IntPtr.Zero; + GetDesFeature(ref pFrame_Desc, ref pFeature_Desc); + + byte result = Check(ref pFrame_Desc, ref pFeature_Desc, ref pFrame_Source, ref pFeature_Source); + + + if (result < 80) // 濡傛灉鏈�澶у垎鏁板皬浜�80锛岃涓轰汉鑴歌瘑鍒け璐� + { + return (false, "浜鸿劯璇嗗埆澶辫触"); + } + + return (true, "浜鸿劯璇嗗埆鎴愬姛"); // 杩斿洖瀵瑰簲鐨勬枃浠跺悕 + } + + private static void GetSourceFeature(ref List<IntPtr> pFrame_Sources, ref List<IntPtr> pFeature_Sources) + { + DirectoryInfo dir = new DirectoryInfo(picturePath); + fileInfos = dir.GetFiles(); + + foreach (var file in fileInfos) + { + // 璇诲師濮嬪浘璞� + int nWidth1 = 0; + int nHeight1 = 0; + IntPtr pSrcFilepath = Marshal.StringToHGlobalAnsi(file.FullName); + // 璇诲浘璞″垎杈ㄧ巼 + int ret = TH_Faces.ReadImageFile(pSrcFilepath, IntPtr.Zero, 0, ref nWidth1, ref nHeight1, 24); + if (ret < 0 || nWidth1 <= 0 || nHeight1 <= 0) + { + continue; + } + // 鏍规嵁鍥捐薄鍒嗚鲸鐜囧垎閰嶅浘璞℃暟鎹紦鍐插尯骞惰鍑哄浘璞℃暟鎹� + IntPtr pFrame_Source = Marshal.AllocHGlobal(nWidth1 * nHeight1 * 3); + TH_Faces.ReadImageFile(pSrcFilepath, pFrame_Source, nWidth1 * nHeight1 * 3, ref nWidth1, ref nHeight1, 24); + if (ret < 0 || nWidth1 <= 0 || nHeight1 <= 0) + { + Marshal.FreeHGlobal(pFrame_Source); + continue; + } + + // 瀵瑰師濮嬪浘璞¤繘琛屼汉鑴告娴� + TH_Faces.FACE_DETECT_RESULT Face1 = new TH_Faces.FACE_DETECT_RESULT(); + int nNum1 = TH_Faces.IdFaceSdkDetectFace(pFrame_Source, nWidth1, nHeight1, ref Face1); + if (nNum1 <= 0) + { + Marshal.FreeHGlobal(pFrame_Source); + continue; + } + + // 瀵瑰師濮嬪浘璞℃彁鍙栦汉鑴哥壒寰侊紙杈撳叆鍘熷鍥捐薄鍙婂叾浜鸿劯妫�娴嬬粨鏋滐級 + int nFeaSize = TH_Faces.IdFaceSdkFeatureSize(); + IntPtr pFeature_Source = Marshal.AllocHGlobal(nFeaSize); + ret = TH_Faces.IdFaceSdkFeatureGet(pFrame_Source, nWidth1, nHeight1, ref Face1, pFeature_Source); + if (ret != 0) + { + Marshal.FreeHGlobal(pFrame_Source); + Marshal.FreeHGlobal(pFeature_Source); + continue; + } + + pFrame_Sources.Add(pFrame_Source); + pFeature_Sources.Add(pFeature_Source); + } + } + + private static void GetSourceFeature(ref IntPtr pFrame_Source, ref IntPtr pFeature_Source) + { + // 璇诲師濮嬪浘璞� + int nWidth1 = 0; + int nHeight1 = 0; + IntPtr pSrcFilepath = Marshal.StringToHGlobalAnsi(picturePath); + // 璇诲浘璞″垎杈ㄧ巼 + int ret = TH_Faces.ReadImageFile(pSrcFilepath, IntPtr.Zero, 0, ref nWidth1, ref nHeight1, 24); + if (ret < 0 || nWidth1 <= 0 || nHeight1 <= 0) + { + return; + } + // 鏍规嵁鍥捐薄鍒嗚鲸鐜囧垎閰嶅浘璞℃暟鎹紦鍐插尯骞惰鍑哄浘璞℃暟鎹� + pFrame_Source = Marshal.AllocHGlobal(nWidth1 * nHeight1 * 3); + TH_Faces.ReadImageFile(pSrcFilepath, pFrame_Source, nWidth1 * nHeight1 * 3, ref nWidth1, ref nHeight1, 24); + if (ret < 0 || nWidth1 <= 0 || nHeight1 <= 0) + { + Marshal.FreeHGlobal(pFrame_Source); + } + + // 瀵瑰師濮嬪浘璞¤繘琛屼汉鑴告娴� + TH_Faces.FACE_DETECT_RESULT Face1 = new TH_Faces.FACE_DETECT_RESULT(); + int nNum1 = TH_Faces.IdFaceSdkDetectFace(pFrame_Source, nWidth1, nHeight1, ref Face1); + if (nNum1 <= 0) + { + Marshal.FreeHGlobal(pFrame_Source); + } + + // 瀵瑰師濮嬪浘璞℃彁鍙栦汉鑴哥壒寰侊紙杈撳叆鍘熷鍥捐薄鍙婂叾浜鸿劯妫�娴嬬粨鏋滐級 + int nFeaSize = TH_Faces.IdFaceSdkFeatureSize(); + pFeature_Source = Marshal.AllocHGlobal(nFeaSize); + ret = TH_Faces.IdFaceSdkFeatureGet(pFrame_Source, nWidth1, nHeight1, ref Face1, pFeature_Source); + if (ret != 0) + { + Marshal.FreeHGlobal(pFrame_Source); + Marshal.FreeHGlobal(pFeature_Source); + } + } + + private static bool GetDesFeature(ref IntPtr pFrame_Desc, ref IntPtr pFeature_Desc) + { + // 璇荤洰鏍囧浘璞� + int nWidth2 = 0; + int nHeight2 = 0; + IntPtr pDstFilepath = Marshal.StringToHGlobalAnsi(currentPath); + // 璇诲浘璞″垎杈ㄧ巼 + int ret = TH_Faces.ReadImageFile(pDstFilepath, IntPtr.Zero, 0, ref nWidth2, ref nHeight2, 24); + if (ret < 0 || nWidth2 <= 0 || nHeight2 <= 0) + { + return false; + } + // 鏍规嵁鍥捐薄鍒嗚鲸鐜囧垎閰嶅浘璞℃暟鎹紦鍐插尯骞惰鍑哄浘璞℃暟鎹� + pFrame_Desc = Marshal.AllocHGlobal(nWidth2 * nHeight2 * 3); + ret = TH_Faces.ReadImageFile(pDstFilepath, pFrame_Desc, nWidth2 * nHeight2 * 3, ref nWidth2, ref nHeight2, 24); + if (ret < 0 || nWidth2 <= 0 || nHeight2 <= 0) + { + Marshal.FreeHGlobal(pFrame_Desc); + return false; + } + + // 瀵圭洰鏍囧浘璞¤繘琛屼汉鑴告娴� + TH_Faces.FACE_DETECT_RESULT Face2 = new TH_Faces.FACE_DETECT_RESULT(); + int nNum2 = TH_Faces.IdFaceSdkDetectFace(pFrame_Desc, nWidth2, nHeight2, ref Face2); + if (nNum2 <= 0) + { + Marshal.FreeHGlobal(pFrame_Desc); + return false; + } + int nFeaSize = TH_Faces.IdFaceSdkFeatureSize(); + pFeature_Desc = Marshal.AllocHGlobal(nFeaSize); + ret = TH_Faces.IdFaceSdkFeatureGet(pFrame_Desc, nWidth2, nHeight2, ref Face2, pFeature_Desc); + if (ret != 0) + { + Marshal.FreeHGlobal(pFrame_Desc); + Marshal.FreeHGlobal(pFeature_Desc); + return false; + } + + return true; + } + + private static List<byte> Check(ref IntPtr pFrame_Desc, ref IntPtr pFeature_Desc, ref List<IntPtr> pFrame_Sources, ref List<IntPtr> pFeature_Sources) + { + IntPtr hList = TH_Faces.IdFaceSdkListCreate(1000); // 鍒涘缓鏀寔1000涓壒寰佺殑姣斿鍒楄〃 + try + { + List<byte> results = new List<byte>(); + int i, n = 0; + + + for (i = 0; i < pFeature_Sources.Count; i++) + { + // 灏嗕袱涓汉鐨勭壒寰佺爜寰幆浜ゅ弶鎻掑叆姣斿鍒楄〃涓� + int nPos = -1; + n = TH_Faces.IdFaceSdkListInsert(hList, ref nPos, pFeature_Sources[i], 1); + } + if (n != pFeature_Sources.Count) + { + + return results; ; + } + else + { + IntPtr pnScores = Marshal.AllocHGlobal(pFeature_Sources.Count); + n = TH_Faces.IdFaceSdkListCompare(hList, pFeature_Desc, 0, pFeature_Sources.Count, pnScores); // 鐜板満浜轰笌鍔犲叆鍒楄〃涓殑 N 涓汉杩涜涓�瀵瑰姣斿 + if (n != pFeature_Sources.Count) + { + + return results; + } + else + { + byte[] scores = new byte[pFeature_Sources.Count]; + Marshal.Copy(pnScores, scores, 0, pFeature_Sources.Count); + results = scores.ToList(); // 灏嗘瘮瀵圭粨鏋滆浆鎹负 List<byte> 绫诲瀷 + + } + Marshal.FreeHGlobal(pnScores); + } + + return results; + } + finally + { + TH_Faces.IdFaceSdkListDestroy(hList); // 閿�姣佹瘮瀵瑰垪琛� + Marshal.FreeHGlobal(pFrame_Desc); + Marshal.FreeHGlobal(pFeature_Desc); + + foreach (var pFrame in pFrame_Sources) + { + + Marshal.FreeHGlobal(pFrame); + + } + + foreach (var pFeature in pFeature_Sources) + { + + Marshal.FreeHGlobal(pFeature); + } + + TH_Faces.IdFaceSdkUninit(); + } + } + + + private static byte Check(ref IntPtr pFrame_Desc, ref IntPtr pFeature_Desc, ref IntPtr pFrame_Source, ref IntPtr pFeature_Source) + { + try + { + byte score = TH_Faces.IdFaceSdkFeatureCompare(pFeature_Source, pFeature_Desc); + + return score; + } + finally + { + Marshal.FreeHGlobal(pFrame_Desc); + Marshal.FreeHGlobal(pFeature_Desc); + Marshal.FreeHGlobal(pFrame_Source); + Marshal.FreeHGlobal(pFeature_Source); + TH_Faces.IdFaceSdkUninit(); + } + } + } +} -- Gitblit v1.9.3