Ejemplo n.º 1
0
    private void Start()
    {
        m_Error = new Error.ErrorResponse();
        m_PersonCreatedSuccess = new PersonCreateSuccess.PersonCreateSuccessResponse();
        m_TrainingStatus       = new Training.TrainingStatus();
        m_ListOfPersonsInGroup = new List <PersonInGroup.Person>();

        m_AzureManager = GetComponent <AzureManager>();
        m_ApiKey       = m_AzureManager.GetApiKey();
        m_Endpoint     = m_AzureManager.GetEndpoint();
    }
        public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next)
        {
            var context       = new ValidationContext(request);
            var errorResponse = new Error.ErrorResponse();

            foreach (var validation in _validators)
            {
                var validationResponse = await validation.ValidateAsync(context);

                foreach (var error in validationResponse.Errors)
                {
                    errorResponse.AddError(error.ErrorMessage);
                }
            }

            errorResponse.ThrowIfErrors();

            return(await next());
        }
Ejemplo n.º 3
0
    IEnumerator GetPersonGroupTrainedStatus()
    {
        yield return(m_DelayCommon);

        Debug.Log("Getting PersonGroup trained status");
        string trainingStatus = "Unknown";

        yield return(RequestManager.GetPersonGroupTrainingStatus(m_Endpoint, m_ApiKey, m_PersonGroup, value => trainingStatus = value));

        Error.ErrorResponse res_error = JsonUtility.FromJson <Error.ErrorResponse>(trainingStatus);
        Debug.Log("GetPersonGroupTrainedStatus error : " + res_error.error.code);
        Debug.Log("Training status : " + trainingStatus);
        if (res_error.error.code != null)
        {
            Debug.LogError("Something went wrong at GetPersonGroupTrainedStatus ");
        }
        else
        {
            m_PersonGroupTrained = true;
            #region PersonGroup exists (is defined) and has at least one Person defined. So we check the Training Status
            // ここまで来たらグループには一人以上が定義されている。あとはトレーニングされているかどうかのチェック。
            if (m_PersonGroupTrained)
            {
                StartCoroutine(Recognize());
            }
            else
            {
                SetStatusText("PersonGroup には1人以上定義されているが、トレーニングされていない。");
                string trainPersonGroupResult = "Unknown";
                yield return(RequestManager.TrainPersonGroup(m_Endpoint, m_ApiKey, m_PersonGroup, value => trainPersonGroupResult = value));

                if (trainPersonGroupResult == "")
                {
                    Debug.Log("Training success. Trying identification.");
                    StartCoroutine(Recognize());
                }
            }
            #endregion
        }
    }
Ejemplo n.º 4
0
    public static IEnumerator GetPersonGroupTrainingStatus(string endpoint, string apiKey, string personGroupId,
                                                           System.Action <bool> result, System.Action <Error.ErrorResponse> err, System.Action <Training.TrainingStatus> status)
    {
        string parameters = "?subscription-key=" + apiKey;
        string request    = endpoint + "/persongroups/" + personGroupId + "/training" + parameters;

        Error.ErrorResponse     errorResponse  = new Error.ErrorResponse();
        Training.TrainingStatus statusResponse = new Training.TrainingStatus();
        using (UnityWebRequest www = UnityWebRequest.Get(request))
        {
            yield return(www.SendWebRequest());

            if (www.isNetworkError)
            {
                errorResponse.error.message = "Network error";
                errorResponse.error.code    = "Network";
                err(errorResponse);
                result(false);
            }
            else
            {
                if (!string.IsNullOrEmpty(www.error))
                {
                    // TODO: Catch other errors too
                    errorResponse = JsonUtility.FromJson <Error.ErrorResponse>(www.error);
                    err(errorResponse);
                    result(false);
                }
                else
                {
                    statusResponse = JsonUtility.FromJson <Training.TrainingStatus>(www.downloadHandler.text);
                    status(statusResponse);
                    result(true);
                }
            }
        }
    }
Ejemplo n.º 5
0
 public PlaidException(Error.ErrorResponse error) : base(error.ToString())
 {
     PlaidError = error;
 }
Ejemplo n.º 6
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        if (ENV_KEY.Equals("None"))
        {
            Debug.LogError("No environment variable key is defined. Stopping.");
            yield break;
        }

        GetApiKey();

        // Check if the person group ID already exists
        bool requestSucceded = false;

        yield return(RequestManager.GetPersonGroup(ENDPOINT, API_KEY, m_PersonGroupId, value => requestSucceded = value));

        if (requestSucceded)
        {
            Debug.Log("Person group ID exists");

            // Check if the target person is already created. We only need one person for this demo.
            List <PersonInGroup.Person> personList = new List <PersonInGroup.Person>();
            yield return(RequestManager.GetPersonListInGroup(ENDPOINT, API_KEY, m_PersonGroupId, res => m_PersonListInGroupNotEmpty = res, value => personList = value));

            if (personList.Count > 0 && personList[0].personId != null)
            {
                // Check if the target person exists in the list
                bool personFound = false;
                for (int i = 0; i < personList.Count; i++)
                {
                    Debug.Log("name : " + personList[i].name);
                    if (personList[i].name.Equals(m_PersonInGroup))
                    {
                        personFound = true;
                    }
                }

                if (!personFound)
                {
                    yield break;
                }

                string id = personList[0].personId;
                Debug.Log("Person found in group with person Id : " + id);

                // Get training status of the person group
                Training.TrainingStatus trainingSuccess = new Training.TrainingStatus();
                Error.ErrorResponse     resError        = new Error.ErrorResponse();
                bool trained = false;
                yield return(RequestManager.GetPersonGroupTrainingStatus(ENDPOINT, API_KEY, m_PersonGroupId, res => trained = res, err => resError = err, value => trainingSuccess = value));

                // If not trained the add images and detect faces
                if (!trained && resError.error.code != null && resError.error.code.Equals("PersonGroupNotTrained"))
                {
                    // Get all images in the specified folder and detect the faces rectangles in them.
                    string[] imageFiles = Directory.GetFiles(m_ImageFolderPath, "*.jpg");
                    for (int i = 0; i < imageFiles.Length; i++)
                    {
                        bool faceFound = false;
                        List <FacesBasic.FacesDetectionResponse> face = new List <FacesBasic.FacesDetectionResponse>();
                        byte[]    imageByte = File.ReadAllBytes(m_ImageFolderPath + "/me" + (i + 1) + ".jpg");
                        Texture2D img       = new Texture2D(1, 1);
                        img.LoadImage(imageByte);
                        yield return(RequestManager.DetectFaces(ENDPOINT, API_KEY, img, res => faceFound = res, value => face = value));

                        // Register the images to the person in person group
                        if (face[0].rect != null)
                        {
                            // Add face to person in group
                            string    faceRect        = "targetFace=" + face[0].rect.left + "," + face[0].rect.top + "," + face[0].rect.width + "," + face[0].rect.height;
                            string    addFaceResponse = "Unknown";
                            bool      addFaceSuccess  = false;
                            byte[]    imgByte         = File.ReadAllBytes(m_ImageFolderPath + "/me" + (i + 1) + ".jpg");
                            Texture2D curImg          = new Texture2D(1, 1);
                            curImg.LoadImage(imgByte);
                            yield return(RequestManager.AddFaceToPersonInGroup(ENDPOINT, API_KEY, m_PersonGroupId, id, curImg, faceRect, res => addFaceSuccess = res, value => addFaceResponse = value));

                            Debug.Log(addFaceResponse);
                        }
                    }


                    string trainPersonGroupResult = "Unknown";
                    bool   trainingResult         = false;
                    yield return(RequestManager.TrainPersonGroup(ENDPOINT, API_KEY, m_PersonGroupId, res => trainingResult = res, value => trainPersonGroupResult = value));

                    if (trainingResult)
                    {
                        Debug.Log("Training success. Stop and restart app to try identification.");
                    }
                }
                else if (trainingSuccess.status.Equals("succeeded"))
                // Person Group is already trained
                {
                    Debug.Log("PersonGroup is already trained");
                    // Try to detect face in test image
                    string[] testImageFiles = Directory.GetFiles(m_TestImagepath, "*.jpg");

                    for (int i = 0; i < testImageFiles.Length; i++)
                    {
                        Debug.Log(testImageFiles[i]);
                        // Detect faces in the test image
                        bool faceFound = false;
                        List <FacesBasic.FacesDetectionResponse> face = new List <FacesBasic.FacesDetectionResponse>();
                        byte[]    testImgByte = File.ReadAllBytes(testImageFiles[i]);
                        Texture2D testImg     = new Texture2D(1, 1);
                        testImg.LoadImage(testImgByte);
                        yield return(RequestManager.DetectFaces(ENDPOINT, API_KEY, testImg, res => faceFound = res, value => face = value));

                        // Identify faces in the test image
                        bool identified = false;
                        List <IdentifiedFaces.IdentifiedFacesResponse> facesList = new List <IdentifiedFaces.IdentifiedFacesResponse>();
                        string errors = "";
                        yield return(RequestManager.Identify(ENDPOINT, API_KEY, m_PersonGroupId, face.ToArray(), res => identified = res, err => errors = err, value => facesList = value));

                        if (identified)
                        {
                            Debug.Log(facesList);
                        }
                    }
                }
            }
            else
            {
                Debug.Log("Person not found");
                PersonCreateSuccess.PersonCreateSuccessResponse successResponse = new PersonCreateSuccess.PersonCreateSuccessResponse();
                bool createdPerson = false;
                yield return(RequestManager.CreatePersonInGroup(ENDPOINT, API_KEY, m_PersonGroupId, m_PersonInGroup, "First person", res => createdPerson = res, value => successResponse = value));

                if (createdPerson)
                {
                    Debug.Log("Successfully created Person in Group. Stop and restart app to try identification");
                }
                else
                {
                    Debug.Log("Failed to create Person in Group");
                }
            }
        }
        else
        {
            Debug.Log("Person group ID does not exist. Creating Person Group");
            bool createSucceded = false;
            yield return(RequestManager.CreatePersonGroup(ENDPOINT, API_KEY, m_PersonGroupId, "ShinMisato", "Visitors who came to our Shin Misato theme park", value => createSucceded = value));

            if (createSucceded)
            {
                Debug.Log("Successfully created Person Group. Stop and restart app to try performing identification");
            }
            else
            {
                Debug.Log("Failed to create Person Group. Something went wrong. Please debug.");
            }
        }
    }
Ejemplo n.º 7
0
    public IEnumerator GetTrainingStatus(string personGroup)
    {
        yield return(RequestManager.GetPersonGroupTrainingStatus(m_Endpoint, m_ApiKey, personGroup, result => personGroupTrained = result, err => m_Error = err, value => m_TrainingStatus = value));

        if (!personGroupTrained && m_Error.error.code.Equals(Constants.AZURE_PERSONGROUPNOTTRAINED_CODE) && OnPersonGroupNotTrained != null)
        {
            OnPersonGroupNotTrained.Invoke();
        }
        else if (personGroupTrained && m_TrainingStatus.status.Equals(Constants.AZURE_PERSONGROUPTRAINSUCCESS) && OnPersonGroupTrained != null)
        {
            OnPersonGroupTrained.Invoke();
        }
    }