using System;
using System.Collections.Generic;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using System.Runtime.InteropServices;
using System.IO;
namespace YOLODetectProcessLib
{
public class LiverLesionDetectImg
{
private IInferenceNetwork _inferNet;
///
/// 通知订阅者,推理过程中发生了错误
///
public event EventHandler NotifyError;
public LiverLesionDetectImg(int numCPU, string netDir)
{
try
{
_inferNet = new InferNetOnnxDetectLiverLesion();
_inferNet.LoadNetwork(numCPU, netDir);
}
catch (Exception excep)
{
throw new Exception("Failed at Loading network:" + excep);
}
}
///
/// 实现单幅图的乳腺病灶检测
///
/// rawImg格式图像
/// 是否已经裁切
///
public List ProcessLiverLesionImg(RawImage rawImg, bool isCropped)
{
try
{
YOLODetectProcessLib.Rect cropRect = new YOLODetectProcessLib.Rect(0, 0, rawImg.Width, rawImg.Height); ;
if (!isCropped)
{
if (!UsImageRegionSegUtils.CropWithCvCore(rawImg, out cropRect))
{
NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Failed at UsImageRegionSegUtils.CropWithCvCore")));
}
}
//对一张图进行推理
InferenceNetworkInputImage inferInput = new InferenceNetworkInputImage(rawImg, cropRect);
var inferResult = _inferNet.Process(new InferenceNetworkInputImage[] { inferInput });
if (inferResult == null)
{
NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Unexpected result size for the breast object detect model of one image.")));
}
if (inferResult.Length != 1)
{
NotifyError?.Invoke(this, new ErrorEventArgs(new Exception("Unexpected result size for the breast object detect model of one image.")));
}
if (inferResult[0].Length == 0)
{
return new List();
}
else
{
List results = new List();
for (int ni = 0; ni < inferResult[0].Length; ni++)
{
results.Add(new DetectedBreastLesionResult(inferResult[0][ni].Label, inferResult[0][ni].Confidence, inferResult[0][ni].BoundingBox));
}
return results;
}
}
catch (Exception excep)
{
NotifyError?.Invoke(this, new ErrorEventArgs(excep));
return new List();
}
}
///
/// 销毁
///
public void Dispose()
{
DoDispose();
GC.SuppressFinalize(this);
}
///
/// 析构函数
///
~LiverLesionDetectImg()
{
DoDispose();
}
private void DoDispose()
{
_inferNet?.Dispose();
_inferNet = null;
}
}
}