private FaceAPI.Contract.Face CreateFace(FaceAPI.Contract.FaceRectangle rect) { return(new FaceAPI.Contract.Face { FaceRectangle = new FaceAPI.Contract.FaceRectangle { Left = rect.Left, Top = rect.Top, Width = rect.Width, Height = rect.Height } }); }
public Image CropImage(Image img, Microsoft.ProjectOxford.Face.Contract.FaceRectangle rect) { var croppedImg = new CroppedBitmap(ConvertImageToBitmapImage(img), new Int32Rect(rect.Left, rect.Top, rect.Width, rect.Height)); return(ConvertBitmapToImage(ConvertCroppedBitmapToBitmap(croppedImg))); }
/// <summary> /// Event handlers for the Data Grid View "Images In DB" /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="Column"></param> /// <param name="DataGridViewColumns"></param> /// <exception cref="Facial_Recognition_Library.Controls.DataGridViewsFaceDetectException">This is thrown with an image contains more than one face. We cannot register a face when more than one is within the image.</exception> /// <returns></returns> public static DataGridViewsProcessResult DgvImagesInDbAsync(ref object sender, DataGridViewCellEventArgs e, DataGridViewColumn Column, params DataGridViewColumn[] DataGridViewColumns) { throw new NotImplementedException("The ASYNC methods have not been compeleted. Please review and remove this exception before use."); DataGridViewsProcessResult methodResult = new DataGridViewsProcessResult(); DataGridView dgvImagesInDb = (DataGridView)sender; if (Column != null) { DataGridViewColumn ResendToDetect = Column; DataGridViewColumn dgvBtnAddToCurrentFaceList = Column; DataGridViewColumn SystemIDdgvtxtColumn = null; DataGridViewColumn faceRectangleDataGridViewTextBoxColumn = null; DataGridViewColumn imageBytesDataGridViewImageColumn = null; bool GridViewColumnsInitialized = false; if (DataGridViewColumns?.Length > 0) { try { SystemIDdgvtxtColumn = DataGridViewColumns.First(dgvc => dgvc.Name == "SystemIDdgvtxtColumn"); faceRectangleDataGridViewTextBoxColumn = DataGridViewColumns.First(dgvc => dgvc.Name == "faceRectangleDataGridViewTextBoxColumn"); imageBytesDataGridViewImageColumn = DataGridViewColumns.First(dgvc => dgvc.Name == "imageBytesDataGridViewImageColumn"); GridViewColumnsInitialized = true; } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); System.Diagnostics.Debug.WriteLine($"Failed to retrieve the columns for data retrieval."); methodResult.Success = false; } } else { SystemIDdgvtxtColumn = dgvImagesInDb.Columns.OfType <DataGridViewColumn>().First(dgvc => dgvc.Name == "SystemIdDataGridViewTextBoxColumn"); faceRectangleDataGridViewTextBoxColumn = dgvImagesInDb.Columns.OfType <DataGridViewColumn>().First(dgvc => dgvc.Name == "rectangleDataGridViewTextBoxColumn"); imageBytesDataGridViewImageColumn = dgvImagesInDb.Columns.OfType <DataGridViewColumn>().First(dgvc => dgvc.Name == "dataGridViewImageColumn2"); } //Providing this style of Index lookup allows the Columns to be moved around. so the function stays the same if (e.ColumnIndex == dgvImagesInDb.Columns[ResendToDetect.Name].Index) { System.Diagnostics.Debug.WriteLine($"Row index is {e.RowIndex}"); Guid FaceSystemID = new Guid(dgvImagesInDb[SystemIDdgvtxtColumn.Index, e.RowIndex].Value.ToString()); string Rectangle = dgvImagesInDb[faceRectangleDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString(); var ImageData = dgvImagesInDb[imageBytesDataGridViewImageColumn.Index, e.RowIndex].Value; if (ImageData is byte[] ImageArray)//if ImageData is not Null. { string FaceListID = string.Empty; Task.Run(async() => { using (var db = new LiveEduFaceModel()) { Microsoft.ProjectOxford.Face.Contract.Face[] FaceDetect = null; FaceDetect = await Facial_Recognition_Library.API.ProjectOxfordAPI.DetectFace(new MemoryStream(ImageArray)); if (FaceDetect.Length > 1) { //Output the image to a temporary storage and show it to the end user WITH the Rectangles on it, this way the user can SEE the faces detected. throw new Facial_Recognition_Library.Controls.DataGridViewsFaceDetectException($@"More than one face found in this image. Please review image in temp folder {Facial_Recognition_Library.IO.FileSystemManager.TempImageLocationForErrors} "); } var FaceToUpdate = db.MyFaces.Where((f) => f.ID == FaceSystemID); foreach (var face in FaceToUpdate) { face.FaceID = FaceDetect[0].FaceId; face.DateLastDetected = DateTime.Now; } db.SaveChanges(); methodResult.Success = true; methodResult.ShouldRefreshDatabaseDataGrids = true; return(methodResult); } }); } else { System.Diagnostics.Debug.WriteLine($"Image Array was empty. You must ensure that there is an image with which to send. (cannot send 0 bytes)"); methodResult.Success = false; } return(methodResult); } else if (e.ColumnIndex == dgvImagesInDb.Columns[dgvBtnAddToCurrentFaceList.Name].Index) { System.Diagnostics.Debug.WriteLine($"Row index is {e.RowIndex}"); if (GridViewColumnsInitialized) { //TODO: Move this to the libary : dgvImagesInDb_CellClick : dgvImagesInDb.Columns[dgvBtnAddToCurrentFaceList.Name].Index Guid FaceSystemID = new Guid(dgvImagesInDb[SystemIDdgvtxtColumn.Index, e.RowIndex].Value.ToString()); string Rectangle = dgvImagesInDb[faceRectangleDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString(); var ImageData = dgvImagesInDb[imageBytesDataGridViewImageColumn.Index, e.RowIndex].Value; byte[] ImageArray = ImageData as byte[]; if (ImageArray != null) { string FaceListID = string.Empty; using (var db = new LiveEduFaceModel()) { FaceListID = db.MyFaceLists.First().FaceListId; Microsoft.ProjectOxford.Face.Contract.FaceRectangle faceRectangle = Newtonsoft.Json.JsonConvert.DeserializeObject <Microsoft.ProjectOxford.Face.Contract.FaceRectangle>(Rectangle); var FaceListFacePersistantID = Facial_Recognition_Library.API.ProjectOxfordAPI.AddFaceToFaceListByFaceListID(FaceListID, new MemoryStream(ImageArray), null, faceRectangle).Result; var FaceToUpdate = db.MyFaces.Where((f) => f.ID == FaceSystemID); foreach (var face in FaceToUpdate) { face.FaceListPersistantID = FaceListFacePersistantID; face.DateLastDetected = DateTime.Now; } db.SaveChanges(); } } else { System.Diagnostics.Debug.WriteLine($"Image Array was empty. You must ensure that there is an image with which to send. (cannot send 0 bytes)"); methodResult.Success = false; } } else { System.Diagnostics.Debug.WriteLine($"Columns needed for lookup failed to retrieve, check the execptions list."); } return(methodResult); } } else { return(methodResult); } return(null); }