增加旋钮识别

This commit is contained in:
820689062 2024-07-24 10:48:05 +08:00
parent 3a9d6c5e7e
commit 618c799818
2 changed files with 140 additions and 89 deletions

View File

@ -10,7 +10,7 @@ using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames; using static System.Net.Mime.MediaTypeNames;
using Point = OpenCvSharp.Point; using Point = OpenCvSharp.Point;
using Size = OpenCvSharp.Size; using Size = OpenCvSharp.Size;
using System;
using OpenCvSharp; using OpenCvSharp;
using OpenCvSharp.Features2D; using OpenCvSharp.Features2D;
using OpenCvSharp.Flann; using OpenCvSharp.Flann;
@ -20,6 +20,8 @@ namespace HisenceYoloDetection
{ {
public static class CheckDiffSciHelper public static class CheckDiffSciHelper
{ {
public static Mat ProcessImage(Mat image, Rect fillRect) public static Mat ProcessImage(Mat image, Rect fillRect)
{ {
// 获取图像尺寸 // 获取图像尺寸
@ -229,20 +231,6 @@ namespace HisenceYoloDetection
{ {
Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED); Cv2.DrawContours(blackhatImg, new Point[][] { contour }, -1, Scalar.Black, thickness: Cv2.FILLED);
// 框选轮廓 // 框选轮廓
string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath2, img2);
string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg);
}
else
{
Rect boundingRect = Cv2.BoundingRect(contour);
Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2);
isMatch = false;
string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png"); string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ok", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png");
// 保存结果 // 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
@ -252,8 +240,32 @@ namespace HisenceYoloDetection
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png"); //string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg); Cv2.ImWrite(savePath, blackhatImg);
} }
else
{
Rect boundingRect = Cv2.BoundingRect(contour);
Cv2.Rectangle(img2, boundingRect, Scalar.Red, thickness: 2);
isMatch = false;
string savePath2 = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_Rect.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath2, img2);
string savePath = Path.Combine("D:\\Hisence\\Test\\2\\ng", Path.GetFileNameWithoutExtension(path1) + filename + "_diff.png");
// 保存结果
//string savePath = Path.Combine(saveDir, Path.GetFileNameWithoutExtension(path2) + "_diff.png");
Cv2.ImWrite(savePath, blackhatImg);
}
} }
// 新增的白色面积占比判断
double whiteArea1 = Cv2.CountNonZero(thr1);
double whiteArea2 = Cv2.CountNonZero(thr2);
double ratio1 = whiteArea1 / (thr1.Rows * thr1.Cols);
double ratio2 = whiteArea2 / (thr2.Rows * thr2.Cols);
if (Math.Abs(ratio1 - ratio2) >= 0.9)
{
isMatch = true;
}
return isMatch; return isMatch;
} }

View File

@ -1605,25 +1605,7 @@ namespace HisenceYoloDetection
//2第一次拍照 //2第一次拍照
//if (IfCam2Triger) //if (IfCam2Triger)
{ {
// IfCam2Triger = false;
//OCR识别
//MLRequest req = new MLRequest();
//req.currentMat = Cam2ImgShowBar;
////req.currentMat = Cv2.ImRead("D:\\Hisence\\类型\\1\\bar.jpg");
////相机识别的字符串
//string IOcrBAr = DetMachineBar(ref req);
//DateTime dt = DateTime.Now;
//using (StreamWriter sw = new StreamWriter("D://Hisence//logsBar.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");
// sw.WriteLine(IOcrBAr + "\n");
// sw.Flush();
//}
// IOcrBAr = "BatchW9659ModelWNHPI74SCPSDE";
// IOcrBAr = "W821PWMS27106WD2";
// IOcrBAr=
//根据条码数据库比对 //根据条码数据库比对
_runHandleAfter.Reset(); _runHandleAfter.Reset();
if (xKNow == null) if (xKNow == null)
@ -1654,22 +1636,7 @@ namespace HisenceYoloDetection
if (xKNow.Detect != "") if (xKNow.Detect != "")
{ {
////此时运行的洗衣机是和之前一个语言模型
//if (IfChangeLanguage == IOcrBAr)
//{
//}
//else
//{
// //本地存在这个OCR.josn参数
// if (File.Exists(xKNow.OcrParm))
// {
// paddleOcrModel.Load(xKNow.OcrParm, "CPU");
// IfChangeLanguage = IOcrBAr;
// }
//}
myLog("型号匹配成功" + IOcrBAr, DateTime.Now); myLog("型号匹配成功" + IOcrBAr, DateTime.Now);
} }
@ -1689,10 +1656,10 @@ namespace HisenceYoloDetection
Defet_OnDetectionDone(whiteMat, 1); Defet_OnDetectionDone(whiteMat, 1);
Defet_OnDetectionDone(whiteMat, 2); Defet_OnDetectionDone(whiteMat, 2);
Defet_OnDetectionDone(whiteMat, 3); Defet_OnDetectionDone(whiteMat, 3);
Defet_OnDetectionDone(whiteMat, 4); //Defet_OnDetectionDone(whiteMat, 4);
Defet_OnDetectionDone(whiteMat, 5); //Defet_OnDetectionDone(whiteMat, 5);
Defet_OnDetectionDone(whiteMat, 6); //Defet_OnDetectionDone(whiteMat, 6);
Defet_OnDetectionDone(whiteMat, 7); //Defet_OnDetectionDone(whiteMat, 7);
XK_HisenceWord xK_MatchDet = new XK_HisenceWord(); XK_HisenceWord xK_MatchDet = new XK_HisenceWord();
xK_MatchDet.TwoIFWhile = xK_HisenceSQLWord.TwoIFWhile; xK_MatchDet.TwoIFWhile = xK_HisenceSQLWord.TwoIFWhile;
@ -1818,48 +1785,48 @@ namespace HisenceYoloDetection
OKOrNGShow.Image = NGbitmap; OKOrNGShow.Image = NGbitmap;
})); }));
//melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 1);//红灯
//melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯 melsecPLCTCPDriver.WriteInt(YellowLightingAdress, 0);//黄灯
//melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 0);//绿灯
//melsecPLCTCPDriver.WriteInt(WaringAdress, 1);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 1);//报警
////加上人为判断是否是NG洗衣机 //加上人为判断是否是NG洗衣机
//DialogResult dr = MessageBox.Show("是否误检?", "是否误检", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); DialogResult dr = MessageBox.Show("是否误检?", "是否误检", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
//if (dr == DialogResult.OK) if (dr == DialogResult.OK)
//{ {
// melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
// melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯
// melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
// OKDsums++; OKDsums++;
// WUsums++; WUsums++;
// this.Invoke(new Action(() => this.Invoke(new Action(() =>
// { {
// double percent = (double)WUsums / AllDsums; double percent = (double)WUsums / AllDsums;
// string percentText = percent.ToString("0.0%");//最后percentText的值为10.0% string percentText = percent.ToString("0.0%");//最后percentText的值为10.0%
// textBox1.Text = percentText; textBox1.Text = percentText;
// OKOrNGShow.Image = OKbitmap; OKOrNGShow.Image = OKbitmap;
// })); }));
// myLog("匹配失败", DateTime.Now); myLog("匹配失败", DateTime.Now);
//} }
//else else
//{ {
// NGDsums++; NGDsums++;
// melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警 melsecPLCTCPDriver.WriteInt(WaringAdress, 0);//报警
// melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯 melsecPLCTCPDriver.WriteInt(RedLightingAdress, 0);//红灯
// melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯 melsecPLCTCPDriver.WriteInt(GreenLightingAdress, 1);//绿灯
// this.Invoke(new Action(() => this.Invoke(new Action(() =>
// { {
// OKOrNGShow.Image = NGbitmap; OKOrNGShow.Image = NGbitmap;
// })); }));
// myLog("匹配成功", DateTime.Now); myLog("匹配成功", DateTime.Now);
//} }
@ -3423,6 +3390,7 @@ namespace HisenceYoloDetection
Rect rectsql = CheckDiffSciHelper.strChangeRect(SQlxK_HisenceWord.TwoRect); Rect rectsql = CheckDiffSciHelper.strChangeRect(SQlxK_HisenceWord.TwoRect);
Rect rectDet = CheckDiffSciHelper.strChangeRect(xK_HisenceWord.TwoRect); Rect rectDet = CheckDiffSciHelper.strChangeRect(xK_HisenceWord.TwoRect);
juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rectsql, rectDet, (bool)SQlxK_HisenceWord.TwoIFWhile, "D://Hisence//Test"); juanjiMatch = CheckDiffSciHelper.CheckDiffSci(PathSql, CutBlockMat, rectsql, rectDet, (bool)SQlxK_HisenceWord.TwoIFWhile, "D://Hisence//Test");
// juanjiMatch = true; // juanjiMatch = true;
@ -3454,8 +3422,8 @@ namespace HisenceYoloDetection
Rect rect = new Rect(0, 0, 0, 0); Rect rect = new Rect(0, 0, 0, 0);
string PathSql = SQlxK_HisenceWord.ThreeblockPath; string PathSql = SQlxK_HisenceWord.ThreeblockPath;
bool iswhite = IsMostlyWhite(PathSql);
juanjiMatch = CheckDiffSciHelper1.CheckDiffSci(PathSql, CutBlockMat, rect, rect, false, "D://Hisence//Test1"); juanjiMatch = CheckDiffSciHelper1.CheckDiffSci(PathSql, CutBlockMat, rect, rect, iswhite, "D://Hisence//Test1");
if (!OneIF1 || !juanjiMatch) if (!OneIF1 || !juanjiMatch)
{ {
OneIF = true;//待修改6.28 OneIF = true;//待修改6.28
@ -3644,6 +3612,72 @@ namespace HisenceYoloDetection
} }
static bool IsMostlyWhite(string imagePath)
{
Bitmap bitmap = new Bitmap(imagePath);
int width = bitmap.Width;
int height = bitmap.Height;
int blackCount = 0;
int whiteCount = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
Color pixelColor = bitmap.GetPixel(x, y);
int r = pixelColor.R;
int g = pixelColor.G;
int b = pixelColor.B;
// 判断是否为黑色
if (r <= 70 && g <= 70 && b <= 70)
{
blackCount++;
}
else
{
whiteCount++;
}
}
}
int totalPixels = width * height;
double blackRatio = (double)blackCount / totalPixels;
// 如果黑色比例大于等于0.6返回false否则返回true
return blackRatio < 0.6;
}
static string matchBtnColor(Mat img, Rect rect)
{
// 提取指定区域
Mat roi = new Mat(img, rect);
// 计算平均颜色
Scalar mean = Cv2.Mean(roi);
// 获取平均颜色的RGB值
double r = mean.Val2;
double g = mean.Val1;
double b = mean.Val0;
// 计算平均灰度值
double averageGray = (r + g + b) / 3;
// 判断颜色类别
if (averageGray < 60)
{
return "黑色";
}
else if (averageGray >= 60 && averageGray <= 150)
{
return "银色";
}
else
{
return "白色";
}
}
private void bnGetParam2_Click_1(object sender, EventArgs e) private void bnGetParam2_Click_1(object sender, EventArgs e)
{ {
@ -3765,5 +3799,10 @@ namespace HisenceYoloDetection
{ {
} }
private void label27_Click(object sender, EventArgs e)
{
}
} }
} }