Esempio n. 1
0
        public double CompareSimilarity(FaceEmbeddings emb1, FaceEmbeddings emb2)
        {
            var mat1 = emb1.CreateMatFromEmbeddings(emb1.Embeddings);
            var mat2 = emb2.CreateMatFromEmbeddings(emb2.Embeddings);

            // l2 normalize the vectors
            for (int i = 0; i < 128; i++)
            {
                mat1.put(0, i, mat1.get(0, i)[0] / Core.norm(mat1, 4));
            }

            for (int i = 0; i < 128; i++)
            {
                mat2.put(0, i, mat2.get(0, i)[0] / Core.norm(mat2, 4));
            }

            if (mat1 == null || mat2 == null)
            {
                return(1000.0);
            }

            var ab      = mat1.dot(mat2);
            var aMagni  = Core.norm(mat1);
            var bMagni  = Core.norm(mat2);
            var cos_sim = ab / (aMagni * bMagni);

            mat1.Dispose(); mat2.Dispose();

            return(1 - cos_sim);
        }
Esempio n. 2
0
        /// <summary>
        /// Calculate the euclidean distance between 2 FaceEmbeddings objects.
        /// </summary>
        /// <param name="emb1"></param>
        /// <param name="emb2"></param>
        /// <returns> double </returns>
        public double CompareEmbeddings(FaceEmbeddings emb1, FaceEmbeddings emb2)
        {
            var mat1 = emb1.CreateMatFromEmbeddings(emb1.Embeddings);
            var mat2 = emb2.CreateMatFromEmbeddings(emb2.Embeddings);

            if (mat1 == null || mat2 == null)
            {
                return(1000.0);
            }

            var diff   = mat1 - mat2;
            var result = diff.dot(diff);

            result = Mathf.Sqrt((float)result);
            return(result);
        }
Esempio n. 3
0
        public FaceEmbeddings ExtractFaceEmbeddings(Texture2D imageTex, UnityEngine.Rect ROI)
        {
            //if true, The error log of the Native side OpenCV will be displayed on the Unity Editor Console.
            Utils.setDebugMode(true);

            var embedder = Dnn.readNetFromTorch(model_filepath);

            Mat img = new Mat(imageTex.height, imageTex.width, CvType.CV_8UC3);

            Imgproc.cvtColor(img, img, Imgproc.COLOR_BGR2RGB);
            Utils.texture2DToMat(imageTex, img);

            if (img.empty())
            {
                Debug.LogError(input_filepath + " is not loaded. Please see \"StreamingAssets/dnn/setup_dnn_module.pdf\". ");
                img = new Mat(424, 640, CvType.CV_8UC3, new Scalar(0, 0, 0));
            }

            Mat cropped_face = img.submat((int)ROI.y, (int)ROI.y + (int)ROI.height, (int)ROI.x, (int)ROI.width + (int)ROI.x);

            Imgproc.cvtColor(cropped_face, cropped_face, Imgproc.COLOR_BGR2RGB);
            var faceBlob = Dnn.blobFromImage(cropped_face, scalefactor, new Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);

            embedder.setInput(faceBlob);

            var netOut = embedder.forward();

            var embeddings = new FaceEmbeddings(netOut, 128);

            if (gameObject.GetComponent <Renderer>() != null && displayBB)
            {
                GenericUtils.AdjustImageScale(cropped_face, this.gameObject);
                Texture2D texture = new Texture2D(cropped_face.cols(), cropped_face.rows(), TextureFormat.RGBA32, false);
                Utils.matToTexture2D(cropped_face, texture);
                gameObject.GetComponent <Renderer>().material.mainTexture = texture;
            }

            embedder.Dispose();
            cropped_face.Dispose();
            img.Dispose();
            netOut.Dispose();

            return(embeddings);
        }