/// <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> /// 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> /// 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> /// 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> /// 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); }
/// <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); }