Commit 6856c7d7 刘韬

修正算法错误时没有返回false

1 个父辈 894ec6ca
......@@ -42,9 +42,14 @@
<Reference Include="log4net">
<HintPath>..\dll\log4net.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\AutoCountMachine-Single\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Numerics" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
......@@ -57,6 +62,7 @@
<Compile Include="haobo_v1.cs" />
<Compile Include="HaoboSDK\HBI_FPD_DLL_v2.cs" />
<Compile Include="HaoboSDK\HBI_FPD_DLL_v1.cs" />
<Compile Include="NeoX.cs" />
<Compile Include="ONNXAlgoMatch.cs" />
<Compile Include="eyemLib.cs" />
<Compile Include="haobo_v2.cs" />
......@@ -81,6 +87,7 @@
<Content Include="DLL\tbb.dll" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
<None Include="bin\Debug\CRCallback.lib" />
<None Include="DLL\resnet18.bin" />
<None Include="DLL\resnet18.param" />
......
using Newtonsoft.Json;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
public static class NeoX
{
[DllImport("NeoX.dll", CharSet = CharSet.Ansi)]
internal static extern IntPtr ReadX(string filename);
/// <summary>
/// 是否有NeoX算法
/// </summary>
/// <returns></returns>
public static bool HasNeox()
{
if (File.Exists("NeoX.dll"))
return true;
if (File.Exists(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "NeoX.dll")))
return true;
return false;
}
internal static string[] ConverterType(string[] types, bool isToNeoX)
{
for (int i = 0; i < types.Length; i++)
types[i] = NeoX.ConverterType(types[i], isToNeoX);
return types;
}
/// <summary>
/// 转换新老算法的名称
/// </summary>
/// <param name="type"></param>
/// <param name="isToNeoX"></param>
/// <returns></returns>
public static string ConverterType(string type, bool isToNeoX)
{
if (HasNeox() && isToNeoX)
{
if (type.ToLower() == "auto")
type = "ID_TYPE_X";
if (type == "IP_LONG_PARTS")
type = "ID_TYPE_A";
if (type == "IP_SQUARE_PARTS")
type = "ID_TYPE_D";
if (type == "IP_GEN_PARTS")
type = "ID_TYPE_C";
if (type == "IP_DYNAMIC_PARTS")
type = "ID_TYPE_D";
}
else
{
if (type == "ID_TYPE_B" || type == "ID_TYPE_A")
type = "IP_LONG_PARTS";
if (type == "ID_TYPE_C")
type = "IP_GEN_PARTS";
if (type == "ID_TYPE_D")
type = "IP_SQUARE_PARTS";
if (type == "IP_GEN_PARTS" || type.StartsWith("ID"))
type = "IP_DEFAULT_PARTS";
}
return type;
}
[HandleProcessCorruptedStateExceptions]
internal static bool AlgoC(string filename, int shrinkOffset, int Corner, string type, out int count, out Bitmap bitmap)
{
var d = new NeoXData();
d.imgPath = filename;
d.outPath = Corner + "-Mark.png";
d.shrinkOffset = shrinkOffset;
d.Corner = Corner;
d.Algo = type;
var s = JsonConvert.SerializeObject(d);
count = 0;
bitmap = null;
try
{
if (File.Exists(d.outPath))
File.Delete(d.outPath);
var i = ReadX(s);
var ss = Marshal.PtrToStringAnsi(i);
ss = ss.Replace("\\", "\\\\");
var r = JsonConvert.DeserializeObject<NeoXResult>(ss);
count = r.result;
if (File.Exists(d.outPath))
{
var data = File.ReadAllBytes(d.outPath);
var ms = new MemoryStream(data);
bitmap = (Bitmap)Bitmap.FromStream(ms);
return true;
}
else
{
Console.WriteLine(ss);
Debug.WriteLine(ss);
return false;
}
}
catch (Exception ex)
{
return false;
}
}
internal static string SplitBmp(string filename, int corner)
{
Image image = Image.FromFile(filename);
// 原始图像的宽度和高度
int width = image.Width;
int height = image.Height;
// 创建一个Bitmap对象,用于存储图像像素
Bitmap bitmap = new Bitmap(image);
// 切分图像为4宫格
int cellWidth = width / 2;
int cellHeight = height / 2;
// 根据角落确定切分区域
Rectangle sourceRect;
switch (corner)
{
case 1 - 1:// Corner.TopLeft:
sourceRect = new Rectangle(0, 0, cellWidth, cellHeight);
break;
case 4 - 1:// Corner.TopRight:
sourceRect = new Rectangle(cellWidth, 0, cellWidth, cellHeight);
break;
case 2 - 1:// Corner.BottomLeft:
sourceRect = new Rectangle(0, cellHeight, cellWidth, cellHeight);
break;
case 3 - 1:// Corner.BottomRight:
sourceRect = new Rectangle(cellWidth, cellHeight, cellWidth, cellHeight);
break;
default:
throw new ArgumentException("Invalid corner specified.");
}
// 创建指定角落格子的图像副本
Bitmap cornerCell = bitmap.Clone(sourceRect, bitmap.PixelFormat);
cornerCell.Save("temp_" + corner + ".png", ImageFormat.Png);
cornerCell.Dispose();
bitmap.Dispose();
return "temp_" + corner + ".png";
}
internal static void MargBmp(Bitmap src, Bitmap sp, int corner)
{
//外部type是从0开始, corner从1开始
corner = corner + 1;
// 原始图像的宽度和高度
int width = src.Width;
int height = src.Height;
// 切分图像为4宫格
int cellWidth = width / 2;
int cellHeight = height / 2;
// 根据角落确定切分区域
Rectangle sourceRect;
switch (corner)
{
case 1:// Corner.TopLeft:
sourceRect = new Rectangle(0, 0, cellWidth, cellHeight);
break;
case 4:// Corner.TopRight:
sourceRect = new Rectangle(cellWidth, 0, cellWidth, cellHeight);
break;
case 2:// Corner.BottomLeft:
sourceRect = new Rectangle(0, cellHeight, cellWidth, cellHeight);
break;
case 3:// Corner.BottomRight:
sourceRect = new Rectangle(cellWidth, cellHeight, cellWidth, cellHeight);
break;
default:
throw new ArgumentException("Invalid corner specified.");
}
using (Graphics g = Graphics.FromImage(src))
{
g.DrawImage(sp, sourceRect);
Pen pen = new Pen(Color.Red, 5);
g.DrawRectangle(pen, sourceRect);
}
}
}
internal class NeoXData
{
//输入图像路径
public string imgPath;
//输出图像路径
public string outPath;
//算法名称
public string Algo;
//剪裁像素
public int shrinkOffset;
//剪裁角落
public int Corner = 0;
}
internal class NeoXResult
{
public int result;
public string error;
}
......@@ -6,6 +6,7 @@ using System.IO;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading;
using System.Threading.Tasks;
using static Asa.eyemLib;
......@@ -250,6 +251,15 @@ namespace Asa
[HandleProcessCorruptedStateExceptions]
public static int GetLocalCountIrregular(string path, int ShrinkOffset, string type, out int[] count, out Bitmap BmpDstImg)
{
type = NeoX.ConverterType(type,true);
if (NeoX.HasNeox() && type.StartsWith("ID"))
{
var result = NeoX.AlgoC(path, ShrinkOffset,0, type, out int c, out Bitmap bitmap);
count = new int[4] { c, 0, 0, 0 };
BmpDstImg = bitmap;
return result?0:-1;
}
type = NeoX.ConverterType(type, false);
count = null;
BmpDstImg = null;
string fileName = Path.GetFileNameWithoutExtension(path);
......@@ -287,20 +297,94 @@ namespace Asa
}
/// <summary>
/// 4盘算法点料
/// 1 4
/// 5
/// 2 3
/// </summary>
[HandleProcessCorruptedStateExceptions]
public static int GetCountObjectIrregularPartsMultiopt(string path, int ShrinkOffset, string[] type, out int[] count, out Bitmap BmpDstImg)
{
count = null;
BmpDstImg = null;
//string fileName = Path.GetFileNameWithoutExtension(path);
List<Task> tasks = new List<Task>();
Dictionary<int, (int, Bitmap)> ccresult = new Dictionary<int, (int, Bitmap)>();
int flag = eyemImageRead(path, -1, out EyemImage eyem);
if (flag != 0) return -1;
type = NeoX.ConverterType(type,true);
int n = GetCountObjectIrregularPartsMultiopt(eyem, ShrinkOffset, type, out count, out BmpDstImg);
if (NeoX.HasNeox())
{
Dictionary<int,string> ccdata = new Dictionary<int, string>();
for (int i = 0; i < type.Length; i++)
{
if (type[i].StartsWith("ID"))
{
ccdata.Add(i, type[i]);
}
}
foreach (var ck in ccdata.Keys.ToArray())
{
var t = Task.Run(() => {
int Corner = ck;
try
{
NeoX.AlgoC(path, ShrinkOffset, Corner + 1, ccdata[ck], out int c, out Bitmap bitmap);
lock (ccresult)
{
ccresult.Add(Corner, (c, bitmap));
}
}
catch {
lock (ccresult)
{
ccresult.Add(Corner, (-1, null));
}
}
});
tasks.Add(t);
}
eyemImageFree(ref eyem);
}
int n = 0;
if (type.Count((s) => { return s.StartsWith("IP"); }) > 0)
{
type = NeoX.ConverterType(type, false);
int flag = eyemImageRead(path, -1, out EyemImage eyem);
if (flag != 0) return -1;
try
{
n = GetCountObjectIrregularPartsMultiopt(eyem, ShrinkOffset, type, out count, out BmpDstImg);
}
catch {
BmpDstImg = DeepClone(new Bitmap(path));
count = new int[4];
}
eyemImageFree(ref eyem);
}
else
{
BmpDstImg = DeepClone(new Bitmap(path));
count = new int[4];
}
if (tasks.Count > 0)
{
Task.WaitAll(tasks.ToArray(),30*1000);
lock (ccresult)
{
foreach (var Corner in ccresult.Keys.ToArray())
{
var d = ccresult[Corner];
if (d.Item1 >= 0 && d.Item2!=null)
{
NeoX.MargBmp(BmpDstImg, d.Item2, Corner);
d.Item2.Dispose();
}
count[Corner] = d.Item1;
}
}
}
return n;
}
/// <summary>
......@@ -321,7 +405,8 @@ namespace Asa
setSkipProcessID(-1);
int[] ipReelNum = new int[4];
int n = eyemCountObjectIrregularPartsMultiopt(eyem, tpRoi, GetAlgoIndex(type), ipReelNum, out EyemImage tpDstImg);
var typeindex = GetAlgoIndex(type);
int n = eyemCountObjectIrregularPartsMultiopt(eyem, tpRoi, typeindex, ipReelNum, out EyemImage tpDstImg);
count = ipReelNum;
if (n == 0)
BmpDstImg = eyemCvtToBitmap(tpDstImg);
......@@ -330,7 +415,19 @@ namespace Asa
}
public static T DeepClone<T>(T _object)
{
T dstobject;
using (MemoryStream mStream = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(mStream, _object);
mStream.Seek(0, SeekOrigin.Begin);//指定当前流的位置为流的开头。
dstobject = (T)bf.Deserialize(mStream);
mStream.Close();
}
return dstobject;
}
[HandleProcessCorruptedStateExceptions]
public static int GetLocalCountTemplate(string path, int ShrinkOffset, string template, out int[] count, out Bitmap BmpDstImg)
......@@ -585,6 +682,7 @@ namespace Asa
CARREY,
HAOBO_V2
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
\ No newline at end of file
......@@ -152,6 +152,7 @@ namespace Asa
#endregion
// 日志回调
private delegate void TCallBack(string msg);
private static TCallBack sld = new TCallBack(TLogCallback);
......
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<log4net>
<appender name="CarerayImage" type="log4net.Appender.RollingFileAppender">
<file value="logs/CarerayImage.log"/>
<param name="Encoding" value="UTF-8"/>
<appendToFile value="true"/>
<rollingStyle value="Date"/>
<datePattern value="yyyy-MM-dd"/>
<file value="logs/CarerayImage.log" />
<param name="Encoding" value="UTF-8" />
<appendToFile value="true" />
<rollingStyle value="Date" />
<datePattern value="yyyy-MM-dd" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="[%date][%t]%-5p %m%n"/>
<conversionPattern value="[%date][%t]%-5p %m%n" />
</layout>
</appender>
<root>
<level value="Debug"/>
<appender-ref ref="CarerayImage"/>
<level value="Debug" />
<appender-ref ref="CarerayImage" />
</root>
</log4net>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!