391 lines
15 KiB
C#
391 lines
15 KiB
C#
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;
|
||
using System.Xml;
|
||
using System.Runtime.InteropServices;
|
||
|
||
namespace HisenceYoloDetection
|
||
{
|
||
|
||
public static class ManagerModelHelper
|
||
{
|
||
public static string RootPath = "D:\\Hisence\\SQLImages\\";
|
||
|
||
|
||
|
||
|
||
/// <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 MatMessage(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://Test");
|
||
DateTime dt = DateTime.Now;
|
||
using (StreamWriter sw = new StreamWriter("D://Hisence//logsMatch.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(oneBlockWordSql + " " + oneBlockWordDet + "\n");
|
||
sw.WriteLine(twoBlockWordSql + " " + twoBlockWordDet + "\n");
|
||
sw.WriteLine(threeBlockWordSql + " " + threeBlockWordDet + "\n");
|
||
sw.WriteLine(fourBlockWordSql + " " + fourBlockWordDet + "\n");
|
||
sw.WriteLine(fiveBlockWordSql + " " + fiveBlockWordDet + "\n");
|
||
sw.WriteLine(sixBlockWordSql + " " + sixBlockWordDet + "\n");
|
||
sw.WriteLine(sevenBlockWordSql + " " + sevenBlockWordDet + "\n");
|
||
sw.WriteLine(eightBlockWordSql + " " + eightBlockWordDet + "\n");
|
||
sw.WriteLine(" 卷积匹配 " + twoif2 + "\n");
|
||
sw.Flush();
|
||
}
|
||
//第三块区域一直都是false
|
||
if (OneIF && TwoIF && ThreeIF && FourIF && FiveIF && SixIF && SenvenIF && EightIF && twoif2)
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
}
|
||
public static bool StrMatch(string SqlText,string DetText)
|
||
{
|
||
// 计算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;
|
||
|
||
}
|
||
static bool AreArraysEqual(string[] array1, string[] array2)
|
||
{
|
||
if (array1.Length != array2.Length)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
// 将数组排序
|
||
Array.Sort(array1);
|
||
Array.Sort(array2);
|
||
|
||
// 逐个比较对应位置的数字
|
||
for (int i = 0; i < array1.Length; i++)
|
||
{
|
||
if (array1[i] != array2[i])
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
static bool AreMoreThanHalfEqual(string[] array1, string[] array2)
|
||
{
|
||
string Sqltext = array1.Join("");
|
||
string Realtext = array2.Join("");
|
||
int io = 0;
|
||
|
||
foreach (char ch2 in Realtext)
|
||
{
|
||
foreach (char ch1 in Sqltext)
|
||
{
|
||
if (ch1 == ch2)
|
||
{
|
||
io++;
|
||
Console.WriteLine(ch1);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
//// 将数组转换为集合
|
||
//HashSet<string> set1 = new HashSet<string>(array1);
|
||
//HashSet<string> set2 = new HashSet<string>(array2);
|
||
|
||
//// 计算交集的数量
|
||
//int intersectionCount = set1.Intersect(set2).Count();
|
||
|
||
// 判断交集数量是否超过一半
|
||
return io >=Sqltext.Length / 2;
|
||
}
|
||
public static bool StrMatch2(string SqlText, string DetText)
|
||
{
|
||
|
||
string[] numbers = FindNumbers(SqlText);
|
||
|
||
if (numbers.Length>0)
|
||
{
|
||
//Console.WriteLine("字符串中包含数字:" + number);
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("字符串中不包含数字");
|
||
}
|
||
|
||
string[] numbers2= FindNumbers(DetText);
|
||
|
||
|
||
if (numbers2.Length>0)
|
||
{
|
||
//Console.WriteLine("字符串中包含数字:" + detnumber);
|
||
}
|
||
else
|
||
{
|
||
Console.WriteLine("字符串中不包含数字");
|
||
}
|
||
bool areEqual ;
|
||
if (numbers2.Length>0&& numbers.Length > 0)
|
||
{
|
||
areEqual = AreMoreThanHalfEqual(numbers, numbers2);
|
||
}
|
||
else
|
||
{
|
||
areEqual = AreArraysEqual(numbers, numbers2);
|
||
}
|
||
|
||
|
||
//string[] sArraysql = Regex.Split(SqlText, "##", RegexOptions.IgnoreCase);
|
||
//string[] sArraydet = Regex.Split(DetText, "##", RegexOptions.IgnoreCase);
|
||
//for(int i=0;i< sArraydet.Length;i++)
|
||
//{
|
||
// string s = sArraydet[i];
|
||
// for (int j = 0; j < sArraysql.Length; j++)
|
||
// {
|
||
|
||
// }
|
||
//}
|
||
//bool areEqual = sArraysql.OrderBy(x => x).SequenceEqual(sArraydet.OrderBy(x => x));
|
||
return areEqual;
|
||
|
||
}
|
||
static string[] FindNumbers(string str)
|
||
{
|
||
Regex regex = new Regex(@"\d+");
|
||
MatchCollection matches = regex.Matches(str);
|
||
string[] numbers = new string[matches.Count];
|
||
for (int i = 0; i < matches.Count; i++)
|
||
{
|
||
numbers[i] = matches[i].Value;
|
||
}
|
||
return numbers;
|
||
}
|
||
|
||
static string GetNumber(string str)
|
||
{
|
||
Regex regex = new Regex(@"\d+");
|
||
Match match = regex.Match(str);
|
||
if (match.Success)
|
||
{
|
||
return match.Value;
|
||
}
|
||
else
|
||
{
|
||
return null;
|
||
}
|
||
}
|
||
public static bool isMatchStr(string SqlText, string DetText)
|
||
{
|
||
|
||
|
||
if ((SqlText == "" || SqlText == null) && (DetText == "" || DetText == null))
|
||
{
|
||
return true;
|
||
}
|
||
else
|
||
{
|
||
if (SqlText != null && DetText != null)
|
||
{
|
||
if (SqlText.Contains("##") && DetText.Contains("##"))
|
||
{
|
||
return StrMatch2(SqlText, DetText);
|
||
}
|
||
else if(SqlText != "" && DetText != "")
|
||
{
|
||
return StrMatch2(SqlText, DetText);
|
||
}
|
||
else
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
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];
|
||
}
|
||
|
||
|
||
}
|
||
}
|