Commit ae41cb88 张士柳

1 个父辈 17d157dc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace eyemLib_Sharp
{
public class YoloWrapper : IDisposable
{
private const int MaxObjects = 1000;
[DllImport("yolo_cpp_dll.dll", EntryPoint = "init")]
private static extern int InitializeYolo(string configurationFilename, string weightsFilename, int gpu);
[DllImport("yolo_cpp_dll.dll", EntryPoint = "detect_image")]
private static extern int DetectImage(string filename, ref BboxContainer container);
[DllImport("yolo_cpp_dll.dll", EntryPoint = "detect_mat")]
private static extern int DetectImage(IntPtr pArray, int nSize, ref BboxContainer container);
[DllImport("yolo_cpp_dll.dll", EntryPoint = "dispose")]
private static extern int DisposeYolo();
[StructLayout(LayoutKind.Sequential)]
public struct bbox_t
{
public UInt32 x, y, w, h; // (x,y) - top-left corner, (w, h) - width & height of bounded box
public float prob; // confidence - probability that the object was found correctly
public UInt32 obj_id; // class of object - from range [0, classes-1]
public UInt32 track_id; // tracking id for video (0 - untracked, 1 - inf - tracked object)
public UInt32 frames_counter;
public float x_3d, y_3d, z_3d; // 3-D coordinates, if there is used 3D-stereo camera
};
[StructLayout(LayoutKind.Sequential)]
public struct BboxContainer
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = MaxObjects)]
public bbox_t[] candidates;
}
public YoloWrapper(string configurationFilename, string weightsFilename, int gpu)
{
InitializeYolo(configurationFilename, weightsFilename, gpu);
}
public void Dispose()
{
DisposeYolo();
}
public bbox_t[] Detect(string filename)
{
var container = new BboxContainer();
var count = DetectImage(filename, ref container);
return container.candidates;
}
public bbox_t[] Detect(byte[] imageData)
{
var container = new BboxContainer();
var size = Marshal.SizeOf(imageData[0]) * imageData.Length;
var pnt = Marshal.AllocHGlobal(size);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(imageData, 0, pnt, imageData.Length);
var count = DetectImage(pnt, imageData.Length, ref container);
if (count == -1)
{
throw new NotSupportedException($"{"yolo_cpp_dll.dll"} has no OpenCV support");
}
}
catch (Exception)
{
return null;
}
finally
{
// Free the unmanaged memory.
Marshal.FreeHGlobal(pnt);
}
return container.candidates;
}
}
}
...@@ -1699,79 +1699,6 @@ namespace eyemLib_Sharp ...@@ -1699,79 +1699,6 @@ namespace eyemLib_Sharp
eyemImageFree(ref image); eyemImageFree(ref image);
} }
public static void eyemReadImageToolE(string fileName, IntPtr hModelID)
{
Stopwatch sw = new Stopwatch();
sw.Restart();
EyemImage image;
EyemImage tpDstImg = new EyemImage();
int flag = eyemImageRead(fileName, -1, out image);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
EyemRect tpRoi = new EyemRect();
tpRoi.iXs = 50; tpRoi.iYs = 50;
tpRoi.iWidth = image.iWidth - 100;
tpRoi.iHeight = image.iHeight - 100;
//
int[] pNumObj = new int[4];
string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
//获取用于制作模板的图像
//flag = eyemAchvTemplateImage(image, tpRoi, out tpDstImg);
//测试插入模板
//eyemInsertModel(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
////测试获取模板
//EyemModelID tpModelID0 = new EyemModelID();
//eyemAchvModelByName("D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl", hModelID, ref tpModelID0);
string selectModel = "";
flag = eyemMatchTemplateModel(tpDstImg, hModelID, ref selectModel);
//根据名称获取模板
EyemModelID tpModelID = new EyemModelID();
eyemAchvModelByName(selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, ref tpModelID);
EyemImage tpModeImg = new EyemImage();
tpModeImg.iChannels = 1; tpModeImg.iDepth = 0;
tpModeImg.iWidth = tpModelID.iWidth; tpModeImg.iHeight = tpModelID.iHeight; tpModeImg.vpImage = tpModelID.vpImage;
string str = Marshal.PtrToStringAnsi(tpModelID.lpszName);
Bitmap bitmap = eyemCvtToBitmap(tpModeImg);
bitmap.Dispose();
//if (bitmap != null)
//{
// bitmap.Save(System.Windows.Forms.Application.StartupPath + "\\ResOut\\" + file);
//}
//最好释放掉,如果对象供其他接口使用要先释放
eyemImageFree(ref tpDstImg);
//点料
eyemCountObjectIrregularPartsE(image, tpRoi, file.Replace(".png", ""), selectModel.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)[0], hModelID, pNumObj, out tpDstImg);
//测试移除模板
//eyemRemoveModelByName(hModelID, "D:\\模板文件及图像\\df871193-6632-48f9-abfe-540c3fc49c3f.tpl");
//free image
eyemImageFree(ref tpDstImg);
eyemImageFree(ref image);
sw.Stop();
Console.WriteLine("耗时:" + sw.ElapsedMilliseconds.ToString() + ",结果:" + pNumObj + ",所用模板:" + str);
}
public static void eyemReadProcssedImage(string fileName) public static void eyemReadProcssedImage(string fileName)
{ {
EyemImage image; EyemImage image;
...@@ -1801,7 +1728,6 @@ namespace eyemLib_Sharp ...@@ -1801,7 +1728,6 @@ namespace eyemLib_Sharp
{ {
bitmap.Save("D:\\ResOut\\" + file); bitmap.Save("D:\\ResOut\\" + file);
} }
//建议释放掉 //建议释放掉
bitmap.Dispose(); bitmap.Dispose();
} }
...@@ -1846,79 +1772,11 @@ namespace eyemLib_Sharp ...@@ -1846,79 +1772,11 @@ namespace eyemLib_Sharp
int[] iArrRes = new int[tpRois.Count]; int[] iArrRes = new int[tpRois.Count];
//int iRet = eyemAOIForTSAV(tpImages[0], tpImages[i], hGlobal, tpRois.Count); //int iRet = eyemAOIForTSAV(tpImages[0], tpImages[i], hGlobal, tpRois.Count);
int iRet = eyemTrackFeature(tpRefImg, tpNextImg, hGlobal, tpRois.Count, Marshal.UnsafeAddrOfPinnedArrayElement(iArrRes, 0), out tpDstImg); int iRet = eyemTrackFeature(tpRefImg, tpNextImg, hGlobal, tpRois.Count, Marshal.UnsafeAddrOfPinnedArrayElement(iArrRes, 0), out tpDstImg);
//过滤出有效信号(连续存在数帧以上才算,防止误触发),或者直接用iArrRes的值作为信号
//toSingleFilter(iArrRes, ref bitSingle);
//for (int j = 0; j < bitSingle.Length; j++)
//{
// //设定信号持续帧数阈值
// if (bitSingle[j] > 15)
// {
// //检测到信号
// Console.WriteLine("检测到:" + j + "处有信号");
// }
//}
eyemImageFree(ref tpDstImg); eyemImageFree(ref tpDstImg);
//释放资源 //释放资源
Marshal.FreeHGlobal(hGlobal); Marshal.FreeHGlobal(hGlobal);
} }
public static void toSingleFilter(int[] iArrRes, ref int[] bitSingle)
{
for (int i = 0; i < iArrRes.Length; i++)
{
bitSingle[i] += iArrRes[i];
if (bitSingle[i] >= 1)//信号起始
{
//如果有一帧没检测到就重新累计
if (iArrRes[i] == 0)
{
bitSingle[i] = 0;
}
}
}
}
public static void eyemTest()
{
Stopwatch sw = new Stopwatch();
sw.Restart();
EyemImage image;
EyemImage tpDstImg = new EyemImage();
int flag = eyemImageRead(".\\1.png", -1, out image);
if (flag != 0)
{
Console.WriteLine("读图失败!");
return;
}
flag = eyemInitNNDataCodeModel(".\\darknet\\detect-tiny.cfg", ".\\darknet\\detect-tiny.weights", "", "");
EyemRect tpRoi = new EyemRect();
tpRoi.iXs = 0; tpRoi.iYs = 0;
tpRoi.iWidth = image.iWidth;
tpRoi.iHeight = image.iHeight;
//<解码测试
int ipNum; EyemBarCode* tpResults;
DataCodeHandle hObject;
flag = eyemDetectAndDecodeUseNN(image, tpRoi, out hObject, out tpResults, out ipNum, out tpDstImg);
hObject.Dispose();
sw.Stop();
Bitmap bitmap = eyemCvtToBitmap(tpDstImg);
if (bitmap != null)
{
bitmap.Save(".\\" + "predictions.png");
}
Console.WriteLine("时间花费:" + sw.ElapsedMilliseconds.ToString());
//free image
eyemImageFree(ref tpDstImg);
eyemImageFree(ref image);
}
#region 文件重新命名 #region 文件重新命名
public static void eyemRenameFile(string filePath) public static void eyemRenameFile(string filePath)
{ {
......
...@@ -15,84 +15,17 @@ namespace eyemLib_Sharp ...@@ -15,84 +15,17 @@ namespace eyemLib_Sharp
{ {
di.Create(); di.Create();
} }
//EyemLib.eyemRenameFile(@"D:\新建文件夹");
//初始化 //初始化
EyemLib.Init(); EyemLib.Init();
string[] fileNames = Directory.GetFiles(@"D:\批量测试图像", "*.*", SearchOption.AllDirectories); string[] fileNames = Directory.GetFiles(@"D:\批量测试图像", "*.*", SearchOption.AllDirectories);
//EyemLib.eyemTestVideoCapture(@"D:\批量测试图像");
//int iter = 0;
//for (int i = 0; i < 10000; i++)
//{
// ParallelOptions po = new ParallelOptions();
// po.MaxDegreeOfParallelism = 2;
// Parallel.ForEach(fileNames, po, fn =>
// {
// EyemLib.eyemReadImageTool(fn);
// });
// iter++;
// if (iter > 50)
// {
// iter = 0;
// Console.Clear();
// }
// Thread.Sleep(10);
//}
//for (int i = 0; i < 5000; i++)
//{
foreach (var item in fileNames) foreach (var item in fileNames)
{ {
EyemLib.eyemReadImageTool(item); EyemLib.eyemReadImageTool(item);
} }
// iter++;
// if (iter > 200)
// {
// iter = 0;
// Console.Clear();
// }
//}
//for (int i = 0; i < 1; i++)
//{
// EyemLib.eyemTestVideoCapture("D:\\插件完成检测\\视频\\ios3.mov");
//}
//IntPtr hModelID;
//EyemLib.eyemInitModelE(out hModelID);
//int sum = 0;
//for (int i = 0; i < 5000; i++)
//{
// //for (int j = 0; j < fileNames.Length; j++)
// //{
// // EyemLib.eyemReadImageToolE(fileNames[j], hModelID);
// //}
// sum++;
// //并行测试
// ParallelOptions po = new ParallelOptions();
// po.MaxDegreeOfParallelism = 3;
// Parallel.ForEach(fileNames, po, fn =>
// {
// EyemLib.eyemReadImageToolE(fn, hModelID);
// });
// if (sum > 50)
// {
// sum = 0;
// Console.Clear();
// }
//}
//EyemLib.eyemReleaseModelE(ref hModelID);
EyemLib.Free(); EyemLib.Free();
Console.Write("请按任意键继续。。。"); Console.Write("请按任意键继续。。。");
Console.ReadKey(); Console.ReadKey();
} }
......
...@@ -48,7 +48,6 @@ ...@@ -48,7 +48,6 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Darknet.cs" />
<Compile Include="EyemLib.cs" /> <Compile Include="EyemLib.cs" />
<Compile Include="EyemLibDemo.cs" /> <Compile Include="EyemLibDemo.cs" />
<Compile Include="log4cpp\ILog4CPP.cs" /> <Compile Include="log4cpp\ILog4CPP.cs" />
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!