コード例 #1
0
        private void UserListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (UserListBox.SelectedIndex == -1)
            {
                return;
            }
            int UserId       = UserTable.Keys.ToArray()[UserListBox.SelectedIndex];
            var UserFaceData = FaceData
                               .Where(x => x.ForeignKey == UserId)
                               .ToArray();

            FaceImageList.Images.Clear();
            FaceImageList.Images.AddRange(UserFaceData.Select(x =>
                                                              x.Image
                                                              ).ToArray());

            UserFaceListView.Items.Clear();
            for (int i = 0; i < UserFaceData.Count(); i++)
            {
                UserFaceListView.Items.Add(new ListViewItem()
                {
                    ImageIndex = i,
                    Text       = UserFaceData[i].PrimaryKey.ToString()
                });
            }
        }
コード例 #2
0
        private void DeleteImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (UserFaceListView.SelectedItems.Count == 0)
            {
                return;
            }
            foreach (var item in UserFaceListView.SelectedItems)
            {
                var id = int.Parse(((ListViewItem)item).Text);
                FaceData = FaceData.Where(x => x.PrimaryKey != id)
                           .ToArray();
            }
            FaceData = FaceData.Select((x, i) => {
                x.PrimaryKey = i;
                return(x);
            }).ToArray();

            if (realSenseProgram.recognitionConfig != null)
            {
                realSenseProgram.recognitionConfig.SetDatabase(FaceData);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }

            UserListBox_SelectedIndexChanged(null, null);
        }
コード例 #3
0
        private void DeleteUserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (UserListBox.SelectedIndex == -1)
            {
                return;
            }
            if (MessageBox.Show(
                    "您確定刪除該使用者所有的臉部辨識資料嗎?",
                    "刪除使用者", MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question
                    ) != DialogResult.Yes)
            {
                return;
            }
            var key = UserTable.Keys.ToArray()[UserListBox.SelectedIndex];

            UserTable.Remove(key);
            FaceData = FaceData.Where(x => x.ForeignKey != key).ToArray();

            if (realSenseProgram.recognitionConfig != null)
            {
                realSenseProgram.recognitionConfig.SetDatabase(FaceData);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }
            tabControl1_SelectedIndexChanged(null, null);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        private void ExportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var folder = new FolderBrowserDialog();

            if (folder.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            //realSenseProgram.Paush();

            var path = folder.SelectedPath + $"\\Database-{DateTime.UtcNow.ToString("yyyyMMdd_HHmmss")}";

            Directory.CreateDirectory(path);
            byte[] userTable = FaceDatabaseFile.UserTableToCSVBinary(UserTable);

            FileStream   userTableFile       = new FileStream(path + "\\UserTable.csv", FileMode.Create);
            BinaryWriter userTableFileWriter = new BinaryWriter(userTableFile);

            userTableFileWriter.Write(userTable);
            userTableFileWriter.Flush();
            userTableFileWriter.Close();
            userTableFile.Close();

            foreach (var user in UserTable.Keys)
            {
                var path2 = path + "\\" + user;
                Directory.CreateDirectory(path2);
                var userFaces = FaceData.Where(x => x.ForeignKey == user);
                foreach (var face in userFaces)
                {
                    face.Image.Save(path2 + "\\" + face.PrimaryKey + ".jpg");
                }
            }
            MessageBox.Show(
                "資料庫已成功匯出至指定目錄",
                "匯出成功",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
            //realSenseProgram.UnPaush();
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        private void registerButton_Click(object sender, EventArgs e)
        {
            CurentDataLocked = true;

            realSenseProgram.Paush();//暫停影像更新

            //按下註冊瞬間的名稱
            var onClickName  = CurrentName;
            var registerForm = new RegisterForm()
            {
                UserName = onClickName,
                Picture  = FaceImage
            };

            if (registerForm.ShowDialog() != DialogResult.OK)
            {
                realSenseProgram.UnPaush();
                return;
            }

            int?registedUserId = null;

            if (UserTable.ContainsValue(registerForm.UserName))
            {
                //重複項目,選取使用者後registedUserId
                Dictionary <int, Image> mapping = new Dictionary <int, Image>();
                var userIds = UserTable.Where(x => x.Value == registerForm.UserName).Select(x => x.Key).ToArray();
                for (int i = 0; i < userIds.Length; i++)
                {
                    mapping[userIds[i]] =
                        FaceData.Where(x =>
                                       x.ForeignKey == userIds[i]).FirstOrDefault()?.Image;
                    if (mapping[userIds[i]] == null)
                    {
                        mapping[userIds[i]] = new Bitmap(128, 128);
                        using (Graphics g = Graphics.FromImage(mapping[userIds[i]])) {
                            g.DrawString("找不到圖片", new Font("Arial", 16), Brushes.Black, 0, 0);
                        }
                    }
                }

                var dupForm = new DuplicateUserForm()
                {
                    IdFaceMapping = mapping
                };

                if (dupForm.ShowDialog() == DialogResult.OK)
                {
                    registedUserId = dupForm.UserId;
                }
            }

            //註冊並寫入資料庫
            var userId = Current.RegisterUser();

            if (userId == -1)
            {
                MessageBox.Show(
                    "註冊過程出現異常,請再嘗試一次",
                    "註冊失敗", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                CurentDataLocked = false;
                realSenseProgram.UnPaush();
                return;
            }

            //取得資料庫緩衝區
            var buffer =
                CurrentData.QueryRecognitionModule()
                .GetDatabaseBuffer();

            FaceData = RecognitionFaceData
                       .FromDatabaseBuffer(buffer);

            //有選擇使用者,但是註冊後ID不合,修正ID
            if (registedUserId.HasValue && userId != registedUserId.Value)
            {
                var userItem = FaceData.Last();
                userItem.ForeignKey = registedUserId.Value;
                userId = userItem.ForeignKey;
                FaceDatabaseManager.UpdateBuffer(buffer, userItem);
                //FaceDatabaseManager.ClearRemovedBuffer(buffer);

                realSenseProgram.recognitionConfig
                .SetDatabaseBuffer(buffer);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }
            else if (!registedUserId.HasValue && UserTable.ContainsKey(userId))
            {
                //新建使用者,但是UserID指向了現有使用者
                var userItem = FaceData.Last();
                //新建使用者ID
                userItem.ForeignKey = UserTable.ToArray().Select(x => x.Key).Max() + 1;
                userId = userItem.ForeignKey;
                FaceDatabaseManager.UpdateBuffer(buffer, userItem);

                //FaceDatabaseManager.ClearRemovedBuffer(buffer);
                realSenseProgram.recognitionConfig
                .SetDatabaseBuffer(buffer);
                realSenseProgram.moduleConfiguration.ApplyChanges();
            }

            UserTable[userId] = registerForm.UserName;

            realSenseProgram.UnPaush();//暫停影像更新
            CurentDataLocked = false;
        }