优化界面,增加自动插入,原始图像遮挡旋钮,post请求当天日期。

This commit is contained in:
820689062 2024-07-23 00:22:27 +08:00
parent 812cfd725f
commit 3a9d6c5e7e
3 changed files with 446 additions and 504 deletions

View File

@ -20,6 +20,49 @@ namespace HisenceYoloDetection
{
public static class CheckDiffSciHelper
{
public static Mat ProcessImage(Mat image, Rect fillRect)
{
// 获取图像尺寸
int width = image.Width;
int height = image.Height;
// 定义左下角 30x30 矩形框
int rectSize = 30;
int rectX = 0;
int rectY = height - rectSize; // 确保是左下角
// 防止越界
if (rectY < 0 || rectX < 0 || rectSize > width || rectSize > height)
{
Console.WriteLine("图像尺寸不足以获取指定区域");
return image;
}
Rect roi = new Rect(rectX, rectY, rectSize, rectSize);
Mat roiMat = new Mat(image, roi);
// 计算平均颜色值
Scalar meanColor = Cv2.Mean(roiMat);
Vec3b fillColor = new Vec3b((byte)meanColor.Val0, (byte)meanColor.Val1, (byte)meanColor.Val2);
// 防止越界
if (fillRect.X < 0 || fillRect.Y < 0 || fillRect.X + fillRect.Width > width || fillRect.Y + fillRect.Height > height)
{
Console.WriteLine("填充区域超出图像范围");
return image;
}
// 填充指定区域
for (int y = fillRect.Y; y < fillRect.Y + fillRect.Height; y++)
{
for (int x = fillRect.X; x < fillRect.X + fillRect.Width; x++)
{
image.Set<Vec3b>(y, x, fillColor);
}
}
return image;
}
/// <summary>
///
/// </summary>
@ -37,6 +80,7 @@ namespace HisenceYoloDetection
return false;
}
// Cv2.Resize(img1, img1, new Size(550, 270));
img1 = ProcessImage(img1, sqlrect);
Mat gimg1 = new Mat();
Cv2.CvtColor(img1, gimg1, ColorConversionCodes.BGR2GRAY);
Mat thr1 = new Mat();
@ -61,7 +105,7 @@ namespace HisenceYoloDetection
return false;
}
// Cv2.Resize(img2, img2, new Size(550, 270));
img2 = ProcessImage(img2, detrect);
Rect bottomleftRect= new Rect(0,img1.Height-30,30,30);
Scalar avgColor1 = Cv2.Mean(new Mat(img1,bottomleftRect));
Mat gimg2 = new Mat();

File diff suppressed because it is too large Load Diff

View File

@ -54,7 +54,7 @@ namespace HisenceYoloDetection
//OnDetectionDone += Defet_OnDetectionDone;
};
}
MeachineState meachineState= MeachineState.machine_closing;
MeachineState meachineState = MeachineState.machine_closing;
MGSCameraDriver Cam1 = new MGSCameraDriver();
MGSCameraDriver Cam2 = new MGSCameraDriver();
public static string RootPath = "D:\\Hisence\\SQLImages\\";
@ -1211,200 +1211,8 @@ namespace HisenceYoloDetection
// }
//});
}
/// <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)
{
_runHandleBefore.WaitOne();
//这里改成和海信的对接需求
#if false
{
// Oldm_sKEYENCEBar= m_sKEYENCEBar;
//获取请求
//报警地址
string url = "https://inner-apisix.hisense.com/mes-plus/api/Fcc/GetWorkOrderBOMlnfo?user_key=k7gzo1fsfcami7n5hmpxtybyluf9xeue";
// string url = "https://echo.hoppscotch.io";
string PostUrl = url;
DateTime dt = DateTime.Now;
string starttime = string.Format("{0:yyyy_mm_dd}", dt); //17 17 2017 2017
string startTime = starttime;
string EndTime = starttime;
string lineBody = "ZZ-P";//线体
string MaterialDescripts = "制冷器";
string MaterialDescripts2 = "工艺管";
JObject patientinfo = new JObject();
JArray ids = new JArray();
ids.Add(MaterialDescripts);
ids.Add(MaterialDescripts2);
patientinfo["startTime"] = startTime;
patientinfo["EndTime"] = EndTime;
patientinfo["lineBody"] = lineBody;
patientinfo["MaterialDescripts"] = ids;
string sendData = JsonConvert.SerializeObject(patientinfo);
//eg: 发送Url需要的格式sendData={"ids":[123],"Name":小黑}
string resultData = Post(sendData, PostUrl);
resultData = "{\r\n\t\"Result\": [],\r\n\t\"success\": false,\r\n\t\"ReturnMsg\": \"参数-starttime 不正确!\"\r\n}";
JObject jo = (JObject)JsonConvert.DeserializeObject(resultData);
string sucessIf = jo["success"].ToString();
if (sucessIf.ToLower() == "true")
{
string ProductType = jo["ProductType"].ToString();
string FinalDefIo = jo["FinalDefIo"].ToString();
//WEBAPI得到条码
xKNow = GetModeFromBar(ProductType);//从数据库中查询到这个条码的四轴的值
//解析字段
}
}
#endif
// if(Oldm_sKEYENCEBar!= m_sKEYENCEBar)
xKNow = GetModeFromBar("VWJ070541V0WW824F120223");//从数据库中查询到这个条码的四轴的值
if (xKNow == null)
{
myLog("数据库没有条码匹配" + m_sKEYENCEBar, DateTime.Now);
break;
}
//if(xKNow.OcrBar!= Old_xKNow.OcrBar)
//{
//}
if (Regex.IsMatch(xKNow.MoveX.ToString(), @"^[0-9]+$") && Regex.IsMatch(xKNow.MoveY.ToString(), @"^[0-9]+$") && Regex.IsMatch(xKNow.MoveZ.ToString(), @"^[0-9]+$"))
{
// Old_xKNow = xKNow;
myLog("型号匹配成功" + xKNow.OcrBar, DateTime.Now);
//MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ);// PLC控制电机移动四轴
}
break;
}
Thread.Sleep(2);
bBar = false;
}
}
}
}
Thread.Sleep(10);
});
}
#region
/// <summary>
/// 显示结果
@ -1448,7 +1256,7 @@ namespace HisenceYoloDetection
int iNum0 = 0;
int iNum3 = 0;
Thread.CurrentThread.Priority = ThreadPriority.Highest;
while (meachineState==MeachineState.machine_opening)
while (meachineState == MeachineState.machine_opening)
{
// myResetEvent.Set();
PLC548 = true;
@ -1494,19 +1302,21 @@ namespace HisenceYoloDetection
//这里改成和海信的对接需求
string OcrBar = "";
string FinalDefIDToFind = m_sKEYENCEBar;// 目标 FinalDefID
OcrBar = FormBarType(FinalDefIDToFind);
this.Invoke(new Action(() =>
{
ScannerBox.Text = FinalDefIDToFind;
textBox3.Text = OcrBar;
}));
myLog("扫码器结果" + m_sKEYENCEBar, DateTime.Now);
#if false
//#if false
//是否给Pos请求加上时间超时
OcrBar = FormBarType(FinalDefIDToFind);
#else
OcrBar = "OZ.洗衣机.WF3G7014-NVW001CZA.WSG374A.白";
#endif
//#else
// OcrBar = "OZ.洗衣机.WF3G7014-NVW001CZA.WSG374A.白";
//#endif
// if(Oldm_sKEYENCEBar!= m_sKEYENCEBar)
@ -1826,7 +1636,8 @@ namespace HisenceYoloDetection
{
myLog("未匹配成功" + xKNow.OcrBar, DateTime.Now);
myLog("开始插入模板", DateTime.Now);
//Insert_auto();
Insert_auto(true, Cam1ImgOne, m_sKEYENCEBar, 8974, 0, 0);
break;
}
string IOcrBAr = xKNow.OcrBar;
@ -2713,12 +2524,23 @@ namespace HisenceYoloDetection
string jsonData = @"
{
""StartTime"": ""2024-07-1"",
""EndTime"": ""2024-07-22"",
""EndTime"": ""2024-07-20"",
""LineBody"": ""BZ-PC""
}";
// 获取当天日期
string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
// 解析JSON字符串为JObject
JObject jsonObject = JObject.Parse(jsonData);
// 更新EndTime字段
jsonObject["EndTime"] = currentDate;
// 将JObject转回JSON字符串
string updatedJsonData = jsonObject.ToString();
string postData = "inputvalue=" + Uri.EscapeDataString(jsonData);
string postData = "inputvalue=" + Uri.EscapeDataString(updatedJsonData);
try
{
@ -2747,8 +2569,6 @@ namespace HisenceYoloDetection
}
public void Insert_auto(bool ifwhiteBan, Mat img, string SNtext, int MoveX, int MoveY, int MoveZ)
{
// PaddleOcrModel IpaddleOcrModel = new PaddleOcrModel();
// IpaddleOcrModel.Load(ModelChangePath, "CPU");
string IOcrBAr = FormBarType(SNtext);
@ -2762,20 +2582,9 @@ namespace HisenceYoloDetection
sw.Flush();
}
//将所有的块裁剪 识别字符对比字符串
//进行推理
//从第一次拍照全图范围内目标检测然后进行OCR识别
MLRequest CamOneI = new MLRequest();
CamOneI.currentMat = Cv2.ImRead(Cam1OnePath);
CamOneI.currentMat = img;
CamOneI.ResizeWidth = 640;
CamOneI.ResizeHeight = 640;
CamOneI.Score = 0.3f;
@ -2803,19 +2612,11 @@ namespace HisenceYoloDetection
InsertXKHisenceWordData(xkWord);//往关键字表中插入一条数据
//文本区域
//List<string> bingji = strMatListOne.Union(strMatListTwo).ToList();//并(全)集
//List<string> Fuzzybingji = strMatFuzzyListOne.Union(strMatFuzzyListTwo).ToList();//并(全)集
string OcrTextone = strMatListOne.Join("##");
string OcrTextTwo = strMatFuzzyListOne.Join("##");
//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 = "";
//找到识别区域
@ -2841,14 +2642,7 @@ namespace HisenceYoloDetection
DateTime dt2 = DateTime.Now;
XKHisence xK = new XKHisence("1", IOcrBAr, MoveX, MoveY, MoveZ, detstr, OcrTextone, MoveX, MoveY, MoveZ, ModelChangePath, "", OcrTextTwo);
//string log="D:\\Hisence\\detImages\\" + dt2.Year.ToString() + dt2.Month.ToString() + dt2.Day.ToString() + dt2.Hour.ToString() + dt2.Minute.ToString() + dt2.Millisecond.ToString() + "2result.log";
// using (StreamWriter sw=new StreamWriter(log, true))
// {
// // sw.WriteLine(xK.OcrBar+"\n");
// sw.WriteLine(xK.OcrText);
// sw.WriteLine(xK.FuzzyOcrText);
// }
//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 + "','" + xK.FuzzyOcrText + "')";
int i = SQLiteHelper.ExecuteSql(sql);
@ -3946,5 +3740,30 @@ namespace HisenceYoloDetection
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);
melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);
}
private void label24_Click(object sender, EventArgs e)
{
}
private void label25_Click(object sender, EventArgs e)
{
}
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void label18_Click(object sender, EventArgs e)
{
}
private void canvas2_Load(object sender, EventArgs e)
{
}
}
}