Ejemplo n.º 1
0
        public override void Draw(Canvas canvas)
        {
            if (this.mLFace == null)
            {
                return;
            }
            IList <MLPosition> face3dPoints = mLFace.Get3DKeyPoints(ML3DFace.LandmarkFive);

            float[] projectionMatrix = new float[4 * 4];
            float[] viewMatrix       = new float[4 * 4];
            mLFace.Get3DProjectionMatrix(projectionMatrix, 1, 10);
            mLFace.Get3DViewMatrix(viewMatrix);

            int frameHeight = (int)UnScaleX(overlay.Height); // Image height
            int frameWidth  = (int)UnScaleY(overlay.Width);  // Image Width

            float[]            adaptMatrix  = { frameWidth / 2, 0, frameWidth / 2, 0, -frameHeight / 2, frameHeight / 2, 0, 0, 1 };
            IList <MLPosition> face2dPoints = TranslateTo2D(face3dPoints, projectionMatrix, viewMatrix, adaptMatrix);

            StringBuilder sb = new StringBuilder();
            // Draw 2D points
            Paint numPaint;

            numPaint          = new Paint();
            numPaint.Color    = Color.Red;
            numPaint.TextSize = frameHeight / 80;
            for (int i = 0; i < face2dPoints.Count; i++)
            {
                MLPosition point = face2dPoints.ElementAt(i);
                canvas.DrawPoint(TranslateX(point.GetX().FloatValue()), TranslateY(point.GetY().FloatValue()), boxPaint);
                canvas.DrawText("" + i, TranslateX(point.GetX().FloatValue()), TranslateY(point.GetY().FloatValue()), numPaint);
                sb.Append(point.GetX() + " " + point.GetY() + "\n");
            }
        }
Ejemplo n.º 2
0
        private IList <MLPosition> TranslateTo2D(IList <MLPosition> face3dPoints, float[] projectionMatrix, float[] viewMatrix, float[] adaptMatrix)
        {
            IList <MLPosition> face2dPoints = new List <MLPosition>();

            for (int i = 0; i < face3dPoints.Count; i++)
            {
                MLPosition curPoint = face3dPoints.ElementAt(i);
                float[]    curVec   = { (float)curPoint.GetX(), (float)curPoint.GetY(), (float)curPoint.GetZ(), 1 };
                //1 V*Vec
                float[] temp1 = MatrixMulti(viewMatrix, 4, 4, curVec);
                //2 P*(V*Vec)
                float[] temp2 = MatrixMulti(projectionMatrix, 4, 4, temp1);
                //3 calculations x’ y'
                float[] temp3 = { temp2[0] / temp2[3], temp2[1] / temp2[3], 1 };
                //4 calculations X Y coordinates
                float[] point = MatrixMulti(adaptMatrix, 3, 3, temp3);
                face2dPoints.Add(new MLPosition((Java.Lang.Float)point[0], (Java.Lang.Float)point[1]));
            }
            return(face2dPoints);
        }
        public override void Draw(Canvas canvas)
        {
            if (this.mFace == null)
            {
                return;
            }
            float start = 350f;
            float x     = start;
            float width = 500f;
            float y     = this.overlay.Height - 300.0f;
            Dictionary <string, float> emotions = new Dictionary <string, float>();

            emotions.Add("Smiling", this.mFace.Emotions.SmilingProbability);
            emotions.Add("Neutral", this.mFace.Emotions.NeutralProbability);
            emotions.Add("Angry", this.mFace.Emotions.AngryProbability);
            emotions.Add("Fear", this.mFace.Emotions.FearProbability);
            emotions.Add("Sad", this.mFace.Emotions.SadProbability);
            emotions.Add("Disgust", this.mFace.Emotions.DisgustProbability);
            emotions.Add("Surprise", this.mFace.Emotions.SurpriseProbability);
            //List<string> result = emotions.ToList<string>();

            DecimalFormat decimalFormat = new DecimalFormat("0.000");

            // Draw the facial feature value.
            canvas.DrawText("Left eye: " + decimalFormat.Format(this.mFace.Features.LeftEyeOpenProbability), x, y,
                            this.probilityPaint);
            x = x + width;
            canvas.DrawText("Right eye: " + decimalFormat.Format(this.mFace.Features.RightEyeOpenProbability), x, y,
                            this.probilityPaint);
            y = y - 40.0f;
            x = start;
            canvas.DrawText("Moustache Probability: " + decimalFormat.Format(this.mFace.Features.MoustacheProbability),
                            x, y, this.probilityPaint);
            x = x + width;
            canvas.DrawText("Glass Probability: " + decimalFormat.Format(this.mFace.Features.SunGlassProbability), x,
                            y, this.probilityPaint);
            y = y - 40.0f;
            x = start;
            canvas.DrawText("Hat Probability: " + decimalFormat.Format(this.mFace.Features.HatProbability), x, y,
                            this.probilityPaint);
            x = x + width;
            canvas.DrawText("Age: " + this.mFace.Features.Age, x, y, this.probilityPaint);
            y = y - 40.0f;
            x = start;
            String sex = (this.mFace.Features.SexProbability > 0.5f) ? "Female" : "Male";

            canvas.DrawText("Gender: " + sex, x, y, this.probilityPaint);
            x = x + width;
            canvas.DrawText("RotationAngleY: " + decimalFormat.Format(this.mFace.RotationAngleY), x, y, this.probilityPaint);
            y = y - 40.0f;
            x = start;
            canvas.DrawText("RotationAngleZ: " + decimalFormat.Format(this.mFace.RotationAngleZ), x, y, this.probilityPaint);
            x = x + width;
            canvas.DrawText("RotationAngleX: " + decimalFormat.Format(this.mFace.RotationAngleX), x, y, this.probilityPaint);
            y = y - 40.0f;
            x = start;
            var sortedDict = from entry in emotions orderby entry.Value descending select entry;

            canvas.DrawText(sortedDict.ElementAt(0).Key, x, y, this.probilityPaint);

            // Draw a face contour.
            if (this.mFace.FaceShapeList != null)
            {
                foreach (MLFaceShape faceShape in this.mFace.FaceShapeList)
                {
                    if (faceShape == null)
                    {
                        continue;
                    }
                    IList <MLPosition> points = faceShape.Points;
                    for (int i = 0; i < points.Count; i++)
                    {
                        MLPosition point = points.ElementAt(i);
                        canvas.DrawPoint(this.TranslateX(point.GetX().FloatValue()), this.TranslateY(point.GetY().FloatValue()),
                                         this.boxPaint);
                        if (i != (points.Count - 1))
                        {
                            MLPosition next = points.ElementAt(i + 1);;
                            if (point != null && point.GetX() != null && point.GetY() != null)
                            {
                                if (i % 3 == 0)
                                {
                                    canvas.DrawText(i + 1 + "", this.TranslateX(point.GetX().FloatValue()),
                                                    this.TranslateY(point.GetY().FloatValue()), this.textPaint);
                                }
                                canvas.DrawLines(new float[] { this.TranslateX(point.GetX().FloatValue()),
                                                               this.TranslateY(point.GetY().FloatValue()), this.TranslateX(next.GetX().FloatValue()),
                                                               this.TranslateY(next.GetY().FloatValue()) }, this.GetPaint(faceShape));
                            }
                        }
                    }
                }
            }
            // Face Key Points
            foreach (MLFaceKeyPoint keyPoint in this.mFace.FaceKeyPoints)
            {
                if (keyPoint != null)
                {
                    MLPosition point = keyPoint.Point;
                    canvas.DrawCircle(this.TranslateX((float)point.GetX()), this.TranslateY((float)point.GetY()), 10f, this.landmarkPaint);
                }
            }
        }