123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- using AI.Common.Interface;
- using AIDiagnosis.Common.Models;
- using System;
- using System.Collections.Generic;
- using Vinno.AI.CommonSDK.Enums;
- using Vinno.AI.CommonSDK.Models;
- using Vinno.AI.CustomizeDiagnosisSDK.Models;
- using Vinno.AI.CustomizeDiagnosisService.Models;
- using Vinno.AI.Service.Common.Tools;
- namespace Vinno.AI.CustomizeDiagnosisService.Tools
- {
- internal static class AIConvertHelper
- {
- internal static AICustomizeDetectedObjectInfo[] ConvertIDetectedObjectArrayToAICustomizeDetectedObjectInfoArray(IDetectedObject[] detectedObjects)
- {
- if (detectedObjects == null)
- {
- return null;
- }
- AICustomizeDetectedObjectInfo[] aiCustomizeDetectedObjectInfo = new AICustomizeDetectedObjectInfo[detectedObjects.Length];
- for (int i = 0; i < detectedObjects.Length; i++)
- {
- var aiDetectedObjectInfo = ConvertIDetectedObjectToAICustomizeDetectedObjectInfo(detectedObjects[i]);
- aiCustomizeDetectedObjectInfo[i] = aiDetectedObjectInfo;
- }
- return aiCustomizeDetectedObjectInfo;
- }
- private static AICustomizeDetectedObjectInfo ConvertIDetectedObjectToAICustomizeDetectedObjectInfo(IDetectedObject detectedObject)
- {
- if (detectedObject == null)
- {
- return null;
- }
- var label = ConvertILabelToAILabel(detectedObject.Label);
- var score = detectedObject.Score;
- var boundingBox = ConvertIGeometryToAIGeometry(detectedObject.BoundingBox);
- var contour = ConvertIContourToAIContour(detectedObject.Contour);
- var description = ConvertIDesriptionToAIDescription(detectedObject.Description);
- AICustomizeMeasurement[] measurements;
- if (detectedObject.Measurements == null)
- {
- measurements = null;
- }
- else
- {
- measurements = new AICustomizeMeasurement[detectedObject.Measurements.Length];
- for (int i = 0; i < detectedObject.Measurements.Length; i++)
- {
- measurements[i] = ConvertIMeasurementToAIMeasurement(detectedObject.Measurements[i]);
- }
- }
- return new AICustomizeDetectedObjectInfo(label, score, boundingBox, contour, description, measurements);
- }
- private static AICustomizeLabel ConvertILabelToAILabel(ILabel label)
- {
- if (label == null)
- {
- return null;
- }
- var name = label.Name;
- var index = label.Index;
- var groupName = label.GroupName;
- List<AICustomizeLabelExtraInfo> aiExtraInfos;
- if (label.ExtraInfos == null)
- {
- aiExtraInfos = null;
- }
- else
- {
- aiExtraInfos = new List<AICustomizeLabelExtraInfo>();
- foreach (var extraInfo in label.ExtraInfos)
- {
- var aiExtraInfo = ConvertILabelExtraInfoToAIILabelExtraInfo(extraInfo);
- aiExtraInfos.Add(aiExtraInfo);
- }
- }
- return new AICustomizeLabel(name, index, groupName, aiExtraInfos);
- }
- private static AICustomizeLabelExtraInfo ConvertILabelExtraInfoToAIILabelExtraInfo(ILabelExtraInfo extraInfo)
- {
- if (extraInfo == null)
- {
- return null;
- }
- var value = extraInfo.Value;
- List<string> possibleValues;
- if (extraInfo.PossibleValues == null)
- {
- possibleValues = null;
- }
- else
- {
- possibleValues = new List<string>();
- foreach (var possibleValue in extraInfo.PossibleValues)
- {
- possibleValues.Add(possibleValue);
- }
- }
- return new AICustomizeLabelExtraInfo(value, possibleValues);
- }
- private static AICustomizeGeometry ConvertIGeometryToAIGeometry(IGeometry boundingBox)
- {
- if (boundingBox == null)
- {
- return null;
- }
- var contour = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(boundingBox.Contour);
- return new AICustomizeGeometry(contour);
- }
- private static AICustomizeContour ConvertIContourToAIContour(IContour contour)
- {
- if (contour == null)
- {
- return null;
- }
- AIContourPoints[] contours;
- if (contour.Contours == null)
- {
- contours = null;
- }
- else
- {
- contours = new AIContourPoints[contour.Contours.Length];
- for (int i = 0; i < contour.Contours.Length; i++)
- {
- contours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.Contours[i]);
- }
- }
- AIHierarchy[] hierarchies;
- if (contour.Hierarchies == null)
- {
- hierarchies = null;
- }
- else
- {
- hierarchies = new AIHierarchy[contour.Hierarchies.Length];
- for (int i = 0; i < contour.Hierarchies.Length; i++)
- {
- hierarchies[i] = AICommonServiceConvertHelperV2.ConvertHierarchyToAIHierarchy(contour.Hierarchies[i]);
- }
- }
- var boundingBox = AICommonServiceConvertHelperV2.ConvertRectToAIRect(contour.BoundingBox);
- AIContourPoints[] externalContours;
- if (contour.ExternalContours == null)
- {
- externalContours = null;
- }
- else
- {
- externalContours = new AIContourPoints[contour.ExternalContours.Length];
- for (int i = 0; i < contour.ExternalContours.Length; i++)
- {
- externalContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.ExternalContours[i]);
- }
- }
- AIContourPoints[] outerContours;
- if (contour.OuterContours == null)
- {
- outerContours = null;
- }
- else
- {
- outerContours = new AIContourPoints[contour.OuterContours.Length];
- for (int i = 0; i < contour.OuterContours.Length; i++)
- {
- outerContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.OuterContours[i]);
- }
- }
- AIContourPoints[] innerContours;
- if (contour.InnerContours == null)
- {
- innerContours = null;
- }
- else
- {
- innerContours = new AIContourPoints[contour.InnerContours.Length];
- for (int i = 0; i < contour.InnerContours.Length; i++)
- {
- innerContours[i] = AICommonServiceConvertHelperV2.ConvertContourPointsToAIContourPoints(contour.InnerContours[i]);
- }
- }
- return new AICustomizeContour(contours, hierarchies, boundingBox, externalContours, outerContours, innerContours);
- }
- private static AICustomizeDescription ConvertIDesriptionToAIDescription(IDescription description)
- {
- if (description == null)
- {
- return null;
- }
- return new AICustomizeDescription(description.Descriptions);
- }
- private static AICustomizeMeasurement ConvertIMeasurementToAIMeasurement(IMeasurement measurement)
- {
- if (measurement == null)
- {
- return null;
- }
- AICustomizeGeometry[] measureMarks;
- if (measurement.MeasureMarks == null)
- {
- measureMarks = null;
- }
- else
- {
- measureMarks = new AICustomizeGeometry[measurement.MeasureMarks.Length];
- for (int i = 0; i < measurement.MeasureMarks.Length; i++)
- {
- measureMarks[i] = ConvertIGeometryToAIGeometry(measurement.MeasureMarks[i]);
- }
- }
- AICustomizeMeasureValues[] measureValues;
- if (measurement.MeasureValues == null)
- {
- measureValues = null;
- }
- else
- {
- measureValues = new AICustomizeMeasureValues[measurement.MeasureValues.Length];
- for (int i = 0; i < measurement.MeasureValues.Length; i++)
- {
- measureValues[i] = ConvertIMeasureValuesToAIMeasureValues(measurement.MeasureValues[i]);
- }
- }
- var cmPerPixel = measurement.CmPerPixel;
- return new AICustomizeMeasurement(measureMarks, measureValues, cmPerPixel);
- }
- private static AICustomizeMeasureValues ConvertIMeasureValuesToAIMeasureValues(IMeasureValues measureValues)
- {
- if (measureValues == null)
- {
- return null;
- }
- Dictionary<AIEnumMeasureUnitNames, double> unitList = new Dictionary<AIEnumMeasureUnitNames, double>();
- foreach (var unit in Enum.GetValues(typeof(EnumMeasureUnitNames)))
- {
- try
- {
- if (unit is EnumMeasureUnitNames)
- {
- var value = measureValues.GetValue((EnumMeasureUnitNames)unit);
- unitList.Add((AIEnumMeasureUnitNames)unit, value);
- }
- }
- catch
- {
- }
- }
- return new AICustomizeMeasureValues(unitList);
- }
- internal static CustomizeUltrasoundImageRegion ConvertAICustomizeUltrasoundImageRegionInfoToIUltrasoundImageRegion(AICustomizeUltrasoundImageRegionInfo aiCustomizeUltrasoundImageRegionInfo)
- {
- if (aiCustomizeUltrasoundImageRegionInfo == null)
- {
- return null;
- }
- var centerDepth = aiCustomizeUltrasoundImageRegionInfo.CenterDepthInPixel;
- var depthSpan = aiCustomizeUltrasoundImageRegionInfo.DepthSpanInPixel;
- var beamPosition = aiCustomizeUltrasoundImageRegionInfo.BeamPosition;
- var widthSpan = aiCustomizeUltrasoundImageRegionInfo.WidthSpan;
- var contour = AICommonServiceConvertHelperV2.ConvertAIContourPointsToContourPoints(aiCustomizeUltrasoundImageRegionInfo.Contour);
- return new CustomizeUltrasoundImageRegion(centerDepth, depthSpan, beamPosition, widthSpan, contour);
- }
- internal static AIEngineInfo ConvertEngineInfoToAIEngineInfo(EngineInfo engineInfo, bool isCustomize)
- {
- if (engineInfo == null)
- {
- return null;
- }
- if (Enum.TryParse(engineInfo.EngineName, out AIEnumEngineName name) && Enum.TryParse(engineInfo.EngineType, out AIEnumEngineType type))
- {
- return new AIEngineInfo(name, type, engineInfo.IsEnabled, engineInfo.Version, engineInfo.Vendor, engineInfo.ExpiredTime, engineInfo.RelatedDllList);
- }
- else if (isCustomize)
- {
- return new AIEngineInfo(engineInfo.EngineName, engineInfo.EngineType, engineInfo.IsEnabled, engineInfo.Version, engineInfo.Vendor, engineInfo.ExpiredTime, engineInfo.RelatedDllList);
- }
- else
- {
- return null;
- }
- }
- }
- }
|