/// <summary> /// Adds the face to a person in a person-group. /// </summary> /// <returns>The persisted face (only faceId is set).</returns> /// <param name="groupId">Person-group ID.</param> /// <param name="personId">Person ID.</param> /// <param name="userData">User data.</param> /// <param name="faceRect">Face rect or null.</param> /// <param name="imageBytes">Image bytes.</param> public PersonFace AddFaceToPerson(string groupId, string personId, string userData, FaceRectangle faceRect, byte[] imageBytes) { if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string sFaceRect = faceRect != null?string.Format("{0},{1},{2},{3}", faceRect.left, faceRect.top, faceRect.width, faceRect.height) : string.Empty; string requestUrl = string.Format("{0}/persongroups/{1}/persons/{2}/persistedFaces?userData={3}&targetFace={4}", GetFaceServiceUrl(), groupId, personId, userData, sFaceRect); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/octet-stream", imageBytes, headers, true, false); PersonFace face = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); face = JsonUtility.FromJson <PersonFace>(reader.ReadToEnd()); } else { ProcessFaceError(response); } return(face); }
/// <summary> /// Gets the person-group's training status. /// </summary> /// <returns>The group's training status.</returns> /// <param name="groupId">Person-group ID.</param> public TrainingStatus GetPersonGroupTrainingStatus(string groupId) { if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string requestUrl = string.Format("{0}/persongroups/{1}/training", GetFaceServiceUrl(), groupId); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "GET", "", null, headers, true, false); TrainingStatus status = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); status = JsonUtility.FromJson <TrainingStatus>(reader.ReadToEnd()); } else { ProcessFaceError(response); } return(status); }
/// <summary> /// Updates the person's name or userData field. /// </summary> /// <param name="groupId">Person-group ID.</param> /// <param name="personId">Person ID.</param> public void UpdatePersonData(string groupId, Person person) { if (person == null) { return; } if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string requestUrl = string.Format("{0}/persongroups/{1}/persons/{2}", GetFaceServiceUrl(), groupId, person.personId); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); string sJsonContent = JsonUtility.ToJson(new PersonRequest(person.name, person.userData)); byte[] btContent = Encoding.UTF8.GetBytes(sJsonContent); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "PATCH", "application/json", btContent, headers, true, false); if (CloudWebTools.IsErrorStatus(response)) { ProcessFaceError(response); } }
/// <summary> /// Lists the people in a person-group. /// </summary> /// <returns>The people in group.</returns> /// <param name="groupId">Person-group ID.</param> public Person[] ListPersonsInGroup(string groupId) { 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); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "GET", "application/json", null, headers, true, false); Person[] persons = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); //persons = JsonConvert.DeserializeObject<Person[]>(reader.ReadToEnd(), jsonSettings); string newJson = "{ \"persons\": " + reader.ReadToEnd() + "}"; PersonCollection personColl = JsonUtility.FromJson <PersonCollection>(newJson); persons = personColl.persons; } else { ProcessFaceError(response); } return(persons); }
/// <summary> /// Deletes existing person from a person group. Persisted face images of the person will also be deleted. /// </summary> /// <param name="groupId">Person-group ID.</param> /// <param name="personId">Person ID.</param> public void DeletePerson(string groupId, string personId) { Debug.Log("DeletePerson_Starting User Deletion\n"); if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string requestUrl = string.Format("{0}/persongroups/{1}/persons/{2}", GetFaceServiceUrl(), groupId, personId); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "DELETE", "application/json", null, headers, true, false); if (CloudWebTools.IsErrorStatus(response)) { Debug.Log("DeletePerson_Error - Response Below\n"); ProcessFaceError(response); } Debug.Log("DeletePerson_User Deleted From Cloud\n"); }
/// <summary> /// Creates a person group. /// </summary> /// <returns><c>true</c>, if person group was created, <c>false</c> otherwise.</returns> /// <param name="groupId">Person-group ID.</param> /// <param name="name">Group name (max 128 chars).</param> /// <param name="userData">User data (max 16K).</param> public bool CreatePersonGroup(string groupId, string groupName, string userData) { if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string requestUrl = string.Format("{0}/persongroups/{1}", GetFaceServiceUrl(), groupId); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); //string sJsonContent = JsonConvert.SerializeObject(new { name = groupName, userData = userData }, jsonSettings); string sJsonContent = JsonUtility.ToJson(new PersonGroupRequest(groupName, userData)); byte[] btContent = Encoding.UTF8.GetBytes(sJsonContent); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "PUT", "application/json", btContent, headers, true, false); if (CloudWebTools.IsErrorStatus(response)) { ProcessFaceError(response); return(false); } return(true); }
/// <summary> /// Detects the faces in the given image. /// </summary> /// <returns>List of detected faces.</returns> /// <param name="imageBytes">Image bytes.</param> public Face[] DetectFaces(byte[] imageBytes) { 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}", GetFaceServiceUrl(), true, false, "age,gender,smile,headPose"); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/octet-stream", imageBytes, headers, true, false); Face[] faces = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); //faces = JsonConvert.DeserializeObject<Face[]>(reader.ReadToEnd(), jsonSettings); string newJson = "{ \"faces\": " + reader.ReadToEnd() + "}"; FaceCollection facesCollection = JsonUtility.FromJson <FaceCollection>(newJson); faces = facesCollection.faces; } else { ProcessFaceError(response); } return(faces); }
/// <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 faceServiceHost = ServiceHost.Replace("[location]", faceServiceLocation); string requestUrl = string.Format("{0}/detect?returnFaceId={1}&returnFaceLandmarks={2}&returnFaceAttributes={3}", faceServiceHost, true, false, "age,gender,smile,facialHair,glasses"); //Debug.Log("Request: " + requestUrl); 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); //Debug.Log("Response: " + www.text); // 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); //Debug.Log("Faces-count: " + facesCollection.faces.Length); //if(facesCollection.faces.Length > 0) // Debug.Log("Face0: " + facesCollection.faces[0].faceId + ", Gender: " + facesCollection.faces[0].faceAttributes.gender + ", Age: " + facesCollection.faces[0].faceAttributes.age); faces = facesCollection.faces; } else { ProcessFaceError(www); } }
/// <summary> /// Recognizes the emotions. /// </summary> /// <returns>The array of recognized emotions.</returns> /// <param name="imageBytes">Image bytes.</param> /// <param name="faceRects">Detected face rectangles, or null.</param> public Emotion[] RecognizeEmotions(byte[] imageBytes, FaceRectangle[] faceRects) { if (string.IsNullOrEmpty(emotionSubscriptionKey)) { throw new Exception("The emotion-subscription key is not set."); } StringBuilder faceRectsStr = new StringBuilder(); if (faceRects != null) { foreach (FaceRectangle rect in faceRects) { 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 } } string requestUrl = string.Format("{0}/recognize??faceRectangles={1}", GetEmotionServiceUrl(), faceRectsStr); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", emotionSubscriptionKey); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/octet-stream", imageBytes, headers, true, false); Emotion[] emotions = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); //emotions = JsonConvert.DeserializeObject<Emotion[]>(reader.ReadToEnd(), jsonSettings); string newJson = "{ \"emotions\": " + reader.ReadToEnd() + "}"; EmotionCollection emotionCollection = JsonUtility.FromJson <EmotionCollection>(newJson); emotions = emotionCollection.emotions; } else { ProcessFaceError(response); } return(emotions); }
/// <summary> /// Identifies the given faces. /// </summary> /// <returns>Array of identification results.</returns> /// <param name="groupId">Group ID.</param> /// <param name="faces">Array of detected faces.</param> /// <param name="maxCandidates">Maximum allowed candidates pro face.</param> public IdentifyResult[] IdentifyFaces(string groupId, ref Face[] faces, int maxCandidates) { if (string.IsNullOrEmpty(faceSubscriptionKey)) { throw new Exception("The face-subscription key is not set."); } string[] faceIds = new string[faces.Length]; for (int i = 0; i < faces.Length; i++) { faceIds[i] = faces[i].faceId; } if (maxCandidates <= 0) { maxCandidates = 1; } string requestUrl = string.Format("{0}/identify", GetFaceServiceUrl()); Dictionary <string, string> headers = new Dictionary <string, string>(); headers.Add("ocp-apim-subscription-key", faceSubscriptionKey); string sJsonContent = JsonUtility.ToJson(new IdentityRequest(groupId, faceIds, maxCandidates)); byte[] btContent = Encoding.UTF8.GetBytes(sJsonContent); HttpWebResponse response = CloudWebTools.DoWebRequest(requestUrl, "POST", "application/json", btContent, headers, true, false); IdentifyResult[] results = null; if (!CloudWebTools.IsErrorStatus(response)) { StreamReader reader = new StreamReader(response.GetResponseStream()); string newJson = "{ \"identityResults\": " + reader.ReadToEnd() + "}"; IdentifyResultCollection resultCollection = JsonUtility.FromJson <IdentifyResultCollection>(newJson); results = resultCollection.identityResults; } else { ProcessFaceError(response); } return(results); }
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); } }
/// <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); }
/// <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); }
/// <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); }
/// <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)); } } } }
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); } } } }