Esempio n. 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()
                });
            }
        }
Esempio n. 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);
        }
Esempio n. 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);
        }
Esempio n. 4
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();
        }
Esempio n. 5
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;
        }