hisence-yolo-detection/HisenceYoloDetection/ManagerModelHelper.cs

480 lines
20 KiB
C#
Raw Normal View History

2024-06-11 11:40:47 +08:00
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using XKRS.Device.SimboVision.SimboHelper;
using System.Diagnostics.Eventing.Reader;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Drawing;
using Microsoft.VisualBasic;
namespace HisenceYoloDetection
{
public static class ManagerModelHelper
{
public static string RootPath = "D:\\Hisence\\SQLImages\\";
/// <summary>
/// 全图洗衣机 裁剪之后 OCR识别的结果
/// </summary>
/// <param name="saveimage"></param>
/// <param name="CutMat"></param>
/// <param name="currentMatC">全图图片</param>
/// <param name="cam1TwoML">全局图片上的目标定位结果(包括定位矩形框)</param>
/// <param name="cam1Button"></param>
/// <param name="xK_HisenceWord"></param>
/// <param name="strMatList">返回的定位框的结果</param>
/// <param name="strMatRefList"></param>
/// <param name="IOcrModel"></param>
public static void InsertSqlRunDataButton(bool saveimage,ref Mat CutMat, ref Mat currentMatC, MLResult cam1TwoML, MLResult cam1Button, ref XK_HisenceWord xK_HisenceWord, /*ref List<string> strMatList, ref List<string> strMatRefList, */ref PaddleOcrModel IOcrModel)
{
#if true
//try
//{
Mat mResultCut = currentMatC.Clone();
Rect areaBlack=new Rect();
//旋钮的位置
if (cam1Button.ResultDetails.Count == 1)
{
Mat mResultCuti = mResultCut.Clone();
int rectsx = cam1Button.ResultDetails[0].Rect.X;
int rectsy = cam1Button.ResultDetails[0].Rect.Y;
int rectsWidth = cam1Button.ResultDetails[0].Rect.Width;
int rectsHeight = cam1Button.ResultDetails[0].Rect.Height;
areaBlack = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
}
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;
string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay;
Rect area2 = new Rect();
if (blockIndex == "2")//根据旋钮扩大范围
{
areaBlack.X -= rectsx;
areaBlack.Y -= rectsy;
area2 = areaBlack;
string TwoRectStr= CheckDiffSciHelper.rectChangeStr(area2);
xK_HisenceWord.TwoRect = TwoRectStr;
//Mat matCutblack = new Mat(mResultCuti, area2);
//if((bool)xK_HisenceWord.TwoIFWhile)
//{
// matCutblack.SetTo(Scalar.Black);
//}
//else
//{
// matCutblack.SetTo(Scalar.Black);
//}
//rectsx -= rectsWidth;
//rectsy -= 50;
//rectsWidth = rectsWidth + 2 * rectsWidth;
//rectsHeight = rectsHeight + 2 + 50;
Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
Mat matCut = new Mat(mResultCuti, area);
CutMat = matCut.Clone();
//Mat TwoCut = new Mat(mResultCuti, area2);
//TwoCut.SetTo(Scalar.Black);
//OCR识别裁剪图片
MLRequest reqcut = new MLRequest();
reqcut.currentMat = matCut.Clone();
MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut);
//if (mLCut.IsSuccess)
//{
// DateTime dt = DateTime.Now;
// mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg");
//}
BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord);
}
else
{
Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
Mat matCut = new Mat(mResultCuti, area);
//OCR识别裁剪图片
MLRequest reqcut = new MLRequest();
reqcut.currentMat = matCut.Clone();
MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut);
//if (mLCut.IsSuccess)
//{
// DateTime dt = DateTime.Now;
// mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg");
//}
BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord);
}
//插入数据库
}
//}
//catch (Exception ex)
//{
//}
#endif
}
/// <summary>
/// 全图洗衣机 裁剪之后 OCR识别的结果
/// </summary>
/// <param name="currentMatC">全图图片</param>
/// <param name="cam1TwoML">全局图片上的目标定位结果(包括定位矩形框)</param>
/// <param name="strMatList">返回的定位框的结果</param>
public static void InsertSqlRunData(bool saveimage, ref Mat currentMatC, MLResult cam1TwoML, ref XK_HisenceWord xK_HisenceWord, /*ref List<string> strMatList, ref List<string> strMatRefList,*/ ref PaddleOcrModel IOcrModel)
{
#if true
//try
//{
Mat mResultCut = currentMatC.Clone();
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;
string blockIndex = cam1TwoML.ResultDetails[i].LabelDisplay;
Rect area = new Rect(rectsx, rectsy, rectsWidth, rectsHeight);
Mat matCut = new Mat(mResultCuti, area);
//OCR识别裁剪图片
MLRequest reqcut = new MLRequest();
reqcut.currentMat = matCut.Clone();
MLResult mLCut = IOcrModel.RunInferenceFixed(reqcut);
//if (mLCut.IsSuccess)
//{
// DateTime dt = DateTime.Now;
// mLCut.ResultMap.Save("D:\\Hisence\\detImages\\OCR" + dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Millisecond.ToString() + "2result.jpg");
//}
BlockChangeFun(saveimage, blockIndex, ref matCut, ref mLCut, ref xK_HisenceWord);
//插入数据库
}
//}
//catch (Exception ex)
//{
//}
#endif
}
/// <summary>
///
/// </summary>
/// <param name="saveimage">是否保存</param>
/// <param name="blockIndex">裁剪的一块索引</param>
/// <param name="CutBlockMat">裁剪的一张图片</param>
/// <param name="mLcut">裁剪图片的一些信息</param>
/// <param name="xK_HisenceWord">要存储入数据库的东西</param>
public static void BlockChangeFun(bool saveimage, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref XK_HisenceWord xK_HisenceWord)
{
string ocrBar = xK_HisenceWord.OcrBar;
//存放关键字和所有字符串
List<string> OcrTextinsert = new List<string>();//存放关键字
List<string> OcrFuzzyTextInsert = new List<string>();//存放模糊字
string CutSavePath = "";
CombineMessage(saveimage, ocrBar, blockIndex, ref CutBlockMat, ref mLcut, ref OcrTextinsert, ref OcrFuzzyTextInsert, ref CutSavePath);
switch (blockIndex)
{
case "1"://完全匹配 重量信息
{
xK_HisenceWord.OneblockPath = CutSavePath;
xK_HisenceWord.OneblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.OneblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "2"://控制面板 匹配
{
xK_HisenceWord.TwoblockPath = CutSavePath;
xK_HisenceWord.TwoblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.TwoblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "3"://第三块板匹配
{
xK_HisenceWord.ThreeblockPath = CutSavePath;
xK_HisenceWord.ThreeblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.ThreeblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "4"://贴纸匹配
{
xK_HisenceWord.FourblockPath = CutSavePath;
xK_HisenceWord.FourblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.FourblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "5"://贴纸匹配
{
xK_HisenceWord.FiveblockPath = CutSavePath;
xK_HisenceWord.FiveblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.FiveblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "6"://贴纸匹配
{
xK_HisenceWord.SixblockPath = CutSavePath;
xK_HisenceWord.SixblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.SixblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "7"://贴纸匹配
{
xK_HisenceWord.SevenblockPath = CutSavePath;
xK_HisenceWord.SevenblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.SevenblockText = OcrFuzzyTextInsert.Join("##");
}
break;
case "8"://贴纸匹配
{
xK_HisenceWord.EightblockPath = CutSavePath;
xK_HisenceWord.EightblockMainWord = OcrTextinsert.Join("##");
xK_HisenceWord.EightblockText = OcrFuzzyTextInsert.Join("##");
}
break;
}
}
/// <summary>
///
/// </summary>
/// <param name="saveimage">是否保存本地图片</param>
/// <param name="OcrBar">唯一条形码</param>
/// <param name="blockIndex">区块号</param>
/// <param name="CutBlockMat">图像</param>
/// <param name="mLcut">图像上的数据</param>
/// <param name="OcrTextinsert">关键字</param>
/// <param name="OcrFuzzyTextInsert">所有字</param>
/// <param name="cutSavepath">图片保存路径</param>
public static void CombineMessage(bool saveimage, string OcrBar, string blockIndex, ref Mat CutBlockMat, ref MLResult mLcut, ref List<string> OcrTextinsert, ref List<string> OcrFuzzyTextInsert, ref string cutSavepath)
{
//在这里面找到带数字的关键字 将所有字也存放在数据库中
for (int j = 0; j < mLcut.ResultDetails.Count; j++)
{
string jdetial = mLcut.ResultDetails[j].LabelDisplay;
string result = Regex.Replace(jdetial, "[ \\[ \\] \\^ \\-_*×――(^)$%~!@#$…&%¥—+=<>《》!??:?`·、。,;,.;/\"‘’“”-]", "");
if (Regex.IsMatch(result, @"\d"))
{
OcrTextinsert.Add(result);
}
OcrFuzzyTextInsert.Add(result);
}
if (saveimage)
{
DateTime dt = DateTime.Now;
string namecutSavepath = OcrBar + "\\" + blockIndex + "\\" + OcrBar + "result.jpg";
cutSavepath = Path.Combine(RootPath, namecutSavepath);
//得到目录
if (!Directory.Exists(Path.GetDirectoryName(cutSavepath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(cutSavepath));
}
Cv2.ImWrite(cutSavepath, CutBlockMat);
}
}
public static List<XK_HisenceWord> GetModeWordFromBar(string SkBar)
{
List<XK_HisenceWord> cslist = SQLiteHelper.ExecuteQuery($"select * from XK_HisenceWord where OCRBar='{SkBar}' ", r => new XK_HisenceWord
{
OcrBar = r["OcrBar"].ToString(),
OneblockPath = r["OneblockPath"].ToString(),
OneblockMainWord = r["OneblockMainWord"].ToString(),
OneblockText = r["OneblockText"].ToString(),
TwoRect = r["TwoRect"].ToString(),
TwoIFWhile = r["TwoIFWhile"].ToBool(),
TwoblockPath = r["TwoblockPath"].ToString(),
TwoblockMainWord = r["TwoblockMainWord"].ToString(),
TwoblockText = r["TwoblockText"].ToString(),
ThreeblockPath = r["ThreeblockPath"].ToString(),
ThreeblockMainWord = r["ThreeblockMainWord"].ToString(),
ThreeblockText = r["ThreeblockText"].ToString(),
FourblockPath = r["FourblockPath"].ToString(),
FourblockMainWord = r["FourblockMainWord"].ToString(),
FourblockText = r["FourblockText"].ToString(),
FiveblockPath = r["FiveblockPath"].ToString(),
FiveblockMainWord = r["FiveblockMainWord"].ToString(),
FiveblockText = r["FiveblockText"].ToString(),
SixblockPath = r["SixblockPath"].ToString(),
SixblockMainWord = r["SixblockMainWord"].ToString(),
SixblockText = r["SixblockText"].ToString(),
SevenblockPath = r["SevenblockPath"].ToString(),
SevenblockMainWord = r["SevenblockMainWord"].ToString(),
SevenblockText = r["SevenblockText"].ToString(),
EightblockPath = r["EightblockPath"].ToString(),
EightblockMainWord = r["EightblockMainWord"].ToString(),
EightblockText = r["EightblockText"].ToString()
});
return cslist;
}
public static bool IsMatchSQLText(ref Mat detMat, ref XK_HisenceWord XKSQL, ref XK_HisenceWord XKDet)
{
try
{
string TwoRectstr=XKSQL.TwoRect;
string oneBlockWordSql = XKSQL.OneblockMainWord;
string twoBlockWordSql = XKSQL.TwoblockMainWord;
string threeBlockWordSql = XKSQL.ThreeblockMainWord;
string fourBlockWordSql = XKSQL.FourblockMainWord;
string fiveBlockWordSql = XKSQL.FiveblockMainWord;
string sixBlockWordSql = XKSQL.SixblockMainWord;
string sevenBlockWordSql = XKSQL.SevenblockMainWord;
string eightBlockWordSql = XKSQL.EightblockMainWord;
string oneBlockWordDet = XKDet.OneblockMainWord;
string twoBlockWordDet = XKDet.TwoblockMainWord;
string threeBlockWordDet = XKDet.ThreeblockMainWord;
string fourBlockWordDet = XKDet.FourblockMainWord;
string fiveBlockWordDet = XKDet.FiveblockMainWord;
string sixBlockWordDet = XKDet.SixblockMainWord;
string sevenBlockWordDet = XKDet.SevenblockMainWord;
string eightBlockWordDet = XKDet.EightblockMainWord;
bool OneIF = isMatchStr(oneBlockWordSql, oneBlockWordDet);
bool TwoIF = isMatchStr(twoBlockWordSql, twoBlockWordDet);
bool ThreeIF = isMatchStr(threeBlockWordSql, threeBlockWordDet);
bool FourIF = isMatchStr(fourBlockWordSql, fourBlockWordDet);
bool FiveIF = isMatchStr(fiveBlockWordSql, fiveBlockWordDet);
bool SixIF = isMatchStr(sixBlockWordSql, sixBlockWordDet);
bool SenvenIF = isMatchStr(sevenBlockWordSql, sevenBlockWordDet);
bool EightIF = isMatchStr(eightBlockWordSql, eightBlockWordDet);
//第二快 卷积匹配
string PathSql = XKSQL.TwoblockPath;
//
Rect rectsql = CheckDiffSciHelper.strChangeRect(TwoRectstr);
Rect rectDet = CheckDiffSciHelper.strChangeRect(XKDet.TwoRect);
bool twoif2 = CheckDiffSciHelper.CheckDiffSci(PathSql, detMat, rectsql, rectDet,(bool)XKSQL.TwoIFWhile, "D://");
//第三块区域一直都是false
if (OneIF && TwoIF && ThreeIF && FourIF && FiveIF && SixIF && SenvenIF && EightIF&& twoif2)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
public static bool isMatchStr(string SqlText, string DetText)
{
if (SqlText.Contains("##") && DetText.Contains("##"))
{
// 计算Levenshtein距离
int distance = LevenshteinDistance(SqlText, DetText);
// 计算相似度相似度等于1减去标准化的Levenshtein距离
double similarity = 1 - ((double)distance / Math.Max(SqlText.Length, DetText.Length));
bool areEqual = false;
if (similarity<0.5)
{
areEqual = false;
}
else
{
areEqual = true;
}
//string[] sArraysql = Regex.Split(SqlText, "##", RegexOptions.IgnoreCase);
//string[] sArraydet = Regex.Split(DetText, "##", RegexOptions.IgnoreCase);
//bool areEqual = sArraysql.OrderBy(x => x).SequenceEqual(sArraydet.OrderBy(x => x));
return areEqual;
}
else if ((SqlText == "" || SqlText == null) && (DetText == "" || DetText == null))
{
return true;
}
else
{
return false;
}
}
// 计算Levenshtein距离
static int LevenshteinDistance(string string1, string string2)
{
int[,] dp = new int[string1.Length + 1, string2.Length + 1];
for (int i = 0; i <= string1.Length; i++)
dp[i, 0] = i;
for (int j = 0; j <= string2.Length; j++)
dp[0, j] = j;
for (int i = 1; i <= string1.Length; i++)
{
for (int j = 1; j <= string2.Length; j++)
{
int cost = string1[i - 1] == string2[j - 1] ? 0 : 1;
dp[i, j] = Math.Min(Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1), dp[i - 1, j - 1] + cost);
}
}
return dp[string1.Length, string2.Length];
}
}
}