private void AddImageToolStripMenuItem_Click(object sender, EventArgs e) { if (UserListBox.SelectedIndex == -1) { return; } var open = new OpenFileDialog() { FileName = "", Multiselect = true, Filter = "JPEG Files(*.jpg)|*.jpg|" + "PNG Files(*.png)|*.png|" + "BMP Files(*.bmp)|*.bmp|" + "所有圖片類型|*.jpg;*.png;*.bmp" }; if (open.ShowDialog() != DialogResult.OK) { return; } var key = UserTable.Keys.ToArray()[UserListBox.SelectedIndex]; foreach (var file in open.FileNames) { var newData = new RecognitionFaceData(); newData.PrimaryKey = FaceData.Count(); newData.ForeignKey = key; newData.Image = new Bitmap(new Bitmap(file), 128, 128); var faceData = FaceData; Array.Resize(ref faceData, FaceData.Length + 1); faceData[faceData.Length - 1] = newData; FaceData = faceData; } if (realSenseProgram.recognitionConfig != null) { realSenseProgram.recognitionConfig.SetDatabase(FaceData); realSenseProgram.moduleConfiguration.ApplyChanges(); } UserListBox_SelectedIndexChanged(null, null); }
private void ImportToolStripMenuItem_Click(object sender, EventArgs e) { var folder = new FolderBrowserDialog(); if (folder.ShowDialog() != DialogResult.OK) { return; } var path = folder.SelectedPath; int temp_ = 0;//無用變數 var subDirs = new DirectoryInfo(path).EnumerateDirectories() .Where(x => int.TryParse(x.Name, out temp_)); #region UserTable讀取 var userTablePath = path + $"\\UserTable.csv"; Dictionary <int, string> userTable = new Dictionary <int, string>(); if (!File.Exists(userTablePath)) { var importUserTable = MessageBox.Show( "找不到UserTable.csv檔案,該檔案用以將使用者ID與姓名對應," + "該檔案雖非必要,但姓名將會使用ID作為代替,您是否繼續匯入?", "匯入時發現問題", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (importUserTable != DialogResult.Yes) { return; } foreach (var dir in subDirs) { userTable[int.Parse(dir.Name)] = dir.Name; } } else { var fileStream = new FileStream(userTablePath, FileMode.Open); userTable = FaceDatabaseFile.CSVBinaryToUserTable( FaceDatabaseFile.StreamToBytes(fileStream)); } #endregion #region 照片讀取 List <RecognitionFaceData> faceData = new List <RecognitionFaceData>(); int IdStart = 0; if (FaceData.Count() > 0) { IdStart = FaceData.Select(x => x.PrimaryKey).Max() + 1; } foreach (var dir in subDirs) { foreach (var file in dir.EnumerateFiles()) { var faceImage = new RecognitionFaceData(); faceImage.PrimaryKey = IdStart++; faceImage.ForeignKey = int.Parse(dir.Name); faceImage.Image = new Bitmap(file.FullName); faceData.Add(faceImage); } } if (FaceData == null || FaceData.Count() == 0) { FaceData = faceData.ToArray(); } else { FaceData = FaceData.Union(faceData).ToArray(); } if (UserTable == null || UserTable.Count == 0) { UserTable = userTable; } else { var TEMP = UserTable.Union(userTable).ToArray(); foreach (var keyvalue in TEMP) { UserTable[keyvalue.Key] = keyvalue.Value; } } SaveFileToolStripMenuItem.Enabled = true; if (realSenseProgram.recognitionConfig != null) { realSenseProgram.recognitionConfig.SetDatabase(FaceData); realSenseProgram.moduleConfiguration.ApplyChanges(); } tabControl1_SelectedIndexChanged(null, null); #endregion MessageBox.Show( "已成功自指定目錄讀取資料", "匯入成功", MessageBoxButtons.OK, MessageBoxIcon.Information); }