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 /// /// 通知订阅者,推理过程中发生了错误 /// public event EventHandler NotifyError; /// /// 通知订阅者,有log要记 /// public event EventHandler NotifyLogWrite; /// /// 实现单幅图心脏轮廓的分割 /// /// rawImg格式图像 /// 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[] { }; } } /// /// 销毁 /// public void Dispose() { DoDispose(); GC.SuppressFinalize(this); } /// /// 析构函数 /// ~HumanSurfaceHeartSegment() { DoDispose(); } #endregion #region constructor /// /// 构造函数 /// /// /// /// /// public HumanSurfaceHeartSegment(int numCpu, string netDir) { try { _inferNet = new InferNetOnnxHumanSurfaceHeartSeg(); if (!_inferNet.NetworkLoaded) { //string netDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Networks"); // 设置inferCore的参数 Dictionary config = new Dictionary{ { 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 /// /// 主动销毁 /// private void DoDispose() { _inferNet?.Dispose(); _inferNet = null; } #endregion } }