AIConvertHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using AI.Common.Interface;
  2. using AIDiagnosis.Common.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using Vinno.AI.CommonSDK.Enums;
  6. using Vinno.AI.CommonSDK.Models;
  7. using Vinno.AI.CustomizeDiagnosisSDK.Models;
  8. using Vinno.AI.CustomizeDiagnosisService.Models;
  9. using Vinno.AI.Service.Common.Tools;
  10. namespace Vinno.AI.CustomizeDiagnosisService.Tools
  11. {
  12. internal static class AIConvertHelper
  13. {
  14. internal static AICustomizeDetectedObjectInfo[] ConvertIDetectedObjectArrayToAICustomizeDetectedObjectInfoArray(IDetectedObject[] detectedObjects)
  15. {
  16. if (detectedObjects == null)
  17. {
  18. return null;
  19. }
  20. AICustomizeDetectedObjectInfo[] aiCustomizeDetectedObjectInfo = new AICustomizeDetectedObjectInfo[detectedObjects.Length];
  21. for (int i = 0; i < detectedObjects.Length; i++)
  22. {
  23. var aiDetectedObjectInfo = ConvertIDetectedObjectToAICustomizeDetectedObjectInfo(detectedObjects[i]);
  24. aiCustomizeDetectedObjectInfo[i] = aiDetectedObjectInfo;
  25. }
  26. return aiCustomizeDetectedObjectInfo;
  27. }
  28. private static AICustomizeDetectedObjectInfo ConvertIDetectedObjectToAICustomizeDetectedObjectInfo(IDetectedObject detectedObject)
  29. {
  30. if (detectedObject == null)
  31. {
  32. return null;
  33. }
  34. var label = ConvertILabelToAILabel(detectedObject.Label);
  35. var score = detectedObject.Score;
  36. var boundingBox = ConvertIGeometryToAIGeometry(detectedObject.BoundingBox);
  37. var contour = ConvertIContourToAIContour(detectedObject.Contour);
  38. var description = ConvertIDesriptionToAIDescription(detectedObject.Description);
  39. AICustomizeMeasurement[] measurements;
  40. if (detectedObject.Measurements == null)
  41. {
  42. measurements = null;
  43. }
  44. else
  45. {
  46. measurements = new AICustomizeMeasurement[detectedObject.Measurements.Length];
  47. for (int i = 0; i < detectedObject.Measurements.Length; i++)
  48. {
  49. measurements[i] = ConvertIMeasurementToAIMeasurement(detectedObject.Measurements[i]);
  50. }
  51. }
  52. return new AICustomizeDetectedObjectInfo(label, score, boundingBox, contour, description, measurements);
  53. }
  54. private static AICustomizeLabel ConvertILabelToAILabel(ILabel label)
  55. {
  56. if (label == null)
  57. {
  58. return null;
  59. }
  60. var name = label.Name;
  61. var index = label.Index;
  62. var groupName = label.GroupName;
  63. List<AICustomizeLabelExtraInfo> aiExtraInfos;
  64. if (label.ExtraInfos == null)
  65. {
  66. aiExtraInfos = null;
  67. }
  68. else
  69. {
  70. aiExtraInfos = new List<AICustomizeLabelExtraInfo>();
  71. foreach (var extraInfo in label.ExtraInfos)
  72. {
  73. var aiExtraInfo = ConvertILabelExtraInfoToAIILabelExtraInfo(extraInfo);
  74. aiExtraInfos.Add(aiExtraInfo);
  75. }
  76. }
  77. return new AICustomizeLabel(name, index, groupName, aiExtraInfos);
  78. }
  79. private static AICustomizeLabelExtraInfo ConvertILabelExtraInfoToAIILabelExtraInfo(ILabelExtraInfo extraInfo)
  80. {
  81. if (extraInfo == null)
  82. {
  83. return null;
  84. }
  85. var value = extraInfo.Value;
  86. List<string> possibleValues;
  87. if (extraInfo.PossibleValues == null)
  88. {
  89. possibleValues = null;
  90. }
  91. else
  92. {
  93. possibleValues = new List<string>();
  94. foreach (var possibleValue in extraInfo.PossibleValues)
  95. {
  96. possibleValues.Add(possibleValue);
  97. }
  98. }
  99. return new AICustomizeLabelExtraInfo(value, possibleValues);
  100. }
  101. private static AICustomizeGeometry ConvertIGeometryToAIGeometry(IGeometry boundingBox)
  102. {
  103. if (boundingBox == null)
  104. {
  105. return null;
  106. }
  107. var contour = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(boundingBox.Contour);
  108. return new AICustomizeGeometry(contour);
  109. }
  110. private static AICustomizeContour ConvertIContourToAIContour(IContour contour)
  111. {
  112. if (contour == null)
  113. {
  114. return null;
  115. }
  116. AIContourPoints[] contours;
  117. if (contour.Contours == null)
  118. {
  119. contours = null;
  120. }
  121. else
  122. {
  123. contours = new AIContourPoints[contour.Contours.Length];
  124. for (int i = 0; i < contour.Contours.Length; i++)
  125. {
  126. contours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.Contours[i]);
  127. }
  128. }
  129. AIHierarchy[] hierarchies;
  130. if (contour.Hierarchies == null)
  131. {
  132. hierarchies = null;
  133. }
  134. else
  135. {
  136. hierarchies = new AIHierarchy[contour.Hierarchies.Length];
  137. for (int i = 0; i < contour.Hierarchies.Length; i++)
  138. {
  139. hierarchies[i] = AICommonServiceConvertHelperV2.ConvertHierarchyToAIHierarchy(contour.Hierarchies[i]);
  140. }
  141. }
  142. var boundingBox = AICommonServiceConvertHelperV2.ConvertRectToAIRect(contour.BoundingBox);
  143. AIContourPoints[] externalContours;
  144. if (contour.ExternalContours == null)
  145. {
  146. externalContours = null;
  147. }
  148. else
  149. {
  150. externalContours = new AIContourPoints[contour.ExternalContours.Length];
  151. for (int i = 0; i < contour.ExternalContours.Length; i++)
  152. {
  153. externalContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.ExternalContours[i]);
  154. }
  155. }
  156. AIContourPoints[] outerContours;
  157. if (contour.OuterContours == null)
  158. {
  159. outerContours = null;
  160. }
  161. else
  162. {
  163. outerContours = new AIContourPoints[contour.OuterContours.Length];
  164. for (int i = 0; i < contour.OuterContours.Length; i++)
  165. {
  166. outerContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.OuterContours[i]);
  167. }
  168. }
  169. AIContourPoints[] innerContours;
  170. if (contour.InnerContours == null)
  171. {
  172. innerContours = null;
  173. }
  174. else
  175. {
  176. innerContours = new AIContourPoints[contour.InnerContours.Length];
  177. for (int i = 0; i < contour.InnerContours.Length; i++)
  178. {
  179. innerContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.InnerContours[i]);
  180. }
  181. }
  182. return new AICustomizeContour(contours, hierarchies, boundingBox, externalContours, outerContours, innerContours);
  183. }
  184. private static AICustomizeDescription ConvertIDesriptionToAIDescription(IDescription description)
  185. {
  186. if (description == null)
  187. {
  188. return null;
  189. }
  190. return new AICustomizeDescription(description.Descriptions);
  191. }
  192. private static AICustomizeMeasurement ConvertIMeasurementToAIMeasurement(IMeasurement measurement)
  193. {
  194. if (measurement == null)
  195. {
  196. return null;
  197. }
  198. AICustomizeGeometry[] measureMarks;
  199. if (measurement.MeasureMarks == null)
  200. {
  201. measureMarks = null;
  202. }
  203. else
  204. {
  205. measureMarks = new AICustomizeGeometry[measurement.MeasureMarks.Length];
  206. for (int i = 0; i < measurement.MeasureMarks.Length; i++)
  207. {
  208. measureMarks[i] = ConvertIGeometryToAIGeometry(measurement.MeasureMarks[i]);
  209. }
  210. }
  211. AICustomizeMeasureValues[] measureValues;
  212. if (measurement.MeasureValues == null)
  213. {
  214. measureValues = null;
  215. }
  216. else
  217. {
  218. measureValues = new AICustomizeMeasureValues[measurement.MeasureValues.Length];
  219. for (int i = 0; i < measurement.MeasureValues.Length; i++)
  220. {
  221. measureValues[i] = ConvertIMeasureValuesToAIMeasureValues(measurement.MeasureValues[i]);
  222. }
  223. }
  224. var cmPerPixel = measurement.CmPerPixel;
  225. return new AICustomizeMeasurement(measureMarks, measureValues, cmPerPixel);
  226. }
  227. private static AICustomizeMeasureValues ConvertIMeasureValuesToAIMeasureValues(IMeasureValues measureValues)
  228. {
  229. if (measureValues == null)
  230. {
  231. return null;
  232. }
  233. Dictionary<AIEnumMeasureUnitNames, double> unitList = new Dictionary<AIEnumMeasureUnitNames, double>();
  234. foreach (var unit in Enum.GetValues(typeof(EnumMeasureUnitNames)))
  235. {
  236. try
  237. {
  238. if (unit is EnumMeasureUnitNames)
  239. {
  240. var value = measureValues.GetValue((EnumMeasureUnitNames)unit);
  241. unitList.Add((AIEnumMeasureUnitNames)unit, value);
  242. }
  243. }
  244. catch
  245. {
  246. }
  247. }
  248. return new AICustomizeMeasureValues(unitList);
  249. }
  250. internal static CustomizeUltrasoundImageRegion ConvertAICustomizeUltrasoundImageRegionInfoToIUltrasoundImageRegion(AICustomizeUltrasoundImageRegionInfo aiCustomizeUltrasoundImageRegionInfo)
  251. {
  252. if (aiCustomizeUltrasoundImageRegionInfo == null)
  253. {
  254. return null;
  255. }
  256. var centerDepth = aiCustomizeUltrasoundImageRegionInfo.CenterDepthInPixel;
  257. var depthSpan = aiCustomizeUltrasoundImageRegionInfo.DepthSpanInPixel;
  258. var beamPosition = aiCustomizeUltrasoundImageRegionInfo.BeamPosition;
  259. var widthSpan = aiCustomizeUltrasoundImageRegionInfo.WidthSpan;
  260. var contour = AICommonServiceConvertHelperV2.ConvertAIContourPointsToContourPoints(aiCustomizeUltrasoundImageRegionInfo.Contour);
  261. return new CustomizeUltrasoundImageRegion(centerDepth, depthSpan, beamPosition, widthSpan, contour);
  262. }
  263. internal static AIEngineInfo ConvertEngineInfoToAIEngineInfo(EngineInfo engineInfo, bool isCustomize)
  264. {
  265. if (engineInfo == null)
  266. {
  267. return null;
  268. }
  269. if (Enum.TryParse(engineInfo.EngineName, out AIEnumEngineName name) && Enum.TryParse(engineInfo.EngineType, out AIEnumEngineType type))
  270. {
  271. return new AIEngineInfo(name, type, engineInfo.IsEnabled, engineInfo.Version, engineInfo.Vendor, engineInfo.ExpiredTime, engineInfo.RelatedDllList);
  272. }
  273. else if (isCustomize)
  274. {
  275. return new AIEngineInfo(engineInfo.EngineName, engineInfo.EngineType, engineInfo.IsEnabled, engineInfo.Version, engineInfo.Vendor, engineInfo.ExpiredTime, engineInfo.RelatedDllList);
  276. }
  277. else
  278. {
  279. return null;
  280. }
  281. }
  282. }
  283. }