123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using AI.Common.Log;
- using AI.Common;
- using RUSInferNet.Vision;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using RUSInferNet;
- namespace HumanOrganSegDemo.HumanBodyPartAnalyser.OrganSegment
- {
- public class HumanSurfaceHeartSegment : IHumanSurfaceOrganSegment
- {
- #region field
- private IInferenceNetwork _inferNet;
- private static InferenceConfig _inferenceCore = new InferenceConfig();
- private RawImage _rawImg;
- #endregion
- #region public funcs
- /// <summary>
- /// 通知订阅者,推理过程中发生了错误
- /// </summary>
- public event EventHandler<ErrorEventArgs> NotifyError;
- /// <summary>
- /// 通知订阅者,有log要记
- /// </summary>
- public event EventHandler<LogEventArgs> NotifyLogWrite;
- /// <summary>
- /// 实现单幅图心脏轮廓的分割
- /// </summary>
- /// <param name="rawImg">rawImg格式图像</param>
- /// <returns></returns>
- public IDetectedObject[] EvaluateOneImage(RawImage image, Rect roi)
- {
- try
- {
- _rawImg = image;
- //Rect cropRect = new Rect(0, 0, _rawImg.Width, _rawImg.Height);
- IContour[] imgROIs = new IContour[1];
- imgROIs[0] = roi.ToContour();
- //对一张图进行推理
- InferenceNetworkInputImage inferInput = new InferenceNetworkInputImage(_rawImg, imgROIs);
- IDetectedObject[] inferResult = _inferNet.Process(inferInput);
- var count = inferResult.Length;
- for (int ni = 0; ni < count; ni++)
- {
- inferResult[ni] = (DetectedObject)inferResult[ni];
- }
- return inferResult;
- }
- catch (Exception excep)
- {
- NotifyError?.Invoke(this, new ErrorEventArgs(excep));
- return new DetectedObject[] { };
- }
- }
- /// <summary>
- /// 销毁
- /// </summary>
- public void Dispose()
- {
- DoDispose();
- GC.SuppressFinalize(this);
- }
- /// <summary>
- /// 析构函数
- /// </summary>
- ~HumanSurfaceHeartSegment()
- {
- DoDispose();
- }
- #endregion
- #region constructor
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="numCpu"></param>
- /// <param name="netDir"></param>
- /// <param name="workName"></param>
- /// <exception cref="Exception"></exception>
- public HumanSurfaceHeartSegment(int numCpu, string netDir)
- {
- try
- {
- _inferNet = new InferNetOnnxHumanSurfaceHeartSeg();
- if (!_inferNet.NetworkLoaded)
- {
- //string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks");
- // 设置inferCore的参数
- Dictionary<EnumInferenceConfigKey, string> config = new Dictionary<EnumInferenceConfigKey, string>{
- { EnumInferenceConfigKey.CPU_THREADS_NUM, numCpu.ToString()}};
- _inferenceCore.SetConfig(config, EnumDeviceType.CPU);
- _inferNet.LoadNetwork(_inferenceCore, EnumDeviceType.CPU, netDir);
- }
- }
- catch (Exception excep)
- {
- throw new Exception("Failed at Loading network:" + excep);
- }
- }
- #endregion
- #region private funcs
- /// <summary>
- /// 主动销毁
- /// </summary>
- private void DoDispose()
- {
- _inferNet?.Dispose();
- _inferNet = null;
- }
- #endregion
- }
- }
|