/// <summary>
    /// Detects the faces in the given image.
    /// </summary>
    /// <returns>List of detected faces.</returns>
    /// <param name="imageBytes">Image bytes.</param>
    public IEnumerator DetectFaces(byte[] imageBytes)
    {
        faces = null;

        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        string requestUrl = string.Format("{0}/detect?returnFaceId={1}&returnFaceLandmarks={2}&returnFaceAttributes={3}",
                                          ServiceHost, true, false, "age,gender,smile,facialHair,glasses");

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);

        headers.Add("Content-Type", "application/octet-stream");
        headers.Add("Content-Length", imageBytes.Length.ToString());

        WWW www = new WWW(requestUrl, imageBytes, headers);

        yield return(www);

//		if (!string.IsNullOrEmpty(www.error))
//		{
//			throw new Exception(www.error + " - " + requestUrl);
//		}

        if (!CloudWebTools.IsErrorStatus(www))
        {
            //faces = JsonConvert.DeserializeObject<Face[]>(www.text, jsonSettings);
            string          newJson         = "{ \"faces\": " + www.text + "}";
            FacesCollection facesCollection = JsonUtility.FromJson <FacesCollection>(newJson);
            faces = facesCollection.faces;
        }
        else
        {
            ProcessFaceError(www);
        }
    }
Exemple #2
0
    public IEnumerator DetectFaces(byte[] imageBytes)
    {
        faces = null;

        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        string requestUrl = string.Format("{0}/detect?returnFaceId={1}&returnFaceLandmarks={2}&returnFaceAttributes={3}",
                                          ServiceHost, true, false, "age,gender,smile,facialHair,glasses");

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("ocp-apim-subscription-key", "8b06ad0b9148481c9cc3d60e042425e4");

        headers.Add("Content-Type", "application/octet-stream");
        headers.Add("Content-Length", imageBytes.Length.ToString());
        WWW www = new WWW(requestUrl, imageBytes, headers);

        yield return(www);

        if (!CloudWebTools.IsErrorStatus(www))
        {
            string          newJson         = "{ \"faces\": " + www.text + "}";
            FacesCollection facesCollection = JsonUtility.FromJson <FacesCollection>(newJson);
            faces = facesCollection.faces;
            string retrived = www.text;
            string response = cleanResponse(www.text);
            aFace  face     = JsonUtility.FromJson <aFace>(response);
            // Debug.Log("Calling with FaceID: " + face.faceId);
            // www.Dispose();
            // GC.Collect();
            matchingFace(face.faceId);
        }
        else
        {
            ProcessFaceError(www);
        }
    }
Exemple #3
0
    /// <summary>
    /// Adds the person to a group.
    /// </summary>
    /// <returns>The person to group.</returns>
    /// <param name="groupId">Person-group ID.</param>
    /// <param name="personName">Person name (max 128 chars).</param>
    /// <param name="userData">User data (max 16K).</param>
    public Person AddPersonToGroup(string groupId, string personName, string userData)
    {
        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        string requestUrl = string.Format("{0}/persongroups/{1}/persons", GetFaceServiceUrl(), groupId);

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);

        string sJsonContent = JsonUtility.ToJson(new PersonRequest(personName, userData));

        byte[]          btContent = Encoding.UTF8.GetBytes(sJsonContent);
        HttpWebResponse response  = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/json", btContent, headers, true, false);

        Person person = null;

        if (!CloudWebTools.IsErrorStatus(response))
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            person = JsonUtility.FromJson <Person>(reader.ReadToEnd());

            //if(person.PersonId != null)
            {
                person.name     = personName;
                person.userData = userData;
            }
        }
        else
        {
            ProcessFaceError(response);
        }

        return(person);
    }
Exemple #4
0
    /// <summary>
    /// Trains the person-group.
    /// </summary>
    /// <returns><c>true</c>, if person-group training was successfully started, <c>false</c> otherwise.</returns>
    /// <param name="groupId">Group identifier.</param>
    public bool TrainPersonGroup(string groupId)
    {
        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        string requestUrl = string.Format("{0}/persongroups/{1}/train", GetFaceServiceUrl(), groupId);

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);

        HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "", null, headers, true, false);

        if (CloudWebTools.IsErrorStatus(response))
        {
            ProcessFaceError(response);
            return(false);
        }

        return(true);
    }
Exemple #5
0
    /// <summary>
    /// Trains the person-group.
    /// </summary>
    /// <returns><c>true</c>, if person-group training was successfully started, <c>false</c> otherwise.</returns>
    /// <param name="groupId">Group identifier.</param>
    public bool TrainPersonGroup(string groupId)
    {
        Debug.Log("TrainPersonGroup_Starting Group Training\n");

        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        string requestUrl = string.Format("{0}/persongroups/{1}/train", GetFaceServiceUrl(), groupId);

        // Dictionary<string, string> headers = new Dictionary<string, string>();
        // headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);

        HttpWebRequest request = WebRequest.CreateHttp(requestUrl);

        request.Headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);
        request.Method        = "POST";
        request.ContentLength = 0;
        request.ContentType   = "application/json";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        // HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/json", null, headers, true, false);

        if (CloudWebTools.IsErrorStatus(response))
        {
            Debug.Log("TrainPersonGroup_Error - Response Below\n");
            Debug.Log(response.StatusCode);

            ProcessFaceError(response);
            return(false);
        }

        Debug.Log("TrainPersonGroup_Group Trained in Cloud\n");
        return(true);
    }
Exemple #6
0
    // --------------------------------------------------------------------------------- //

    // processes the error status in response
    private void ProcessFaceError(HttpWebResponse response)
    {
        StreamReader reader       = new StreamReader(response.GetResponseStream());
        string       responseText = reader.ReadToEnd();

        //ClientError ex = JsonConvert.DeserializeObject<ClientError>(responseText);
        ClientError ex = JsonUtility.FromJson <ClientError>(responseText);

        if (ex.error != null && ex.error.code != null)
        {
            string sErrorMsg = !string.IsNullOrEmpty(ex.error.code) && ex.error.code != "Unspecified" ?
                               ex.error.code + " - " + ex.error.message : ex.error.message;
            throw new System.Exception(sErrorMsg);
        }
        else
        {
            //ServiceError serviceEx = JsonConvert.DeserializeObject<ServiceError>(responseText);
            ServiceError serviceEx = JsonUtility.FromJson <ServiceError>(responseText);

            if (serviceEx != null && serviceEx.statusCode != null)
            {
                string sErrorMsg = !string.IsNullOrEmpty(serviceEx.statusCode) && serviceEx.statusCode != "Unspecified" ?
                                   serviceEx.statusCode + " - " + serviceEx.message : serviceEx.message;
                throw new System.Exception(sErrorMsg);
            }
            else
            {
                throw new System.Exception("Error " + CloudWebTools.GetStatusCode(response) + ": " + CloudWebTools.GetStatusMessage(response) + "; Url: " + response.ResponseUri);
            }
        }
    }
    private void ProcessFaceError(WWW www)
    {
        //ClientError ex = JsonConvert.DeserializeObject<ClientError>(www.text);
        ClientError ex = JsonUtility.FromJson <ClientError>(www.text);

        if (ex.error != null && ex.error.code != null)
        {
            string sErrorMsg = !string.IsNullOrEmpty(ex.error.code) && ex.error.code != "Unspecified" ?
                               ex.error.code + " - " + ex.error.message : ex.error.message;
            throw new System.Exception(sErrorMsg);
        }
        else
        {
            //ServiceError serviceEx = JsonConvert.DeserializeObject<ServiceError>(www.text);
            ServiceError serviceEx = JsonUtility.FromJson <ServiceError>(www.text);

            if (serviceEx != null && serviceEx.statusCode != null)
            {
                string sErrorMsg = !string.IsNullOrEmpty(serviceEx.statusCode) && serviceEx.statusCode != "Unspecified" ?
                                   serviceEx.statusCode + " - " + serviceEx.message : serviceEx.message;
                throw new System.Exception(sErrorMsg);
            }
            else
            {
                throw new System.Exception("Error " + CloudWebTools.GetStatusCode(www) + ": " + CloudWebTools.GetStatusMessage(www) + "; Url: " + www.url);
            }
        }
    }
    /// <summary>
    /// Detects the faces in the given image.
    /// </summary>
    /// <returns>List of detected faces.</returns>
    /// <param name="imageBytes">Image bytes.</param>
    public IEnumerator DetectFaces(byte[] imageBytes)
    {
        faces = null;

        if (string.IsNullOrEmpty(faceSubscriptionKey))
        {
            throw new Exception("The face-subscription key is not set.");
        }

        // detect faces
        string faceServiceHost = FaceServiceHost.Replace("[location]", faceServiceLocation);
        string requestUrl      = string.Format("{0}/detect?returnFaceId={1}&returnFaceLandmarks={2}&returnFaceAttributes={3}",
                                               faceServiceHost, true, false, "age,gender,smile,facialHair,glasses");

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("ocp-apim-subscription-key", faceSubscriptionKey);

        headers.Add("Content-Type", "application/octet-stream");
        headers.Add("Content-Length", imageBytes.Length.ToString());

        WWW www = new WWW(requestUrl, imageBytes, headers);

        yield return(www);

//		if (!string.IsNullOrEmpty(www.error))
//		{
//			throw new Exception(www.error + " - " + requestUrl);
//		}

        if (!CloudWebTools.IsErrorStatus(www))
        {
            //faces = JsonConvert.DeserializeObject<Face[]>(www.text, jsonSettings);
            string          newJson         = "{ \"faces\": " + www.text + "}";
            FacesCollection facesCollection = JsonUtility.FromJson <FacesCollection>(newJson);
            faces = facesCollection.faces;
        }
        else
        {
            ProcessFaceError(www);
        }

        if (/**recognizeEmotions &&*/ !string.IsNullOrEmpty(emotionSubscriptionKey))
        {
            // get face rectangles
            StringBuilder faceRectsStr = new StringBuilder();

            if (faces != null)
            {
                foreach (Face face in faces)
                {
                    FaceRectangle rect = face.faceRectangle;
                    faceRectsStr.AppendFormat("{0},{1},{2},{3};", rect.left, rect.top, rect.width, rect.height);
                }

                if (faceRectsStr.Length > 0)
                {
                    faceRectsStr.Remove(faceRectsStr.Length - 1, 1);                     // drop the last semicolon
                }
            }

            // recognize emotions
            string emotionServiceHost = EmotionServiceHost.Replace("[location]", emotionServiceLocation);
            requestUrl = string.Format("{0}/recognize??faceRectangles={1}", emotionServiceHost, faceRectsStr);

            headers = new Dictionary <string, string>();
            headers.Add("ocp-apim-subscription-key", emotionSubscriptionKey);

            headers.Add("Content-Type", "application/octet-stream");
            headers.Add("Content-Length", imageBytes.Length.ToString());

            www = new WWW(requestUrl, imageBytes, headers);
            yield return(www);

            Emotion[] emotions = null;
            if (!CloudWebTools.IsErrorStatus(www))
            {
                //emotions = JsonConvert.DeserializeObject<Emotion[]>(reader.ReadToEnd(), jsonSettings);
                string            newJson           = "{ \"emotions\": " + www.text + "}";
                EmotionCollection emotionCollection = JsonUtility.FromJson <EmotionCollection>(newJson);
                emotions = emotionCollection.emotions;
            }
            else
            {
                ProcessFaceError(www);
            }

            if (emotions != null)
            {
                // match the emotions to faces
                int matched = 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));
                }
            }
        }
    }
Exemple #9
0
    public IEnumerator matchingFace(string faceId)
    {
        // Debug.Log("Starting with FaceID" + faceId);
        string theFace;

        //if(faceId == "") {
        //    theFace = "9887a4a0-fd3c-4685-8e61-d600184c395e";
        //} else {
        //    theFace = faceId;
        //}
        theFace = faceId;
        string requestUrl = string.Format("{0}/verify", ServiceHost);
        Dictionary <string, string> header2 = new Dictionary <string, string>();

        header2.Add("ocp-apim-subscription-key", "8b06ad0b9148481c9cc3d60e042425e4");
        //header2.Add("faceId1", "19ce87ee-ad25-4ebc-b166-b58dad20c48c");
        // faceId2 is the static target
        header2.Add("faceId2", "0b2ebd57-7316-4e44-aa9b-1fb702015a41");
        header2.Add("Host", "westus.api.cognitive.microsoft.com");
        header2.Add("Content-Type", "application/json");
        string body = "{ \"faceId1\": \"" + faceId + "\", \"faceId2\": \"0b2ebd57-7316-4e44-aa9b-1fb702015a41\" }";

        //string body = "{ \"faceId1\": \"19ce87ee-ad25-4ebc-b166-b58dad20c48c\", \"faceId2\": \"0b2ebd57-7316-4e44-aa9b-1fb702015a41\" }";
        // Debug.Log("Starting Webclient");
        byte[] bodyValue = Encoding.ASCII.GetBytes(body);
        //AsyncOperation www2 = new WWW(requestUrl, new byte[0], header);
        // Debug.Log(header2);
        WWW www2 = new WWW(requestUrl, bodyValue, header2);

        // Debug.Log("Waiting...");
        yield return(www2);

        while (!www2.isDone)
        {
        }
        if (www2.isDone)
        {
            // atempted to yield thread
            // yield return new WaitForSeconds(0.75f);
            //yield WaitForSeconds(5);
            // Debug.Log(faceId);
            //yield return www2;
            // Debug.Log("Returned: " + www2.text);
            if (!CloudWebTools.IsErrorStatus(www2))
            {
                string response = cleanResponse(www2.text);
                bool   temp     = isTrue(response);
                // Debug.Log(temp);
                // Debug.Log(response.Substring(15, 16));
                char[] theResponse = response.ToCharArray();
                if (theResponse[14] == 't')
                {
                    matching = true;
                    yield return(null);
                }
                else
                {
                    matching = false;
                    yield return(null);
                }
            }
        }
    }