예제 #1
0
    // performs face detection
    private IEnumerator DoFaceDetection()
    {
        // get the image to detect
        Face[]    faces      = null;
        Texture2D texCamShot = null;

        if (cameraShot)
        {
            texCamShot = (Texture2D)cameraShot.texture;
            SetHintText("Wait...");
        }

        // get the face manager instance
        CloudFaceManager faceManager = CloudFaceManager.Instance;

        if (!faceManager)
        {
            SetHintText("Check if the FaceManager component exists in the scene.");
        }
        else if (texCamShot)
        {
            byte[] imageBytes = texCamShot.EncodeToJPG();
            yield return(null);

            //faces = faceManager.DetectFaces(texCamShot);
            AsyncTask <Face[]> taskFace = new AsyncTask <Face[]>(() => {
                return(faceManager.DetectFaces(imageBytes));
            });

            taskFace.Start();
            yield return(null);

            while (taskFace.State == TaskState.Running)
            {
                yield return(null);
            }

            if (string.IsNullOrEmpty(taskFace.ErrorMessage))
            {
                faces = taskFace.Result;

                if (faces != null && faces.Length > 0)
                {
                    // stick to detected face rectangles
                    FaceRectangle[] faceRects = new FaceRectangle[faces.Length];

                    for (int i = 0; i < faces.Length; i++)
                    {
                        faceRects[i] = faces[i].faceRectangle;
                    }

                    yield return(null);

                    // get the emotions of the faces
                    if (recognizeEmotions)
                    {
                        //Emotion[] emotions = faceManager.RecognizeEmotions(texCamShot, faceRects);
                        AsyncTask <Emotion[]> taskEmot = new AsyncTask <Emotion[]>(() => {
                            return(faceManager.RecognizeEmotions(imageBytes, faceRects));
                        });

                        taskEmot.Start();
                        yield return(null);

                        while (taskEmot.State == TaskState.Running)
                        {
                            yield return(null);
                        }

                        if (string.IsNullOrEmpty(taskEmot.ErrorMessage))
                        {
                            Emotion[] emotions = taskEmot.Result;
                            int       matched  = faceManager.MatchEmotionsToFaces(ref faces, ref emotions);

                            if (matched != faces.Length)
                            {
                                Debug.Log(string.Format("Matched {0}/{1} emotions to {2} faces.", matched, emotions.Length, faces.Length));
                            }
                        }
                        else
                        {
                            SetHintText(taskEmot.ErrorMessage);
                        }
                    }

                    CloudFaceManager.DrawFaceRects(texCamShot, faces, FaceDetectionUtils.FaceColors);
                    //SetHintText("Click on the camera image to make a shot");
                    SetHintText(hintMessage);
                    SetResultText(faces);
                }
                else
                {
                    SetHintText("No face(s) detected.");
                }
            }
            else
            {
                SetHintText(taskFace.ErrorMessage);
            }
        }

        yield return(null);
    }