// 告警图片校验 private string Validation(AlarmImage alarmImage) { if (alarmImage.State == DetectType.UnKnown && videoInfoList.Count(f => f.VideoName == alarmImage.Video) == 0) { return("该图片不在应统计视频范围内"); } return(null); }
// 更新统计结果 public void UpdateAlarmImageItem(AlarmImage item) { var _list = alarmImageData.Where(f => Path.GetFileName(f.ImagePath) == Path.GetFileName(item.ImagePath)).ToList(); if (_list.Count == 0) { alarmImageData.Add(item); } else { _list[0] = item; } }
// 更新数据 private void UpdateData(AlarmImage item) { // 更新数据库 if (dbConnection != null) { SQLiteCommand cmd = new SQLiteCommand { Connection = dbConnection, CommandText = string.Format($"UPDATE AlarmImageTab SET scene = '{item.Scene}', incident = '{item.Incident}', state = '{(int)item.State}', count = '{item.IncidentCount}' " + $"WHERE image = '{CurrentAlarmImage().ImagePath}'") }; cmd.ExecuteNonQuery(); } // 更新UI数据 dataWindow.UpdateAlarmImageItem(CurrentAlarmImage()); /*********************** 统计结果(详细数据) ********************/ dataWindow.SetDetailData(GetDetailData()); }
// 添加告警图片 public void AddAlarmImageItem(AlarmImage item) { alarmImageData.Add(item); }
// 恢复上次状态 public void Resume() { if (!File.Exists("./data.db")) { CreateBlankTabs(); return; } string DBPath = "Data Source = " + AppDomain.CurrentDomain.BaseDirectory + @"data.db"; dbConnection = new SQLiteConnection(DBPath); if (dbConnection == null) { MessageWindow.ShowDialog("无法连接到数据库 data.db", this); return; } dbConnection.Open(); SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = dbConnection; // 连接数据库 // 标注视频 cmd.CommandText = "SELECT * FROM VideoInfoTab"; using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { // VideoDataTab(Scene, Video, Incident, Count) string _scene = dataReader.GetString(0); string _video = dataReader.GetString(1); string _incident = dataReader.GetString(2); int _count = dataReader.GetInt32(3); var _videoInfoItem = new VideoInfo { Scene = _scene, VideoName = _video, Incident = _incident, Count = _count }; videoInfoList.Add(_videoInfoItem); } } // 告警图片 cmd.CommandText = "SELECT * FROM AlarmImageTab"; using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { // (id INTEGER, image VARCHAR(512) PRIMARY KEY, scene VARCHAR(64), incident VARCHAR(64), video VARCHAR(256), frame INTEGER, state INTEGER) int _id = dataReader.GetInt32(0); string _imgPath = dataReader.GetString(1); string _scene = dataReader.GetString(2); string _incident = dataReader.GetString(3); string _videoName = dataReader.GetString(4); int _frame = dataReader.GetInt32(5); int _state = dataReader.GetInt32(6); int _count = dataReader.GetInt32(7); var _alarmDataItem = new AlarmImage { ID = _id, ImagePath = _imgPath, Video = _videoName, Scene = _scene, Incident = _incident, Frame = _frame, State = (DetectType)_state, IncidentCount = _count }; alarmImageListAll.Add(_alarmDataItem); dataWindow.AddAlarmImageItem(_alarmDataItem); } } // 测试视频 cmd.CommandText = "SELECT * FROM TestVideoTab"; using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { // (VideoName VARCHAR(256) PRIMARY KEY, Directory VARCHAR(256)) string _videoName = dataReader.GetString(0); string _videoPath = dataReader.GetString(1); var _testVideoItem = new TestVideo { VideoName = _videoName, VideoPath = _videoPath }; testVideoList.Add(_testVideoItem); dataWindow.AddTestVideoItem(_testVideoItem); } } // 告警图片路径 cmd.CommandText = "SELECT * FROM DataPathTab WHERE Item = 'AlarmImagePath'"; using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { if (dataReader.Read()) { string _imagePath = dataReader.GetString(1); if (Directory.Exists(_imagePath)) { fileTree.Clear(); fileTree.Add(new FileRecord { Info = new DirAndFileInfo { FullName = _imagePath } }); } else { MessageWindow.ShowDialog($"无法访问告警图片路径 [{_imagePath}]"); } } } // 上一次统计的告警图片路径 cmd.CommandText = "SELECT * FROM DataPathTab WHERE Item = 'LastImagePath'"; using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { if (dataReader.Read()) { string _dir = dataReader.GetString(1); if (Directory.Exists(_dir)) { foreach (var item in alarmImageListAll) { if (item.ImagePath.Contains(_dir)) { alarmImageList.Add(item); } } } else { MessageWindow.ShowDialog($"无法访问告警图片路径 [{_dir}]"); } } } this.Title = $"[{1}/{alarmImageList.Count}] {Path.GetFileName(CurrentAlarmImage()?.ImagePath)}"; SetSelectedSceneItem(CurrentAlarmImage()?.Scene); SetSelectedIncidentItem(CurrentAlarmImage()?.Incident); // 更细统计数据 dataWindow.SetDetailData(GetDetailData()); }
// 解析告警图片文件夹 private List <AlarmImage> ParseAlarmImage(string alarmImagePath) { if (Directory.Exists(alarmImagePath)) { List <AlarmImage> images = new List <AlarmImage>(); // 递归获取所有jpg文件 string[] alarmImages = Utility.Director(alarmImagePath).Where(f => { string ex = Path.GetExtension(f); return(ex == ".jpg" || ex == ".png" || ex == ".bmp"); }).ToArray(); // 图片命名方式: 视频名___事件_帧号.jpg int _id = 0; List <string> error_msg = new List <string>(); foreach (string img in alarmImages) { string name = Path.GetFileNameWithoutExtension(img); string[] strs = Regex.Split(name, "___", RegexOptions.IgnoreCase); if (strs.Length < 2) { //MessageWindow.Show("告警图片命名格式错误\n" + img, this); error_msg.Add(img); continue; } string[] infos = strs[1].Split('_'); string _scene; string _incident = infos[0]; string _video = strs[0]; var _list = videoInfoList.Where(f => Path.GetFileNameWithoutExtension(f.VideoName) == _video).ToList(); if (_list.Count > 0) { _video = _list[0].VideoName; _scene = _list[0].Scene; } else { _video += ".h264"; _scene = "UnKnown"; } // 场景下拉列表 if (Scenes.Count(item => item.Name == _scene) == 0) { Scenes.Add(new SceneItem { Display = _scene, Name = _scene }); } // 事件类型下拉列表 if (Incidents.Count(item => item.Name == _incident) == 0) { Incidents.Add(new IncidentItem { Display = _incident, Name = _incident }); } var _alarmDataItem = new AlarmImage { ID = _id++, ImagePath = img, Video = _video, Scene = _scene, Incident = _incident, Frame = Convert.ToInt32(infos[1]), State = DetectType.UnKnown, IncidentCount = 0 }; images.Add(_alarmDataItem); } // 错误信息 if (error_msg.Count > 0) { StringBuilder error_str = new StringBuilder("告警图片格式错误:" + "\n"); error_msg.ForEach(it => error_str.Append(it + "\n")); MessageWindow.Show(error_str.ToString()); } return(images); } else { MessageWindow.ShowDialog($"无法访问告警图片文件夹 [{alarmImagePath}]", this); } return(null); }
// 合并数据 private void MergeData(object sender, RoutedEventArgs e) { var openFileDialog = new OpenFileDialog() { Filter = "Data Base file (.db)|*.db|All files (*.*)|*.*" }; string fileName = null; if (openFileDialog.ShowDialog() == true) { fileName = openFileDialog.FileName; } if (string.IsNullOrEmpty(fileName)) { return; } string _dbPath = $"Data Source = {fileName}"; SQLiteCommand cmd = new SQLiteCommand { Connection = new SQLiteConnection(_dbPath), CommandText = "SELECT * FROM AlarmImageTab WHERE state != 0" }; cmd.Connection.Open(); List <AlarmImage> _alarmImageList = new List <AlarmImage>(); using (SQLiteDataReader dataReader = cmd.ExecuteReader()) { while (dataReader.Read()) { var _alarmDataItem = new AlarmImage { ID = dataReader.GetInt32(0), ImagePath = dataReader.GetString(1), Scene = dataReader.GetString(2), Incident = dataReader.GetString(3), Video = dataReader.GetString(4), Frame = dataReader.GetInt32(5), State = (DetectType)(dataReader.GetInt32(6)), IncidentCount = dataReader.GetInt32(7) }; _alarmImageList.Add(_alarmDataItem); } cmd.Connection.Close(); } // 合并数据 if (dbConnection == null) { MessageWindow.ShowDialog("无法连接到数据库", this); return; } cmd = new SQLiteCommand { Connection = dbConnection }; cmd.CommandText = "BEGIN"; cmd.ExecuteNonQuery(); foreach (var item in _alarmImageList) { if (item.State != DetectType.UnKnown) { var imgs = alarmImageListAll.Where(f => Path.GetFileName(f.ImagePath) == Path.GetFileName(item.ImagePath)).ToList(); if (imgs.Count == 0) // 新的告警图片 { alarmImageListAll.Add(item); // 更新数据库 cmd.CommandText = string.Format($"INSERT INTO AlarmImageTab(id, image, scene, incident, video, frame, state, count)" + $" VALUES ('{item.ID}', '{item.ImagePath}','{item.Scene}','{item.Incident}', '{item.Video}', '{item.Frame}', '{(int)item.State}', '{item.IncidentCount}')"); cmd.ExecuteNonQuery(); } else // 已存在的告警图片 { imgs[0].State = item.State; imgs[0].IncidentCount = item.IncidentCount; // 更新数据库 cmd.CommandText = string.Format($"UPDATE AlarmImageTab SET state = '{(int)item.State}', count = '{item.IncidentCount}' WHERE image = '{ imgs[0].ImagePath}'"); cmd.ExecuteNonQuery(); } dataWindow.UpdateAlarmImageItem(item); } } cmd.CommandText = "COMMIT"; cmd.ExecuteNonQuery(); // 更新数据窗口 dataWindow.SetDetailData(GetDetailData()); MessageWindow.ShowDialog("完成数据合并", this); }