修改图片闪烁,yolo先定位在识别,误检率和报警灯

This commit is contained in:
xhm\HP 2024-07-05 11:22:41 +08:00
parent a018a8f54d
commit eb55c5b784
6 changed files with 848 additions and 651 deletions

View File

@ -12,6 +12,7 @@
</PropertyGroup>
<ItemGroup>
<None Remove="MainForm.resx~RF4efdcc4.TMP" />
<None Remove="ManagerModelHelper.cs~RF97ff9f.TMP" />
<None Remove="MelsecPLCTCPDriver.cs~RFacf25a.TMP" />
</ItemGroup>

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,7 @@ using System.Net;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using XKRS.Device.SimboVision.SimboHelper;
using XKRS.UI;
@ -64,6 +65,9 @@ namespace HisenceYoloDetection
List<XKHisence> xKHisences = new List<XKHisence>();
static AutoResetEvent myResetEvent = new AutoResetEvent(false);
AutoResetEvent _runHandleBefore = new AutoResetEvent(false);
ManualResetEvent _runHandleAfter = new ManualResetEvent(false);
bool PLC545 = false;
bool PLC548 = false;
@ -103,7 +107,11 @@ namespace HisenceYoloDetection
string Cam1OnePath = "";
string Cam1TwoPath = "";
string ModelChangePath = "";
//报警灯
string GreenLightingAdress = "";
string YellowLightingAdress = "";
string RedLightingAdress = "";
string WaringAdress = "";
/// <summary>
/// 查看相机列表
/// </summary>
@ -191,8 +199,21 @@ namespace HisenceYoloDetection
/// <param name="e"></param>
private void MainForm_Load(object sender, EventArgs e)
{
//报警地址
string s1 = "gorenjeSTEAMTECHSNOWFLAKEDRUM65kg";
if (_runHandleBefore == null)
{
_runHandleBefore = new AutoResetEvent(false);
}
if (_runHandleAfter == null)
{
_runHandleAfter = new ManualResetEvent(false);
}
string s1 = "gorenjeSTEAMTECHSNOWFLAKEDRUM65kg";
string s2 = "STEAMTECHSNOWFLAKEDRUM65kg";
bool st = ManagerModelHelper.StrMatch(s1, s2);
@ -268,6 +289,8 @@ namespace HisenceYoloDetection
OnSpeedShow();//线程开启将速度显示在界面上
OnPostionShow();//线程开启将位置显示在界面上
melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 1);//黄灯
//t = new System.Timers.Timer(10000);//实例化Timer类设置间隔时间为10000毫秒
//t.Elapsed += new System.Timers.ElapsedEventHandler(Execute);//到达时间的时候执行事件;
//t.AutoReset = true;//设置是执行一次false还是一直执行(true)
@ -277,7 +300,7 @@ namespace HisenceYoloDetection
}
public void Execute(object source, System.Timers.ElapsedEventArgs e)
{
t.Stop(); //先关闭定时器
//melsecPLCTCPDriver.WriteInt("548", 1);
//Thread.Sleep(1000);
@ -525,7 +548,7 @@ namespace HisenceYoloDetection
// tbFrameRate.Enabled = false;
bnGetParam.Enabled = false;
bnSetParam.Enabled = false;
}
private void button3_Click(object sender, EventArgs e)
{
@ -592,7 +615,7 @@ namespace HisenceYoloDetection
//Scanner.OnClientDataReceived += OnDataReceived;
//Scanner.btnSendMsg("LON\r");//发送开启指令
//myLog("发送恩士扫码枪开启指令", DateTime.Now);
//PLC使能开启
melsecPLCTCPDriver.WriteInt("581", 0);//定位松开
melsecPLCTCPDriver.WriteInt("504", 0);//定位松开
@ -983,11 +1006,12 @@ namespace HisenceYoloDetection
}
if (iNum1 >= 2)
{
if (bBar)
{
_runHandleBefore.WaitOne();
bBarTriger = true;//修改成相机拍照获取型号
Cam2.SnapshotCount = 0;
Cam2.Snapshot();
xKNow = GetModeFromBar("VWJ070633V0WW80F0120356");//从数据库中查询到这个条码的四轴的值
//xKNow = GetModeFromBar(m_sKEYENCEBar);//从数据库中查询到这个条码的四轴的值
@ -1094,15 +1118,15 @@ namespace HisenceYoloDetection
{
//if (bBarTriger)
{
#if true
if(!StopM)
#if false
if (!StopM)
{
melsecPLCTCPDriver.WriteInt("550", 1);//阻塞
StopM = true;
}
myLog("第一次采集图像", DateTime.Now);
//移动模组 读三次测距的地址 看是否往前移动
MoveToP(5815, YPos, ZPos);
@ -1123,7 +1147,7 @@ namespace HisenceYoloDetection
double disreal = average * (-0.54054) + 1324.3232;
//测距距离-标准距离=要移动的距离
double dismove = disreal - 876;
if (dismove > 0 && dismove < 4000)
if (dismove > -4000 && dismove < 4000)
{
MoveToP(5815, YPos, ZPos + (int)(dismove));
}
@ -1135,18 +1159,18 @@ namespace HisenceYoloDetection
Cam2.SnapshotCount = 0;
Cam2.Snapshot();
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);
melsecPLCTCPDriver.WriteInt("550", 0);//方行
//Thread.Sleep(300);//洗衣机可能过不去
//Thread.Sleep(300);//洗衣机可能过不去
//if (xKNow != null)
//{
// MoveToP(xKNow.MoveX, xKNow.MoveY, xKNow.MoveZ);
@ -1277,8 +1301,8 @@ namespace HisenceYoloDetection
{
//Cam1ImgOne = Cv2.ImRead("D:\\Hisence\\类型\\1\\001.jpg"); ;
Cam1ImgOne = cameraMat;
Cam1ImgOne = Cv2.ImRead("D:\\Hisence\\类型\\1\\001.jpg"); ;
//Cam1ImgOne = cameraMat;
IfCam1OneTriger = true;
originMatShow.Image = cameraMat.ToBitmap();
//pictureBox1.Image = cameraMat.ToBitmap();
@ -1288,13 +1312,13 @@ namespace HisenceYoloDetection
//第二次拍照
if (SnapshotCount == 2)
{
// Mat mat = Cv2.ImRead("D:\\Hisence\\类型\\1\\002.jpg");
// Cam1ImgTwo = mat;
Cam1ImgTwo = cameraMat;
Cam1ImgTwo = Cv2.ImRead("D:\\Hisence\\类型\\1\\002.jpg");
//Cam1ImgTwo = cameraMat;
IfCam1TwoTriger = true;
originMatShow2.Image = cameraMat.ToBitmap();
_runHandleAfter.Set();
}
@ -1320,8 +1344,9 @@ namespace HisenceYoloDetection
// Mat mat = Cv2.ImRead("F:\\海信洗衣机\\cam1\\2024517161641.jpg");
Cam2ImgShowBar = cameraMat;
IfCam2Triger = true;
originMat2Show.Image = cameraMat.ToBitmap();
originMat2Show.Image = cameraMat.ToBitmap();
//_runHandleAfter.Set();
// 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");
@ -1329,6 +1354,7 @@ namespace HisenceYoloDetection
public volatile int AllDsums = 0;
public volatile int NGDsums = 0;
public volatile int OKDsums = 0;
public volatile int WUsums = 0;
/// <summary>
/// 开始识别
/// </summary>
@ -1341,6 +1367,8 @@ namespace HisenceYoloDetection
{
try
{
//信号量阻塞 使其不在总数增加和闪烁
_runHandleAfter.WaitOne();
//相机2第一次拍照
if (IfCam2Triger)
{
@ -1360,7 +1388,8 @@ namespace HisenceYoloDetection
sw.WriteLine(IOcrBAr + "\n");
sw.Flush();
}
IOcrBAr = "BatchW96DHModelWF3S7021BWPl";
// IOcrBAr = "BatchW9659ModelWNHPI74SCPSDE";
IOcrBAr = "W821PWMS27106WD2";
//根据条码数据库比对
xKNow = GetModeFromBar(IOcrBAr);//从数据库中查询到这个条码的四轴的值
List<XK_HisenceWord> xkWordList = ManagerModelHelper.GetModeWordFromBar(IOcrBAr);
@ -1405,8 +1434,11 @@ namespace HisenceYoloDetection
}
//相机1第二次拍照
if (IfCam1TwoTriger && bBarTriger)
// if (IfCam1TwoTriger && bBarTriger)
{
IfCam1TwoTriger = false;
bBarTriger = false;
_runHandleAfter.Reset();
//初始化板
Defet_OnDetectionDone(whiteMat, 1);
Defet_OnDetectionDone(whiteMat, 2);
@ -1415,8 +1447,7 @@ namespace HisenceYoloDetection
Defet_OnDetectionDone(whiteMat, 5);
Defet_OnDetectionDone(whiteMat, 6);
Defet_OnDetectionDone(whiteMat, 7);
XK_HisenceWord xK_MatchDet = new XK_HisenceWord();
xK_MatchDet.TwoIFWhile = xK_HisenceSQLWord.TwoIFWhile;
AllDsums++;
@ -1438,25 +1469,14 @@ namespace HisenceYoloDetection
List<string> strMatListOne = new List<string>();
List<string> strMatFuzzyListOne = new List<string>();
Mat mResultCut = req.currentMat.Clone();
Mat mCut = new Mat();
Stopwatch sw = new Stopwatch();
sw.Start();
Dictionary<string, bool> keyValueResult = new Dictionary<string, bool>();
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");
ResultMatShow.Image = mL.ResultMap;
}
else
{
Mat res_yolo = Cv2.ImRead("ng.jpg");
ResultMatShow.Image = res_yolo.ToBitmap();
}//无法框定导致图片闪烁加else无法解决
//进行推理
MLRequest req2 = new MLRequest();
req2.currentMat = Cam1ImgTwo;
@ -1468,18 +1488,18 @@ namespace HisenceYoloDetection
req2.iouThreshold = 0.4f;//检测IOU
req2.out_node_name = "output";
MLResult mL2 = simboObjectDetection.RunInferenceFixed(req2);
List<string> strMatListTwo = new List<string>();
List<string> strMatFuzzyListTwo = new List<string>();
Mat mResultCut2 = req2.currentMat.Clone();
Dictionary<string, bool> keyValueResult2 = new Dictionary<string, bool>();
InsertSqlRunData(ref keyValueResult2, false, ref mResultCut2, mL2, ref xK_HisenceSQLWord, ref xK_MatchDet, ref strMatListTwo, ref strMatFuzzyListTwo, ref paddleOcrModel);
if (mL2.ResultMap != null)
{
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;
}
List<string> strMatListTwo = new List<string>();
List<string> strMatFuzzyListTwo = new List<string>();
Mat mResultCut2 = req2.currentMat.Clone();
Dictionary<string, bool> keyValueResult = new Dictionary<string, bool>();
Dictionary<string, bool> keyValueResult2 = new Dictionary<string, bool>();
#region
//执行定位数组块
string[] listLabels = xKNow.Detect.Split(",");
@ -1507,6 +1527,25 @@ namespace HisenceYoloDetection
HashSet<string> set1 = new HashSet<string>(listLabels);
HashSet<string> set2 = new HashSet<string>(RealLabels);
bool MatchStr = set1.SetEquals(set2);
#endregion
//块比对完了 进行详细比对
if (MatchStr)
{
//Mat mCut = new Mat();
Stopwatch sw = new Stopwatch();
sw.Start();
InsertSqlRunDataButton(ref keyValueResult, false, ref mResultCut, mL, mLButton, ref xK_HisenceSQLWord, ref xK_MatchDet, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel);
InsertSqlRunData(ref keyValueResult2, false, ref mResultCut2, mL2, ref xK_HisenceSQLWord, ref xK_MatchDet, ref strMatListTwo, ref strMatFuzzyListTwo, ref paddleOcrModel);
sw.Stop();
myLog("插入面板时间" + sw.ElapsedMilliseconds, DateTime.Now);
}
if (MatchStr)
{
@ -1523,9 +1562,7 @@ namespace HisenceYoloDetection
// int sqlblocksum=
IfCam1TwoTriger = false;
bBarTriger = false;
melsecPLCTCPDriver.WriteInt("550", 0);//方行
if (MatchStr)
@ -1543,14 +1580,39 @@ namespace HisenceYoloDetection
}
else
{
NGDsums++;
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);//红灯
melsecPLCTCPDriver.WriteInt(WaringAdress, 1);//报警
myLog("匹配失败", DateTime.Now);
//界面显示
// InsertXK_HisenceWordMatchData(xK_MatchDet, false);
this.Invoke(new Action(() =>
//加上人为判断是否是NG洗衣机
DialogResult dr = MessageBox.Show("是否误检?", "是否误检", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
if (dr == DialogResult.OK)
{
OKOrNGShow.Image = NGbitmap;
}));
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
OKDsums++;
WUsums++;
this.Invoke(new Action(() =>
{
double percent = (double)WUsums/ AllDsums;
string percentText = percent.ToString("0.0%");//最后percentText的值为10.0%
textBox1.Text = percentText;
OKOrNGShow.Image = OKbitmap;
}));
}
else
{
NGDsums++;
melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
this.Invoke(new Action(() =>
{
OKOrNGShow.Image = NGbitmap;
}));
}
}
@ -1559,7 +1621,7 @@ namespace HisenceYoloDetection
}
catch (Exception e)
{
throw e;
}
finally
{
@ -1639,7 +1701,7 @@ namespace HisenceYoloDetection
{
m_sKEYENCEBar = System.Text.Encoding.UTF8.GetString(data);
// myLog("型号Ocr" + m_sKEYENCEBar, DateTime.Now);
_runHandleBefore.Set();
bBar = true;
}
@ -1838,6 +1900,7 @@ namespace HisenceYoloDetection
*/
//OnGetBar();
melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//黄灯
TriggerCameral2();
ReadyDetect();
myLog("开启流程成功", DateTime.Now);
@ -2235,9 +2298,9 @@ namespace HisenceYoloDetection
List<string> strMatListOne = new List<string>();
List<string> strMatFuzzyListOne = new List<string>();
Mat mResultCut = CamOneI.currentMat.Clone();
Mat mCut = new Mat();
//Mat mCut = new Mat();
Dictionary<string, bool> keyValueResult = new Dictionary<string, bool>();
InsertSqlRunDataButton(ref keyValueResult, true, ref mCut, ref mResultCut, mL3, mLButton, ref xK_HisenceSQLWord, ref xkWord, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel);
InsertSqlRunDataButton(ref keyValueResult, true, ref mResultCut, mL3, mLButton, ref xK_HisenceSQLWord, ref xkWord, ref strMatListOne, ref strMatFuzzyListOne, ref paddleOcrModel);
@ -2641,37 +2704,37 @@ namespace HisenceYoloDetection
{
case 1:
{
DefetShow1.ImaMAt = detectionImage;
canvas1.ImaMAt = detectionImage;
}
break;
case 2:
{
DefetShow2.ImaMAt = detectionImage;
canvas2.ImaMAt = detectionImage;
}
break;
case 3:
{
DefetShow3.ImaMAt = detectionImage;
canvas3.ImaMAt = detectionImage;
}
break;
case 4:
{
DefetShow4.ImaMAt = detectionImage;
canvas4.ImaMAt = detectionImage;
}
break;
case 5:
{
DefetShow5.ImaMAt = detectionImage;
canvas5.ImaMAt = detectionImage;
}
break;
case 6:
{
DefetShow6.ImaMAt = detectionImage;
canvas6.ImaMAt = detectionImage;
}
break;
case 7:
{
DefetShow7.ImaMAt = detectionImage;
// canvas7.ImaMAt = detectionImage;
}
break;
case 8:
@ -2701,7 +2764,7 @@ namespace HisenceYoloDetection
/// <param name="strMatList">返回的定位框的结果</param>
/// <param name="strMatRefList"></param>
/// <param name="IOcrModel"></param>
public void InsertSqlRunDataButton(ref Dictionary<string, bool> 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<string> strMatList, ref List<string> strMatRefList, ref PaddleOcrModel IOcrModel)
public void InsertSqlRunDataButton(ref Dictionary<string, bool> keyValueResult, bool saveimage, ref Mat currentMatC, MLResult cam1TwoML, MLResult cam1Button, ref XK_HisenceWord SQlxK_HisenceWord, ref XK_HisenceWord xK_HisenceWord, ref List<string> strMatList, ref List<string> strMatRefList, ref PaddleOcrModel IOcrModel)
{
#if true
//try
@ -2923,7 +2986,7 @@ namespace HisenceYoloDetection
//显示错误的板
DisplayResult(mLcut);
Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 1);
string Path_1 = Path.Combine("D:\\Hisence\\Test\\1\\ng", filenameone + "_res.png");
Cv2.ImWrite(Path_1, mLcut.ResultMap.ToMat());
@ -3081,22 +3144,22 @@ namespace HisenceYoloDetection
Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.SixblockPath;
//第六块卷积设置一直为true
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test");
juanjiMatch = true;
if (!OneIF1 || !juanjiMatch)
if (!OneIF)
{
OneIF = false;
// DisplayResult(mLcut);
//显示错误的板
// mLcut.ResultMap;
Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 6);
}
else
{
OneIF = true;
Defet_OnDetectionDone(OKMat, 6);
}
{
OneIF = false;
// DisplayResult(mLcut);
//显示错误的板
// mLcut.ResultMap;
Defet_OnDetectionDone(mLcut.ResultMap.ToMat(), 6);
}
else
{
OneIF = true;
Defet_OnDetectionDone(OKMat, 6);
}
}
}
@ -3228,5 +3291,10 @@ namespace HisenceYoloDetection
{
melsecPLCTCPDriver.WriteInt("550", 0);
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
melsecPLCTCPDriver.WriteInt("550", 0);
}
}
}

View File

@ -11,7 +11,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();

View File

@ -12,7 +12,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();

View File

@ -399,6 +399,7 @@ namespace XKRS.UI
}
#endregion
#region
GPathList.ForEach(path =>
{