Example #1
0
        // Update is called once per frame
        void Update()
        {
            if (webCamTextureToMatHelper.IsPlaying() && webCamTextureToMatHelper.DidUpdateThisFrame())
            {
                Mat rgbaMat = webCamTextureToMatHelper.GetMat();



                Imgproc.cvtColor(rgbaMat, grayMat, Imgproc.COLOR_RGBA2GRAY);
                Imgproc.equalizeHist(grayMat, grayMat);

                // detect faces
                cascade.detectMultiScale(grayMat, faces, 1.1, 2, 2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
                                         new Size(grayMat.cols() * 0.2, grayMat.rows() * 0.2), new Size());

                if (faces.total() > 0)
                {
                    // fit landmarks for each found face
                    List <MatOfPoint2f> landmarks = new List <MatOfPoint2f>();
                    facemark.fit(grayMat, faces, landmarks);


                    Rect[] rects = faces.toArray();
                    for (int i = 0; i < rects.Length; i++)
                    {
                        //Debug.Log ("detect faces " + rects [i]);

                        Imgproc.rectangle(rgbaMat, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(255, 0, 0, 255), 2);
                    }

                    // draw them
                    for (int i = 0; i < landmarks.Count; i++)
                    {
                        MatOfPoint2f lm       = landmarks[i];
                        float[]      lm_float = new float[lm.total() * lm.channels()];
                        MatUtils.copyFromMat <float>(lm, lm_float);

                        DrawFaceLandmark(rgbaMat, ConvertArrayToPointList(lm_float), new Scalar(0, 255, 0, 255), 2);

                        //for (int j = 0; j < lm_float.Length; j = j + 2)
                        //{
                        //    Point p = new Point(lm_float[j], lm_float[j + 1]);
                        //    Imgproc.circle(rgbaMat, p, 2, new Scalar(255, 0, 0, 255), 1);
                        //}
                    }
                }


                //     rgbaMat.convertTo(rgbaMat, CvType.CV_8UC3);
                //    Debug.Log(rgbaMat.type());
                //  OpenCVForUnity.XphotoModule.Xphoto.applyChannelGains(rgbaMat, effectsMat, 1.0f,220.0f, 1.0f);
                //  OpenCVForUnity.XphotoModule.Xphoto.oilPainting(effects, effects, 10, 10);
                Utils.fastMatToTexture2D(rgbaMat, texture);
            }
        }