hisence-yolo-detection/HisenceYoloDetection/MainForm.cs
2024-05-31 10:14:57 +08:00

2043 lines
73 KiB
C#

using DVPCameraType;
using Microsoft.VisualBasic.Logging;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using OpenCvSharp.Extensions;
using OpenCvSharp.XFeatures2D;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Net;
using System.Numerics;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using XKRS.Device.SimboVision.SimboHelper;
using static HisenceYoloDetection.MainForm;
using static System.Runtime.InteropServices.JavaScript.JSType;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace HisenceYoloDetection
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
MGSCameraDriver Cam1 = new MGSCameraDriver();
MGSCameraDriver Cam2 = new MGSCameraDriver();
SQLiteHelper sqlhelper = new SQLiteHelper();
TCPClienDriver Scanner = new TCPClienDriver();
MelsecPLCTCPDriver melsecPLCTCPDriver = new MelsecPLCTCPDriver();
SimboObjectDetection simboObjectDetection = new SimboObjectDetection();
PaddleOcrModel paddleOcrModel = new PaddleOcrModel();
PaddleOcrModelCountry paddleOcrModelCountry = new PaddleOcrModelCountry();
List<XKHisence> xKHisences = new List<XKHisence>();
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
bool PLC545 = false;
bool PLC548 = false;
string m_sKEYENCEBar;
string IfChangeLanguage = "";//是否换洗衣机的型号
bool bTriger = true;//是否可以触发检测
bool bBarTriger = false;//第一个地址完成后才能触发第二个位置
bool bBar = false;//是否可以反馈条码
bool IfCam1OneTriger = false;//相机1第一次拍照
bool IfCam1TwoTriger = false;//相机1第2次拍照
bool IfCam2Triger = false;//相机2第一次拍照
Mat Cam1ImgOne = new Mat();//实时相机1的照片
Mat Cam1ImgTwo = new Mat();//实时相机1的照片
Mat Cam2ImgShowBar = new Mat();//实时相机2的照片 用来识别条码
XKHisence xKNow;//数据库中的洗衣机数据
bool bOn = false;
////////////////////////////
public static int m_CamCount = 0;
public int m_n_dev_count = 0;
public static dvpCameraInfo[] m_info = new dvpCameraInfo[16];
bool ifXZeroValue = false;
bool ifYZeroValue = false;
bool ifZZeroValue = false;
bool ifXForward = false;
bool ifYForward = false;
bool ifZForward = false;
bool ifXBackward = false;
bool ifYBackward = false;
bool ifZBackward = false;
//录入数据库
string BarPath = "";
string Cam1OnePath = "";
string Cam1TwoPath = "";
string ModelChangePath = "";
/// <summary>
/// 查看相机列表
/// </summary>
public void InitDevList()
{
dvpStatus status;
uint i, n = 0;
dvpCameraInfo dev_info = new dvpCameraInfo();
// "n" represents the number of cameras that is enumerated successfully, the drop-down list contains each camera's FriendlyName.
DevNameCombo.Items.Clear();
// Get the number of cameras that has been connected to a computer.
status = DVPCamera.dvpRefresh(ref n);
Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
m_n_dev_count = (int)n;
if (status == dvpStatus.DVP_STATUS_OK)
{
m_CamCount = 0;
for (i = 0; i < n; i++)
{
// Acquire each camera's information one by one.
status = DVPCamera.dvpEnum(i, ref dev_info);
Debug.Assert(status == dvpStatus.DVP_STATUS_OK);
if (status == dvpStatus.DVP_STATUS_OK)
{
m_info[m_CamCount] = dev_info;
int item = -1;
// add User Define Name
item = DevNameCombo.Items.Add(dev_info.UserID);
if (item == 0)
{
DevNameCombo.SelectedIndex = item;
}
m_CamCount++;
if (item == 0)
{
DevNameCombo.SelectedIndex = item;
}
}
}
}
}
System.Timers.Timer SpeedTimer = new System.Timers.Timer();
System.Timers.Timer PostionTimer = new System.Timers.Timer();
public delegate void MyDelegateUI(); //定义委托类型
MyDelegateUI myDelegateUI; //声明委托对象
public delegate void MyPosDelegateUI(); //定义委托类型
MyPosDelegateUI myPosDelegateUI; //声明委托对象
public delegate void MyLogDelegateUI(); //定义委托类型
MyLogDelegateUI myLogDelegateUI; //声明日志委托对象
private void Application_ThreadException(Exception e)
{
string msg = e.Message;
using (StreamWriter sw = new StreamWriter("D://ThreadException.log", true))
{
sw.WriteLine("================================");
sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
sw.WriteLine(msg);
sw.Flush();
}
}
/// <summary>
/// 主窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
string vBarPath = "D:\\Hisence\\202452915589671.jpg";
string vCam1OnePath = "D:\\Hisence\\4\\202452915596701.jpg";
string vCam1TwoPath = "D:\\Hisence\\4\\202452915599711.jpg";
string vModelChangePath = "D:\\Hisence\\config\\chinese.json";
if (File.Exists(vBarPath) && File.Exists(vCam1OnePath) && File.Exists(vCam1TwoPath) && File.Exists(vModelChangePath))
{
BarPath = vBarPath;
Cam1OnePath = vCam1OnePath;
Cam1TwoPath = vCam1TwoPath;
ModelChangePath = vModelChangePath;
}
ifXZeroValue = XZerorbx.Checked;
ifYZeroValue = YZerorbx.Checked;
InitDevList();
this.WindowState = FormWindowState.Maximized;
string Dectionstr = "D:\\Hisence\\best.onnx";
string CountryStr = "D:\\Hisence\\config\\chinese.json";
if (File.Exists(Dectionstr) && File.Exists(CountryStr))
{
}
else
{
MessageBox.Show("检测" + Dectionstr + " " + CountryStr + "模型有无");
return;
}
//初始化检测驱动
simboObjectDetection.Load(Dectionstr, "CPU", "images", 640, 640);
paddleOcrModelCountry.Load(CountryStr, "CPU");
paddleOcrModel.Load(CountryStr, "CPU");
IfChangeLanguage = CountryStr;
//相机配置
StartDecBtn.Enabled = false;
//myThread = new Thread(doWork);
myDelegateUI = new MyDelegateUI(initAll);//绑定委托
myPosDelegateUI = new MyPosDelegateUI(initAll2);//绑定委托
myLogDelegateUI = new MyLogDelegateUI(initAll3);//绑定委托
//初始化PLC
melsecPLCTCPDriver.Start();
//bool ismatch = IsMatchOcrText("WF3S7021BB", strMatListListOne);
//相机回调
melsecPLCTCPDriver.Heartbeat -= PLcHeratAdress;
melsecPLCTCPDriver.Heartbeat += PLcHeratAdress;
OnSpeedShow();//线程开启将速度显示在界面上
OnPostionShow();//线程开启将位置显示在界面上
}
private void initAll() //信息处理函数定义
{
//richTextBox1.AppendText("TEST line \r");
if (HspeedList.Count == 33)
{
lnEditmiXHS.Text = HspeedList[1].ToString();
lnEditmiYHS.Text = HspeedList[6].ToString();
lnEditmiZHS.Text = HspeedList[11].ToString();
//定位速度
lnEditmiXS.Text = HspeedList[0].ToString();
lnEditmiYS.Text = HspeedList[5].ToString();
lnEditmiZS.Text = HspeedList[10].ToString();
}
if (disShow != 0)
{
//TODO实时公式 知道测距移动多少
double disreal = disShow * (-0.54054) + 1324.3232;
DistanceShow.Text = Math.Ceiling(disreal).ToString();
//换算成一元一函数
//y = -0.54054x + 1324.3232
}
}
private void initAll2() //信息处理函数定义
{
if (relPosStrList.Count == 31)
{
// XPostion.Text = "";
XPostion.Text = relPosStrList[0].ToString();
//YPostion.Text = "";
YPostion.Text = relPosStrList[5].ToString();
// ZPostion.Text = "";
ZPostion.Text = relPosStrList[10].ToString();
}
}
string Logtxt = "";
private void initAll3() //信息处理函数定义
{
richTextBox1.AppendText(Logtxt);
AllDSum.Text = AllDsums.ToString();
OKDNum.Text = OKDsums.ToString();
NGDNum.Text = NGDsums.ToString();
using (StreamWriter stream = new StreamWriter("D://information.log", true))
{
stream.WriteLine(Logtxt);
stream.Flush();
}
}
private void OnSpeedShow()
{
Task.Run(() =>
{
while (true)
{
try
{
UpDatePlcValue(600, 2);
//从404 地址 读33个地址
UpDatePlcValue(404, 33);
if (HspeedList.Count == 33)
{
//手动速度
this.Invoke(myDelegateUI); //richTextBox1.AppendText("TEST line \r");
Application.DoEvents();
}
if (disShow != 0)
{
this.Invoke(myDelegateUI); //richTextBox1.AppendText("TEST line \r");
Application.DoEvents();
}
}
catch (Exception ex)
{
}
Thread.Sleep(200);
}
});
}
private void OnPostionShow()
{
Task.Run(() =>
{
while (true)
{
try
{
UpDatePlcValue(102, 31);
if (relPosStrList.Count == 31)
{
this.Invoke(myPosDelegateUI); //richTextBox1.AppendText("TEST line \r");
Application.DoEvents();
}
}
catch (Exception ex)
{
}
Thread.Sleep(200);
}
});
}
/// <summary>
/// 相机打开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CamOpenBtn_Click(object sender, EventArgs e)
{
Cam1.Start("Cam1");
bnGetParam.Enabled = true;
SetCtrlWhenOpen();
Cam1OpenBtn.BackColor = Color.Green;
}
private void SetCtrlWhenOpen()
{
tbExposure.Enabled = true;
tbGain.Enabled = true;
// tbFrameRate.Enabled = true;
bnGetParam.Enabled = true;
bnSetParam.Enabled = true;
}
private void bnGetParam_Click(object sender, EventArgs e)
{
tbExposure.Text = Cam1.dvpGetExposure().ToString();
tbGain.Text = Cam1.dvpGetAnalogGain().ToString();
}
private void bnGetParam2_Click(object sender, EventArgs e)
{
//tbExposure2.Text = Cam1.dvpGetExposure().ToString();
//tbGain2.Text = Cam1.dvpGetAnalogGain().ToString();
}
/// <summary>
/// 相机2打开
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cam2OpenBtn_Click(object sender, EventArgs e)
{
// Cam2.Start("Cam2");
// bnGetParam2.Enabled = true;
// Cam2OpenBtn.BackColor = Color.Green;
}
private void button2_Click(object sender, EventArgs e)
{
Cam1.Stop();
SetCtrlWhenClose();
}
private void SetCtrlWhenClose()
{
tbExposure.Enabled = false;
tbGain.Enabled = false;
// tbFrameRate.Enabled = false;
bnGetParam.Enabled = false;
bnSetParam.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
Cam2.Stop();
}
/// <summary>
/// 相机1软触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SnapshotCam1_Click(object sender, EventArgs e)
{
Cam1.Snapshot();
}
/// <summary>
/// 相机2软触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SnapshotCam2_Click(object sender, EventArgs e)
{
//Cam2.Snapshot();
}
int TimesCalled = 0;
Bitmap OKbitmap = new Bitmap("OK.jpg");
Bitmap NGbitmap = new Bitmap("NG.jpg");
/// <summary>
/// 主线程调用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void InitMachine_Click(object sender, EventArgs e)
{
try
{
//初始化相机
DataSet dataSet = SQLiteHelper.Query("select * from XK_HisenceDet");
dataGridView1.DataSource = dataSet.Tables[0];
// xKNow = GetModeFromBar("VWJ070633V0WW80F0120356");//从数据库中查询到这个条码的四轴的值
//string sql = "insert into XK_HisenceDet VALUES('" + xKNow.Type + "','" + xKNow.OcrBar + "'," + xKNow.MoveX + "," + xKNow.MoveY + "," +
// xKNow.MoveZ + ",'" + "123" + "',null," + xKNow.MoveTwoZ + "," + xKNow.MoveTwoY + "," + xKNow.MoveTwoZ + ", '" + DateTime.Now + "'," + 1 + ")";
//SQLiteHelper.ExecuteSql(sql);
Cam1.Start("Cam1");
if (Cam1.IfSuccess)
{
myLog("Cam1相机启动", DateTime.Now);
}
Cam2.Start("Cam2");
if (Cam2.IfSuccess)
{
myLog("Cam2相机启动", DateTime.Now);
}
Cam1.OnHImageOutput -= OnCameraMatOutPut;
Cam1.OnHImageOutput += OnCameraMatOutPut;
Cam2.OnHImageOutput -= OnCameraMatOutPut2;
Cam2.OnHImageOutput += OnCameraMatOutPut2;
OKOrNGShow.Image = OKbitmap;
//初始化扫码枪
//Scanner.Strart();
//Scanner.OnClientDataReceived -= OnDataReceived;
//Scanner.OnClientDataReceived += OnDataReceived;
//Scanner.btnSendMsg("LON\r");//发送开启指令
//myLog("发送恩士扫码枪开启指令", DateTime.Now);
//PLC使能开启
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", 8000);
melsecPLCTCPDriver.WriteInt("404", 100000); //写入定位速度
melsecPLCTCPDriver.WriteInt("414", 8000);
melsecPLCTCPDriver.WriteInt("424", 8000);
melsecPLCTCPDriver.WriteInt("500", 0);
melsecPLCTCPDriver.WriteInt("500", 1);
Thread.Sleep(200);
bOn = true;
melsecPLCTCPDriver.WriteInt("580", 0);//归零
melsecPLCTCPDriver.WriteInt("580", 1);//归零
Thread.Sleep(200);
melsecPLCTCPDriver.WriteInt("580", 0);
Thread.Sleep(200);
while (true)
{
int IfXZeros = melsecPLCTCPDriver.ReadInt("102");
int IfYZeros = melsecPLCTCPDriver.ReadInt("112");
int IfZZeros = melsecPLCTCPDriver.ReadInt("122");
if (IfXZeros <= 40 && IfYZeros <= 140 && IfZZeros <= 40)
{
StartDecBtn.Enabled = true;
break;
}
Thread.Sleep(10);
}
myLog("设备全部启动完成", DateTime.Now);
}
catch (Exception ex)
{
Application_ThreadException(ex);
}
}
/// <summary>
/// 全图洗衣机 裁剪之后 OCR识别的结果
/// </summary>
/// <param name="currentMatC"></param>
/// <param name="cam1TwoML"></param>
/// <param name="strMatListList"></param>
private void InsertSqlRunData(ref Mat currentMatC, MLResult cam1TwoML, ref List<string> strMatListList, ref PaddleOcrModel IOcrModel)
{
#if false
try
{
List<Mat> mats = new List<Mat>();
Mat mResultCut = currentMatC.Clone();
string OcrTextinsert = "";
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;
Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
Mat matroi = new Mat(mResultCuti, area);
mats.Add(matroi);
//OCR识别裁剪图片
MLRequest reqcut = new MLRequest();
reqcut.currentMat = matroi.Clone();
MLResultModel mLcut = IOcrModel.RunInferenceFixed(reqcut);
for (int j = 0; j < mLcut.ResultDetails.Count; j++)
{
string jdetial = mLcut.ResultDetails[j].LabelDisplay;
strMatListList.Add(jdetial);
OcrTextinsert += jdetial + "##";
}
}
}
catch (Exception ex)
{
}
#endif
}
/// <summary>
/// 数据库中条码所对应的OCR文本是否与识别出来的文本相似
/// </summary>
/// <param name="WashBar"></param>
/// <param name="DetMatstrList"></param>
/// <returns></returns>
private bool IsMatchOcrText(string WashBar, List<string> DetMatstrList)
{
try
{
xKNow = GetModeFromBar(WashBar);//从数据库中查询到这个条码的四轴的值
List<string> strMatListListB = new List<string>();
if (xKNow.OcrText.Contains("##"))
{
string[] sArray = Regex.Split(xKNow.OcrText, "##", RegexOptions.IgnoreCase);
for (int i = 0; i < sArray.Count(); i++)
{
if (sArray[i] != "")
{
strMatListListB.Add(sArray[i]);
}
}
}
var firstNotSecond = DetMatstrList.Except(strMatListListB, StringComparer.OrdinalIgnoreCase).ToList();
var secondNotFirst = strMatListListB.Except(DetMatstrList, StringComparer.OrdinalIgnoreCase).ToList();
if (firstNotSecond.Count() == 0 && secondNotFirst.Count() == 0)
{
return true;
}
return false;
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 实时移动模组
/// </summary>
public void MoveToP(int currentXP, int currentYP, int currentZP)
{
melsecPLCTCPDriver.WriteInt("581", 0);
melsecPLCTCPDriver.WriteInt("402", currentXP);
melsecPLCTCPDriver.WriteInt("412", currentYP);
melsecPLCTCPDriver.WriteInt("422", currentZP);
melsecPLCTCPDriver.WriteInt("581", 1);
Thread.Sleep(200);
melsecPLCTCPDriver.WriteInt("581", 0);
while (true)
{
int IfXZeros = melsecPLCTCPDriver.ReadInt("102");
int IfYZeros = melsecPLCTCPDriver.ReadInt("112");
int IfZZeros = melsecPLCTCPDriver.ReadInt("122");
if ((IfXZeros < currentXP + 20) && (IfXZeros > currentXP - 20) && (IfYZeros <= currentYP + 20) && (IfYZeros >= currentYP - 20) && (IfZZeros <= currentZP + 20) && (IfZZeros >= currentZP - 20))//TODO:设置第一次拍摄的位置
{
break;
}
Thread.Sleep(10);
}
}
public int disShow = new int();
public List<int> MonitorValues { get; set; } = new List<int>();
public List<int> relPosStrList { get; set; } = new List<int>();
public List<int> HspeedList { get; set; } = new List<int>();
/// <summary>
/// 实时刷新PLC地址的值
/// </summary>
///
public void UpDatePlcValue(int Address, int length)
{
//Task.Run(() =>
//{
// while (true)
// {
try
{
//实时刷新机械手的位置
List<int> newValues = melsecPLCTCPDriver.GetMonitorValues(Address, length);
if (newValues == null || newValues.Count == 0)
return;
Stopwatch sw = new Stopwatch();
sw.Start();
if (Address == 600)
{
disShow = melsecPLCTCPDriver.ReadInt(Convert.ToString(Address));
}
if (Address == 102) //若起始地址为102
{
if (relPosStrList.Count == newValues.Count)
{
var tempNew = new List<int>(newValues);
var tempOld = new List<int>(relPosStrList);
//MonitorCheckAndInvoke(tempNew, tempOld);
}
relPosStrList = new List<int>(newValues);
// relPosStrList = MonitorValues;
//实时显示在界面上
}
if (Address == 404)
{
if (HspeedList.Count == newValues.Count)
{
var tempNew = new List<int>(newValues);
var tempOld = new List<int>(HspeedList);
//MonitorCheckAndInvoke(tempNew, tempOld);
}
HspeedList = new List<int>(newValues);
//HspeedList = MonitorValues;
//实时显示在界面上
}
sw.Stop();
if (sw.ElapsedMilliseconds > 20)
{
//LogAsync(DateTime.Now, LogLevel.Detail, $"{this.Name}轮询时间:{sw.ElapsedMilliseconds}");
}
Thread.Sleep(20);
}
catch (Exception ex)
{
}
// }
//});
}
/// <summary>
/// 监听洗衣机545是否触发后面对射传感器 相机
/// </summary>
public void TrigerCamera1()
{
//开启一个线程监听PLC得值
Task.Run(() =>
{
int iNum1 = 0;
int iNum0 = 0;
while (true)
{
// myResetEvent.Set();
PLC545 = true;
int m_iCureent545 = melsecPLCTCPDriver.ReadInt("545");
////int m_iCureent545 = CMPLC.SLMPReadD(545);
if (m_iCureent545 != 1)
{
iNum0++;
}
else
{
iNum0 = 0;
}
if (iNum0 >= 5)
{
while (true)
{
m_iCureent545 = melsecPLCTCPDriver.ReadInt("545");
if (m_iCureent545 == 1)
{
iNum1++;
}
else
{
iNum1 = 0;
}
if (iNum1 >= 5)
{
//Console.WriteLine("第二次采集图像");
//Cam1.Snapshot();
//Cam2.Snapshot();
//IfCam1Triger = false;
//IfCam2Triger = false;
iNum0 = 0;
iNum1 = 0;
break;
}
}
}
Thread.Sleep(100);
}
});
}
/// <summary>
/// 获取基恩士读码器的型号
/// </summary>
public void OnGetBar()
{
Task.Run(() =>
{
int iNum0 = 0;
int iNum1 = 0;
// const char pCR[4] = { 0x4C, 0x4F, 0x4E, 0x0D };开启
//const char pOFF[5] = { 0x4C, 0x4F, 0x46, 0x46, 0x0D };关闭
Thread.CurrentThread.Priority = ThreadPriority.Highest;
while (true)
{
//545触发时为0
// myResetEvent.Set();
PLC545 = true;
int m_iCureent545 = melsecPLCTCPDriver.ReadInt("545");
if (m_iCureent545 != 1)
{
iNum0++;
}
else
{
iNum0 = 0;
}
if (iNum0 >= 1)
{
//接受条码指令
while (true)
{
bTriger = false;
//读到544一直是1持续两s 拍照
m_iCureent545 = melsecPLCTCPDriver.ReadInt("545");
if (m_iCureent545 == 1)
{
iNum1++;
}
else
{
iNum1 = 0;
}
if (iNum1 >= 2)
{
if (bBar)
{
bBarTriger = true;//修改成相机拍照获取型号
Cam2.SnapshotCount = 0;
Cam2.Snapshot();
xKNow = GetModeFromBar("VWJ070633V0WW80F0120356");//从数据库中查询到这个条码的四轴的值
//xKNow = GetModeFromBar(m_sKEYENCEBar);//从数据库中查询到这个条码的四轴的值
if (Regex.IsMatch(xKNow.MoveX.ToString(), @"^[0-9]+$") && Regex.IsMatch(xKNow.MoveY.ToString(), @"^[0-9]+$") && Regex.IsMatch(xKNow.MoveZ.ToString(), @"^[0-9]+$"))
{
myLog("型号匹配成功" + m_sKEYENCEBar, DateTime.Now);
bBarTriger = true;
//MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ);// PLC控制电机移动四轴
}
break;
}
Thread.Sleep(2);
bBar = false;
}
}
}
}
Thread.Sleep(10);
});
}
/// <summary>
/// 监听洗衣机544是否触发后面对射传感器 相机
/// </summary>
public void TriggerCameral2()
{
//开启一个线程监听PLC得值
Task.Run(() =>
{
try
{
int iNum1 = 0;
int iNum0 = 0;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
while (true)
{
// myResetEvent.Set();
PLC548 = true;
int m_iCureent548 = melsecPLCTCPDriver.ReadInt("548");
if (m_iCureent548 != 0)
{
iNum0++;
}
else
{
iNum0 = 0;
}
if (iNum0 >= 1)
{
while (true)
{
bTriger = false;
//读到544一直是1持续两s 拍照
m_iCureent548 = melsecPLCTCPDriver.ReadInt("548");
if (m_iCureent548 == 0)
{
iNum1++;
}
else
{
iNum1 = 0;
}
if (iNum1 >= 2)
{
//if (bBarTriger)
{
myLog("第一次采集图像", DateTime.Now);
//移动模组 读三次测距的地址 看是否往前移动
List<int> DisList = new List<int>();
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;
MoveToP(8974, 0, (int)(disreal));
Thread.Sleep(500);
Cam1.SnapshotCount = 0;
Cam1.Snapshot();
Cam2.SnapshotCount = 0;
Cam2.Snapshot();
// myLog("第二次采集图像", DateTime.Now);
//if (xKNow != null)
//{
// MoveToP(xKNow.MoveTwoX, xKNow.MoveTwoY, xKNow.MoveTwoZ);
//}
// currentXP = 54964;
MoveToP(54964, 0, (int)(disreal));
//Thread.Sleep(3000);
Cam1.Snapshot();
MoveToP(8974, 0, 0);
//if (xKNow != null)
//{
// MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ);
//}
myLog("采集图像成功", DateTime.Now);
//模组复位
bTriger = true;//可以进行检测识别了
bBarTriger = false;//读码器可以不工作了
iNum0 = 0;
iNum1 = 0;
break;
}
}
}
}
Thread.Sleep(10);
}
}
catch (Exception e)
{
}
});
}
/// <summary>
/// PLC心跳监听
/// </summary>
/// <param name="ifPlcHeart"></param>
public void PLcHeratAdress(int ifPlcHeart)
{
if (ifPlcHeart == 1)
{
myLog("PLC监听失败", DateTime.Now);
}
else
{
}
}
/// <summary>
/// 机1软触发的回调
/// </summary>
/// <param name="dt"></param>
/// <param name="cameraMat"></param>
/// <param name="SnapshotCount"></param>
public void OnCameraMatOutPut(DateTime dt, Mat cameraMat, int SnapshotCount)
{
string fitImageFolder = "D://cam1//";
if (!Directory.Exists(fitImageFolder))
{
Directory.CreateDirectory(fitImageFolder);
}
//第一次拍照
if (SnapshotCount == 1)
{
// Mat mat = Cv2.ImRead("F:\\海信洗衣机\\cam1\\2024517161641.jpg");
Cam1ImgOne = cameraMat;
IfCam2Triger = true;
originMatShow.Image = cameraMat.ToBitmap();
//pictureBox1.Image = cameraMat.ToBitmap();
}
//第二次拍照
if (SnapshotCount == 2)
{
// Mat mat = Cv2.ImRead("F:\\海信洗衣机\\cam1\\2024517164541.jpg");
Cam1ImgTwo = cameraMat;
IfCam1TwoTriger = true;
originMatShow2.Image = cameraMat.ToBitmap();
//pictureBox1.Image = cameraMat.ToBitmap();
}
pictureBox1.Image = cameraMat.ToBitmap();
cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg");
}
/// <summary>
/// 相机2触发回调
/// </summary>
/// <param name="dt"></param>
/// <param name="cameraMat"></param>
/// <param name="SnapshotCount"></param>
public void OnCameraMatOutPut2(DateTime dt, Mat cameraMat, int SnapshotCount)
{
string fitImageFolder = "D://cam2//";
if (!Directory.Exists(fitImageFolder))
{
Directory.CreateDirectory(fitImageFolder);
}
//第一次拍照
if (SnapshotCount == 1)
{
// Mat mat = Cv2.ImRead("F:\\海信洗衣机\\cam1\\2024517161641.jpg");
Cam2ImgShowBar = cameraMat;
IfCam2Triger = true;
originMat2Show.Image = cameraMat.ToBitmap();
//pictureBox1.Image = cameraMat.ToBitmap();
}
pictureBox2.Image = cameraMat.ToBitmap();
cameraMat.ImWrite(fitImageFolder + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1.jpg");
}
public volatile int AllDsums = 0;
public volatile int NGDsums = 0;
public volatile int OKDsums = 0;
/// <summary>
/// 开始识别
/// </summary>
public void ReadyDetect()
{
Task.Run(() =>
{
Thread.CurrentThread.Priority = ThreadPriority.Highest;
while (true)
{
try
{
//相机2第一次拍照
if (IfCam2Triger)
{
//OCR识别
MLRequest req = new MLRequest();
req.currentMat = Cam2ImgShowBar;
//req.currentMat = Cv2.ImRead("D:\\Hisence\\202452414231.jpg");
MLResultModel mL = paddleOcrModelCountry.RunInferenceFixed(req);
//数据库比对
xKNow = GetModeFromBar(mL.WashMachineBar);//从数据库中查询到这个条码的四轴的值
if (xKNow.OcrBar == null)
{
myLog("未匹配成功" + IfChangeLanguage, DateTime.Now);
break;
}
if (xKNow.OcrText != "" && xKNow.Detect != "")
{
//此时运行的洗衣机是和之前一个语言模型
if (IfChangeLanguage == mL.WashMachineBar)
{
}
else
{
//本地存在这个OCR.josn参数
if (File.Exists(xKNow.OcrParm))
{
paddleOcrModel.Load(xKNow.OcrParm, "CPU");
IfChangeLanguage = mL.WashMachineBar;
}
}
myLog("型号匹配成功" + IfChangeLanguage, DateTime.Now);
bBarTriger = true;
}
//从数据库中匹配完才可以进行模组移动
//如果这次型号和上次的型号一样 就不用换识别模型ll
IfCam2Triger = false;
}
//相机1第二次拍照
if (IfCam1TwoTriger && bBarTriger)
{
AllDsums++;
//进行推理
MLRequest req = new MLRequest();
req.currentMat = Cam1ImgOne;
req.ResizeWidth = 640;
req.ResizeHeight = 640;
req.Score = 0.3f;
req.in_lable_path = "D:\\Hisence\\ClassName.txt";//标签路径
req.confThreshold = 0.3f;//模型置信度
req.iouThreshold = 0.4f;//检测IOU
req.out_node_name = "output";
MLResult mL = simboObjectDetection.RunInferenceFixed(req);
DateTime dt = DateTime.Now;
//将所有的块裁剪 识别字符对比字符串
List<string> strMatListListOne = new List<string>();
Mat mResultCut = req.currentMat.Clone();
InsertSqlRunData(ref mResultCut, mL, ref strMatListListOne, ref paddleOcrModel);
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");
ResultMatShow.Image = mL.ResultMap;
//进行推理
MLRequest req2 = new MLRequest();
req2.currentMat = Cam1ImgTwo;
req2.ResizeWidth = 640;
req2.ResizeHeight = 640;
req2.Score = 0.3f;
req2.in_lable_path = "D:\\Hisence\\ClassName.txt";//标签路径
req2.confThreshold = 0.3f;//模型置信度
req2.iouThreshold = 0.4f;//检测IOU
req2.out_node_name = "output";
MLResult mL2 = simboObjectDetection.RunInferenceFixed(req2);
List<string> strMatListListTwo = new List<string>();
Mat mResultCut2 = req2.currentMat.Clone();
InsertSqlRunData(ref mResultCut2, mL2, ref strMatListListTwo, ref paddleOcrModel);
mL2.ResultMap.Save("D://cam1//" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg");
ResultMatShow2.Image = mL2.ResultMap;
//执行比对 这个模型2和3一样
string[] listLabels = xKNow.Detect.Split(",");
MLResult MLsum = mL;
for (int i = 0; i < mL2.ResultDetails.Count; i++)
{
MLsum.ResultDetails.Add(mL2.ResultDetails[i]);
}
string[] RealLabels = new string[MLsum.ResultDetails.Count];
string detstr = "";
for (int i = 0; i < MLsum.ResultDetails.Count; i++)
{
RealLabels[i] = MLsum.ResultDetails[i].LabelName;
if (i < MLsum.ResultDetails.Count - 1)
{
detstr += RealLabels[i] + ",";
}
else
{
detstr += RealLabels[i];
}
}
IfCam1TwoTriger = false;
bBarTriger = false;
bool isMatch = false;
string OcrTextinsert = "";
List<string> bingji = strMatListListOne.Union(strMatListListTwo).ToList();//并(全)集
for (int j = 0; j < bingji.Count; j++)
{
string jdetial = bingji[j];
OcrTextinsert += jdetial + "##";
}
bool ismatch = IsMatchOcrText(xKNow.OcrBar, bingji);
// 求差集
//var except = listLabels.Except(RealLabels);
//Console.WriteLine("数组的差集:");
//if (except.Count() == 0)
//{
// // isMatch = true;
//}
//foreach (var num in except)
//{
// if ((num == "2" || num == "3") && except.Count() == 1)
// {
// // isMatch = true;
// break;
// }
// else
// {
// // isMatch = false;
// break;
// }
//}
if (isMatch)
{
OKDsums++;
myLog("匹配成功", DateTime.Now);
//界面显示
string sql = "insert into XK_HisenceDet VALUES('" + xKNow.Type + "','" + xKNow.OcrBar + "'," + xKNow.MoveX + "," + xKNow.MoveY + "," +
xKNow.MoveZ + ",'" + detstr + "','" + OcrTextinsert + "'," + xKNow.MoveTwoX + "," + xKNow.MoveTwoY + "," + xKNow.MoveTwoZ + ", '" + DateTime.Now + "'," + 1 + ")";
SQLiteHelper.ExecuteSql(sql);
OKOrNGShow.Image = OKbitmap;
}
else
{
NGDsums++;
myLog("匹配失败", DateTime.Now);
//界面显示
string sql = "insert into XK_HisenceDet VALUES('" + xKNow.Type + "','" + xKNow.OcrBar + "'," + xKNow.MoveX + "," + xKNow.MoveY + "," +
xKNow.MoveZ + ",'" + detstr + "','" + OcrTextinsert + "'," + xKNow.MoveTwoX + "," + xKNow.MoveTwoY + "," + xKNow.MoveTwoZ + ", '" + DateTime.Now + "'," + 0 + ")";
SQLiteHelper.ExecuteSql(sql);
OKOrNGShow.Image = NGbitmap;
}
}
}
catch (Exception e)
{
}
finally
{
}
Thread.Sleep(10);
}
});
}
/// <summary>
/// 根据条码从数据库中获取型号
/// </summary>
/// <param name="SkBar"></param>
/// <returns></returns>
public XKHisence GetModeFromBar(string SkBar)
{
//从模板库里读
DataSet ds = SQLiteHelper.Query($"select * from XK_Hisence where OCRBar='{SkBar}' ");
XKHisence XK_hisence = new XKHisence();
//cbx.DataSource = ds;
DataTableReader rdr = ds.CreateDataReader();
//cbx.Rows.Clear();
while (rdr.Read())//读取表中数据
{
// xKOCROfficeWord.ID = (int)rdr["ID"];
XK_hisence.Type = (string)rdr["Type"];
XK_hisence.OcrBar = (string)rdr["OcrBar"];
XK_hisence.MoveX = (int)rdr["MoveX"];
XK_hisence.MoveY = (int)rdr["MoveY"];
XK_hisence.MoveZ = (int)rdr["MoveZ"];
XK_hisence.Detect = (string)rdr["Detect"];
XK_hisence.OcrText = (string)rdr["OcrText"];
XK_hisence.MoveTwoX = (int)rdr["MoveTwoX"];
XK_hisence.MoveTwoY = (int)rdr["MoveTwoY"];
XK_hisence.MoveTwoZ = (int)rdr["MoveTwoZ"];
XK_hisence.OcrParm = (string)rdr["OcrParm"];
XK_hisence.Language = (string)rdr["Language"];
//xKHisences.Add(XK_hisence);
return XK_hisence;
//additem(XK_hisence);
}
return XK_hisence;
}
/// <summary>
/// 接受条码的回调函数
/// </summary>
/// <param name="data"></param>
public void OnDataReceived(byte[] data)
{
//
if (data.Length > 10)
{
m_sKEYENCEBar = System.Text.Encoding.UTF8.GetString(data);
// myLog("型号Ocr" + m_sKEYENCEBar, DateTime.Now);
bBar = true;
}
}
/// <summary>
/// PLC的初始化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
melsecPLCTCPDriver.Start();
myLog("连接PLC成功", DateTime.Now);
}
/// <summary>
/// 日志
/// </summary>
/// <param name="txt"></param>
/// <param name="date"></param>
private void myLog(string txt, DateTime date)
{
Logtxt = txt + " 时间:" + date.ToString() + "\n";
this.Invoke(myLogDelegateUI); //richTextBox1.AppendText("TEST line \r");
Application.DoEvents();
}
private void readAdress_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(readPLc.Text.ToString(), @"^[0-9]+$"))
{
int m_iCureent545 = melsecPLCTCPDriver.ReadInt(readPLc.Text.ToString());
showPLC.Text = m_iCureent545.ToString();
myLog("读取" + readPLc.Text.ToString() + "完成", DateTime.Now);
}
}
private void wirteAdress_Click(object sender, EventArgs e)
{
try
{
if (Regex.IsMatch(WriteAdress.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(PLCValue.Text.ToString(), @"^[0-9]+$"))
{
melsecPLCTCPDriver.WriteInt(WriteAdress.Text.ToString(), Convert.ToInt16(PLCValue.Text));
myLog("写" + WriteAdress.Text.ToString() + "完成", DateTime.Now);
}
}
catch (Exception ex) { }
}
private void ScanDevList_Click(object sender, EventArgs e)
{
InitDevList();
}
private void PLCPowerON_Click(object sender, EventArgs e)
{
if (bOn == true)
{
melsecPLCTCPDriver.WriteInt("500", 0);
// PLCcontrol->WritePlc(500, 0);
bOn = false;
PLCPowerON.Text = "使能On";
myLog("使能On完成", DateTime.Now);
}
else
{
melsecPLCTCPDriver.WriteInt("500", 1);
bOn = true;
PLCPowerON.Text = "使能Off";
myLog("使能Off", DateTime.Now);
}
}
bool IfPLCDiskZero = false;
bool IfPLCPostion = false;
private void PLCDiskZero_Click(object sender, EventArgs e)
{
//if (!IfPLCDiskZero)
//{
melsecPLCTCPDriver.WriteInt("580", 0);
//归零
melsecPLCTCPDriver.WriteInt("580", 1);
Thread.Sleep(200);
melsecPLCTCPDriver.WriteInt("580", 0);
IfPLCDiskZero = true;
this.txtSetValue.Text = "1";
myLog("580开始归零", DateTime.Now);
//}
//else
//{
// melsecPLCTCPDriver.WriteInt("580", 0);
// IfPLCDiskZero = false;
// this.txtSetValue.Text = "0";
// myLog("580停止归零", DateTime.Now);
//}
}
private void PLCPostion_Click(object sender, EventArgs e)
{
//if (!IfPLCPostion)
//{
try
{
if (Regex.IsMatch(lnEditmiXOP.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiYOP.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiZOP.Text.ToString(), @"^[0-9]+$"))
{
if (lnEditmiXOP.Text != "" && lnEditmiYOP.Text != "" && lnEditmiZOP.Text != "")
{
//通过读取编辑栏的数值 作为目标位置
int iXTPosition = Convert.ToInt16(lnEditmiXOP.Text); //定位X轴位置
int iYTPosition = Convert.ToInt16(lnEditmiYOP.Text);
int iZTPosition = Convert.ToInt16(lnEditmiZOP.Text);
//把要定位的位置写入
melsecPLCTCPDriver.WriteInt("402", iXTPosition);
melsecPLCTCPDriver.WriteInt("412", iYTPosition);
melsecPLCTCPDriver.WriteInt("422", iZTPosition);
string logs = "写入定位位置 X" + iXTPosition + "Y:" + iYTPosition + "Z:" + iZTPosition + " ";
myLog(logs, DateTime.Now);
}
}
melsecPLCTCPDriver.WriteInt("581", 1);
Thread.Sleep(1000);
melsecPLCTCPDriver.WriteInt("581", 0);
IfPLCPostion = true;
this.txtSetValue.Text = "1";
myLog("XYZ开始定位581", DateTime.Now);
}
catch (Exception ex)
{
}
//}
//else
//{
// melsecPLCTCPDriver.WriteInt("581", 0);
// IfPLCPostion = false;
// this.txtSetValue.Text = "0";
// myLog("XYZ停止581", DateTime.Now);
//}
}
private void StartDecBtn_Click(object sender, EventArgs e)
{
//OnGetBar();
TriggerCameral2();
ReadyDetect();
myLog("开启流程成功", DateTime.Now);
}
private void InsertSqlBtn_Click(object sender, EventArgs e)
{
InsertSqlFrm insertSqlFrm = new InsertSqlFrm();
// insertSqlFrm.
//insertSqlFrm.Parent = this;
insertSqlFrm.ShowDialog();
}
private void RefeshData_Click(object sender, EventArgs e)
{
DataSet dataSet = SQLiteHelper.Query("select * from XK_HisenceDet");
dataGridView1.DataSource = dataSet.Tables[0];
}
private void panel8_Paint(object sender, PaintEventArgs e)
{
}
private void SetHandleSpeed_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(lnEditmiXHS.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiYHS.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiZHS.Text.ToString(), @"^[0-9]+$"))
{
if (SetlnEditmiXH.Text != "" && SetlnEditmiYH.Text != "" && SetlnEditmiZH.Text != "")
{
int XHspeed = Convert.ToInt32(SetlnEditmiXH.Text); //X轴手动速度
int YHspeed = Convert.ToInt32(SetlnEditmiYH.Text); //Y轴手动速度
int ZHspeed = Convert.ToInt32(SetlnEditmiZH.Text); //Z轴手动速度
melsecPLCTCPDriver.WriteInt("406", XHspeed); //写入手动速度
melsecPLCTCPDriver.WriteInt("416", YHspeed);
melsecPLCTCPDriver.WriteInt("426", ZHspeed);
string logs = "写入手动速度 X" + XHspeed + "Y:" + YHspeed + "Z:" + ZHspeed + " ";
myLog(logs, DateTime.Now);
}
}
}
private void SetAutoSpeed_Click(object sender, EventArgs e)
{
if (Regex.IsMatch(lnEditmiXS.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiYS.Text.ToString(), @"^[0-9]+$") && Regex.IsMatch(lnEditmiZS.Text.ToString(), @"^[0-9]+$"))
{
if (SetlnEditmiXOP.Text != "" && SetlnEditmiYOP.Text != "" && SetlnEditmiZOP.Text != "")
{
int Xspeed = Convert.ToInt32(SetlnEditmiXOP.Text); //X轴定位速度
int Yspeed = Convert.ToInt32(SetlnEditmiYOP.Text); //Y轴定位速度
int Zspeed = Convert.ToInt32(SetlnEditmiZOP.Text); //Z轴定位速度
melsecPLCTCPDriver.WriteInt("404", Xspeed); //写入定位速度
melsecPLCTCPDriver.WriteInt("414", Yspeed);
melsecPLCTCPDriver.WriteInt("424", Zspeed);
string logs = "写入定位速度 X" + Xspeed + "Y:" + Yspeed + "Z:" + Zspeed + " ";
myLog(logs, DateTime.Now);
}
}
}
//按下状态标识
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)
{
if (!ifYZeroValue)
{
this.YZerorbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("513", 1);
ifYZeroValue = true;
this.txtSetValue.Text = "1";
myLog("Y轴开始归零", DateTime.Now);
}
else
{
this.YZerorbx.Checked = false;
melsecPLCTCPDriver.WriteInt("513", 0);
ifYZeroValue = false;
this.txtSetValue.Text = "0";
myLog("Y轴停止归零", DateTime.Now);
}
}
private void ZZerorbx_Click(object sender, EventArgs e)
{
if (!ifZZeroValue)
{
this.ZZerorbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("523", 1);
ifZZeroValue = true;
this.txtSetValue.Text = "1";
myLog("Z轴开始归零", DateTime.Now);
}
else
{
this.ZZerorbx.Checked = false;
melsecPLCTCPDriver.WriteInt("523", 0);
ifZZeroValue = false;
this.txtSetValue.Text = "0";
myLog("Z轴停止归零", DateTime.Now);
}
}
private void XForwardrbx_Click(object sender, EventArgs e)
{
if (!ifXForward)
{
this.XForwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("502", 1);
ifXForward = true;
this.txtSetValue.Text = "1";
myLog("X轴开始正向", DateTime.Now);
}
else
{
this.XForwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("502", 0);
ifXForward = false;
this.txtSetValue.Text = "0";
myLog("X轴停止正向", DateTime.Now);
}
}
private void YForwardrbx_Click(object sender, EventArgs e)
{
if (!ifYForward)
{
this.YForwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("512", 1);
ifYForward = true;
this.txtSetValue.Text = "1";
myLog("Y轴开始正向", DateTime.Now);
}
else
{
this.YForwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("512", 0);
ifYForward = false;
this.txtSetValue.Text = "0";
myLog("Y轴停止正向", DateTime.Now);
}
}
private void ZForwardrbx_Click(object sender, EventArgs e)
{
if (!ifZForward)
{
this.ZForwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("522", 1);
ifZForward = true;
this.txtSetValue.Text = "1";
myLog("Z轴开始正向", DateTime.Now);
}
else
{
this.ZForwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("522", 0);
ifZForward = false;
this.txtSetValue.Text = "0";
myLog("Z轴停止正向", DateTime.Now);
}
}
private void XBackwardrbx_Click(object sender, EventArgs e)
{
if (!ifXBackward)
{
this.XBackwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("501", 1);
ifXBackward = true;
this.txtSetValue.Text = "1";
myLog("X轴开始反向", DateTime.Now);
}
else
{
this.XBackwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("501", 0);
ifXBackward = false;
this.txtSetValue.Text = "0";
myLog("X轴停止反向", DateTime.Now);
}
}
private void YBackwardrbx_Click(object sender, EventArgs e)
{
if (!ifYBackward)
{
this.YBackwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("511", 1);
ifYBackward = true;
this.txtSetValue.Text = "1";
myLog("Y轴开始反向", DateTime.Now);
}
else
{
this.YBackwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("511", 0);
ifYBackward = false;
this.txtSetValue.Text = "0";
myLog("Y轴停止反向", DateTime.Now);
}
}
private void ZBackwardrbx_Click(object sender, EventArgs e)
{
if (!ifZBackward)
{
this.ZBackwardrbx.Checked = true;
//归零
melsecPLCTCPDriver.WriteInt("521", 1);
ifZBackward = true;
this.txtSetValue.Text = "1";
myLog("Z轴开始反向", DateTime.Now);
}
else
{
this.ZBackwardrbx.Checked = false;
melsecPLCTCPDriver.WriteInt("521", 0);
ifZBackward = false;
this.txtSetValue.Text = "0";
myLog("Z轴停止反向", DateTime.Now);
}
}
private void button1_Click(object sender, EventArgs e)
{
string ss = "BB0VA5E0000BCQ5L0114";
}
private void Cam2OpenBtn_Click_1(object sender, EventArgs e)
{
Cam2.Start("Cam2");
bnGetParam2.Enabled = true;
//SetCtrlWhenOpen();
Cam2OpenBtn.BackColor = Color.Green;
}
private void button4_Click_1(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请打开图片";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
//File fileOpen = new File(fName);
//isFileHaveName = true;
//richTextBox1.Text = fileOpen.ReadFile();
//richTextBox1.AppendText("");
DetectBArMatbox.Text = fName;
BarPath = fName;
}
}
private void InsertBtn_Click(object sender, EventArgs e)
{
try
{
string type = TypeBox.Text;
string OcrBar = OcrBarBox.Text;
string moveX = moveXbox.Text;
string moveY = moveYbox.Text;
string moveZ = moveZbox.Text;
string Detect = DetectBArMatbox.Text;
string movetwoX = moveTwoXbox.Text;
string movetwoY = moveTwoYbox.Text;
string movetwoZ = moveTwoZbox.Text;
if (!Regex.IsMatch(moveX, @"^[0-9]+$"))
{
MessageBox.Show("第一次拍照移动的X只能是数字");
return;
}
if (!Regex.IsMatch(moveY, @"^[0-9]+$"))
{
MessageBox.Show("第一次拍照移动的Y只能是数字");
return;
}
if (!Regex.IsMatch(moveZ, @"^[0-9]+$"))
{
MessageBox.Show("第一次拍照移动的Z只能是数字");
return;
}
if (!Regex.IsMatch(movetwoX, @"^[0-9]+$"))
{
MessageBox.Show("第二次拍照移动的X只能是数字");
return;
}
if (!Regex.IsMatch(movetwoY, @"^[0-9]+$"))
{
MessageBox.Show("第二次拍照移动的Y只能是数字");
return;
}
if (!Regex.IsMatch(movetwoZ, @"^[0-9]+$"))
{
MessageBox.Show("第二次拍照移动的Z只能是数字");
return;
}
if (OcrBar != "" && Detect != "" && BarPath != "" && Cam1OnePath != "" && Cam1TwoPath != "" && ModelChangePath != "")
{
int HmoveX = (int)Convert.ToInt64(moveX);
int HmoveY = (int)Convert.ToInt64(moveY);
int HmoveZ = (int)Convert.ToInt64(moveZ);
int HmovetwoX = (int)Convert.ToInt64(movetwoX);
int HmovetwoY = (int)Convert.ToInt64(movetwoY);
int HmovetwoZ = (int)Convert.ToInt64(movetwoZ);
InsertXK_Hisence(HmoveX, HmoveY, HmoveZ, HmovetwoX, HmovetwoY, HmovetwoZ);
}
else
{
MessageBox.Show("插入的数据不能为NULL");
}
}
catch (Exception es)
{
}
}
public void InsertXK_Hisence(int MoveX, int MoveY, int MoveZ, int MoveTwoX, int MoveTwoY, int MoveTwoZ)
{
PaddleOcrModel IpaddleOcrModel = new PaddleOcrModel();
IpaddleOcrModel.Load(ModelChangePath, "CPU");
MLRequest OcrBari = new MLRequest();
OcrBari.currentMat = Cv2.ImRead(BarPath);
MLResultModel mL2 = IpaddleOcrModel.RunInferenceFixed(OcrBari);
string IOcrBAr = "";
for (int v = 0; v < mL2.ResultDetails.Count(); v++)
{
string iv = mL2.ResultDetails[v].LabelDisplay;
IOcrBAr += iv;
}
using (StreamWriter sw = new StreamWriter("D://123.log", true))
{
sw.WriteLine("\n");
sw.WriteLine(IOcrBAr);
sw.Flush();
}
MLRequest CamOneI = new MLRequest();
CamOneI.currentMat = Cv2.ImRead(Cam1OnePath);
CamOneI.ResizeWidth = 640;
CamOneI.ResizeHeight = 640;
CamOneI.Score = 0.3f;
CamOneI.in_lable_path = "D:\\Hisence\\ClassName.txt";//标签路径
CamOneI.confThreshold = 0.3f;//模型置信度
CamOneI.iouThreshold = 0.4f;//检测IOU
CamOneI.out_node_name = "output";
MLResult mL3 = simboObjectDetection.RunInferenceFixed(CamOneI);
if (mL3.IsSuccess)
{
DateTime dt = DateTime.Now;
mL3.ResultMap.Save("D:\\Hisence\\detImages\\" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "1result.jpg");
}
List<string> strMatListListOne = new List<string>();
Mat mResultCut = CamOneI.currentMat.Clone();
InsertSqlRunData(ref mResultCut, mL3, ref strMatListListOne, ref IpaddleOcrModel);
MLRequest CamTwoI = new MLRequest();
CamTwoI.currentMat = Cv2.ImRead(Cam1TwoPath);
CamTwoI.ResizeWidth = 640;
CamTwoI.ResizeHeight = 640;
CamTwoI.Score = 0.3f;
CamTwoI.in_lable_path = "D:\\Hisence\\ClassName.txt";//标签路径
CamTwoI.confThreshold = 0.3f;//模型置信度
CamTwoI.iouThreshold = 0.4f;//检测IOU
CamTwoI.out_node_name = "output";
MLResult mL4 = simboObjectDetection.RunInferenceFixed(CamTwoI);
if (mL4.IsSuccess)
{
DateTime dt = DateTime.Now;
mL4.ResultMap.Save("D:\\Hisence\\detImages\\" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg");
}
List<string> strMatListListTwo = new List<string>();
Mat mResultCut2 = CamTwoI.currentMat.Clone();
InsertSqlRunData(ref mResultCut2, mL4, ref strMatListListTwo, ref IpaddleOcrModel);
//文本区域
string OcrTextinsert = "";
List<string> bingji = strMatListListOne.Union(strMatListListTwo).ToList();//并(全)集
for (int j = 0; j < bingji.Count; j++)
{
string jdetial = bingji[j];
OcrTextinsert += jdetial + "##";
}
string detstr = "";
//找到识别区域
MLResult MLsum = mL3;
for (int v = 0; v < mL4.ResultDetails.Count; v++)
{
MLsum.ResultDetails.Add(mL4.ResultDetails[v]);
}
for (int v = 0; v < MLsum.ResultDetails.Count; v++)
{
if (v < MLsum.ResultDetails.Count - 1)
{
detstr += MLsum.ResultDetails[v].LabelName + ",";
}
else
{
detstr += MLsum.ResultDetails[v].LabelName ;
}
}
XKHisence xK = new XKHisence("1", IOcrBAr, MoveX, MoveY, MoveZ, detstr, OcrTextinsert, MoveTwoX, MoveTwoY, MoveTwoZ, ModelChangePath, "");
//IpaddleOcrModel.FreeModel();
string sql = "insert into XK_Hisence VALUES('" + xK.Type + "','" + xK.OcrBar + "'," + xK.MoveX + "," + xK.MoveY + "," + xK.MoveZ + ",'" + xK.Detect + "','" + xK.OcrText + "'," + xK.MoveTwoX + "," + xK.MoveTwoY + "," + xK.MoveTwoZ + ",'" + xK.OcrParm + "','" + xK.Language + "')";
int i = SQLiteHelper.ExecuteSql(sql);
if (i == 1)
{
MessageBox.Show("插入一条数据");
}
//bool ismatch = IsMatchOcrText("WF3S7021BB", strMatListListOne);
//相机回调
}
private void queryALLBtn_Click(object sender, EventArgs e)
{
try
{
DataSet dataSet = SQLiteHelper.Query("select * from XK_Hisence");
InsertDataDgv.DataSource = dataSet.Tables[0];
}
catch (Exception es)
{
}
}
private void QueryoneBtn_Click(object sender, EventArgs e)
{
try
{
string type = TypeBox.Text;
string OcrBar = OcrBarBox.Text;
string moveX = moveXbox.Text;
string moveY = moveYbox.Text;
string moveZ = moveZbox.Text;
string Detect = DetectBArMatbox.Text;
string movetwoX = moveTwoXbox.Text;
string movetwoY = moveTwoYbox.Text;
string movetwoZ = moveTwoZbox.Text;
string sql = "select * from XK_Hisence where type='" + type + "' or OcrBar='" + OcrBar + "'";
DataSet dataSet = SQLiteHelper.Query(sql);
InsertDataDgv.DataSource = dataSet.Tables[0];
}
catch (Exception es)
{
}
}
private void OpenDirBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请打开图片";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
//File fileOpen = new File(fName);
//isFileHaveName = true;
//richTextBox1.Text = fileOpen.ReadFile();
//richTextBox1.AppendText("");
Cam1MatCbx.Text = fName;
Cam1OnePath = fName;
}
}
private void OpenTwoDirBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请打开图片";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
//File fileOpen = new File(fName);
//isFileHaveName = true;
//richTextBox1.Text = fileOpen.ReadFile();
//richTextBox1.AppendText("");
Cam2MatCbx.Text = fName;
Cam1TwoPath = fName;
}
}
private void openModelBtn_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请打开json";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string fName = openFileDialog.FileName;
//File fileOpen = new File(fName);
//isFileHaveName = true;
//richTextBox1.Text = fileOpen.ReadFile();
//richTextBox1.AppendText("");
modelChangeCbx.Text = fName;
ModelChangePath = fName;
}
}
private void SnapshotCam2_Click_1(object sender, EventArgs e)
{
Cam2.Snapshot();
}
private void bnGetParam2_Click_1(object sender, EventArgs e)
{
tbExposure2.Text = Cam2.dvpGetExposure().ToString();
tbGain2.Text = Cam2.dvpGetAnalogGain().ToString();
}
}
}