URMAnalysisManager.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SixLabors.ImageSharp;
  5. using SixLabors.ImageSharp.Processing;
  6. using Vinno.DataTypes;
  7. using Vinno.Infrastructure;
  8. using WingInterfaceLibrary.Request.RemedicalAISelected;
  9. using WingServerCommon.Log;
  10. namespace WingAIDiagnosisService.URMManage
  11. {
  12. public partial class URMAnalysisManager
  13. {
  14. public URMAnalysisManager()
  15. {
  16. AppendEnvironmentPath();
  17. WatchingVideoProcessor();
  18. }
  19. public URMAnalysisV2Result DoURMAnalysis(List<string> arguments, string outputFolder, string coverFile)
  20. {
  21. try
  22. {
  23. var arg2Params = arguments;
  24. // var familyName = "Linear";
  25. // var typeInfo = "Linear_U5-15LE";
  26. // var nativeId = "Breast";
  27. // var VecLens = 568;
  28. // var Mlas = 128;
  29. // var FramesGot = 2200;
  30. // var CenterFrequencyMhz = 4.7;
  31. // var SupersonicSpeed = 1540;
  32. // var FrequencyMin = 6.5;
  33. // var FrameRateDisplay = 101.9;
  34. // var Depth = 0;
  35. // var X0 = 0;
  36. // var Z0 = 0;
  37. // var ProbeType = "1";
  38. // var ScaleOfPixel_x = 0.214925914347303;
  39. // var ScaleOfPixel_y = 0.61038961038961;
  40. // var section = "URMAnalysisParam:Level4";
  41. // var MOCOYN = "0";
  42. // var MidProc_DeciFactor = 1;
  43. // var VectorStartForMid = 11;
  44. // var isliner = false;
  45. // // 前端调用时传入
  46. // var startX = 47;
  47. // var startY = 14;
  48. // var width = 74;
  49. // var height = 492;
  50. // int? CustomizedStartFrame = null;
  51. // int? CustomizedEndFrame = null;
  52. var UrmDatapath = arg2Params[0];
  53. var familyName = arg2Params[1];
  54. var typeInfo = arg2Params[2];
  55. var nativeId = arg2Params[3];
  56. var VecLens = ConvertToInt(arg2Params[4]);
  57. var Mlas = ConvertToInt(arg2Params[5]);
  58. var FramesGot = ConvertToInt(arg2Params[6]);
  59. var CenterFrequencyMhz = ConvertToDouble(arg2Params[7]);
  60. var SupersonicSpeed = ConvertToInt(arg2Params[8]);
  61. var FrequencyMin = ConvertToDouble(arg2Params[9]);
  62. var FrameRateDisplay = ConvertToDouble(arg2Params[10]);
  63. var Depth = ConvertToInt(arg2Params[11]);
  64. var X0 = ConvertToInt(arg2Params[12]);
  65. var Z0 = ConvertToInt(arg2Params[13]);
  66. var ProbeType = arg2Params[14];
  67. var ScaleOfPixel_x = ConvertToDouble(arg2Params[15]);
  68. var ScaleOfPixel_y = ConvertToDouble(arg2Params[16]);
  69. var section = arg2Params[17];
  70. var MOCOYN = arg2Params[18];
  71. var MidProc_DeciFactor = ConvertToInt(arg2Params[19]);
  72. var VectorStartForMid = ConvertToInt(arg2Params[20]);
  73. var isliner = arg2Params[21]?.ToLower() == "true";
  74. // 前端调用时传入
  75. var startX = ConvertToInt(arg2Params[22]);
  76. var startY = ConvertToInt(arg2Params[23]);
  77. var width = ConvertToInt(arg2Params[24]);
  78. var height = ConvertToInt(arg2Params[25]);
  79. int? CustomizedStartFrame = ConvertToIntCanNull(arg2Params[26]);
  80. int? CustomizedEndFrame = ConvertToIntCanNull(arg2Params[27]);
  81. var roiXPercent = ConvertToDouble(arg2Params[28]);
  82. var roiYPercent = ConvertToDouble(arg2Params[29]);
  83. var roiWPercent = ConvertToDouble(arg2Params[30]);
  84. var roiHPercent = ConvertToDouble(arg2Params[31]);
  85. // 开始执行分析
  86. URMFileIO urmFileIO = new URMFileIO(familyName: familyName, typeInfo: typeInfo, nativeId: nativeId);
  87. urmFileIO.UpdateParamJson(UrmDatapath, VecLens, Mlas, FramesGot, CenterFrequencyMhz, SupersonicSpeed, FrequencyMin, FrameRateDisplay, Depth, X0, Z0, ProbeType, ScaleOfPixel_x, ScaleOfPixel_y, section, MOCOYN, MidProc_DeciFactor, VectorStartForMid, startX, startY, width, height, CustomizedStartFrame, CustomizedEndFrame, out string urmParams);
  88. var modeRecord = new URMAnalysisModeRecord();
  89. if (!Directory.Exists(outputFolder))
  90. {
  91. Directory.CreateDirectory(outputFolder);
  92. }
  93. bool success = URMAnalysisProcess.GetInstance().URMAnalysis(urmParams, modeRecord);
  94. // 读取输出
  95. var SrcWidth = modeRecord.URMSrcWidth;
  96. var SrcHeight = modeRecord.URMSrcHeight;
  97. var denPath = Path.Combine(outputFolder, "srcDenArray.bin");
  98. var dirPath = Path.Combine(outputFolder, "srcDirArray.bin");
  99. var velPath = Path.Combine(outputFolder, "srcVelArray.bin");
  100. var angelPath = Path.Combine(outputFolder, "srcAngleArray.bin");
  101. var maskPath = Path.Combine(outputFolder, "srcMaskArray.bin");
  102. var pointsPath = Path.Combine(outputFolder, "URMPoints.bin");
  103. var pointNumsPath = Path.Combine(outputFolder, "URMPointNums.bin");
  104. ResultDataIOHelper.WriteArrayToFile(denPath, modeRecord.URMDenArray, SrcWidth * SrcHeight);
  105. ResultDataIOHelper.WriteArrayToFile(dirPath, modeRecord.URMDirArray, SrcWidth * SrcHeight);
  106. ResultDataIOHelper.WriteArrayToFile(velPath, modeRecord.URMVelArray, SrcWidth * SrcHeight);
  107. ResultDataIOHelper.WriteArrayToFile(angelPath, modeRecord.URMAngleImgArray, SrcWidth * SrcHeight);
  108. ResultDataIOHelper.WriteByteArrayToFile(maskPath, modeRecord.URMMask, SrcWidth * SrcHeight);
  109. ResultDataIOHelper.WriteURMPointsToFile(pointsPath, modeRecord.URMPoints);
  110. ResultDataIOHelper.WriteURMPointNumsToFile(pointNumsPath, modeRecord.URMPointNums);
  111. int coverImageWidth = 0;
  112. int coverImageHeight = 0;
  113. using (var image = Image.Load(coverFile))
  114. {
  115. coverImageWidth = image.Width;
  116. coverImageHeight = image.Height;
  117. }
  118. var imgBase64 = DoUrmProcess(new UrmProcessRequest
  119. {
  120. FamilyName = familyName,
  121. TypeInfo = typeInfo,
  122. NativeId = nativeId,
  123. Isliner = isliner,
  124. ScaleOfPixel_x = ScaleOfPixel_x,
  125. ScaleOfPixel_y = ScaleOfPixel_y,
  126. Urmsrcwidth = SrcWidth,
  127. Urmsrcheight = SrcHeight,
  128. DenDataPath = denPath,
  129. DirDataPath = dirPath,
  130. VelDataPath = velPath,
  131. AngleDataPath = angelPath,
  132. MaskDataPath = maskPath,
  133. Res = modeRecord.URMRes,
  134. UrmImageType = "0",
  135. UrmBlend = true,
  136. DownsampleIndex = 3,
  137. IntPowerDen = 4,
  138. IntPowerDir = 4,
  139. SigmaGauss = 2.4f,
  140. VessScale = 1,
  141. VelMaxScaler = 1,
  142. VelMinScaler = 0,
  143. Iterations = 1,
  144. ImgProcessVer = 1,
  145. ZoomOn = false,
  146. ZoomRoix = 0,
  147. ZoomRoiy = 0,
  148. ZoomRoiwidth = 1,
  149. ZoomRoiheight = 1,
  150. LeftRight = false,
  151. UpDown = false,
  152. UrmTraceDPoints = new DPoint[] { },
  153. Roix = (int)(coverImageWidth * roiXPercent),
  154. Roiy = (int)(coverImageHeight * roiYPercent),
  155. Roiwidth = (int)(coverImageWidth * roiWPercent),
  156. Roiheight = (int)(coverImageHeight * roiHPercent),
  157. ScreenWidth = coverImageWidth,
  158. ScreenHeight = coverImageHeight,
  159. });
  160. var mergeFilePath = coverFile;
  161. if (!string.IsNullOrWhiteSpace(imgBase64))
  162. {
  163. mergeFilePath = Path.Combine(outputFolder, $"{Guid.NewGuid():N}.png");
  164. using (var image1 = Image.Load(coverFile))
  165. {
  166. // 将base64字符串转换为字节数组
  167. byte[] bytes = Convert.FromBase64String(imgBase64);
  168. // 将字节数组转换为Image对象
  169. using (var image2 = Image.Load(bytes))
  170. {
  171. // 将第二张图片画在第一张图片上
  172. image1.Mutate(ctx => ctx.DrawImage(image2, new Point(0, 0), 1f));
  173. // 保存合成后的图片
  174. image1.Save(mergeFilePath);
  175. }
  176. }
  177. }
  178. var outMsg = new URMAnalysisV2Result
  179. {
  180. URMSrcWidth = modeRecord.URMSrcWidth,
  181. URMSrcHeight = modeRecord.URMSrcHeight,
  182. URMRes = modeRecord.URMRes,
  183. ConverImageUrl = mergeFilePath,
  184. };
  185. return outMsg;
  186. }
  187. catch (Exception ex)
  188. {
  189. WingServerCommon.Log.Logger.WriteLineWarn($"AIService URMAnalysisManager DoURMAnalysis error, ex:{ex}");
  190. }
  191. return null;
  192. }
  193. private void AppendEnvironmentPath()
  194. {
  195. var urmFolder = Path.Combine(ServiceManager.RootPath, "URM");
  196. var privatePath = @$"C:\VinnoApp_Lab\Depends;C:\VinnoApp_Lab\ipp_2018;AIMeasure;AI;Depends;DicomMpeg;Syncfusion;RemoteDiagnosis;vCloud;Test;VFetus;{urmFolder}";
  197. bool enforcePentiumMode = false;
  198. string workPathPerCPU = string.Empty;
  199. string workPathForGPU = string.Empty;
  200. workPathPerCPU = Path.Combine(ServiceManager.RootPath, "NON_PENTIUM");
  201. privatePath = privatePath + ";" + workPathPerCPU + ";";
  202. string gpuName = string.Empty;
  203. workPathForGPU = Path.Combine(ServiceManager.RootPath, "NVIDIA");
  204. if (!string.IsNullOrEmpty(workPathForGPU))
  205. {
  206. privatePath = privatePath + workPathForGPU + ";";
  207. }
  208. if (!string.IsNullOrEmpty(privatePath))
  209. {
  210. string path = Environment.GetEnvironmentVariable("PATH");
  211. string newPath = string.Empty;
  212. if (path != null)
  213. {
  214. string upperPath = path.ToUpper();
  215. var subPathes = privatePath.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  216. foreach (var subPath in subPathes)
  217. {
  218. VinnoDirectoryInfo dir = null;
  219. if (Path.IsPathRooted(subPath))
  220. {
  221. dir = new VinnoDirectoryInfo(subPath);
  222. }
  223. else
  224. {
  225. dir = new VinnoDirectoryInfo(Path.Combine(ServiceManager.RootPath, subPath));
  226. }
  227. if (dir.Exists)
  228. {
  229. var fullName = dir.FullName;
  230. if (!upperPath.Contains(fullName.ToUpper()))
  231. {
  232. newPath += ";" + fullName;
  233. }
  234. }
  235. }
  236. if (!string.IsNullOrEmpty(newPath))
  237. {
  238. Environment.SetEnvironmentVariable("PATH", newPath + ";" + path, EnvironmentVariableTarget.Process);
  239. }
  240. }
  241. }
  242. }
  243. private int ConvertToInt(string value)
  244. {
  245. if (string.IsNullOrWhiteSpace(value))
  246. {
  247. return 0;
  248. }
  249. if (int.TryParse(value, out int x))
  250. {
  251. return x;
  252. }
  253. return 0;
  254. }
  255. private int? ConvertToIntCanNull(string value)
  256. {
  257. if (string.IsNullOrWhiteSpace(value))
  258. {
  259. return null;
  260. }
  261. if (int.TryParse(value, out int x))
  262. {
  263. return x;
  264. }
  265. return 0;
  266. }
  267. private double ConvertToDouble(string value)
  268. {
  269. if (string.IsNullOrWhiteSpace(value))
  270. {
  271. return 0;
  272. }
  273. if (double.TryParse(value, out double x))
  274. {
  275. return x;
  276. }
  277. return 0;
  278. }
  279. }
  280. }