diff --git a/HisenceYoloDetection/HisenceYoloDetection.csproj b/HisenceYoloDetection/HisenceYoloDetection.csproj index 8d806fa..3727869 100644 --- a/HisenceYoloDetection/HisenceYoloDetection.csproj +++ b/HisenceYoloDetection/HisenceYoloDetection.csproj @@ -12,6 +12,7 @@ + diff --git a/HisenceYoloDetection/MLResultDisplay.cs b/HisenceYoloDetection/MLResultDisplay.cs new file mode 100644 index 0000000..36b2c2e --- /dev/null +++ b/HisenceYoloDetection/MLResultDisplay.cs @@ -0,0 +1,216 @@ + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + + +namespace HisenceYoloDetection +{ + public class DetectResultDisplay + { + //深度学习 显示结果 + private List mlResultList = null; + public List MLResultList + { + get => mlResultList; + set + { + if (mlResultList != value) + { + mlResultList = value; + } + } + } + + + + string DisplayTxt = ""; + Bitmap ResultImage = null; + PointF StartPoint = new PointF(); + Brush FontBrush = new SolidBrush(Color.Green); + Pen DetectResultRectPen = new Pen(new SolidBrush(Color.Green)); + + public Font Font { get; private set; } + ResultState ResultState = ResultState.DetectNG; + + Font DetectResultFont = new Font(new FontFamily("Tahoma"), 15, GraphicsUnit.World); + + public int ImageWidth { get; set; } + public int ImageHeight { get; set; } + public DetectResultDisplay() { } + + //public DetectResultDisplay(NetResult result, List specs, ResultState resultState, int imageWidth) + //{ + // ImageWidth = imageWidth; + + // ResultState = resultState; + + // displayTxt = resultState.ToString() + "\r\n"; + // if (resultState != ResultState.OK) + // { + // fontBrush = new SolidBrush(Color.Red); + // } + + // NetResult = result; + // SpecList = specs; + + // Font = new Font(new FontFamily("Tahoma"), 35 * ImageWidth / 1400, GraphicsUnit.World); + // startPoint = new PointF(150 * ImageWidth / 1400, 150 * ImageWidth / 1400); + //} + + public DetectResultDisplay(List ResultDetails, Bitmap resultImage, string displayTxt) + { + ImageWidth = resultImage.Width; + ImageHeight = resultImage.Height; + var longSide = ImageWidth > ImageHeight ? ImageWidth : ImageHeight; + + MLResultList = ResultDetails; + + ResultImage = resultImage; + DisplayTxt = displayTxt; + if (ResultState != ResultState.OK) + { + FontBrush = new SolidBrush(Color.Red); + DetectResultRectPen = new Pen(new SolidBrush(Color.Red)); + } + Font = new Font(new FontFamily("Tahoma"), 35 * longSide / 1400, GraphicsUnit.World); + DetectResultFont = new Font(new FontFamily("Tahoma"), 25 * longSide / 1400, GraphicsUnit.World); + StartPoint = new PointF(100 * ImageWidth / 1400, 100 * ImageHeight / 1400); + } + + public object Clone() + { + return null; + } + + + public void Draw(Graphics g) + { + //画检测结果图 + if (ResultImage != null && ResultState != ResultState.OK) + { + g.DrawImage(ResultImage, new Point(0, 0)); + } + //画文字 + if (!string.IsNullOrWhiteSpace(DisplayTxt)) + { + g.DrawString(DisplayTxt, Font, FontBrush, StartPoint); + } + //画外接矩形+label 深度学习 + if (MLResultList != null && MLResultList.Count > 0) + { + MLResultList.ForEach(d => + { + g.DrawRectangle(DetectResultRectPen, d.Rect); + + string locationTxt = $"{d.LabelDisplay}"; + var locationX = d.Rect.X; + var locationY = d.Rect.Y <= 20 ? d.Rect.Y + 20 : d.Rect.Y - 20; + g.DrawString(locationTxt, DetectResultFont, FontBrush, locationX, locationY); + }); + } + //画spec信息 + + //if (DetectResult != null && DetectResult.NetResult?.DetectDetails?.Count > 0) + //{ + // DetectResult.NetResult?.DetectDetails.ForEach(d => + // { + // g.DrawRectangle(defectRectPen, d.Rect); + + // string locationTxt = $"{d.Rect.X},{d.Rect.Y}"; + // g.DrawString(locationTxt, defectFont, fontBrush, d.Rect.X, d.Rect.Y - 5); + // }); + //} + + //float fontHeight = g.MeasureString(displayTxt, Font).Height; + //startPoint.Y += fontHeight * 1.2f; + + //var defects = DetectResult.NetResult?.DetectDetails; + //if (defects != null && defects.Count > 0) + //{ + // defects.ForEach(d => + // { + // g.DrawString($"{d.ClassName} X:{d.Rect.X.ToString("f2")} Y:{d.Rect.Y.ToString("f2")} S:{d.Area}", Font, d.FinalResult == EnumHelper.ResultState.OK ? fontBrushOK : fontBrushNG, startPoint); + + // startPoint.Y += fontHeight; + // }); + //} + + //DetectResult.Specs.ForEach(s => + //{ + // g.DrawString($"{s.Code}:{(s.ActualValue ?? 0).ToString("f2")}", Font, s.MeasureResult ?? false == true ? fontBrushOK : fontBrushNG, startPoint); + + // startPoint.Y += fontHeight; + //}); + } + + public string GetDisplayText() + { + return ""; + } + + + } + + public class RectResultDisplay + { + ResultState ResultState = ResultState.DetectNG; + public string DisplayTxt = ""; + Color FillColor = Color.Lime; + int FontSize = 15; + RectangleF Rect = new RectangleF(); + bool IsFilled = false; + + public RectResultDisplay() { } + + public RectResultDisplay(ResultState _resultState, RectangleF _rect, string _displayTxt, Color _fillColor, bool _isFilled, int _fontSize) + { + ResultState = _resultState; + Rect = _rect; + DisplayTxt = _displayTxt; + FillColor = _fillColor; + IsFilled = _isFilled; + FontSize = _fontSize; + } + + public object Clone() + { + RectResultDisplay rect = new RectResultDisplay(); + + rect.ResultState = ResultState; + rect.Rect = Rect; + rect.DisplayTxt = DisplayTxt; + rect.FillColor = FillColor; + rect.FontSize = FontSize; + rect.IsFilled = IsFilled; + + return rect; + } + + public void Draw(Graphics g) + { + g.DrawRectangle(new Pen(FillColor, 1), Rect.X, Rect.Y, Rect.Width, Rect.Height); + + if (IsFilled) + { + g.FillRectangle(new SolidBrush(Color.FromArgb(20, FillColor)), Rect); + } + + Font font = new Font("Tahoma", FontSize); + var txtSize = g.MeasureString(DisplayTxt, font); + + g.DrawString(DisplayTxt, font, new SolidBrush(FillColor), (float)(Rect.X + Rect.Width / 2.0 - txtSize.Width / 2.0), Rect.Y + Rect.Height + 5); + } + + public string GetDisplayText() + { + return $"{ResultState} {DisplayTxt} ({Rect.X},{Rect.Y},{Rect.Width},{Rect.Height})"; + } + + + } +} diff --git a/HisenceYoloDetection/MainForm.Designer.cs b/HisenceYoloDetection/MainForm.Designer.cs index d49c6ff..c244e44 100644 --- a/HisenceYoloDetection/MainForm.Designer.cs +++ b/HisenceYoloDetection/MainForm.Designer.cs @@ -230,7 +230,7 @@ tabPage2.Location = new Point(4, 26); tabPage2.Name = "tabPage2"; tabPage2.Padding = new Padding(3); - tabPage2.Size = new Size(1319, 765); + tabPage2.Size = new Size(1276, 765); tabPage2.TabIndex = 1; tabPage2.Text = "PLC"; tabPage2.UseVisualStyleBackColor = true; @@ -288,9 +288,9 @@ // // txtSetValue // - txtSetValue.Location = new Point(670, 78); + txtSetValue.Location = new Point(574, 78); txtSetValue.Name = "txtSetValue"; - txtSetValue.Size = new Size(100, 23); + txtSetValue.Size = new Size(196, 23); txtSetValue.TabIndex = 14; // // groupBox4 @@ -686,11 +686,11 @@ XZerorbx.Text = "X归零"; XZerorbx.TextAlign = ContentAlignment.MiddleCenter; XZerorbx.UseVisualStyleBackColor = true; - XZerorbx.Click += radioButton1_Click; + XZerorbx.Click += XZerorbx_Click; // // PLCPostion // - PLCPostion.Location = new Point(6, 258); + PLCPostion.Location = new Point(6, 266); PLCPostion.Name = "PLCPostion"; PLCPostion.Size = new Size(97, 101); PLCPostion.TabIndex = 2; @@ -828,7 +828,7 @@ tabPage1.Location = new Point(4, 26); tabPage1.Name = "tabPage1"; tabPage1.Padding = new Padding(3); - tabPage1.Size = new Size(1319, 765); + tabPage1.Size = new Size(1276, 765); tabPage1.TabIndex = 0; tabPage1.Text = "相机"; tabPage1.UseVisualStyleBackColor = true; @@ -1105,7 +1105,7 @@ tabControl1.Location = new Point(0, 0); tabControl1.Name = "tabControl1"; tabControl1.SelectedIndex = 0; - tabControl1.Size = new Size(1327, 795); + tabControl1.Size = new Size(1284, 795); tabControl1.TabIndex = 0; // // tabPage5 @@ -1113,7 +1113,7 @@ tabPage5.Controls.Add(panel4); tabPage5.Location = new Point(4, 26); tabPage5.Name = "tabPage5"; - tabPage5.Size = new Size(1319, 765); + tabPage5.Size = new Size(1276, 765); tabPage5.TabIndex = 3; tabPage5.Text = "流程"; tabPage5.UseVisualStyleBackColor = true; @@ -1124,7 +1124,7 @@ panel4.Dock = DockStyle.Fill; panel4.Location = new Point(0, 0); panel4.Name = "panel4"; - panel4.Size = new Size(1319, 765); + panel4.Size = new Size(1276, 765); panel4.TabIndex = 0; // // panel5 @@ -1135,7 +1135,7 @@ panel5.Dock = DockStyle.Top; panel5.Location = new Point(0, 0); panel5.Name = "panel5"; - panel5.Size = new Size(1319, 762); + panel5.Size = new Size(1276, 762); panel5.TabIndex = 0; // // DefetShow5 @@ -1471,7 +1471,7 @@ tabPage3.Controls.Add(panel2); tabPage3.Location = new Point(4, 26); tabPage3.Name = "tabPage3"; - tabPage3.Size = new Size(1319, 765); + tabPage3.Size = new Size(1276, 765); tabPage3.TabIndex = 4; tabPage3.Text = "录入新型号"; tabPage3.UseVisualStyleBackColor = true; @@ -1641,9 +1641,9 @@ // // QueryoneBtn // - QueryoneBtn.Location = new Point(896, 245); + QueryoneBtn.Location = new Point(838, 222); QueryoneBtn.Name = "QueryoneBtn"; - QueryoneBtn.Size = new Size(93, 23); + QueryoneBtn.Size = new Size(115, 53); QueryoneBtn.TabIndex = 25; QueryoneBtn.Text = "条件查询"; QueryoneBtn.UseVisualStyleBackColor = true; @@ -1668,9 +1668,9 @@ // // InsertBtn // - InsertBtn.Location = new Point(896, 155); + InsertBtn.Location = new Point(838, 92); InsertBtn.Name = "InsertBtn"; - InsertBtn.Size = new Size(93, 23); + InsertBtn.Size = new Size(115, 47); InsertBtn.TabIndex = 22; InsertBtn.Text = "插入"; InsertBtn.UseVisualStyleBackColor = true; @@ -1796,9 +1796,9 @@ // // queryALLBtn // - queryALLBtn.Location = new Point(896, 196); + queryALLBtn.Location = new Point(838, 157); queryALLBtn.Name = "queryALLBtn"; - queryALLBtn.Size = new Size(93, 23); + queryALLBtn.Size = new Size(115, 59); queryALLBtn.TabIndex = 2; queryALLBtn.Text = "查询全部"; queryALLBtn.UseVisualStyleBackColor = true; @@ -1827,7 +1827,7 @@ richTextBox1.Dock = DockStyle.Bottom; richTextBox1.Location = new Point(0, 801); richTextBox1.Name = "richTextBox1"; - richTextBox1.Size = new Size(1327, 176); + richTextBox1.Size = new Size(1284, 176); richTextBox1.TabIndex = 1; richTextBox1.Text = ""; // @@ -1840,7 +1840,7 @@ // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1327, 977); + ClientSize = new Size(1284, 977); Controls.Add(richTextBox1); Controls.Add(tabControl1); Icon = (Icon)resources.GetObject("$this.Icon"); diff --git a/HisenceYoloDetection/MainForm.cs b/HisenceYoloDetection/MainForm.cs index 1961bf4..dc3ff59 100644 --- a/HisenceYoloDetection/MainForm.cs +++ b/HisenceYoloDetection/MainForm.cs @@ -1,10 +1,12 @@ using DVPCameraType; using Microsoft.VisualBasic.Logging; +using Newtonsoft.Json.Linq; using OpenCvSharp; using OpenCvSharp.Dnn; using OpenCvSharp.Extensions; using OpenCvSharp.XFeatures2D; using System; +using System.ComponentModel; using System.Data; using System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder; using System.Data.SQLite; @@ -12,12 +14,15 @@ using System.Diagnostics; using System.Diagnostics.Eventing.Reader; using System.Drawing; using System.Drawing.Drawing2D; +using System.IO; +using System.Linq; using System.Net; using System.Numerics; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using XKRS.Device.SimboVision.SimboHelper; +using XKRS.UI; using static HisenceYoloDetection.MainForm; using static System.Runtime.InteropServices.JavaScript.JSType; using static System.Windows.Forms.VisualStyles.VisualStyleElement; @@ -31,10 +36,15 @@ namespace HisenceYoloDetection public MainForm() { InitializeComponent(); + this.Load += (s, e) => + { + // OnDetectionDone -= Defet_OnDetectionDone; + //OnDetectionDone += Defet_OnDetectionDone; + }; } MGSCameraDriver Cam1 = new MGSCameraDriver(); MGSCameraDriver Cam2 = new MGSCameraDriver(); - + public static string RootPath = "D:\\Hisence\\SQLImages\\"; // HikCameraDriver Cam1 = new HikCameraDriver(); //HikCameraDriver Cam2 = new HikCameraDriver(); @@ -90,6 +100,7 @@ namespace HisenceYoloDetection string Cam1OnePath = ""; string Cam1TwoPath = ""; string ModelChangePath = ""; + /// /// 鿴б /// @@ -145,7 +156,7 @@ namespace HisenceYoloDetection } System.Timers.Timer SpeedTimer = new System.Timers.Timer(); System.Timers.Timer PostionTimer = new System.Timers.Timer(); - + System.Timers.Timer t; public delegate void MyDelegateUI(); //ί MyDelegateUI myDelegateUI; //ίж @@ -169,6 +180,7 @@ namespace HisenceYoloDetection } + /// /// /// @@ -177,8 +189,15 @@ namespace HisenceYoloDetection private void MainForm_Load(object sender, EventArgs e) { + string s1 = "ECO4060##Coton20##Rapide15mns"; + string s2 = "Eco4060##Coton20##Rapide15mns"; + bool st = ManagerModelHelper.StrMatch2(s1, s2); + //Rect rect = new Rect(0, 0, 0, 0); + //string path2 = "D:\\Hisence\\SQLImages\\W80WXWJ060539V0WW80WX120293\\2\\W80WXWJ060539V0WW80WX120293result.jpg"; + //Mat cameraMat = Cv2.ImRead(path2); + //bool ifg = CheckDiffSciHelper.CheckDiffSci(path2, cameraMat, rect, rect, false, "D://Hisence//Test"); string vBarPath = "D:\\Hisence\\\\Lp\\202452414231.jpg"; string vCam1OnePath = "D:\\Hisence\\\\Lp\\001.jpg"; @@ -186,9 +205,10 @@ namespace HisenceYoloDetection string vModelChangePath = "D:\\Hisence\\config\\chinese.json"; string LablePathstr = "D:\\Hisence\\ClassName.txt";//ǩ· string LablePathButtonstr = "D:\\Hisence\\ClassNameButton.txt";//ǩ· - //Mat cameraMat = Cv2.ImRead(vCam1TwoPath); - // canvas1.ImagePath = vCam1TwoPath; - // DefetShow.ImaMAt = cameraMat; + // Mat cameraMat = Cv2.ImRead(vCam1TwoPath); + //Defet_OnDetectionDone(cameraMat, DefetShow1); + // canvas1.ImagePath = vCam1TwoPath; + // DefetShow.ImaMAt = cameraMat; if (File.Exists(LablePathButtonstr) && File.Exists(vBarPath) && File.Exists(vCam1OnePath) && File.Exists(vCam1TwoPath) && File.Exists(vModelChangePath)) { @@ -220,6 +240,11 @@ namespace HisenceYoloDetection paddleOcrModelCountry.Load(CountryStr, "CPU"); paddleOcrModel.Load(CountryStr, "CPU"); IfChangeLanguage = CountryStr; + + MLRequest req = new MLRequest(); + string path = "D:\\Hisence\\\\12\\202461417146451.jpg"; + req.currentMat = Cv2.ImRead(path); + string BarT = DetMachineBar(ref req); // StartDecBtn.Enabled = false; @@ -239,6 +264,80 @@ namespace HisenceYoloDetection OnSpeedShow();//߳̿ٶʾڽ OnPostionShow();//߳̿λʾڽ + //t = new System.Timers.Timer(10000);//ʵTimer࣬üʱΪ10000룻 + //t.Elapsed += new System.Timers.ElapsedEventHandler(Execute);//ʱʱִ¼ + //t.AutoReset = true;//ִһΣfalseһֱִ(true) + //t.Enabled = true;//ǷִSystem.Timers.Timer.Elapsed¼ + //t.Start(); //ʱ + + } + public void Execute(object source, System.Timers.ElapsedEventArgs e) + { + t.Stop(); //ȹرնʱ + //melsecPLCTCPDriver.WriteInt("548", 1); + //Thread.Sleep(1000); + //melsecPLCTCPDriver.WriteInt("548", 0); + //myLog("д" + WriteAdress.Text.ToString() + "", DateTime.Now); + MessageBox.Show("OK!"); + t.Start(); //ִϺٿ + } + + + private string DetMachineBar(ref MLRequest req) + { + + // req.currentMat = Cv2.ImRead("D:\\Hisence\\\\Lp\\202453015229201.jpg"); + MLResult mL = paddleOcrModelCountry.RunInferenceFixed(req); + // DisplayResult(mL); + + //ʶַ + string IOcrBAr = string.Empty; + string[] BarListOne = new string[10]; + int Ypos = 0; + int mid = 9999999; + for (int v = 0; v < Enumerable.Count(mL.ResultDetails); v++) + { + if (mL.ResultDetails.Count() > 0) + { + int Pos = mL.ResultDetails[v].Rect.Y; + if (Pos < mid) + { + mid = Pos; + } + + } + + } + + for (int v = 0; v < Enumerable.Count(mL.ResultDetails); v++) + { + if (mL.ResultDetails.Count() > 0) + { + int Pos = mL.ResultDetails[v].Rect.Y; + + + + if (Math.Abs(Pos - mid) < 80) + { + string iv = mL.ResultDetails[v].LabelDisplay; + string result = Regex.Replace(iv, "[ \\[ \\] \\^ \\-_*DD(^)$%~!@#$&%+=<>!??:?`,.;/\"-]", ""); + + IOcrBAr += result; + + + } + else + { + continue; + } + + + } + + } + //һΪBAr + return IOcrBAr; + } private void initAll() //Ϣ { @@ -448,7 +547,8 @@ namespace HisenceYoloDetection int TimesCalled = 0; Bitmap OKbitmap = new Bitmap("OK.jpg"); Bitmap NGbitmap = new Bitmap("NG.jpg"); - + Mat OKMat = Cv2.ImRead("OK.jpg"); + Mat NGMat = Cv2.ImRead("OK.jpg"); /// /// ̵߳ /// @@ -566,7 +666,7 @@ namespace HisenceYoloDetection //OCRʶüͼƬ MLRequest reqcut = new MLRequest(); reqcut.currentMat = matroi.Clone(); - MLResult mLcut = IOcrModel.RunInferenceFixed(reqcut); + MLResult mLcut = paddleOcrModel.RunInference(reqcut); for (int j = 0; j < mLcut.ResultDetails.Count; j++) @@ -909,8 +1009,34 @@ namespace HisenceYoloDetection }); } - int YPos = 6715; - int ZPos = 18857; + #region ʾ + /// + /// ʾ + /// + /// + private void DisplayResult(MLResult detectResult) + { + string displayTxt = ""; + detectResult.ResultDetails.ForEach(d => + { + displayTxt += $"{d.LabelName} score:{d.Score.ToString("f2")} area:{d.Area.ToString("f2")}\r\n"; + }); + + var resultMask = detectResult.ResultMap; + //Clone(new Rectangle(0, 0, detectResult.VisionImageSet.DetectionResultImage.Width, detectResult.VisionImageSet.DetectionResultImage.Height), detectResult.VisionImageSet.DetectionResultImage.PixelFormat); + + // List detectionResultShapes = new List(detectResult.DetectionResultShapes); + DetectResultDisplay resultDisplay = new DetectResultDisplay(detectResult.ResultDetails, resultMask, displayTxt); + // detectionResultShapes.Add(resultDisplay); + + Graphics g = Graphics.FromImage(resultMask); + resultDisplay.Draw(g); + // resultMask.Save("D://save.png"); + // var resultMap = GetResultImage(resultMask, detectionResultShapes); + } + #endregion + int YPos = 8715; + int ZPos = 25000; /// /// ϴ»544Ƿ񴥷䴫 /// @@ -959,7 +1085,7 @@ namespace HisenceYoloDetection //if (bBarTriger) { - +#if true myLog("һβɼͼ", DateTime.Now); //ƶģ βĵַ Ƿǰƶ MoveToP(5815, YPos, ZPos); @@ -988,7 +1114,7 @@ namespace HisenceYoloDetection // Thread.Sleep(500); Cam1.SnapshotCount = 0; Cam1.Snapshot(); - IfCam2Triger = true; + Cam2.SnapshotCount = 0; Cam2.Snapshot(); myLog("ڶβɼͼ", DateTime.Now); @@ -1013,6 +1139,65 @@ namespace HisenceYoloDetection iNum0 = 0; iNum1 = 0; break; +#else + myLog("һβɼͼ", DateTime.Now); + //ƶģ βĵַ Ƿǰƶ + //MoveToP(5815, YPos, ZPos); + Thread.Sleep(1000);//ɾ + + + + //List DisList = new List(); + //for (int i = 0; i < 5; i++) + //{ + // int m600 = melsecPLCTCPDriver.ReadInt("600"); + // DisList.Add(m600); + //} + //DisList.Remove(DisList.Max()); + //DisList.Remove(DisList.Min()); + //// ʣֵƽ + //double average = DisList.Average(); + //double disreal = average * (-0.54054) + 1324.3232; + ////-׼=Ҫƶľ + //double dismove = disreal - 876; + //if (dismove > 0 && dismove < 4000) + //{ + // MoveToP(5815, YPos, ZPos + (int)(dismove)); + //} + Thread.Sleep(1000);//ɾ + // Thread.Sleep(500); + Cam1.SnapshotCount = 0; + Cam1.Snapshot(); + + // Cam2.SnapshotCount = 0; + //Cam2.Snapshot(); + IfCam2Triger = true; + myLog("ڶβɼͼ", DateTime.Now); + //if (xKNow != null) + //{ + // MoveToP(xKNow.MoveTwoX, xKNow.MoveTwoY, xKNow.MoveTwoZ); + //} + //currentXP = 54964; + Thread.Sleep(2000);//ɾ + + //MoveToP(51043, YPos, ZPos + (int)(dismove)); + Cam1.Snapshot(); + //MoveToP(5815, YPos, ZPos); + //if (xKNow != null) + //{ + // MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ); + //} + myLog("ɼͼɹ", DateTime.Now); + //ģ鸴λ + + bTriger = true;//Խмʶ + bBarTriger = false;//Բ + iNum0 = 0; + iNum1 = 0; + break; + +#endif + } @@ -1043,12 +1228,13 @@ namespace HisenceYoloDetection { if (ifPlcHeart == 1) { + txtSetValue.Text = "PLCʧ" + " ʱ䣺" + DateTime.Now.ToString(); myLog("PLCʧ", DateTime.Now); - + PLCPowerON.BackColor = Color.Red; } else { - + PLCPowerON.BackColor = Color.Green; } } @@ -1069,10 +1255,10 @@ namespace HisenceYoloDetection //һ if (SnapshotCount == 1) { - // Mat mat = Cv2.ImRead("D:\\Hisence\\1\\202451716511.jpg"); - //Mat mat = Cv2.ImRead("D:\\Hisence\\\\Lp\\2024517171911.jpg"); - //Cam1ImgOne = mat; - Cam1ImgOne = cameraMat; + + + // Cam1ImgOne = Cv2.ImRead("D:\\Hisence\\\\1\\202461817464661.jpg"); ; + Cam1ImgOne = cameraMat; IfCam1OneTriger = true; originMatShow.Image = cameraMat.ToBitmap(); //pictureBox1.Image = cameraMat.ToBitmap(); @@ -1082,18 +1268,17 @@ namespace HisenceYoloDetection //ڶ if (SnapshotCount == 2) { - // Mat mat = Cv2.ImRead("D:\\Hisence\\1\\2024517164541.jpg"); - //Mat mat = Cv2.ImRead("D:\\Hisence\\\\Lp\\002.jpg"); + //Mat mat = Cv2.ImRead("D:\\Hisence\\\\1\\002.jpg"); //Cam1ImgTwo = mat; - Cam1ImgTwo = cameraMat; + Cam1ImgTwo = cameraMat; IfCam1TwoTriger = true; originMatShow2.Image = cameraMat.ToBitmap(); - //pictureBox1.Image = cameraMat.ToBitmap(); + } - CamShow1.ImaMAt = cameraMat; + //CamShow1.ImaMAt = cameraMat; cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg"); } @@ -1115,15 +1300,9 @@ namespace HisenceYoloDetection // Mat mat = Cv2.ImRead("F:\\ϴ»\\cam1\\2024517161641.jpg"); Cam2ImgShowBar = cameraMat; IfCam2Triger = true; - originMat2Show.BackgroundImage = cameraMat.ToBitmap(); - //originMat2Show.Image = cameraMat.ToBitmap(); - //pictureBox1.Image = cameraMat.ToBitmap(); + originMat2Show.Image = cameraMat.ToBitmap(); - - - - - CamShow2.ImaMAt = cameraMat; + // CamShow2.ImaMAt = cameraMat; cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg"); } @@ -1145,28 +1324,14 @@ namespace HisenceYoloDetection //2һ if (IfCam2Triger) { - + IfCam2Triger = false; //OCRʶ MLRequest req = new MLRequest(); req.currentMat = Cam2ImgShowBar; - // req.currentMat = Cv2.ImRead("D:\\Hisence\\\\Lp\\202453015229201.jpg"); - MLResult mL = paddleOcrModelCountry.RunInferenceFixed(req); + //req.currentMat = Cv2.ImRead("D:\\Hisence\\\\1\\bar.jpg"); + //ʶַ - string IOcrBAr = ""; - for (int v = 0; v < Enumerable.Count(mL.ResultDetails); v++) - { - if (mL.ResultDetails.Count() > 0) - { - if (mL.ResultDetails.Count() > 0) - { - string iv = mL.ResultDetails[0].LabelDisplay; - string result = Regex.Replace(iv, "[ \\[ \\] \\^ \\-_*DD(^)$%~!@#$&%+=<>!??:?`,.;/\"-]", ""); - - IOcrBAr = result; - } - } - - } + string IOcrBAr = DetMachineBar(ref req); DateTime dt = DateTime.Now; using (StreamWriter sw = new StreamWriter("D://Hisence//logsBar.log", true)) { @@ -1175,7 +1340,7 @@ namespace HisenceYoloDetection sw.WriteLine(IOcrBAr + "\n"); sw.Flush(); } - IOcrBAr = "BatchW81XBModelWNHA62SASUASNVWJ060501V0WW81XB120024"; + IOcrBAr = "BatchW825UModelWNHEI74SAS"; //ݿȶ xKNow = GetModeFromBar(IOcrBAr);//ݿвѯֵ List xkWordList = ManagerModelHelper.GetModeWordFromBar(IOcrBAr); @@ -1187,28 +1352,28 @@ namespace HisenceYoloDetection if (xKNow.OcrBar == null) { - myLog("δƥɹ" + IfChangeLanguage, DateTime.Now); + myLog("δƥɹ" + IOcrBAr, DateTime.Now); break; } if (xKNow.Detect != "") { - //ʱеϴ»Ǻ֮ǰһģ - if (IfChangeLanguage == IOcrBAr) - { + ////ʱеϴ»Ǻ֮ǰһģ + //if (IfChangeLanguage == IOcrBAr) + //{ - } - else - { - //شOCR.josn - if (File.Exists(xKNow.OcrParm)) - { - paddleOcrModel.Load(xKNow.OcrParm, "CPU"); + //} + //else + //{ + // //شOCR.josn + // if (File.Exists(xKNow.OcrParm)) + // { + // paddleOcrModel.Load(xKNow.OcrParm, "CPU"); - IfChangeLanguage = IOcrBAr; - } + // IfChangeLanguage = IOcrBAr; + // } - } - myLog("ͺƥɹ" + IfChangeLanguage, DateTime.Now); + //} + myLog("ͺƥɹ" + IOcrBAr, DateTime.Now); bBarTriger = true; } @@ -1216,7 +1381,7 @@ namespace HisenceYoloDetection //ݿƥſԽģƶ //ͺźϴεͺһ Ͳûʶģll - IfCam2Triger = false; + } //1ڶ @@ -1244,8 +1409,12 @@ namespace HisenceYoloDetection List strMatFuzzyListOne = new List(); Mat mResultCut = req.currentMat.Clone(); Mat mCut = new Mat(); - ManagerModelHelper.InsertSqlRunDataButton(false, ref mCut, ref mResultCut, mL, mLButton, ref xK_MatchDet, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel); - + Stopwatch sw = new Stopwatch(); + sw.Start(); + Dictionary keyValueResult = new Dictionary(); + InsertSqlRunDataButton(ref keyValueResult, false, ref mCut, ref mResultCut, mL, mLButton, ref xK_HisenceSQLWord, ref xK_MatchDet, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel); + sw.Stop(); + myLog("ʱ" + sw.ElapsedMilliseconds, DateTime.Now); if (mL.ResultMap != null) { mL.ResultMap.Save("D://cam1//" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1result.jpg"); @@ -1266,8 +1435,8 @@ namespace HisenceYoloDetection List strMatListTwo = new List(); List strMatFuzzyListTwo = new List(); Mat mResultCut2 = req2.currentMat.Clone(); - - ManagerModelHelper.InsertSqlRunData(false, ref mResultCut2, mL2, ref xK_MatchDet, /*ref strMatListTwo, ref strMatFuzzyListTwo,*/ ref paddleOcrModel); + Dictionary keyValueResult2 = new Dictionary(); + InsertSqlRunData(ref keyValueResult2, false, ref mResultCut2, mL2, ref xK_HisenceSQLWord, ref xK_MatchDet, ref strMatListTwo, ref strMatFuzzyListTwo, ref paddleOcrModel); if (mL2.ResultMap != null) { @@ -1299,10 +1468,26 @@ namespace HisenceYoloDetection } ///ִбȶ Сͼ - bool MatchStr = ManagerModelHelper.IsMatchSQLText(ref mCut, ref xK_HisenceSQLWord, ref xK_MatchDet); - + //bool MatchStr = ManagerModelHelper.IsMatchSQLText(ref mCut, ref xK_HisenceSQLWord, ref xK_MatchDet); + bool MatchStr = true; + keyValueResult.ForEach(x => + { + if (x.Value == false) + { + MatchStr = false; + return; + } + }); + keyValueResult2.ForEach(x => + { + if (x.Value == false) + { + MatchStr = false; + return; + } + }); IfCam1TwoTriger = false; bBarTriger = false; @@ -1487,14 +1672,31 @@ namespace HisenceYoloDetection // PLCcontrol->WritePlc(500, 0); bOn = false; PLCPowerON.Text = "ʹOn"; - myLog("ʹOn", DateTime.Now); + myLog("ʹ", DateTime.Now); + PLCPowerON.BackColor = Color.Red; } else { + melsecPLCTCPDriver.WriteInt("581", 0);//λɿ + melsecPLCTCPDriver.WriteInt("504", 0);//λɿ + melsecPLCTCPDriver.WriteInt("514", 0);//λɿ + melsecPLCTCPDriver.WriteInt("524", 0);//λɿ + melsecPLCTCPDriver.WriteInt("406", 60000); //дֶٶ + melsecPLCTCPDriver.WriteInt("416", 8000); + melsecPLCTCPDriver.WriteInt("426", 10000); + melsecPLCTCPDriver.WriteInt("404", 100000); //д붨λٶ + melsecPLCTCPDriver.WriteInt("414", 8000); + melsecPLCTCPDriver.WriteInt("424", 10000); + + + melsecPLCTCPDriver.WriteInt("500", 0); + + melsecPLCTCPDriver.WriteInt("500", 1); melsecPLCTCPDriver.WriteInt("500", 1); bOn = true; PLCPowerON.Text = "ʹOff"; - myLog("ʹOff", DateTime.Now); + PLCPowerON.BackColor = Color.Green; + myLog("ʹ", DateTime.Now); } } @@ -1647,27 +1849,7 @@ namespace HisenceYoloDetection } //״̬ʶ private bool m_btnState; - private void radioButton1_Click(object sender, EventArgs e) - { - - if (!ifXZeroValue) - { - this.XZerorbx.Checked = true; - // - melsecPLCTCPDriver.WriteInt("503", 1); - ifXZeroValue = true; - this.txtSetValue.Text = "1"; - myLog("XῪʼ", DateTime.Now); - } - else - { - this.XZerorbx.Checked = false; - melsecPLCTCPDriver.WriteInt("503", 0); - ifXZeroValue = false; - this.txtSetValue.Text = "0"; - myLog("Xֹͣ", DateTime.Now); - } - } + private void YZerorbx_Click(object sender, EventArgs e) { @@ -1870,7 +2052,7 @@ namespace HisenceYoloDetection BarPath = fName; } } - + #region ͺ private void InsertBtn_Click(object sender, EventArgs e) { @@ -1963,17 +2145,7 @@ namespace HisenceYoloDetection //ģʶ MLRequest OcrBari = new MLRequest(); OcrBari.currentMat = Cv2.ImRead(BarPath); - MLResult mL2 = paddleOcrModelCountry.RunInferenceFixed(OcrBari); - - string IOcrBAr = ""; - for (int v = 0; v < mL2.ResultDetails.Count(); v++) - { - string ivi = mL2.ResultDetails[v].LabelDisplay; - string result = Regex.Replace(ivi, "[ \\[ \\] \\^ \\-_*DD(^)$%~!@#$&%+=<>!??:?`,.;/\"-]", ""); - - IOcrBAr += result; - } - OcrBarBox.Text = IOcrBAr; + string IOcrBAr = DetMachineBar(ref OcrBari); XK_HisenceWord xkWord = new XK_HisenceWord(); xkWord.OcrBar = IOcrBAr; xkWord.TwoIFWhile = ifwhiteBan; @@ -2017,7 +2189,8 @@ namespace HisenceYoloDetection List strMatFuzzyListOne = new List(); Mat mResultCut = CamOneI.currentMat.Clone(); Mat mCut = new Mat(); - ManagerModelHelper.InsertSqlRunDataButton(true, ref mCut, ref mResultCut, mL3, mLButton, ref xkWord, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel); + Dictionary keyValueResult = new Dictionary(); + InsertSqlRunDataButton(ref keyValueResult, true, ref mCut, ref mResultCut, mL3, mLButton, ref xK_HisenceSQLWord, ref xkWord, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel); @@ -2040,8 +2213,8 @@ namespace HisenceYoloDetection List strMatListTwo = new List(); List strMatFuzzyListTwo = new List(); Mat mResultCut2 = CamTwoI.currentMat.Clone(); - - ManagerModelHelper.InsertSqlRunData(true, ref mResultCut2, mL4, ref xkWord,/* ref strMatListTwo, ref strMatFuzzyListTwo,*/ ref paddleOcrModel); + Dictionary keyValueResult2 = new Dictionary(); + InsertSqlRunData(ref keyValueResult2, true, ref mResultCut2, mL4, ref xK_HisenceSQLWord, ref xkWord, ref strMatListTwo, ref strMatFuzzyListTwo, ref paddleOcrModel); @@ -2307,6 +2480,7 @@ namespace HisenceYoloDetection MessageBox.Show("ؼXK_HisenceWordһ"); } } + #endregion private void queryALLBtn_Click(object sender, EventArgs e) { try @@ -2404,6 +2578,495 @@ namespace HisenceYoloDetection { Cam2.Snapshot(); } + private async void Defet_OnDetectionDone(Mat detectionImage, int BlockId) + { + + await Task.Run(() => + { + if (!this.IsHandleCreated) + return; + + this.Invoke(new Action(() => + { + if (this.IsDisposed) + return; + switch (BlockId) + { + case 1: + { + DefetShow1.ImaMAt = detectionImage; + } + break; + case 2: + { + DefetShow2.ImaMAt = detectionImage; + } + break; + case 3: + { + DefetShow3.ImaMAt = detectionImage; + } + break; + case 4: + { + DefetShow4.ImaMAt = detectionImage; + } + break; + case 5: + { + DefetShow5.ImaMAt = detectionImage; + } + break; + case 6: + { + // DefetShow6.ImaMAt = detectionImage; + } + break; + case 7: + { + // DefetShow7.ImaMAt = detectionImage; + } + break; + case 8: + { + // DefetShow8.ImaMAt = detectionImage; + } + break; + + } + //var display = displayList.FirstOrDefault(u => u.DetectionId == detectionId); + //if (display != null) + //{ + // display.RefreshDetectionResult(image, detectionResults); + //} + })); + }); + } + /// + /// ȫͼϴ» ü֮ OCRʶĽ + /// + /// + /// + /// ȫͼͼƬ + /// ȫͼƬϵĿ궨λλο + /// + /// + /// صĶλĽ + /// + /// + public void InsertSqlRunDataButton(ref Dictionary keyValueResult, bool saveimage, ref Mat CutMat, ref Mat currentMatC, MLResult cam1TwoML, MLResult cam1Button, ref XK_HisenceWord SQlxK_HisenceWord, ref XK_HisenceWord xK_HisenceWord, ref List strMatList, ref List strMatRefList, ref PaddleOcrModel IOcrModel) + { +#if true + //try + //{ + + Mat mResultCut = currentMatC.Clone(); + Rect areaBlack = new Rect(); + //ťλ + if (cam1Button.ResultDetails.Count == 1) + { + Mat mResultCuti = mResultCut.Clone(); + int rectsx = cam1Button.ResultDetails[0].Rect.X; + int rectsy = cam1Button.ResultDetails[0].Rect.Y; + int rectsWidth = cam1Button.ResultDetails[0].Rect.Width; + int rectsHeight = cam1Button.ResultDetails[0].Rect.Height; + areaBlack = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); + + } + + + for (int i = 0; i < cam1TwoML.ResultDetails.Count; i++) + { + Mat mResultCuti = mResultCut.Clone(); + int rectsx = cam1TwoML.ResultDetails[i].Rect.X; + int rectsy = cam1TwoML.ResultDetails[i].Rect.Y; + int rectsWidth = cam1TwoML.ResultDetails[i].Rect.Width; + int rectsHeight = cam1TwoML.ResultDetails[i].Rect.Height; + + string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay; + if(keyValueResult.Keys.Contains(blockIndex)) + { + int indexof = keyValueResult.Keys.ToList().IndexOf(blockIndex); + + int rectsxof = cam1TwoML.ResultDetails[indexof].Rect.X; + if(rectsx> rectsxof) + { + blockIndex = "3"; + } + + } + Rect area2 = new Rect(0, 0, 0, 0); + Mat matCut = new Mat(); + if (blockIndex == "2")//ťΧ + { + + //ťڵťλþ0,0,0,0 + if ((areaBlack.X + areaBlack.Width) < (rectsx + rectsWidth)) + { + areaBlack.X -= rectsx; + areaBlack.Y -= rectsy; + area2 = areaBlack; + } + string TwoRectStr = CheckDiffSciHelper.rectChangeStr(area2); + xK_HisenceWord.TwoRect = TwoRectStr; + + } + Stopwatch sw = Stopwatch.StartNew(); + sw.Start(); + int extendHeight = 50; + rectsHeight += extendHeight; + Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); + matCut = new Mat(mResultCuti, area); + //OCRʶüͼƬ + MLRequest reqcut = new MLRequest(); + reqcut.currentMat = matCut.Clone(); + MLResult mLCut = paddleOcrModel.RunInference(reqcut); + + bool blockMatchI = BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref SQlxK_HisenceWord, ref xK_HisenceWord); + sw.Stop(); + myLog("ÿҪʱ" + sw.ElapsedMilliseconds, DateTime.Now); + + keyValueResult.Add(blockIndex, blockMatchI); + + } + + + //} + //catch (Exception ex) + //{ + + //} +#endif + + + } + + /// + /// ȫͼϴ» ü֮ OCRʶĽ + /// + /// ȫͼͼƬ + /// ȫͼƬϵĿ궨λλο + /// صĶλĽ + public void InsertSqlRunData(ref Dictionary keyValueResult2, bool saveimage, ref Mat currentMatC, MLResult cam1TwoML, ref XK_HisenceWord SQlxK_HisenceWord, ref XK_HisenceWord xK_HisenceWord, ref List strMatList, ref List strMatRefList, ref PaddleOcrModel IOcrModel) + { +#if true + //try + //{ + + + Mat mResultCut = currentMatC.Clone(); + + for (int i = 0; i < cam1TwoML.ResultDetails.Count; i++) + { + Mat mResultCuti = mResultCut.Clone(); + int rectsx = cam1TwoML.ResultDetails[i].Rect.X; + int rectsy = cam1TwoML.ResultDetails[i].Rect.Y; + int rectsWidth = cam1TwoML.ResultDetails[i].Rect.Width; + int rectsHeight = cam1TwoML.ResultDetails[i].Rect.Height; + string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay; + Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); + Mat matCut = new Mat(mResultCuti, area); + Stopwatch sw = new Stopwatch(); + sw.Start(); + //OCRʶüͼƬ + MLRequest reqcut = new MLRequest(); + reqcut.currentMat = matCut.Clone(); + MLResult mLCut = paddleOcrModel.RunInference(reqcut); + + bool blockMatchI = BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref SQlxK_HisenceWord, ref xK_HisenceWord); + keyValueResult2.Add(blockIndex, blockMatchI); + sw.Stop(); + myLog("ÿҪʱ" + sw.ElapsedMilliseconds, DateTime.Now); + } + + + //} + //catch (Exception ex) + //{ + + //} +#endif + + + } + + + /// + /// ݿȶǷȷ + /// + /// Ƿ񱣴 + /// üһ + /// üһͼƬ + /// üͼƬһЩϢ + /// Ҫ洢ݿĶ + public bool BlockChangeFun(bool saveimage, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref XK_HisenceWord SQlxK_HisenceWord, ref XK_HisenceWord xK_HisenceWord) + { + bool OneIF = false; + string CutSavePath = ""; + //ڵһβʱ2 ͼƬĿ¼ + if (saveimage) + { + string OcrBar = xK_HisenceWord.OcrBar; + string namecutSavepath = OcrBar + "\\" + blockIndex + "\\" + OcrBar + "result.jpg"; + + CutSavePath = Path.Combine(RootPath, namecutSavepath); + //õĿ¼ + if (!Directory.Exists(Path.GetDirectoryName(CutSavePath))) + { + Directory.CreateDirectory(Path.GetDirectoryName(CutSavePath)); + } + + Cv2.ImWrite(CutSavePath, CutBlockMat); + } + + //Źؼַֺ + List OcrTextinsert = new List();//Źؼ + List OcrFuzzyTextInsert = new List();//ģ + + //һͼƬϵĹؼֱȶ ʾұע + //MatMessage(saveimage, ocrBar, blockIndex, ref CutBlockMat, ref mLcut, ref OcrTextinsert, ref OcrFuzzyTextInsert, ref CutSavePath); + //ҵֵĹؼ Ҳݿ + for (int j = 0; j < mLcut.ResultDetails.Count; j++) + { + var resultDetail = mLcut.ResultDetails[j]; + if (resultDetail.Score > 0.8) + { + string jdetial = mLcut.ResultDetails[j].LabelDisplay; + string result = Regex.Replace(jdetial, "[ \\[ \\] \\^ \\-_*DD(^)$%~!@#$&%+=<>!??:?`,.;/\"-]", ""); + if (Regex.IsMatch(result, @"\d")) + { + OcrTextinsert.Add(result); + } + OcrFuzzyTextInsert.Add(result); + } + } + DateTime dt = DateTime.Now; + + + string MainWoidStr = OcrTextinsert.Join("##"); //ʵʵĹؼ + string TextWoidStr = OcrFuzzyTextInsert.Join("##"); //ʵʵļ + string SQLStr = "";//־ʾݿĹؼ + bool juanjiMatch = false; + switch (blockIndex) + { + case "1"://ȫƥ Ϣ + { + xK_HisenceWord.OneblockPath = CutSavePath; + xK_HisenceWord.OneblockMainWord = MainWoidStr; + xK_HisenceWord.OneblockText = TextWoidStr; + SQLStr = SQlxK_HisenceWord.OneblockMainWord; + if (!saveimage) + { + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.OneblockMainWord, SQLStr); + if (!OneIF) + { + //ʾİ + DisplayResult(mLcut); + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 1); + } + else + { + Defet_OnDetectionDone(OKMat, 1); + } + + } + + } + break; + case "2":// ƥ + { + xK_HisenceWord.TwoblockPath = CutSavePath; + xK_HisenceWord.TwoblockMainWord = MainWoidStr; + xK_HisenceWord.TwoblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.TwoblockMainWord; + bool OneIF1 = ManagerModelHelper.StrMatch2(xK_HisenceWord.TwoblockMainWord, SQlxK_HisenceWord.TwoblockMainWord); + OneIF1 = true; + //ڶ ƥ + string PathSql = SQlxK_HisenceWord.TwoblockPath; + // + + Rect rectsql = CheckDiffSciHelper.strChangeRect(SQlxK_HisenceWord.TwoRect); + Rect rectDet = CheckDiffSciHelper.strChangeRect(xK_HisenceWord.TwoRect); + juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rectsql, rectDet, (bool)SQlxK_HisenceWord.TwoIFWhile, "D://Hisence//Test"); + // juanjiMatch = true; + + if (!OneIF1 || !juanjiMatch) + { + OneIF = false; + DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 2); + } + else + { + OneIF = true; + Defet_OnDetectionDone(OKMat, 2); + } + } + } + break; + case "3"://ƥ + { + xK_HisenceWord.ThreeblockPath = CutSavePath; + xK_HisenceWord.ThreeblockMainWord = MainWoidStr; + xK_HisenceWord.ThreeblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.ThreeblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.ThreeblockMainWord, SQlxK_HisenceWord.ThreeblockMainWord); + if (!OneIF) + { + DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 3); + } + else + { + Defet_OnDetectionDone(OKMat, 3); + } + + } + } + break; + case "4"://ֽƥ + { + xK_HisenceWord.FourblockPath = CutSavePath; + xK_HisenceWord.FourblockMainWord = MainWoidStr; + xK_HisenceWord.FourblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.FourblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FourblockMainWord, SQlxK_HisenceWord.FourblockMainWord); + if (!OneIF) + { + DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 4); + } + else + { + Defet_OnDetectionDone(OKMat, 4); + } + + } + } + break; + case "5"://ֽƥ + { + xK_HisenceWord.FiveblockPath = CutSavePath; + xK_HisenceWord.FiveblockMainWord = MainWoidStr; + xK_HisenceWord.FiveblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.FiveblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.FiveblockMainWord, SQlxK_HisenceWord.FiveblockMainWord); + if (!OneIF) + { + DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 5); + } + else + { + Defet_OnDetectionDone(OKMat, 5); + } + } + } + break; + case "6"://ֽƥ + { + xK_HisenceWord.SixblockPath = CutSavePath; + xK_HisenceWord.SixblockMainWord = MainWoidStr; + xK_HisenceWord.SixblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.SixblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SixblockMainWord, SQlxK_HisenceWord.SixblockMainWord); + if (!OneIF) + { + // DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 6); + } + else + { + Defet_OnDetectionDone(OKMat, 6); + } + + } + } + break; + case "7"://ֽƥ + { + xK_HisenceWord.SevenblockPath = CutSavePath; + xK_HisenceWord.SevenblockMainWord = MainWoidStr; + xK_HisenceWord.SevenblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.SevenblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.SevenblockMainWord, SQlxK_HisenceWord.SevenblockMainWord); + if (!OneIF) + { + //DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 7); + } + else + { + Defet_OnDetectionDone(OKMat, 7); + } + } + } + break; + case "8"://ֽƥ + { + xK_HisenceWord.EightblockPath = CutSavePath; + xK_HisenceWord.EightblockMainWord = MainWoidStr; + xK_HisenceWord.EightblockText = TextWoidStr; + if (!saveimage) + { + SQLStr = SQlxK_HisenceWord.EightblockMainWord; + OneIF = ManagerModelHelper.StrMatch2(xK_HisenceWord.EightblockMainWord, SQlxK_HisenceWord.EightblockMainWord); + if (!OneIF) + { + // DisplayResult(mLcut); + //ʾİ + // mLcut.ResultMap; + Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 8); + } + else + { + Defet_OnDetectionDone(OKMat, 8); + } + } + } + break; + } + using (StreamWriter sw = new StreamWriter("D://Hisence//logsMatch.log", true)) + { + string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString(); + sw.WriteLine(filename + "\n"); + if (blockIndex == "2") + { + sw.WriteLine("ƥ" + juanjiMatch); + } + sw.WriteLine("" + blockIndex + "ݿ" + SQLStr + "ؼ" + MainWoidStr + "" + OneIF + "\n"); + sw.WriteLine("" + blockIndex + " " + TextWoidStr + "\n"); + + sw.Flush(); + } + return OneIF; + + + } private void bnGetParam2_Click_1(object sender, EventArgs e) { @@ -2425,5 +3088,26 @@ namespace HisenceYoloDetection { } + + private void XZerorbx_Click(object sender, EventArgs e) + { + if (!ifXZeroValue) + { + this.XZerorbx.Checked = true; + // + melsecPLCTCPDriver.WriteInt("503", 1); + ifXZeroValue = true; + this.txtSetValue.Text = "1"; + myLog("XῪʼ", DateTime.Now); + } + else + { + this.XZerorbx.Checked = false; + melsecPLCTCPDriver.WriteInt("503", 0); + ifXZeroValue = false; + this.txtSetValue.Text = "0"; + myLog("Xֹͣ", DateTime.Now); + } + } } } diff --git a/HisenceYoloDetection/ManagerModelHelper.cs b/HisenceYoloDetection/ManagerModelHelper.cs index 9aa0ce4..bc5a96f 100644 --- a/HisenceYoloDetection/ManagerModelHelper.cs +++ b/HisenceYoloDetection/ManagerModelHelper.cs @@ -12,6 +12,8 @@ using System.Diagnostics.Eventing.Reader; using static System.Runtime.InteropServices.JavaScript.JSType; using System.Drawing; using Microsoft.VisualBasic; +using System.Xml; +using System.Runtime.InteropServices; namespace HisenceYoloDetection { @@ -19,267 +21,11 @@ namespace HisenceYoloDetection public static class ManagerModelHelper { public static string RootPath = "D:\\Hisence\\SQLImages\\"; - - /// - /// 全图洗衣机 裁剪之后 OCR识别的结果 - /// - /// - /// - /// 全图图片 - /// 全局图片上的目标定位结果(包括定位矩形框) - /// - /// - /// 返回的定位框的结果 - /// - /// - public static void InsertSqlRunDataButton(bool saveimage,ref Mat CutMat, ref Mat currentMatC, MLResult cam1TwoML, MLResult cam1Button, ref XK_HisenceWord xK_HisenceWord, ref List strMatList, ref List strMatRefList, ref PaddleOcrModel IOcrModel) - { -#if true - //try - //{ - - Mat mResultCut = currentMatC.Clone(); - Rect areaBlack=new Rect(); - //旋钮的位置 - if (cam1Button.ResultDetails.Count == 1) - { - Mat mResultCuti = mResultCut.Clone(); - int rectsx = cam1Button.ResultDetails[0].Rect.X; - int rectsy = cam1Button.ResultDetails[0].Rect.Y; - int rectsWidth = cam1Button.ResultDetails[0].Rect.Width; - int rectsHeight = cam1Button.ResultDetails[0].Rect.Height; - areaBlack = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); - } - for (int i = 0; i < cam1TwoML.ResultDetails.Count; i++) - { - Mat mResultCuti = mResultCut.Clone(); - int rectsx = cam1TwoML.ResultDetails[i].Rect.X; - int rectsy = cam1TwoML.ResultDetails[i].Rect.Y; - int rectsWidth = cam1TwoML.ResultDetails[i].Rect.Width; - int rectsHeight = cam1TwoML.ResultDetails[i].Rect.Height; - - string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay; - Rect area2 = new Rect(); - if (blockIndex == "2"|| blockIndex == "7")//根据旋钮扩大范围 - { - areaBlack.X -= rectsx; - areaBlack.Y -= rectsy; - area2 = areaBlack; - string TwoRectStr= CheckDiffSciHelper.rectChangeStr(area2); - xK_HisenceWord.TwoRect = TwoRectStr; - //Mat matCutblack = new Mat(mResultCuti, area2); - //if((bool)xK_HisenceWord.TwoIFWhile) - //{ - // matCutblack.SetTo(Scalar.Black); - //} - //else - //{ - // matCutblack.SetTo(Scalar.Black); - //} - - - //rectsx -= rectsWidth; - //rectsy -= 50; - //rectsWidth = rectsWidth + 2 * rectsWidth; - //rectsHeight = rectsHeight + 2 + 50; - - Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); - - Mat matCut = new Mat(mResultCuti, area); - CutMat = matCut.Clone(); - - //Mat TwoCut = new Mat(mResultCuti, area2); - //TwoCut.SetTo(Scalar.Black); - //OCR识别裁剪图片 - MLRequest reqcut = new MLRequest(); - reqcut.currentMat = matCut.Clone(); - MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut); - - //if (mLCut.IsSuccess) - //{ - // DateTime dt = DateTime.Now; - // mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg"); - - //} - - BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord); - } - else - { - Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); - - Mat matCut = new Mat(mResultCuti, area); - - //OCR识别裁剪图片 - MLRequest reqcut = new MLRequest(); - reqcut.currentMat = matCut.Clone(); - MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut); - //if (mLCut.IsSuccess) - //{ - // DateTime dt = DateTime.Now; - // mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg"); - - //} - - BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord); - } - - - //插入数据库 - - } - - - //} - //catch (Exception ex) - //{ - - //} -#endif - - - } - - /// - /// 全图洗衣机 裁剪之后 OCR识别的结果 - /// - /// 全图图片 - /// 全局图片上的目标定位结果(包括定位矩形框) - /// 返回的定位框的结果 - public static void InsertSqlRunData(bool saveimage, ref Mat currentMatC, MLResult cam1TwoML, ref XK_HisenceWord xK_HisenceWord, /*ref List strMatList, ref List strMatRefList,*/ ref PaddleOcrModel IOcrModel) - { -#if true - //try - //{ - - - Mat mResultCut = currentMatC.Clone(); - - for (int i = 0; i < cam1TwoML.ResultDetails.Count; i++) - { - Mat mResultCuti = mResultCut.Clone(); - int rectsx = cam1TwoML.ResultDetails[i].Rect.X; - int rectsy = cam1TwoML.ResultDetails[i].Rect.Y; - int rectsWidth = cam1TwoML.ResultDetails[i].Rect.Width; - int rectsHeight = cam1TwoML.ResultDetails[i].Rect.Height; - string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay; - - - Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight); - Mat matCut = new Mat(mResultCuti, area); - - //OCR识别裁剪图片 - MLRequest reqcut = new MLRequest(); - reqcut.currentMat = matCut.Clone(); - MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut); - //if (mLCut.IsSuccess) - //{ - // DateTime dt = DateTime.Now; - // mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg"); - - //} - - BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord); - //插入数据库 - - } - - - //} - //catch (Exception ex) - //{ - - //} -#endif - - - } - - - /// - /// - /// - /// 是否保存 - /// 裁剪的一块索引 - /// 裁剪的一张图片 - /// 裁剪图片的一些信息 - /// 要存储入数据库的东西 - public static void BlockChangeFun(bool saveimage, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref XK_HisenceWord xK_HisenceWord) - { - string ocrBar = xK_HisenceWord.OcrBar; - //存放关键字和所有字符串 - List OcrTextinsert = new List();//存放关键字 - List OcrFuzzyTextInsert = new List();//存放模糊字 - string CutSavePath = ""; - CombineMessage(saveimage, ocrBar, blockIndex, ref CutBlockMat, ref mLcut, ref OcrTextinsert, ref OcrFuzzyTextInsert, ref CutSavePath); - - switch (blockIndex) - { - case "1"://完全匹配 重量信息 - { - xK_HisenceWord.OneblockPath = CutSavePath; - xK_HisenceWord.OneblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.OneblockText = OcrFuzzyTextInsert.Join("##"); - - } - break; - case "2"://控制面板 匹配 - { - xK_HisenceWord.TwoblockPath = CutSavePath; - xK_HisenceWord.TwoblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.TwoblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "3"://第三块板匹配 - { - xK_HisenceWord.ThreeblockPath = CutSavePath; - xK_HisenceWord.ThreeblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.ThreeblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "4"://贴纸匹配 - { - xK_HisenceWord.FourblockPath = CutSavePath; - xK_HisenceWord.FourblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.FourblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "5"://贴纸匹配 - { - xK_HisenceWord.FiveblockPath = CutSavePath; - xK_HisenceWord.FiveblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.FiveblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "6"://贴纸匹配 - { - xK_HisenceWord.SixblockPath = CutSavePath; - xK_HisenceWord.SixblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.SixblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "7"://贴纸匹配 - { - xK_HisenceWord.SevenblockPath = CutSavePath; - xK_HisenceWord.SevenblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.SevenblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - case "8"://贴纸匹配 - { - xK_HisenceWord.EightblockPath = CutSavePath; - xK_HisenceWord.EightblockMainWord = OcrTextinsert.Join("##"); - xK_HisenceWord.EightblockText = OcrFuzzyTextInsert.Join("##"); - } - break; - } - - } - /// + /// /// /// /// 是否保存本地图片 @@ -290,8 +36,9 @@ namespace HisenceYoloDetection /// 关键字 /// 所有字 /// 图片保存路径 - public static void CombineMessage(bool saveimage, string OcrBar, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref List OcrTextinsert, ref List OcrFuzzyTextInsert, ref string cutSavepath) + public static void MatMessage(bool saveimage, string OcrBar, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref List OcrTextinsert, ref List OcrFuzzyTextInsert, ref string cutSavepath) { + //在这里面找到带数字的关键字 将所有字也存放在数据库中 for (int j = 0; j < mLcut.ResultDetails.Count; j++) { @@ -362,7 +109,7 @@ namespace HisenceYoloDetection try { - string TwoRectstr=XKSQL.TwoRect; + string TwoRectstr = XKSQL.TwoRect; string oneBlockWordSql = XKSQL.OneblockMainWord; string twoBlockWordSql = XKSQL.TwoblockMainWord; string threeBlockWordSql = XKSQL.ThreeblockMainWord; @@ -394,10 +141,10 @@ namespace HisenceYoloDetection //第二快 卷积匹配 string PathSql = XKSQL.TwoblockPath; // - + Rect rectsql = CheckDiffSciHelper.strChangeRect(TwoRectstr); Rect rectDet = CheckDiffSciHelper.strChangeRect(XKDet.TwoRect); - bool twoif2 = CheckDiffSciHelper.CheckDiffSci(PathSql, detMat, rectsql, rectDet,(bool)XKSQL.TwoIFWhile, "D://Test"); + bool twoif2 = CheckDiffSciHelper.CheckDiffSci(PathSql, detMat, rectsql, rectDet, (bool)XKSQL.TwoIFWhile, "D://Test"); DateTime dt = DateTime.Now; using (StreamWriter sw = new StreamWriter("D://Hisence//logsMatch.log", true)) { @@ -411,11 +158,11 @@ namespace HisenceYoloDetection sw.WriteLine(sixBlockWordSql + " " + sixBlockWordDet + "\n"); sw.WriteLine(sevenBlockWordSql + " " + sevenBlockWordDet + "\n"); sw.WriteLine(eightBlockWordSql + " " + eightBlockWordDet + "\n"); - sw.WriteLine( " 卷积匹配 " + twoif2 + "\n"); + sw.WriteLine(" 卷积匹配 " + twoif2 + "\n"); sw.Flush(); } //第三块区域一直都是false - if (OneIF && TwoIF && ThreeIF && FourIF && FiveIF && SixIF && SenvenIF && EightIF&& twoif2) + if (OneIF && TwoIF && ThreeIF && FourIF && FiveIF && SixIF && SenvenIF && EightIF && twoif2) { return true; } @@ -434,50 +181,181 @@ namespace HisenceYoloDetection } } + public static bool StrMatch(string SqlText,string DetText) + { + // 计算Levenshtein距离 + int distance = LevenshteinDistance(SqlText, DetText); + + // 计算相似度(相似度等于1减去标准化的Levenshtein距离) + double similarity = 1 - ((double)distance / Math.Max(SqlText.Length, DetText.Length)); + bool areEqual = false; + if (similarity < 0.5) + { + areEqual = false; + } + else + { + areEqual = true; + } + //string[] sArraysql = Regex.Split(SqlText, "##", RegexOptions.IgnoreCase); + //string[] sArraydet = Regex.Split(DetText, "##", RegexOptions.IgnoreCase); + //bool areEqual = sArraysql.OrderBy(x => x).SequenceEqual(sArraydet.OrderBy(x => x)); + return areEqual; + + } + static bool AreArraysEqual(string[] array1, string[] array2) + { + if (array1.Length != array2.Length) + { + return false; + } + + // 将数组排序 + Array.Sort(array1); + Array.Sort(array2); + + // 逐个比较对应位置的数字 + for (int i = 0; i < array1.Length; i++) + { + if (array1[i] != array2[i]) + { + return false; + } + } + + return true; + } + static bool AreMoreThanHalfEqual(string[] array1, string[] array2) + { + int io = 0; + foreach (string ch1 in array1) + { + foreach (string ch2 in array2) + { + if (ch1 == ch2) + { + io++; + Console.WriteLine(ch1); + break; + } + } + } + + //// 将数组转换为集合 + //HashSet set1 = new HashSet(array1); + //HashSet set2 = new HashSet(array2); + + //// 计算交集的数量 + //int intersectionCount = set1.Intersect(set2).Count(); + + // 判断交集数量是否超过一半 + return io > array1.Length / 2; + } + public static bool StrMatch2(string SqlText, string DetText) + { + + string[] numbers = FindNumbers(SqlText); + + if (numbers.Length>0) + { + //Console.WriteLine("字符串中包含数字:" + number); + } + else + { + Console.WriteLine("字符串中不包含数字"); + } + + string[] numbers2= FindNumbers(DetText); + + + if (numbers2.Length>0) + { + //Console.WriteLine("字符串中包含数字:" + detnumber); + } + else + { + Console.WriteLine("字符串中不包含数字"); + } + bool areEqual ; + if (numbers2.Length>2&& numbers.Length > 2) + { + areEqual = AreMoreThanHalfEqual(numbers, numbers2); + } + else + { + areEqual = AreArraysEqual(numbers, numbers2); + } + + + //string[] sArraysql = Regex.Split(SqlText, "##", RegexOptions.IgnoreCase); + //string[] sArraydet = Regex.Split(DetText, "##", RegexOptions.IgnoreCase); + //for(int i=0;i< sArraydet.Length;i++) + //{ + // string s = sArraydet[i]; + // for (int j = 0; j < sArraysql.Length; j++) + // { + + // } + //} + //bool areEqual = sArraysql.OrderBy(x => x).SequenceEqual(sArraydet.OrderBy(x => x)); + return areEqual; + + } + static string[] FindNumbers(string str) + { + Regex regex = new Regex(@"\d+"); + MatchCollection matches = regex.Matches(str); + string[] numbers = new string[matches.Count]; + for (int i = 0; i < matches.Count; i++) + { + numbers[i] = matches[i].Value; + } + return numbers; + } + + static string GetNumber(string str) + { + Regex regex = new Regex(@"\d+"); + Match match = regex.Match(str); + if (match.Success) + { + return match.Value; + } + else + { + return null; + } + } public static bool isMatchStr(string SqlText, string DetText) { - - - if ((SqlText == "" || SqlText == null) && (DetText == "" || DetText == null)) + + + if ((SqlText == "" || SqlText == null) && (DetText == "" || DetText == null)) { return true; } else { - if(SqlText!=null&& DetText!=null) + if (SqlText != null && DetText != null) { if (SqlText.Contains("##") && DetText.Contains("##")) { - // 计算Levenshtein距离 - int distance = LevenshteinDistance(SqlText, DetText); - - // 计算相似度(相似度等于1减去标准化的Levenshtein距离) - double similarity = 1 - ((double)distance / Math.Max(SqlText.Length, DetText.Length)); - bool areEqual = false; - if (similarity < 0.5) - { - areEqual = false; - } - else - { - areEqual = true; - } - //string[] sArraysql = Regex.Split(SqlText, "##", RegexOptions.IgnoreCase); - //string[] sArraydet = Regex.Split(DetText, "##", RegexOptions.IgnoreCase); - //bool areEqual = sArraysql.OrderBy(x => x).SequenceEqual(sArraydet.OrderBy(x => x)); - return areEqual; - + return StrMatch2(SqlText, DetText); + } + else if(SqlText != "" && DetText != "") + { + return StrMatch2(SqlText, DetText); } else { - return true; + return false; } } else { return false; } - + } } // 计算Levenshtein距离 diff --git a/HisenceYoloDetection/MelsecPLCTCPDriver.cs b/HisenceYoloDetection/MelsecPLCTCPDriver.cs index bc8b626..ad6e1bf 100644 --- a/HisenceYoloDetection/MelsecPLCTCPDriver.cs +++ b/HisenceYoloDetection/MelsecPLCTCPDriver.cs @@ -11,7 +11,7 @@ using static OpenCvSharp.FileStorage; -public class MelsecPLCTCPDriver1 +public class MelsecPLCTCPDriver { private MelsecMcNet melsecMc = new MelsecMcNet(); // private HslCommunication.ModBus.ModbusTcpNet melsecMc = new HslCommunication.ModBus.ModbusTcpNet(); diff --git a/HisenceYoloDetection/PaddleOcrModel.cs b/HisenceYoloDetection/PaddleOcrModel.cs index 6bd7b94..b3717c4 100644 --- a/HisenceYoloDetection/PaddleOcrModel.cs +++ b/HisenceYoloDetection/PaddleOcrModel.cs @@ -1,4 +1,4 @@ - +//#define USE_MULTI_THREAD using Newtonsoft.Json; using OpenCvSharp; @@ -183,8 +183,7 @@ namespace XKRS.Device.SimboVision.SimboHelper detectionResultDetail.LabelNo = det.classId; //todo: 标签名相对应 detectionResultDetail.LabelDisplay = det.classname; - detectionResultDetail.Rect = new Rectangle(rectX.ToInt(), rectY.ToInt(), rectWidth.ToInt(), rectHeight.ToInt()); - detectionResultDetail.Score = det.fScore; + detectionResultDetail.Rect = new Rectangle((int)rectX, (int)rectY, (int)rectWidth, (int)rectHeight); detectionResultDetail.Score = det.fScore; detectionResultDetail.LabelName = det.classname; detectionResultDetail.Area = det.area; result.ResultDetails.Add(detectionResultDetail); diff --git a/HisenceYoloDetection/PaddleOcrModelCountry.cs b/HisenceYoloDetection/PaddleOcrModelCountry.cs index 59e244f..c690ad0 100644 --- a/HisenceYoloDetection/PaddleOcrModelCountry.cs +++ b/HisenceYoloDetection/PaddleOcrModelCountry.cs @@ -139,12 +139,21 @@ namespace XKRS.Device.SimboVision.SimboHelper } - private void ConvertJsonResult(string json, ref MLResult result) + + + //string pattern = @"^[A-Za-z0-9]+$"; //@意思忽略转义,+匹配前面一次或多次,$匹配结尾 + + // Match match = Regex.Match(str, pattern); + + + + + private void ConvertJsonResult(string json, ref MLResult result) { // json = "{\"FastDetResult\":[{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654843,\"rect\":[175,99,110,594]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654589,\"rect\":[2608,19,104,661]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.654285,\"rect\":[1275,19,104,662]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.620762,\"rect\":[1510,95,107,600]},{\"cls_id\":0,\"cls\":\"liewen\",\"fScore\":0.617812,\"rect\":[2844,93,106,602]}]}"; // Console.WriteLine("检测结果JSON:" + json); - SegResultCountry detResult = JsonConvert.DeserializeObject(json); + SegResult detResult = JsonConvert.DeserializeObject(json); if (detResult == null) { return; @@ -178,46 +187,27 @@ namespace XKRS.Device.SimboVision.SimboHelper float rectWidth = width; float rectHeight = height; + //string pattern = @"^[A-Za-z0-9]+$"; //@意思忽略转义,+匹配前面一次或多次,$匹配结尾 + + // bool match = Regex.IsMatch(det.classname, pattern); + if (det.classname != ""&& rectWidth>0&& rectHeight>0) + { + DetectionResultDetail detectionResultDetail = new DetectionResultDetail(); + detectionResultDetail.LabelNo = det.classId; + //todo: 标签名相对应 + detectionResultDetail.LabelDisplay = det.classname; + detectionResultDetail.Rect = new Rectangle((int)rectX, (int)rectY, (int)rectWidth, (int)rectHeight); + detectionResultDetail.Score = det.fScore; + detectionResultDetail.LabelName = det.classname; + detectionResultDetail.Area = det.area; + result.ResultDetails.Add(detectionResultDetail); + } + - DetectionResultDetail detectionResultDetail = new DetectionResultDetail(); - detectionResultDetail.LabelNo = det.classId; - //todo: 标签名相对应 - detectionResultDetail.LabelDisplay = det.classname; - detectionResultDetail.Rect = new Rectangle(rectX.ToInt(), rectY.ToInt(), rectWidth.ToInt(), rectHeight.ToInt()); - detectionResultDetail.Score = det.fScore; - detectionResultDetail.LabelName = det.classname; - detectionResultDetail.Area = det.area; - result.ResultDetails.Add(detectionResultDetail); - //if (det.classname.Contains("Model:") || det.classname.Contains("Batch:")) - //{ - // string[] sArray = Regex.Split(det.classname, "Model:", RegexOptions.IgnoreCase); - // string splitBar = ""; - // if (sArray.Count() > 1) - // { - // result.WashMachineBar = sArray[1]; - // string[] aBatch = Regex.Split(sArray[0], "Batch:", RegexOptions.IgnoreCase); - // if (aBatch.Count() > 1) - // { - // result.WashMachineBatch = aBatch[1]; - // } - // } - //} - //if (det.classname.Contains("SN:") ) - //{ - // string[] sArray = Regex.Split(det.classname, "SN:", RegexOptions.IgnoreCase); - // string splitBar = ""; - // if (sArray.Count() > 1) - // { - // result.WashMachineSN = sArray[1]; - - // } - //} } } - - [HandleProcessCorruptedStateExceptions] public MLResult RunInferenceFixed(MLRequest req) diff --git a/HisenceYoloDetection/SimboObjectDetection.cs b/HisenceYoloDetection/SimboObjectDetection.cs index f15bb1b..da5e0f7 100644 --- a/HisenceYoloDetection/SimboObjectDetection.cs +++ b/HisenceYoloDetection/SimboObjectDetection.cs @@ -174,6 +174,8 @@ public class SimboObjectDetection result.ResultDetails.Add(detectionResultDetail); } + result.ResultDetails.Sort((s1, s2) => s1.Rect.X.CompareTo(s2.Rect.X)); + } diff --git a/HisenceYoloDetection/SimboVisionMLBase.cs b/HisenceYoloDetection/SimboVisionMLBase.cs index 23d998f..dbe2240 100644 --- a/HisenceYoloDetection/SimboVisionMLBase.cs +++ b/HisenceYoloDetection/SimboVisionMLBase.cs @@ -2,6 +2,7 @@ using OpenCvSharp; using System; using System.Collections.Generic; +using System.ComponentModel; using System.Drawing; using System.Runtime.InteropServices; @@ -180,8 +181,16 @@ public class MLResult public string ResultMessage; public Bitmap ResultMap; public List ResultDetails = new List(); - + } +public enum ResultState +{ + [Description("检测NG")] + DetectNG = 0, + [Description("OK")] + OK = 1, +} + public static class MLEngine { diff --git a/HisenceYoloDetection/StaticHelper.cs b/HisenceYoloDetection/StaticHelper.cs index ece9334..77a7ec9 100644 --- a/HisenceYoloDetection/StaticHelper.cs +++ b/HisenceYoloDetection/StaticHelper.cs @@ -1,6 +1,7 @@ using Microsoft.CSharp.RuntimeBinder; //using Newtonsoft.Json; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Drawing.Imaging; using System.Dynamic; using System.Linq.Expressions; @@ -13,6 +14,7 @@ using System.Text.RegularExpressions; public static class StaticHelper { + //判断是否为正整数 public static bool IsInt(string inString) { diff --git a/HisenceYoloDetection/VirtualPLCTCPDriver.cs b/HisenceYoloDetection/VirtualPLCTCPDriver.cs index d52874e..cb6e723 100644 --- a/HisenceYoloDetection/VirtualPLCTCPDriver.cs +++ b/HisenceYoloDetection/VirtualPLCTCPDriver.cs @@ -12,7 +12,7 @@ using static OpenCvSharp.FileStorage; -public class MelsecPLCTCPDriver +public class MelsecPLCTCPDriver1 { // private MelsecMcNet melsecMc = new MelsecMcNet(); // private HslCommunication.ModBus.ModbusTcpNet melsecMc = new HslCommunication.ModBus.ModbusTcpNet(); diff --git a/HisenceYoloDetection/yyb.douyin.ico b/HisenceYoloDetection/yyb.douyin.ico deleted file mode 100644 index aad76a4..0000000 Binary files a/HisenceYoloDetection/yyb.douyin.ico and /dev/null differ