// 恢复上次状态 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 <TestVideo> testVideoList = new List <TestVideo>(); // 测试视频(视频名,所在路径) /************************************************************************************/ // 默认构造 public MainWindow() { InitializeComponent(); // 数据绑定 DataContext = alarmImageList; SceneComBox.ItemsSource = Scenes; IncidentComBox.ItemsSource = Incidents; ImageTreeView.ItemsSource = fileTree; // 元数据 if (File.Exists("./MetaData.xml")) { XmlDocument doc = new XmlDocument(); XmlReaderSettings settings = new XmlReaderSettings { IgnoreComments = true }; // 忽略文档里面的注释 XmlReader reader = XmlReader.Create("./MetaData.xml", settings); doc.Load(reader); XmlNode xn = doc.SelectSingleNode("root"); // 根节点 XmlNodeList xnl = xn.ChildNodes; // 根节点的子节点 Scenes.Clear(); Incidents.Clear(); foreach (XmlNode node in xnl) { if (node.Name == "scene") // 场景下拉列表 { XmlNodeList subNodes = node.ChildNodes; foreach (XmlNode item in subNodes) { if (Scenes.Count(f => f.Name == item.Attributes["tag"].Value) == 0) { Scenes.Add(new SceneItem { Display = item.InnerText, Name = item.Attributes["tag"].Value }); } } } if (node.Name == "incident") // 事件下拉列表 { XmlNodeList subNodes = node.ChildNodes; foreach (XmlNode item in subNodes) { if (Incidents.Count(f => f.Name == item.Attributes["tag"].Value) == 0) { Incidents.Add(new IncidentItem { Display = item.InnerText, Name = item.Attributes["tag"].Value }); } } } } reader.Close(); } else { MessageWindow.ShowDialog("Can not read MetaData.xml", this); } // 删除临时数据 if (Directory.Exists("./temp")) { Directory.Delete("./temp", true); } // 恢复上次程序退出前状态 Resume(); }
private void InitDataBase(string alarmImagePath, string videoPath, string xmlFile) { dbConnection?.Close(); dbConnection = null; // 删除旧文件 if (File.Exists("./data.db")) { string ex = DateTime.Now.ToString("yyyyMMddHHmmss"); FileInfo fi = new FileInfo("./data.db"); try { fi.MoveTo("./data.db." + ex, MoveOptions.ReplaceExisting); } catch (IOException e) { MessageWindow.ShowDialog(e.ToString(), this); return; } } // 创建数据表 CreateBlankTabs(); /********************* DataPathTab 数据更新 ********************/ SQLiteCommand cmd = new SQLiteCommand(); cmd.Connection = dbConnection; cmd.CommandText = "BEGIN"; cmd.ExecuteNonQuery(); cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('AlarmImagePath', '{alarmImagePath}')"); cmd.ExecuteNonQuery(); cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('VideoPath', '{videoPath}')"); cmd.ExecuteNonQuery(); cmd.CommandText = string.Format($"INSERT INTO DataPathTab(Item, Path) VALUES ('VideoInfoFilePath', '{xmlFile}')"); cmd.ExecuteNonQuery(); cmd.CommandText = "COMMIT"; cmd.ExecuteNonQuery(); /********************* AlarmImageTab 数据更新 ********************/ cmd.CommandText = "BEGIN"; cmd.ExecuteNonQuery(); foreach (var item in alarmImageListAll) { 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(); } cmd.CommandText = "COMMIT"; cmd.ExecuteNonQuery(); /************************** TestVideoTab 数据更新 ************************************/ cmd.CommandText = "BEGIN"; cmd.ExecuteNonQuery(); foreach (var item in testVideoList) { cmd.CommandText = string.Format($"INSERT INTO TestVideoTab(VideoName, Directory) VALUES ('{item.VideoName}', '{item.VideoPath}')"); cmd.ExecuteNonQuery(); } cmd.CommandText = "COMMIT"; cmd.ExecuteNonQuery(); /************************** MarkVideoTab 数据更新 ************************************/ cmd.CommandText = "BEGIN"; cmd.ExecuteNonQuery(); foreach (var item in videoInfoList) { cmd.CommandText = string.Format($"INSERT INTO VideoInfoTab(Scene, Video, Incident, Count) " + $"VALUES ('{item.Scene}', '{item.VideoName}', '{item.Incident}', '{item.Count}')"); cmd.ExecuteNonQuery(); } cmd.CommandText = "COMMIT"; cmd.ExecuteNonQuery(); }
// 解析告警图片文件夹 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 AboutInfo(object sender, RoutedEventArgs e) { MessageWindow.ShowDialog(_aboutInfo, this); }
// 合并数据 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); }