示例#1
0
        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);
                }
            });
        }
示例#2
0
 private void LoadParams()
 {
     {
         string fp = Application.StartupPath + "\\AlarmParam.json";
         if (!File.Exists(fp))  // 判断是否已有相同文件
         {
             FileStream fs = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
             fs.Close();
         }
         AlarmParam = JsonConvert.DeserializeObject <AlarmParam>(File.ReadAllText(fp));
         if (AlarmParam == null)
         {
             //读取bak文件
             string fp1 = Application.StartupPath + "\\AlarmParam_bak.json";
             if (!File.Exists(fp1))  // 判断是否已有相同文件
             {
                 FileStream fs = new FileStream(fp1, FileMode.Create, FileAccess.ReadWrite);
                 fs.Close();
             }
             AlarmParam = JsonConvert.DeserializeObject <AlarmParam>(File.ReadAllText(fp1));
             if (AlarmParam == null)
             {
                 AlarmParam = new AlarmParam {
                     MaxHurtDistance = 5, MaxDivDistance = 2, MaxErrorDistance = 1.5f, PixelRange = 20, ReservedDays = 7, TotalLoopCount = 0, Velocity = 5, YRange = 10
                 };
             }
         }
     }
     {
         string fp = Application.StartupPath + "\\SerialParam.json";
         if (!File.Exists(fp))  // 判断是否已有相同文件
         {
             FileStream fs = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
             fs.Close();
         }
         SerialParam = JsonConvert.DeserializeObject <SerialParam>(File.ReadAllText(fp));
         if (SerialParam == null)
         {
             //读取bak文件
             string fp1 = Application.StartupPath + "\\SerialParam_bak.json";
             if (!File.Exists(fp1))  // 判断是否已有相同文件
             {
                 FileStream fs = new FileStream(fp1, FileMode.Create, FileAccess.ReadWrite);
                 fs.Close();
             }
             SerialParam = JsonConvert.DeserializeObject <SerialParam>(File.ReadAllText(fp1));
             if (SerialParam == null)
             {
                 SerialParam = new SerialParam
                 {
                     PortName = "COM1",
                     BaudRate = 9600,
                     Parity   = System.IO.Ports.Parity.None,
                     StopBits = System.IO.Ports.StopBits.One,
                     DataBits = 8
                 };
             }
         }
     }
     {
         string fp = Application.StartupPath + "\\DetectParam.json";
         if (!File.Exists(fp))  // 判断是否已有相同文件
         {
             FileStream fs = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
             fs.Close();
         }
         DetectParam = JsonConvert.DeserializeObject <DetectParam>(File.ReadAllText(fp));
         if (DetectParam == null)
         {
             //读取bak文件
             string fp1 = Application.StartupPath + "\\SerialParam_bak.json";
             if (!File.Exists(fp1))  // 判断是否已有相同文件
             {
                 FileStream fs = new FileStream(fp1, FileMode.Create, FileAccess.ReadWrite);
                 fs.Close();
             }
             DetectParam = JsonConvert.DeserializeObject <DetectParam>(File.ReadAllText(fp1));
             if (DetectParam == null)
             {
                 DetectParam = new DetectParam {
                     Left = 50, Right = 500, Up = 200, Down = 300, StartY = 260, AlarmWidth = 1.5f, AlarmDepth = 1.5f, CameraNo = 1
                 };
             }
         }
     }
     {
         string fp = Application.StartupPath + "\\Alarms.json";
         if (!File.Exists(fp))  // 判断是否已有相同文件
         {
             FileStream fs = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
             fs.Close();
         }
         Alarms = JsonConvert.DeserializeObject <ObservableCollection <AlarmRecord> >(File.ReadAllText(fp));
         if (Alarms == null)
         {
             //读取bak文件
             string fp1 = Application.StartupPath + "\\Alarms_bak.json";
             if (!File.Exists(fp1))  // 判断是否已有相同文件
             {
                 FileStream fs = new FileStream(fp1, FileMode.Create, FileAccess.ReadWrite);
                 fs.Close();
             }
             Alarms = JsonConvert.DeserializeObject <ObservableCollection <AlarmRecord> >(File.ReadAllText(fp1));
             if (Alarms == null)
             {
                 Alarms = new ObservableCollection <AlarmRecord>();
             }
         }
     }
 }