Ejemplo n.º 1
0
        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("添加数据失败!");
                    }
                }
            }
        }
Ejemplo n.º 2
0
        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();
        }