hisence-yolo-detection/HisenceYoloDetection/ManagerModelHelper.cs
2024-07-29 13:45:55 +08:00

315 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 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.9)
{
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];
}
}
}