public void GetFeatureVec(object obj) { FeatureData feature = (FeatureData)obj; using (Bitmap bitmapSource = feature.localRead ? new Bitmap(feature.localFileName) : videoSourcePlayer.GetCurrentVideoFrame()) { if (bitmapSource != null) { //Bitmap bitmapSource = BitmapHelper.ScaleToSize(bitmapVideo, 256, 256); float[] vec = new float[FaceApi.FeatureVecLen()]; int[] landmark = new int[FaceApi.LandMarkLen()]; BitmapData bitmapData = bitmapSource.LockBits(new Rectangle(0, 0, bitmapSource.Width, bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); while (isDetectrunning) { Console.WriteLine("waitting Detect"); } ; isDetectrunning = true; bool success = FaceApi.FindFacePtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, out int x1, out int y1, out int width, out int height, landmark); isDetectrunning = false; bitmapSource.UnlockBits(bitmapData); if (success) { bitmapData = bitmapSource.LockBits(new Rectangle(x1, y1, width, height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); for (int idx = 0; idx < landmark.Length / 2; ++idx) { landmark[idx * 2] -= x1; landmark[idx * 2 + 1] -= y1; } while (isFeaRunning) { Console.WriteLine("waitting FeaRunning"); } ; isFeaRunning = true; success = FaceApi.FeatureVecPtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, landmark, vec); isFeaRunning = false; bitmapSource.UnlockBits(bitmapData); } if (success) { float sum = 0; for (int idx = 0; idx < vec.Length; ++idx) { sum += vec[idx]; } SetIDBox(feature, sum + "", vec, bitmapSource.Clone(new Rectangle(x1, y1, width, height), PixelFormat.Format24bppRgb)); } else { MessageBox.Show("添加数据失败!"); } } } }
public void Matching(object obj) { MatchData matchData = (MatchData)obj; if (matchData.bitmapSource != null) { float[] vec = new float[FaceApi.FeatureVecLen()]; BitmapData bitmapData = matchData.bitmapSource.LockBits(new Rectangle(0, 0, matchData.bitmapSource.Width, matchData.bitmapSource.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); while (isFeaRunning) { Console.WriteLine("Matching is Waitting"); } isFeaRunning = true; bool success = FaceApi.FeatureVecPtr(bitmapData.Scan0, bitmapData.Width, bitmapData.Height, bitmapData.Stride, 3, matchData.landmark, vec); isFeaRunning = false; matchData.bitmapSource.UnlockBits(bitmapData); double maxdata = 0; int maxidx1 = 0; int maxidx2 = 0; int index = 0; foreach (FaceData face in facedata) { for (int idx = 0; idx < face.feavec.Count; ++idx) { double result = FaceApi.Distance(vec, face.feavec[idx]); if (result > maxdata) { maxdata = result; maxidx2 = idx; maxidx1 = index; } Console.WriteLine(result + ""); } index += 1; } if (maxdata > threshold) { if (!closing) { string path = imageDir + "/" + facedata[maxidx1].name + "/" + (maxidx2 + 1) + ".jpg"; SetMatchBox(BitmapHelper.ReadImageFile(path), "匹配ID:" + facedata[maxidx1].name /*maxdata + ""*/); } } else { if (!closing) { SetMatchBox(null, ""); } } } matchData.Dispose(); }
private void loadFaceData() { facedata = new List <FaceData>(); if (!Directory.Exists(fileDir)) { Directory.CreateDirectory(fileDir); } if (!Directory.Exists(imageDir)) { Directory.CreateDirectory(imageDir); } if (File.Exists(filePath)) { using (StreamReader sr = new StreamReader(filePath, Encoding.Default)) { String line; FaceData face = new FaceData(); while ((line = sr.ReadLine()) != null) { String[] data = line.Split(' '); if (data[0] == "ID") { face.name = data[1]; } else if (data[0] == "end") { facedata.Add(face); face = new FaceData(); } else { float[] vec = new float[FaceApi.FeatureVecLen()]; for (int idx = 0; idx < vec.Length; ++idx) { vec[idx] = Convert.ToSingle(data[idx]); } face.feavec.Add(vec); } } } if (facedata.Count != 0) { foreach (FaceData face in facedata) { TreeNode[] nodes = new TreeNode[face.feavec.Count]; int idx = 0; foreach (float[] fea in face.feavec) { float sum = 0; foreach (float single in fea) { sum += single; } nodes[idx] = new TreeNode(sum + ""); idx += 1; } TreeNode treeNode = new TreeNode(face.name, nodes); treeView1.Nodes.Add(treeNode); } } } }