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("添加数据失败!"); } } } }
private void AddItem_Click(object sender, EventArgs e) { if (!string.IsNullOrEmpty(idText.Text.Trim())) { string id = idText.Text.Trim(); if (facedata.Count != 0) { foreach (FaceData fd in facedata) { if (fd.name == id) { MessageBox.Show("加入人脸数据失败: 重复的ID"); return; } } } if (addIDThread == null || addIDThread.ThreadState == ThreadState.Stopped) { if (userBox.SelectedIndex == 0) { if (videoSourcePlayer.IsRunning) { FeatureData feature = new FeatureData(id); addIDThread = new Thread(new ParameterizedThreadStart(GetFeatureVec)); addIDThread.Start(feature); idText.Text = ""; } else { MessageBox.Show("摄像头未运行!"); } } else if (userBox.SelectedIndex == 1) { string fileName = FileTools.GetImagePath(); if (fileName != null) { FeatureData feature = new FeatureData(id, fileName); addIDThread = new Thread(new ParameterizedThreadStart(GetFeatureVec)); addIDThread.Start(feature); idText.Text = ""; } } } else { MessageBox.Show("正在添加数据中, 请等待!"); } } else { MessageBox.Show("error: ID 为空!"); } }
private void SetIDBox(FeatureData feature, string data, float[] fea, Image img) { //InvokeRequired属性需要比较调用线程和创建线程的线程ID,如果俩线程ID不同,则返回true if (treeView1.InvokeRequired)//调用和创建该控件的线程是不同的线程,必须调用Invoke方法 { if (!closing) { //创建该方法的委托实例 SetIDback d = new SetIDback(SetIDBox); //调用该委托实例,并传递参数,参数为object类型,使用this调用Invoke(this为当前窗体,是创建该窗体控件的线程) Invoke(d, new object[] { feature, data, fea, img });//this指定创建该控件的线程来Invoke(调用) } } else//调用与创建该控件的线程是同一个线程 { if (closing) { return; } if (!string.IsNullOrEmpty(data)) { foreach (TreeNode node in treeView1.Nodes) { if (node.GetNodeCount(true) != 0) { if (feature.itemID == node.Text) { TreeNode treeNode = node; treeView1.SelectedNode = treeNode; treeView1.SelectedNode.Nodes.Add(data); facedata[node.Index].feavec.Add(fea); SetFaceBox(img, imageDir + "/" + facedata[node.Index].name + "/" + facedata[node.Index].feavec.Count); return; } } } TreeNode[] Data = new TreeNode[1]; Data[0] = new TreeNode(data); TreeNode ID = new TreeNode(feature.itemID, Data); treeView1.Nodes.Add(ID); treeView1.SelectedNode = ID; FaceData face = new FaceData(fea, feature.itemID); facedata.Add(face); if (!Directory.Exists(imageDir + "/" + feature.itemID)) { Directory.CreateDirectory(imageDir + "/" + feature.itemID); } SetFaceBox(img, imageDir + "/" + feature.itemID + "/" + facedata[facedata.Count - 1].feavec.Count); } } }
private void InsertItem_Click(object sender, EventArgs e) { try { if (treeView1.SelectedNode != null) { if (treeView1.SelectedNode.GetNodeCount(true) != 0) { if (addIDThread == null || addIDThread.ThreadState == ThreadState.Stopped) { if (userBox.SelectedIndex == 0) { if (videoSourcePlayer.IsRunning) { FeatureData feature = new FeatureData(treeView1.SelectedNode.Text); addIDThread = new Thread(new ParameterizedThreadStart(GetFeatureVec)); addIDThread.Start(feature); idText.Text = ""; } else { MessageBox.Show("摄像头未运行!"); } } else if (userBox.SelectedIndex == 1) { string fileName = FileTools.GetImagePath(); if (fileName != null) { FeatureData feature = new FeatureData(treeView1.SelectedNode.Text, fileName); addIDThread = new Thread(new ParameterizedThreadStart(GetFeatureVec)); addIDThread.Start(feature); idText.Text = ""; } } } else { MessageBox.Show("正在添加数据中, 请等待!"); } } else { MessageBox.Show("error: 请选择父节点!"); } } } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void AddItemAll(object obj) { addIDAllFlag = true; List <string> files = (List <string>)obj; string itemid = ""; foreach (string file in files) { if (!addIDAllFlag) { break; } string[] end = file.Split('.'); if (end[end.Length - 1] == "jpg" || end[end.Length - 1] == "bmp" || end[end.Length - 1] == "png") { string[] substr = file.Split('\\'); FeatureData feature = new FeatureData(itemid, file); itemid = substr[substr.Length - 2]; feature.itemID = itemid; GetFeatureVec(feature); } } }