private void AddAlarmToStatistics(int avgX, int yPic, float length, DetectResultStatistic detectResultStatistic) { int xPos = avgX; int yPos = serialComm.LoopCount % AlarmRecord.TotalLoopCount; AlarmRecord alarmRecord = new AlarmRecord { XPos = xPos, YPos = yPos, YPic = yPic, LatestOccurTime = DateTime.Now, CreatedTime = DateTime.Now, Length = length }; App.Current?.Dispatcher.Invoke(() => { if (Alarms.Contains(alarmRecord)) { int index = Alarms.IndexOf(alarmRecord); var alarm = Alarms[index]; //alarm.XPos = alarmRecord.XPos; if (AlarmHeadFlag) { if (firstAlarmRecord == null) { firstAlarmRecord = alarmRecord; } if (firstAlarmRecord == alarmRecord) { LoopOffset = alarmRecord.YPos - alarm.YPos; } alarm.YPos = alarmRecord.YPos; AlarmHeadFlag = false; } alarm.Length = alarmRecord.Length; alarm.LatestOccurTime = alarmRecord.LatestOccurTime; } else { Alarms.Add(alarmRecord); Alarm(alarmRecord); } SelectedAlarmIndex = Alarms.IndexOf(alarmRecord); AsyncSaveObject("Alarms", Alarms); }); }
private TimeSpan FindMaxDuration(out int avgX, out int avgY, out DateTime beginTime, out DetectResultStatistic detectResultStatistic) { //撕裂点最长的时间 TimeSpan maxDuration = TimeSpan.Zero; beginTime = DateTime.MinValue; int xSum = 0; int ySum = 0; avgX = 0; avgY = 0; detectResultStatistic = null; foreach (var item in detectResultStatistics) { xSum += item.Key.XPos; ySum += item.Key.YPos; if (DateTime.Now - item.Value.beginTimeStamp > maxDuration) { maxDuration = DateTime.Now - item.Value.beginTimeStamp; beginTime = item.Value.beginTimeStamp; detectResultStatistic = item.Value; avgX = item.Key.XPos; avgY = item.Key.YPos; } } if (detectResultStatistics.Count > 0) { //avgX = xSum / detectResultStatistics.Count; //avgY = ySum / detectResultStatistics.Count; } else { avgX = -1; avgY = -1; } return(maxDuration); }
public void StartDetect() { Task.Run(() => { Task.Run(() => { while (true) { Console.WriteLine("计算: " + FrameCount + " 采集: " + CapCount + "显示: " + DispCount + " 图片队列: " + ImageQueue.Count); FrameCount = 0; CapCount = 0; DispCount = 0; CurrentLoopCount = serialComm.LoopCount; Thread.Sleep(1000); } }); AlgorithmResult[] algorithmResults; while (DetectFlag) { Task.Run(() => { Image <Bgr, byte> image; if (ImageQueue.TryDequeue(out image)) { stDetectParam = DetectParam.GetSt(); AlarmRecord.SetRange(AlarmParam.PixelRange, AlarmParam.YRange); App.Current?.Dispatcher?.Invoke(() => { if (AlarmRecord.TotalLoopCount != AlarmParam.TotalLoopCount) { AlarmRecord.TotalLoopCount = AlarmParam.TotalLoopCount; Alarms.Clear(); AsyncSaveObject("Alarms", Alarms); } }); algorithmResults = Algorithm.DetectImage(image.Ptr, stDetectParam); List <AlgorithmResult> results = new List <AlgorithmResult>(algorithmResults); results.ForEach(result => { if (result.bStop != 0 || result.bWidthReachStop != 0) { DrawRectOnScreen(result.xPos, result.yPos); AddResultToStatistics(result.xPos, result.yPos, image.Clone()); } }); lock (this) { if (detectResultStatistics.Count > 0) { int avgX = 0; int avgY = 0; DateTime beginTime = DateTime.MinValue; DetectResultStatistic detectResultStatistic = null; TimeSpan maxDuration = FindMaxDuration(out avgX, out avgY, out beginTime, out detectResultStatistic); if (avgX == -1) { return; } float length = (float)((maxDuration.Seconds + maxDuration.Milliseconds / 1000.0) * AlarmParam.Velocity); DateTime latestTime = FindLatestTime(); if (DateTime.Now - latestTime > AlarmParam.MaxErrorTime) { DetectState = DetectResultState.皮带正常; AlarmHeadFlag = true; AlarmRecord.LoopOffset = LoopOffset; if (SaveFlag) { SaveFlag = false; SaveAlarmVideo(); } detectResultStatistics.Clear(); } else if (maxDuration > AlarmParam.MaxHurtTime)//逻辑判断有超过maxHurtTime时间,此时发生撕伤警报 { DetectState = DetectResultState.撕伤; AddAlarmToStatistics(avgX, avgY, length, detectResultStatistic); } else if (maxDuration > AlarmParam.MaxDivTime)//逻辑判断有超过maxDivTime时间,此时发生撕裂警报 { DetectState = DetectResultState.撕裂; } } } FrameCount++; } }); Thread.Sleep(10); } }); }