/// <summary> /// Add person info in person table /// </summary> /// <param name="info"></param> /// <param name="photo_id"></param> private void add_person(IdentifyInfos info, int photo_id) { // Open database (or create if doesn't exist) using (var db = new LiteDatabase(connection_string)) { // Get a collection (or create, if doesn't exist) var persons = db.GetCollection <TPerson>("persons"); int face = 0; foreach (KeyValuePair <string, string> p in info.person) { var person = new TPerson { Name = get_name(p.Value), Gender = info.faces[face].FaceAttributes.Gender, Smile = get_smile(info.faces[face].FaceAttributes.Smile), Age = Convert.ToInt32(info.faces[face].FaceAttributes.Age), Emotion = get_emotion(info.faces[face].FaceAttributes.Emotion), Photo_id = photo_id }; // Insert new person in DB (Id will be auto-incremented) persons.Insert(person); face++; } } }
private async Task identify_async() { // List of photos infos List <IdentifyInfos> infos_list = new List <IdentifyInfos>(); // Get current group id string person_group_id = conf.read_group(); // Add person for identification if (await add_person_async(person_group_id)) { // Get all photos from imagelistview foreach (ImageListViewItem item in photos_to_identify.Items) { // Check if file existe if (File.Exists(item.FileName)) { // PROGRESS update_progress("Identify photo", $"Process photo: {Path.GetFileName(item.FileName).ToString()}", ++progress); /*// Get file size (Image file size: Less than 4 MB) * if (new FileInfo(item.FileName).Length >= 4000000) { * continue; * } * * // Get image dimention (Image dimension: Greater than 50 x 50 pixels.) * Image img = null; * img = Image.FromFile(item.FileName); * if ((img.Width > 50) && (img.Height > 50)) { * continue; * }*/ // Identify person in the photos IdentifyInfos ii = await identify_person_async(item.FileName, person_group_id); if (ii != null) { infos_list.Add(ii); } } } // Add infos to db if (!add_photos_db(infos_list)) { //TODO ERROR MessageBox.Show("DB error"); } // Add infos to db and copy photos to identify dir //if(!add_photos(infos_list)) { //TODO ERROR //} } }
/// <summary> /// Add person info in image table /// </summary> /// <param name="info"></param> /// <param name="photo_idv"></param> private void add_image(IdentifyInfos info, int photo_id) { // Open database (or create if doesn't exist) using (var db = new LiteDatabase(connection_string)) { // Get a collection (or create, if doesn't exist) var images = db.GetCollection <TImage>("images"); // Get all tag foreach (string tag in info.info.Description.Tags) { var image = new TImage { Tag = tag, Photo_id = photo_id }; // Insert new image in DB (Id will be auto-incremented) images.Insert(image); } } }
private int add_photo(IdentifyInfos info, int photo_id, string hash) { // Open database (or create if doesn't exist) using (var db = new LiteDatabase(connection_string)) { // Get a collection (or create, if doesn't exist) var photos = db.GetCollection <TPhoto>("photos"); // Create your new Photo instance //Path = info.path, ////////////////////// var photo = new TPhoto { Hash = hash, Name = Path.GetFileName(info.path), Path = get_custom_path(info.path), Width = info.info.Metadata.Width, Height = info.info.Metadata.Height, Date = DateTime.Now }; // Insert new photo in DB (Id will be auto-incremented) and get inserted Id photo_id = photos.Insert(photo).AsInt32; } return(photo_id); }
private async Task <IdentifyInfos> identify_person_async(string path, string person_group_id) { Debug.WriteLine($"Identify photo: {path}"); //TODO // Init class IdentifyInfos infos = new IdentifyInfos { person = new Dictionary <string, string>(), info = new AnalysisResult() }; //Get current photo infos.path = path; // The list of Face attributes to return. IEnumerable <FaceAttributeType> faceAttributes = new FaceAttributeType[] { FaceAttributeType.Gender, FaceAttributeType.Age, FaceAttributeType.Smile, FaceAttributeType.Emotion, FaceAttributeType.Glasses, FaceAttributeType.Hair, FaceAttributeType.Accessories, FaceAttributeType.Blur, FaceAttributeType.Exposure, FaceAttributeType.FacialHair, FaceAttributeType.HeadPose, FaceAttributeType.Makeup, FaceAttributeType.Noise, FaceAttributeType.Occlusion }; try { // Read file using (Stream stream = File.OpenRead(path)) { infos.faces = await face_service_client.DetectAsync(stream, returnFaceId : true, returnFaceLandmarks : false, returnFaceAttributes : faceAttributes); // If face identified in the photo if (infos.faces.Length != 0) { Guid[] faceIds = infos.faces.Select(face => face.FaceId).ToArray(); IdentifyResult[] results = await face_service_client.IdentifyAsync(person_group_id, faceIds); // Process face detected foreach (var identifyResult in results) { Debug.WriteLine("Result of face: {0}", identifyResult.FaceId); if (identifyResult.Candidates.Length == 0) { Debug.WriteLine("No one identified"); infos.person.Add(identifyResult.FaceId.ToString(), "not identified"); } else { // Get top 1 among all candidates returned var candidateId = identifyResult.Candidates[0].PersonId; var person = await face_service_client.GetPersonAsync(person_group_id, candidateId); Debug.WriteLine("Identified as {0}", person.Name); infos.person.Add(identifyResult.FaceId.ToString(), person.Name.ToString()); } } } else { // No face identified } } // If error during person identify //if(infos != null) { // If error during person identify or no person identified if (infos.faces.Length != 0) { //Identify things in the photo infos.info = await identify_image_async(path); } // If error during photo identify if (infos.info.ImageType != null) { return(infos); } else { return(null); } } catch (Exception ex) { infos = null; return(null); } // MessageBox.Show(ex.ToString()); }