diff --git a/TetraParkOCR/Form1.Designer.cs b/TetraParkOCR/Form1.Designer.cs index b99ce95..72978fe 100644 --- a/TetraParkOCR/Form1.Designer.cs +++ b/TetraParkOCR/Form1.Designer.cs @@ -112,6 +112,9 @@ this.label5 = new System.Windows.Forms.Label(); this.lbl_height = new System.Windows.Forms.Label(); this.checkBox1 = new System.Windows.Forms.CheckBox(); + this.label11 = new System.Windows.Forms.Label(); + this.label12 = new System.Windows.Forms.Label(); + this.textBox1 = new System.Windows.Forms.TextBox(); this.tableLayoutPanel_productInformtion.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.Location_Display)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.Ocr_picBox)).BeginInit(); @@ -133,7 +136,7 @@ this.btn_OrderNum.BackColor = System.Drawing.Color.DeepSkyBlue; this.btn_OrderNum.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btn_OrderNum.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.btn_OrderNum.Location = new System.Drawing.Point(519, 26); + this.btn_OrderNum.Location = new System.Drawing.Point(520, 19); this.btn_OrderNum.Name = "btn_OrderNum"; this.btn_OrderNum.Size = new System.Drawing.Size(116, 29); this.btn_OrderNum.TabIndex = 2; @@ -145,7 +148,7 @@ // txt_OrderNum // this.txt_OrderNum.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.txt_OrderNum.Location = new System.Drawing.Point(328, 26); + this.txt_OrderNum.Location = new System.Drawing.Point(329, 19); this.txt_OrderNum.Name = "txt_OrderNum"; this.txt_OrderNum.Size = new System.Drawing.Size(176, 29); this.txt_OrderNum.TabIndex = 3; @@ -154,7 +157,7 @@ // lbl_OrderNum // this.lbl_OrderNum.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.lbl_OrderNum.Location = new System.Drawing.Point(231, 26); + this.lbl_OrderNum.Location = new System.Drawing.Point(232, 19); this.lbl_OrderNum.Name = "lbl_OrderNum"; this.lbl_OrderNum.Size = new System.Drawing.Size(95, 29); this.lbl_OrderNum.TabIndex = 4; @@ -194,7 +197,7 @@ // lbl_verOcr // this.lbl_verOcr.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); - this.lbl_verOcr.Location = new System.Drawing.Point(25, 46); + this.lbl_verOcr.Location = new System.Drawing.Point(14, 53); this.lbl_verOcr.Name = "lbl_verOcr"; this.lbl_verOcr.Size = new System.Drawing.Size(99, 29); this.lbl_verOcr.TabIndex = 9; @@ -1011,6 +1014,9 @@ // // panel_midup // + this.panel_midup.Controls.Add(this.textBox1); + this.panel_midup.Controls.Add(this.label12); + this.panel_midup.Controls.Add(this.label11); this.panel_midup.Controls.Add(this.checkBox1); this.panel_midup.Controls.Add(this.button1); this.panel_midup.Controls.Add(this.listBox1); @@ -1146,6 +1152,36 @@ this.checkBox1.Text = "仅计算坐标"; this.checkBox1.UseVisualStyleBackColor = true; // + // label11 + // + this.label11.AutoSize = true; + this.label11.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.label11.Location = new System.Drawing.Point(209, 59); + this.label11.Name = "label11"; + this.label11.Size = new System.Drawing.Size(183, 16); + this.label11.TabIndex = 29; + this.label11.Text = "当前单颜色段字符长度:"; + this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + // + // label12 + // + this.label12.AutoSize = true; + this.label12.Location = new System.Drawing.Point(458, 61); + this.label12.Name = "label12"; + this.label12.Size = new System.Drawing.Size(17, 12); + this.label12.TabIndex = 31; + this.label12.Text = "mm"; + // + // textBox1 + // + this.textBox1.Font = new System.Drawing.Font("宋体", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.textBox1.Location = new System.Drawing.Point(391, 52); + this.textBox1.Name = "textBox1"; + this.textBox1.Size = new System.Drawing.Size(61, 29); + this.textBox1.TabIndex = 32; + this.textBox1.Text = "19"; + this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); @@ -1266,6 +1302,9 @@ private System.Windows.Forms.Label lbl_height; private System.Windows.Forms.Label label5; private System.Windows.Forms.CheckBox checkBox1; + private System.Windows.Forms.Label label11; + private System.Windows.Forms.TextBox textBox1; + private System.Windows.Forms.Label label12; } } diff --git a/TetraParkOCR/Form1.cs b/TetraParkOCR/Form1.cs index 6dba531..7bda9fd 100644 --- a/TetraParkOCR/Form1.cs +++ b/TetraParkOCR/Form1.cs @@ -90,6 +90,7 @@ namespace TetraPackOCR /// int NumberOfLanes; double m_width, m_Gradient, m_DistX, m_DistY, m_height; + int m_ColorMax = 0; /// /// 定位Vpp文件 /// @@ -105,7 +106,7 @@ namespace TetraPackOCR bool PlcContinueFlag = false; //PLC状态旗帜 int mMatchingStr = 0;//接收当前OCR拍照位置 - List ocrAcc = new List(); + List ocrAcc = new List(); /// /// 相机对象 1和2 /// @@ -142,6 +143,7 @@ namespace TetraPackOCR InitializeCamerDET(); InitializePaddleOCR(); + log.Info("VPP文件加载中..."); try { myJobManagerDET = CogSerializer.LoadObjectFromFile(vppdetFile) as CogJobManager; @@ -169,16 +171,11 @@ namespace TetraPackOCR })); }); - - Task myTask = new Task(action); - //实例化一个线程列表 List tsk = new List(); - //将上面的加载vpp工作加入线程列表中 tsk.Add(myTask); - //读取线程列表内的工作让cpu为其开辟线程空间并执行程序 foreach (var iteam in tsk) { @@ -305,7 +302,6 @@ namespace TetraPackOCR m_dev_cam_ocr.StreamGrabber.ImageGrabbed += StreamGrabber_ImageGrabbed_OCR; m_dev_cam_ocr.StreamGrabber.GrabStarted += StreamGrabber_GrabStarted_OCR; // 打开Software Trigger - // Set Software Trigger m_dev_cam_ocr.TriggerSet.Open(TriggerSourceEnum.Software); if (!m_dev_cam_ocr.GrabUsingGrabLoopThread()) { @@ -775,6 +771,14 @@ namespace TetraPackOCR m_DistY = Convert.ToDouble(Y[1].Replace("mm", "")); lbl_DistY.Text = Y[1].Replace("mm", ""); log.Info("当前订单Y偏移:" + m_DistY); + + string alignColor = sheet3.Cells[n + ocrTextRequest[1].Count - 1, 3].Value.ToString(); + if (alignColor.Contains("单排")) + m_ColorMax = ocrTextRequest.Values.Select(x => x.Count).Max();// ocrTextRequest[1].Count; + else + m_ColorMax = alignColor.Replace("排布", "").Split(',').ToList().Select(int.Parse).Max(); + log.Info("当前排布方式:" + alignColor); + log.Info("单行最大数量:" + m_ColorMax.ToString()); break; } } @@ -977,6 +981,7 @@ namespace TetraPackOCR } private bool autorunFlag = false; private bool debugFlag = false; + private double m_textWidth = 0; private void btn_StarDet_manual_Click(object sender, EventArgs e) { if (camOCROpened == false || camDETOpened == false) @@ -998,6 +1003,7 @@ namespace TetraPackOCR InitLableColumn(); + m_textWidth = Convert.ToDouble(textBox1.Text); ocrAcc.Clear(); debugFlag = checkBox1.Checked; autorunFlag = check_Autorun.Checked; @@ -1122,7 +1128,7 @@ namespace TetraPackOCR //画定位的十字点 CPMARunStatus.DrawPointMarker(xx, yy, CogMisc.DegToRad(rr), CogColorConstants.Orange, Location_Display, "."); log.Info("定位计算已完成。"); - sendToPLC(xx, yy, rr); + SendToPLC(xx, yy, rr); } else { @@ -1368,7 +1374,7 @@ namespace TetraPackOCR Ocr_picBox.BackgroundImage = Bmp; int m1 = ocrTextResult.Count(x => x.Value); log.Info($"OCR识别完成,匹配成功{m1}个字符,匹配失败{ocrTextResult.Count - m1}个字符"); - ShowTheLansRs(mMatchingStr, (float)m1 / (float)ocrTextResult.Count); + ShowTheLansRs(mMatchingStr, (double)m1 / (double)ocrTextResult.Count); } NoticePLCCompleteOCR(); @@ -1539,7 +1545,7 @@ namespace TetraPackOCR /// /// PLC给的当前在第几道拍照 /// OCR字符相似度结果 - void ShowTheLansRs(float mM, float s) + void ShowTheLansRs(int mM, double s) { int mMnum = Convert.ToInt32(mM); switch (mMnum) @@ -1849,194 +1855,205 @@ namespace TetraPackOCR #endregion #region 通讯发送坐标 - private void sendToPLC(double x, double y, double r) + private void SendToPLC(double x, double y, double r) { try { - float xx = (float)x; - float yy = (float)y; - float rr = (float)r; - List ocrxx = new List(); - List ocryy = new List(); + double xx = x; + double yy = y; + double rr = r; - switch (m_ProductStandard) - { - case "TBA1000Slim": - switch (ocrTextRequest[1].Count) - { - case 1: - xx = (float)x - 9.5f; - break; - case 2: - xx = (float)x - 19f; - break; - case 3: - xx = (float)x - 28.5f; - break; - case 4: - xx = (float)x - 38f; - break; - case 5: - xx = (float)x - 47.5f; - break; - case 6: - xx = (float)x - 57f; - break; - } - break; - case "TBA250B": - switch (ocrTextRequest[1].Count) - { - case 1: - xx = (float)x - 9.5f; - break; - case 2: - xx = (float)x - 19f; - break; - case 3: - xx = (float)x - 28.5f; - break; - case 4: - xx = (float)x - 38f; - break; - case 5: - xx = (float)x - 38f; - break; - case 6: - xx = (float)x - 38f; - break; - } - break; - case "TBA125Slim": - switch (ocrTextRequest[1].Count) - { - case 1: - xx = (float)x - 9.5f; - break; - case 2: - xx = (float)x - 19f; - break; - case 3: - xx = (float)x - 28.5f; - break; - case 4: - xx = (float)x - 28.5f; - break; - case 5: - xx = (float)x - 28.5f; - break; - case 6: - xx = (float)x - 28.5f; - break; - } - break; - case "TPA250SQ": - switch (ocrTextRequest[1].Count) - { - case 1: - xx = (float)x - 9.5f; - break; - case 2: - xx = (float)x - 19f; - break; - case 3: - xx = (float)x - 19f; - break; - case 4: - xx = (float)x - 19f; - break; - case 5: - xx = (float)x - 19f; - break; - case 6: - xx = (float)x - 19f; - break; - } - break; - case "TPA200SQ": - switch (ocrTextRequest[1].Count) - { - case 1: - xx = (float)x - 9.5f; - break; - case 2: - xx = (float)x - 9f; - break; - case 3: - xx = (float)x - 9f; - break; - case 4: - xx = (float)x - 9f; - break; - case 5: - xx = (float)x - 9f; - break; - case 6: - xx = (float)x - 9f; - break; - } - break; - } + if (ocrTextRequest[1].Count == 0) + return; + xx = x - (m_textWidth * m_ColorMax) / 2.0; - float ocrx = 0, ocry = 0; - List l = new List(); - l.Add((float)NumberOfLanes); + //switch (m_ProductStandard) + // { + // case "TBA1000Slim": + // switch (ocrTextRequest[1].Count) + // { + // case 1: + // xx = (float)x - 9.5f; + // break; + // case 2: + // xx = (float)x - 19f; + // break; + // case 3: + // xx = (float)x - 28.5f; + // break; + // case 4: + // xx = (float)x - 38f; + // break; + // case 5: + // xx = (float)x - 47.5f; + // break; + // case 6: + // xx = (float)x - 57f; + // break; + // } + // break; + // case "TBA250B": + // switch (ocrTextRequest[1].Count) + // { + // case 1: + // xx = (float)x - 9.5f; + // break; + // case 2: + // xx = (float)x - 19f; + // break; + // case 3: + // xx = (float)x - 28.5f; + // break; + // case 4: + // xx = (float)x - 38f; + // break; + // case 5: + // xx = (float)x - 38f; + // break; + // case 6: + // xx = (float)x - 38f; + // break; + // } + // break; + // case "TBA125Slim": + // switch (ocrTextRequest[1].Count) + // { + // case 1: + // xx = (float)x - 9.5f; + // break; + // case 2: + // xx = (float)x - 19f; + // break; + // case 3: + // xx = (float)x - 28.5f; + // break; + // case 4: + // xx = (float)x - 28.5f; + // break; + // case 5: + // xx = (float)x - 28.5f; + // break; + // case 6: + // xx = (float)x - 28.5f; + // break; + // } + // break; + // case "TPA250SQ": + // switch (ocrTextRequest[1].Count) + // { + // case 1: + // xx = (float)x - 9.5f; + // break; + // case 2: + // xx = (float)x - 19f; + // break; + // case 3: + // xx = (float)x - 19f; + // break; + // case 4: + // xx = (float)x - 19f; + // break; + // case 5: + // xx = (float)x - 19f; + // break; + // case 6: + // xx = (float)x - 19f; + // break; + // } + // break; + // case "TPA200SQ": + // switch (ocrTextRequest[1].Count) + // { + // case 1: + // xx = (float)x - 9.5f; + // break; + // case 2: + // xx = (float)x - 9f; + // break; + // case 3: + // xx = (float)x - 9f; + // break; + // case 4: + // xx = (float)x - 9f; + // break; + // case 5: + // xx = (float)x - 9f; + // break; + // case 6: + // xx = (float)x - 9f; + // break; + // } + // break; + // } + + List locationXY = new List(); + double ocrx = 0, ocry = 0; + locationXY.Add((float)NumberOfLanes); int nol = Convert.ToInt32(NumberOfLanes); log.Info("当前订单OCR区域共" + nol + "组:"); + double degree = (rr * Math.PI) / 180; for (int item = 0; item < nol; item++) { if (item == 0) { - ocrx = xx - ((float)m_DistX) * ((float)Math.Cos((rr * Math.PI) / 180)) - ((float)m_DistY) * ((float)Math.Sin((rr * Math.PI) / 180)); - l.Add(ocrx); - ocry = yy + ((float)m_DistY) * ((float)Math.Cos((rr * Math.PI) / 180)) - ((float)m_DistX) * ((float)Math.Sin((rr * Math.PI) / 180)); - l.Add(ocry); - ocrxx.Add(ocrx); - ocryy.Add(ocry); + ocrx = xx - m_DistX * Math.Cos(degree) - m_DistY * Math.Sin(degree); + ocry = yy + m_DistY * Math.Cos(degree) - m_DistX * Math.Sin(degree); } else { - ocrx = ocrx + ((float)m_width * ((float)Math.Cos((rr * Math.PI) / 180))) + ((float)m_Gradient * ((float)Math.Sin((rr * Math.PI) / 180))); - l.Add(ocrx); - ocry = ocry - ((float)m_Gradient * ((float)Math.Cos((rr * Math.PI) / 180))) + ((float)m_width * ((float)Math.Sin((rr * Math.PI) / 180))); - l.Add(ocry); - ocrxx.Add(ocrx); - ocryy.Add(ocry); + ocrx = ocrx + m_width * Math.Cos(degree) + m_Gradient * Math.Sin(degree); + ocry = ocry - m_Gradient * Math.Cos(degree) + m_width * Math.Sin(degree); } - log.Info("第" + (item + 1) + "组坐标为:X:" + ocrxx[item] + ",Y:" + ocryy[item] + "。"); - if (ocrxx[item] < 0 || ocrxx[item] > 1600) + log.Info("第" + (item + 1) + "组坐标为:X:" + ocrx + ",Y:" + ocry + "。"); + if (ocrx < 0 || ocrx > 1600) { EnableStartDetect(); log.Error("x范围 0,1600!"); NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true)); return; } - if (ocryy[item] < -50 || ocryy[item] > 400) + + if (ocry < - 50) + { + ocrx = ocrx - m_height * Math.Sin(degree); + ocry = ocry + m_height * Math.Cos(degree); + log.Info("更新第" + (item + 1) + "组坐标为:X:" + ocrx + ",Y:" + ocry + "。"); + } + else if (ocry > 400) + { + ocrx = ocrx + m_height * Math.Sin(degree); + ocry = ocry - m_height * Math.Cos(degree); + log.Info("更新第" + (item + 1) + "组坐标为:X:" + ocrx + ",Y:" + ocry + "。"); + } + + if (ocry < -50 || ocry > 400) { EnableStartDetect(); log.Error("y范围 -50,400!"); NoticePLCCompleteDet(DataConverter.FloatToByte(0.0f, true)); return; } + locationXY.Add((float)ocrx); + locationXY.Add((float)ocry); } if (check_Autorun.Checked == false && !debugFlag) { - List d = new List(); - for (int i = 0; i < l.Count; i++) + List plcXY = new List(); + for (int i = 0; i < locationXY.Count; i++) { if (i == 0) { - d.Add(l[i]); + plcXY.Add(locationXY[i]); } else if (i % 2 == 1) { - d.Add(l[l.Count - i - 1]); - d.Add(l[l.Count - i]); + plcXY.Add(locationXY[locationXY.Count - i - 1]); + plcXY.Add(locationXY[locationXY.Count - i]); } } - NoticePLCCompleteDet(DataConverter.FloatToByte(d, true)); + NoticePLCCompleteDet(DataConverter.FloatToByte(plcXY, true)); log.Info("坐标已发送完成。"); } }