/* * public async void Initializev2() * { * InitState = InitStates.InProgress; * try * { * LoadRoamingSettings(); * Windows.Storage.ApplicationData.Current.DataChanged += RoamingDataChanged; * da = new DataAccess(); * * if (localSettings.Values["DbConnString"] != null && localSettings.Values["CardConnString"] != null) * { * DbConnectionString = localSettings.Values["DbConnString"].ToString(); * CardConnString = localSettings.Values["CardConnString"].ToString(); * if (da.TestConnString(DbConnectionString) == true && da.TestConnString(CardConnString) == true) * { * da.StoreConnStrings(DbConnectionString, CardConnString); * op = new Operations(DbConnectionString, CardConnString); * * var selLocation = localSettings.Values["selectedLocation"]; * * if (selLocation != null) * { * string selectedLocation = localSettings.Values["selectedLocation"].ToString(); * * appLocation = await da.GetLocationById(selectedLocation); * if (appLocation != null && appLocation.isDisabled != true) * { * MinHours = await da.GetMinHours(); * * // Gets the default user group for students or trainee * string groupIdString = await da.GetGlobalSetting("DefaultTraineeGroup"); * int.TryParse(groupIdString, out int groupID); * defaultStudentGroup = await da.GetUserGroupById(groupID); * * // Gets the default user gorup for all other users * groupIdString = await da.GetGlobalSetting("DefaultGroup"); * int.TryParse(groupIdString, out groupID); * defaultOtherGroup = await da.GetUserGroupById(groupID); * * InitState = InitStates.Success; * return; * } * } * } * else * { * // Clears the saved connection string as it is invalid. * localSettings.Values["DbConnString"] = null; * localSettings.Values["CardConnString"] = null; * } * } * InitState = InitStates.Completed; * return; * } * catch (Exception) * { * InitState = InitStates.Failed; * } * } */ #endregion public async Task <bool> LoadRegisteredPeople() { try { PersonGroups.Clear(); IEnumerable <PersonGroup> personGroups = await FaceServiceHelper.ListPersonGroupsAsync(this.WorkspaceKey); PersonGroups.AddRange(personGroups.OrderBy(pg => pg.Name)); CurrentPersonGroup = personGroups.FirstOrDefault(); PersonsInCurrentGroup.Clear(); IList <Person> personsInGroup = await FaceServiceHelper.GetPersonsAsync(this.CurrentPersonGroup.PersonGroupId); foreach (Person person in personsInGroup.OrderBy(p => p.Name)) { this.PersonsInCurrentGroup.Add(person); } return(true); } catch (Exception ex) { await Util.GenericApiCallExceptionHandler(ex, "Failure loading Person Groups"); return(false); } }
/// <summary> /// Identify faces and persons inside a picture /// </summary> /// <param name="testImageFile"></param> /// <param name="onlyDetect"></param> /// <returns></returns> public async Task <FaceMessage> Identify(string testImageFile, bool onlyDetect = true) { //create return message FaceMessage message = new FaceMessage { ID = Guid.NewGuid(), Data = DateTime.Now, File = testImageFile, OnlyDetect = onlyDetect }; LogMessage($"Detecting Face: {testImageFile}"); //try read image for (int i = 0; i < 5; i++) { try { //read image using (Stream s = File.OpenRead(testImageFile)) { //detec faces message.DetectedFaces.AddRange( await faceClient.Face.DetectWithStreamAsync( s, true, false, FaceAttributes)); } } catch (Exception) { //probably lock problem with the image, so wait some time Thread.Sleep(100); } finally { //quit the loop i = 10; } } //Only detect faces if (onlyDetect || message.DetectedFaces.Count == 0) { return(message); } //get face id var faceIds = message.DetectedFaces.Select(face => (Guid)(face.FaceId ?? Guid.Empty)); //Get interest groups if (PersonGroups.Count == 0) { PersonGroups.AddRange(await faceClient.PersonGroup.ListAsync()); } //get id as list var ids = faceIds.ToList(); //look at groups foreach (var personGroup in PersonGroups) { try { LogMessage($"Group: {personGroup.Name}"); //look person groups message.IdentifyResults.AddRange(await faceClient.Face.IdentifyAsync(ids, personGroup.PersonGroupId)); //Lok identify results foreach (var identifyResult in message.IdentifyResults) { LogMessage($" Face: {identifyResult.FaceId}"); //look candidates foreach (var candidate in identifyResult.Candidates) { LogMessage($" Candidate({candidate.Confidence}): {candidate.PersonId}"); //try find person var person = await faceClient.PersonGroupPerson.GetAsync(personGroup.PersonGroupId, candidate.PersonId); //dont exist if (person == null) { continue; } //create person inside group return message.CandidateGroups.Add(new CandidateGroup { FaceID = identifyResult.FaceId, PersonGroupId = personGroup.PersonGroupId, PersonGroupName = personGroup.Name, PersonId = person.PersonId, PersonName = person.Name }); LogMessage($" Person: {person.Name} ({personGroup.Name})"); } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } return(message); }
/// <summary> /// Train model based on directory structure /// </summary> /// <param name="path"></param> public async Task TrainModel(string path) { //Clear the person group PersonGroups.Clear(); //check directory if (!Directory.Exists(path)) { throw new ArgumentException($"Trainer Group path does not exist: {path}"); } LogMessage($"Reading: {path}"); //find groups foreach (var strPersonGroup in Directory.GetDirectories(path)) { //Get group name var personGroupName = GetPathName(strPersonGroup); LogMessage($" Creating Group: {personGroupName}"); //try fing group var personGroup = (await faceClient.PersonGroup.ListAsync()).FirstOrDefault(g => g.Name == personGroupName); var personGroupId = String.Empty; //grouo not exists if (personGroup == null) { //create ID personGroupId = GetPathID(strPersonGroup); //Create person Group await faceClient.PersonGroup.CreateAsync(personGroupId, personGroupName); } else { //asign group id personGroupId = personGroup.PersonGroupId; } //Get Persons foreach (var strPerson in Directory.GetDirectories(strPersonGroup)) { //Get persons name var personName = GetPathName(strPerson); LogMessage($" Creating Person: {personName}"); //try get person Person person = (await faceClient.PersonGroupPerson.ListAsync(personGroupId)).FirstOrDefault(p => p.Name == personName); //create if not exists if (person == null) { person = await faceClient.PersonGroupPerson.CreateAsync(personGroupId, personName); } //max 0f 10 faces if (person.PersistedFaceIds != null && person.PersistedFaceIds.Count > 10) { continue; } //Get Faces foreach (string imagePath in Directory.GetFiles(strPerson, "*.jpg")) { LogMessage($" Creating Face: {imagePath}"); //read face using (Stream s = File.OpenRead(imagePath)) { //add do person await faceClient.PersonGroupPerson.AddFaceFromStreamAsync(personGroupId, person.PersonId, s); } } } LogMessage($" Treaining Group: {personGroupId}"); //train model await faceClient.PersonGroup.TrainAsync(personGroupId); } PersonGroups.Clear(); PersonGroups.AddRange(await faceClient.PersonGroup.ListAsync()); }