Beispiel #1
0
        unsafe public static List <Mat> FaceKit(string ImageRoot, string outFolder, int x = 30)
        {
            List <Mat> list = new List <Mat>();

            try
            {
                Image <Bgr, byte> image = new Image <Bgr, byte>(ImageRoot);
                ushort            rows  = (ushort)image.Height;
                ushort            cols  = (ushort)image.Width;
                int    widthStep        = image.MIplImage.WidthStep;
                IntPtr rgbData          = image.MIplImage.ImageData;

                ushort   countObject = 1;
                ushort[] x0          = new ushort[x], y0 = new ushort[x],
                x1 = new ushort[x], y1 = new ushort[x],
                x2 = new ushort[x], y2 = new ushort[x],
                x3 = new ushort[x], y3 = new ushort[x];
                // call từ dll


                int countCrop = Native.FaceKit(rows, cols, widthStep, rgbData, &countObject, x0, y0, x1, y1, x2, y2, x3, y3);


                Debug.WriteLine(countCrop);


                Bitmap           ochuBitmap;
                List <Rectangle> areas  = new List <Rectangle>();
                PointF[]         points = new PointF[4];
                // bên dưới lỗi
                //for (int i = 0; i < countObject; i++)
                //{
                //    //Rectangle area = CVUtils.GetRectangle(x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                //    //var xxx = image.Mat;
                //    //var face = CVUtils.Crop(image, area);

                //    //string name = string.Format(outFolder + "\\face_{0}.jpg", i);
                //    //face.Bitmap.Save(name);
                //    var warp = CVUtils.GetRectangle(image.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                //    warp.Bitmap.Save(string.Format(outFolder + "\\warp_{0}.PNG", i));
                //    //  areas.Add(area);

                //}
                for (int i = 0; i < countObject; i++)
                {
                    var warp = CVUtils.GetRectangle(image.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                    list.Add(warp);
                    warp.Bitmap.Save(string.Format(outFolder + "\\warp_{0}.PNG", i));
                }
                CVUtils.DrawRect(image.ToBitmap(), outFolder + "\\Test.jpg", areas.ToArray());
                image.Dispose();
                //  return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                //   return false;
            }
            return(list);
        }
    private void RecognizeFolder()
    {
        foreach (var file in recoFiles)
        {
            var n            = file.Replace(recFolderPath.text, "");
            var n_2          = n.Remove(0, 1);
            var tex          = CVUtils.LoadImgToTexture($"FR/{recPath.text}/" + n, TextureFormat.RGBA32);
            var input_emb    = faceRecMng.ExtractEmbeddings(tex);
            var dict         = faceRecMng.StoredFaces.serializedDict;
            var matchResults = new Dictionary <string, double>();
            foreach (var key in dict.Keys)
            {
                var dist = faceRecMng.CompareSimilarity(input_emb, dict[key]);
                matchResults.Add(key, dist);
            }

            var matched = (from match in matchResults where match.Value < 0.5 orderby match.Value select match).ToList();
            if (matched.Count == 0)
            {
                Debug.Log("no match");
            }
            else
            {
                Debug.Log(matched[0].Key);
            }
        }
    }
    private void RecognizeBtnListener()
    {
        var tex          = CVUtils.LoadImgToTexture(Utils.getFilePath(recPath.text), TextureFormat.RGB24);
        var input_emb    = faceRecMng.ExtractEmbeddings(tex);
        var dict         = faceRecMng.StoredFaces.serializedDict;
        var matchResults = new Dictionary <string, double>();

        foreach (var key in dict.Keys)
        {
            var dist = faceRecMng.CompareEmbeddings(input_emb, dict[key]);
            matchResults.Add(key, dist);
        }

        //var matched = matchResults.Select(m => m).Where(k => k.Value < 0.99).ToList();
        var matched = (from match in matchResults where match.Value < 0.5 orderby match.Value select match).ToList();

        if (matched.Count == 0)
        {
            text.text = "no match";
        }
        else
        {
            text.text = matched[0].Key;
        }
    }
        protected async Task <Texture2D> GetScreenShot()
        {
            camTex = new Texture2D(0, 0);
            float startCapture = Time.realtimeSinceStartup;
            var   frameBreak   = new WaitForEndOfFrame();

            if (externalCamera)
            {
                camTex = await httpLoader.LoadTextureFromImage()
                ;               while (!textureReceived)
                {
                    await frameBreak;
                }
            }
            else
            {
                //camTex = Resources.Load<Texture2D>("Photos/" + $"debug_{idx}");
                camTex = CVUtils.LoadImgToTexture(photoPath, TextureFormat.RGBA32);
            }
            float endCapture = Time.realtimeSinceStartup;

            captureTime     = GenericUtils.CalculateTimeDifference(startCapture, endCapture);
            textureReceived = true;
            Handheld.Vibrate();
            return(camTex);
        }
 public void LoadNet()
 {
     if (net == null)
     {
         net = CVUtils.LoadModel(model, config, model_filepath, config_filepath);
     }
 }
Beispiel #6
0
        public Mat GetRep(string path)
        {
            var tex = CVUtils.LoadImgToTexture(path, TextureFormat.RGBA32);
            var rep = ExtractFaceEmbeddings(tex, GetBBs(tex));
            Mat mat = rep.CreateMatFromEmbeddings(rep.Embeddings);

            return(mat);
        }
 private IEnumerator AddFolderToDict()
 {
     foreach (var file in imageFiles)
     {
         var n   = file.Replace(addFolderPath.text, "");
         var n_2 = n.Remove(0, 1);
         var tex = CVUtils.LoadImgToTexture("FR/LFW_Train/" + n, TextureFormat.RGBA32);
         faceRecMng.AddFace(n_2, tex);
         yield return(null);
     }
 }
Beispiel #8
0
        public static List <Mat> Recognize_FaceKit(Image <Bgr, byte> imageRGB, int maxFace = 30, bool warpTarget = true)
        {
            var list = new List <Mat>();

            ushort rows      = (ushort)imageRGB.Height;
            ushort cols      = (ushort)imageRGB.Width;
            int    widthStep = imageRGB.MIplImage.WidthStep;
            IntPtr rgbData   = imageRGB.MIplImage.ImageData;

            ushort[] x0 = new ushort[maxFace];
            ushort[] y0 = new ushort[maxFace];
            ushort[] x1 = new ushort[maxFace];
            ushort[] y1 = new ushort[maxFace];
            ushort[] x2 = new ushort[maxFace];
            ushort[] y2 = new ushort[maxFace];
            ushort[] x3 = new ushort[maxFace];
            ushort[] y3 = new ushort[maxFace];

            int countChar = Native.FaceKit(rows, cols, widthStep, rgbData, x0, y0, x1, y1, x2, y2, x3, y3);

            Debug.WriteLine(countChar);
            //if (countChar >= 1)
            //{
            //    for (int i = 0; i < countChar; i++)
            //        Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);

            //}
            for (int i = 0; i < countChar; i++)
            {
                if (warpTarget == true)
                {
                    var warp = CVUtils.GetRectangle(imageRGB.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                    list.Add(warp);
                }
                else
                {
                    var Khung = CVUtils.GetRectangle(x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                    var mat   = CVUtils.Crop(imageRGB, Khung);
                    list.Add(mat);
                }
            }
            return(list);
        }
Beispiel #9
0
        void DrawHistogram(PictureBox window, CVHistogram histo, int channelIdx)
        {
            int imageWidth  = window.Width;
            int imageHeight = window.Height;

            int bins     = histo.BinSizes[0];
            int binWidth = imageWidth / bins;

            if (binWidth <= 0)
            {
                binWidth = 1;
            }

            CVPair  minMax      = histo.MinMaxValue;
            CVImage outputImage = new CVImage(imageWidth, imageHeight, CVDepth.Depth8U, 3);

            outputImage.Zero();

            for (int bin = 0; bin < bins; bin++)
            {
                double binValue  = histo[bin];
                byte   level     = (byte)CVUtils.Round(binValue * 255 / minMax.Second);
                byte   binHeight = (byte)CVUtils.Round(binValue * imageHeight / minMax.Second);

                byte[] color = new byte[3];
                color[channelIdx] = (byte)(((double)bin / (double)bins) * 255);

                byte[] markerColor = new byte[3];
                markerColor[channelIdx] = level;

                Color colColor  = Color.FromArgb(color[2], color[1], color[0]);
                Color colMarker = Color.FromArgb(markerColor[2], markerColor[1], markerColor[0]);


                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colColor);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colMarker);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, 1, binHeight), colMarker);
            }

            window.Image = outputImage.ToBitmap();
            outputImage.Release();
        }
    public Mat GetCroppedFace(Texture2D tex)
    {
        //TickMeter tm = new TickMeter();
        //tm.start();

        var rect = GetBB(tex);

        if (rect.width == 0 && rect.height == 0)
        {
            return(null);
        }
        var img          = CVUtils.TexToMat(tex);
        var ROI          = ExtendBB(img, rect, 0.3f);
        Mat cropped_face = img.submat((int)ROI.y, (int)ROI.y + (int)ROI.height, (int)ROI.x, (int)ROI.width + (int)ROI.x);

        Imgproc.resize(cropped_face, cropped_face, new Size(256, 256));

        //tm.stop();
        //Debug.Log(tm.getTimeMilli());

        return(cropped_face);
    }
Beispiel #11
0
        ///// <summary>
        ///// Lấy ra list các khuôn mặt trong frame
        ///// </summary>
        ///// <param name="ImageRoot">hình ảnh đưa vào</param>
        ///// <param name="maxFace">số lượng mặt tối đa có thể nhận được.giá trị mặc định là 30 nếu không truyền vào</param>
        ///// <returns></returns>
        //unsafe public static List<Mat> GetFace(Image<Bgr, byte> ImageRoot, string outFace, int maxFace = 30)
        //{
        //    var list = new List<Mat>();
        //    ushort rows = (ushort)ImageRoot.Height;
        //    ushort cols = (ushort)ImageRoot.Width;
        //    int widthStep = ImageRoot.MIplImage.WidthStep;
        //    IntPtr rgbData =ImageRoot.MIplImage.ImageData;
        //    ushort countObject = 1;
        //    ushort[] x0 = new ushort[maxFace], y0 = new ushort[maxFace],
        //          x1 = new ushort[maxFace], y1 = new ushort[maxFace],
        //             x2 = new ushort[maxFace], y2 = new ushort[maxFace],
        //                x3 = new ushort[maxFace], y3 = new ushort[maxFace];

        //    int countCrop = 30;
        //    int x = Native.FaceKit(rows, cols, widthStep, rgbData, &countObject, x0, y0, x1, y1, x2, y2, x3, y3);
        //    Debug.WriteLine(string.Format("frame có {0} khuôn mặt", +countCrop));

        //    List<Rectangle> areas = new List<Rectangle>();
        //    for (int i = 0; i < countObject; i++)
        //    {
        //        var warp = CVUtils.GetRectangle(ImageRoot.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
        //        list.Add(warp);
        //        warp.Bitmap.Save(string.Format(outFace + "\\warp_{0}.PNG", DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss_fff")));
        //        Thread.Sleep(300);
        //    }

        //    //CVUtils.DrawRect(ImageRoot.ToBitmap(), outFace + "\\Test.jpg", areas.ToArray());
        //    ImageRoot.Dispose();
        //    return list;
        //}
        /// <summary>
        /// Lấy ra list các khuôn mặt trong frame
        /// </summary>
        /// <param name="ImageRoot">hình ảnh đưa vào</param>
        /// <param name="maxFace">số lượng mặt tối đa có thể nhận được.giá trị mặc định là 30 nếu không truyền vào</param>
        /// <returns></returns>
        public unsafe static List <Mat> GetFace(Image <Bgr, byte> ImageRoot, string outFace, int maxFace = 30)
        {
            var    list      = new List <Mat>();
            ushort rows      = (ushort)ImageRoot.Height;
            ushort cols      = (ushort)ImageRoot.Width;
            int    widthStep = ImageRoot.MIplImage.WidthStep;
            IntPtr rgbData   = ImageRoot.MIplImage.ImageData;

            ushort[] x0 = new ushort[maxFace], y0 = new ushort[maxFace],
            x1 = new ushort[maxFace], y1 = new ushort[maxFace],
            x2 = new ushort[maxFace], y2 = new ushort[maxFace],
            x3 = new ushort[maxFace], y3 = new ushort[maxFace];
            ushort countObjectOLD = 1;
            int    countObject    = 0;

            countObject = Native.GetFace(rows, cols, widthStep, rgbData, x0, y0, x1, y1, x2, y2, x3, y3);
            //  Native.FaceKit(rows, cols, widthStep, rgbData, &countObjectOLD, x0, y0, x1, y1, x2, y2, x3, y3);
            Debug.WriteLine(string.Format("frame có {0} khuôn mặt", +countObject));

            List <Rectangle> areas = new List <Rectangle>();

            for (int i = 0; i < countObject; i++)
            {
                var warp = CVUtils.GetRectangle(ImageRoot.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
                list.Add(warp);
            }
            //for (int i = 0; i < countObjectOLD; i++)
            //{
            //    var warp = CVUtils.GetRectangle(ImageRoot.Mat, x0[i], y0[i], x1[i], y1[i], x2[i], y2[i], x3[i], y3[i]);
            //    list.Add(warp);
            //      warp.Bitmap.Save(string.Format(outFace + "\\warp_{0}.PNG", DateTime.Now.ToString("yyyy_MM_dd HH_mm_ss_fff")));
            //      Thread.Sleep(300);
            //}

            //CVUtils.DrawRect(ImageRoot.ToBitmap(), outFace + "\\Test.jpg", areas.ToArray());
            ImageRoot.Dispose();
            return(list);
        }
Beispiel #12
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // make sure all OpenCV errors are thrown as exceptions.
            CVUtils.ErrorsToExceptions();

            ExampleSelection es = new ExampleSelection();

            Assembly me = Assembly.GetExecutingAssembly();

            foreach (Type t in me.GetTypes())
            {
                if (t.IsSubclassOf(typeof(Form)) && t != typeof(ExampleSelection))
                {
                    Form f = (Form)me.CreateInstance(t.FullName);
                    es.AddForm(f);
                }
            }

            es.ShowDialog();
        }