Commit 11b2d5ab 刘韬

1

1 个父辈 cbdaf239
...@@ -21,8 +21,7 @@ ...@@ -21,8 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DocumentationFile> <DocumentationFile>bin\Debug\Asa.CarerayImage.xml</DocumentationFile>
</DocumentationFile>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
...@@ -51,6 +50,7 @@ ...@@ -51,6 +50,7 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="ONNXAlgoMatch.cs" />
<Compile Include="eyemLib.cs" /> <Compile Include="eyemLib.cs" />
<Compile Include="haobo.cs" /> <Compile Include="haobo.cs" />
<Compile Include="HaoboSDK\HBI_FPD_DLL.cs" /> <Compile Include="HaoboSDK\HBI_FPD_DLL.cs" />
......
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using static Asa.eyemLib;
namespace Asa
{
/// <summary>
/// ONNX算法匹配
/// </summary>
public class ONNXAlgoMatch
{
static Dictionary<Bitmap, float[]> dictFeatures = new Dictionary<Bitmap, float[]>();
static ONNXAlgoMatch() {
string onnxfile = "./darknet/ec_model.onnx";
int flag = eyemInitONNXModel(onnxfile);
if (flag != 0)
{
throw new Exception($"加载ONNX算法模型文件失败!-{onnxfile}");
}
string featurePath = "./darknet/feature.te";
if (!File.Exists(featurePath))
throw new Exception($"加载ONNX算法匹配文件失败!-{featurePath}");
BinaryFormatter bf = new BinaryFormatter();
using (Stream stream = new FileStream(featurePath, FileMode.Open, FileAccess.Read))
{
dictFeatures = (Dictionary<Bitmap, float[]>)bf.Deserialize(stream);
}
}
/// <summary>
/// 匹配算法PID
/// </summary>
/// <param name="fileName"></param>
/// <param name="ShrinkOffset"></param>
/// <param name="matchDeg">匹配百分比</param>
/// <returns></returns>
public static string MatchPID(string fileName, int ShrinkOffset, out float matchDeg) {
int flag = eyemImageRead(fileName, -1, out EyemImage image);
if (flag != 0 || image.iDepth != 2 || image.iChannels != 3)
{
throw new Exception("文件类型错误!");
}
var tpRoi = new EyemRect();
tpRoi.iXs = 0 + ShrinkOffset;
tpRoi.iYs = 0 + ShrinkOffset;
tpRoi.iWidth = image.iWidth - ShrinkOffset * 2;
tpRoi.iHeight = image.iHeight - ShrinkOffset * 2;
matchDeg = 0;
flag = eyemAchvMatchMat(image, tpRoi, out EyemImage extractMat);
if (extractMat.ucpImage == IntPtr.Zero)
{
throw new Exception("获取源图特征部分失败!");
}
//提取特征
float[] fFeature = new float[512];
eyemExtractWithONNX(extractMat, fFeature);
eyemImageFree(ref extractMat);
eyemImageFree(ref image);
//匹配最大值
Dictionary<int, float> dictMatchDeg = new Dictionary<int, float>();
foreach (var item in dictFeatures)
{
float simDeg = calcSimilarity(item.Value.Take(512).ToArray(), fFeature);
var k = (int)item.Value[512];
if (!dictMatchDeg.ContainsKey(k))
dictMatchDeg.Add(k, simDeg);
}
var predict = dictMatchDeg.OrderBy(p => p.Value);
var last = predict.Last();
string label = "PID" + last.Key.ToString().PadLeft(3, '0');
matchDeg = last.Value;
return label;
}
}
}
...@@ -407,6 +407,16 @@ namespace Asa ...@@ -407,6 +407,16 @@ namespace Asa
device.SaveRAW(filePath); device.SaveRAW(filePath);
} }
/// <summary> /// <summary>
/// 设置处理级别
/// </summary>
/// <param name="pl">
/// pl>2.0就用你设置的值,pl<=2.0就等于默认值2.0,,一般来说离线点料机pl=2.0,在线点料机pl=4.0
/// </param>
/// <returns></returns>
public static int setProcessLevel(double pl) {
return eyemLib.setProcessLevel(pl);
}
/// <summary>
/// 校准平板 /// 校准平板
/// </summary> /// </summary>
/// <param name="mode">1:暗场校准,2亮场校准</param> /// <param name="mode">1:暗场校准,2亮场校准</param>
......
...@@ -48,14 +48,15 @@ namespace Asa ...@@ -48,14 +48,15 @@ namespace Asa
//[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] //[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
//internal static extern int eyemCountObjectIrregularParts(EyemImage tpSrcImg, EyemRect tpRoi, string fileName, double dOffset, string strType, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg); //internal static extern int eyemCountObjectIrregularParts(EyemImage tpSrcImg, EyemRect tpRoi, string fileName, double dOffset, string strType, int iMaxArea, int iWinSize, ref string pNumObj, out EyemImage tpDstImg);
//[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] //[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
//internal static extern int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, string fileName, ref string pNumObj, out EyemImage tpDstImg); //internal static extern int eyemCountObjectE(EyemImage tpImage, EyemRect tpRoi, string fileName, ref string pNumObj, out EyemImage tpDstImg);
//[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] //[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
//internal static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, string fileName, string ccTplName, IntPtr hModelIDs, ref string pNumObj, out EyemImage tpDstImg); //internal static extern int eyemCountObjectIrregularPartsE(EyemImage tpImage, EyemRect tpRoi, string fileName, string ccTplName, IntPtr hModelIDs, ref string pNumObj, out EyemImage tpDstImg);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
public static extern void loadImage2Mem(string key, EyemImage tpImage);
...@@ -93,6 +94,47 @@ namespace Asa ...@@ -93,6 +94,47 @@ namespace Asa
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)] [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int eyemAchvMatchMat(EyemImage tpImage, EyemRect tpRoi, out EyemImage tpDstImg); internal static extern int eyemAchvMatchMat(EyemImage tpImage, EyemRect tpRoi, out EyemImage tpDstImg);
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int setProcessLevel(double pl);
#region ONNX识别网络
/// <summary>
/// 初始化ONNX模型(10.0)
/// </summary>
/// <param name="extractorModelPath">模型路径</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int eyemInitONNXModel(string extractorModelPath);
/// <summary>
/// 特征提取器
/// </summary>
/// <param name="tpImage">图像(128X128)</param>
/// <param name="fFeatures">特征</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int eyemExtractWithONNX(EyemImage tpImage, [MarshalAs(UnmanagedType.LPArray)] float[] fFeatures);
/// <summary>
/// 特征匹配
/// </summary>
/// <param name="lhs">向量一</param>
/// <param name="rhs">向量二</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern float calcSimilarity([MarshalAs(UnmanagedType.LPArray)] float[] lhs, [MarshalAs(UnmanagedType.LPArray)] float[] rhs);
/// <summary>
/// 对图像进行采样生成训练样本
/// </summary>
/// <param name="tpImage">图像</param>
/// <param name="iSize">尺寸128(最大224)</param>
/// <param name="ccClassName">类名</param>
/// <param name="ccToPath">保存路径</param>
/// <returns></returns>
[DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
internal static extern int eyemTrainImageSampler(EyemImage tpImage, int iSize, string ccClassName, string ccToPath, out EyemImage tpMatchImg, out EyemImage tpDstImg);
#endregion
// 日志回调 // 日志回调
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!