using OrpaonVision.Core.Results; namespace OrpaonVision.Core.LayerRecognition; /// /// 层识别服务接口。 /// public interface ILayerRecognitionService { /// /// 识别当前层级。 /// /// 推理结果。 /// 当前层级。 /// 取消令牌。 /// 层级识别结果。 Task> RecognizeLayerAsync(InferenceResultDto inference, int currentLayer, CancellationToken cancellationToken = default); /// /// 验证层级稳定性。 /// /// 层级历史记录。 /// 稳定性窗口大小。 /// 取消令牌。 /// 稳定性验证结果。 Task> ValidateLayerStabilityAsync(IReadOnlyList layerHistory, int stabilityWindow = 5, CancellationToken cancellationToken = default); /// /// 检测层级变化。 /// /// 推理结果。 /// 当前层级。 /// 层级历史记录。 /// 取消令牌。 /// 层级变化检测结果。 Task> DetectLayerTransitionAsync(InferenceResultDto inference, int currentLayer, IReadOnlyList layerHistory, CancellationToken cancellationToken = default); /// /// 获取层级特征。 /// /// 推理结果。 /// 取消令牌。 /// 层级特征。 Task> ExtractLayerFeaturesAsync(InferenceResultDto inference, CancellationToken cancellationToken = default); /// /// 训练层级识别模型。 /// /// 训练数据。 /// 取消令牌。 /// 训练结果。 Task> TrainLayerModelAsync(IReadOnlyList trainingData, CancellationToken cancellationToken = default); /// /// 更新层级识别模型。 /// /// 模型数据。 /// 取消令牌。 /// 更新结果。 Task UpdateLayerModelAsync(LayerModelData modelData, CancellationToken cancellationToken = default); /// /// 获取层级识别统计信息。 /// /// 开始时间。 /// 结束时间。 /// 取消令牌。 /// 统计信息。 Task> GetRecognitionStatisticsAsync(DateTime startTime, DateTime endTime, CancellationToken cancellationToken = default); } /// /// 层级识别结果。 /// public sealed class LayerRecognitionResult { /// /// 识别的层级。 /// public int RecognizedLayer { get; init; } /// /// 置信度。 /// public double Confidence { get; init; } /// /// 识别方法。 /// public LayerRecognitionMethod Method { get; init; } /// /// 层级特征。 /// public LayerFeatures Features { get; init; } = new(); /// /// 是否为层级变化。 /// public bool IsLayerTransition { get; init; } /// /// 识别时间。 /// public DateTime RecognitionTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级稳定性结果。 /// public sealed class LayerStabilityResult { /// /// 是否稳定。 /// public bool IsStable { get; init; } /// /// 稳定性得分。 /// public double StabilityScore { get; init; } /// /// 主导层级。 /// public int DominantLayer { get; init; } /// /// 层级分布。 /// public Dictionary LayerDistribution { get; init; } = new(); /// /// 稳定性窗口。 /// public int StabilityWindow { get; init; } /// /// 验证时间。 /// public DateTime ValidationTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } } /// /// 层级变化检测结果。 /// public sealed class LayerTransitionResult { /// /// 是否检测到层级变化。 /// public bool HasTransition { get; init; } /// /// 原始层级。 /// public int FromLayer { get; init; } /// /// 目标层级。 /// public int ToLayer { get; init; } /// /// 变化置信度。 /// public double TransitionConfidence { get; init; } /// /// 变化类型。 /// public LayerTransitionType TransitionType { get; init; } /// /// 检测时间。 /// public DateTime DetectionTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级特征。 /// public sealed class LayerFeatures { /// /// 目标检测数量。 /// public int ObjectCount { get; init; } /// /// 目标类别分布。 /// public Dictionary ObjectClassDistribution { get; init; } = new(); /// /// 平均置信度。 /// public double AverageConfidence { get; init; } /// /// 最大置信度。 /// public double MaxConfidence { get; init; } /// /// 目标位置分布。 /// public List BoundingBoxes { get; init; } = new(); /// /// 图像特征。 /// public ImageFeatures ImageFeatures { get; init; } = new(); /// /// 层级特征向量。 /// public double[] FeatureVector { get; init; } = Array.Empty(); /// /// 特征提取时间。 /// public DateTime ExtractionTimeUtc { get; init; } } /// /// 图像特征。 /// public sealed class ImageFeatures { /// /// 亮度均值。 /// public double BrightnessMean { get; init; } /// /// 对比度。 /// public double Contrast { get; init; } /// /// 纹理复杂度。 /// public double TextureComplexity { get; init; } /// /// 边缘密度。 /// public double EdgeDensity { get; init; } /// /// 颜色分布。 /// public Dictionary ColorDistribution { get; init; } = new(); /// /// 图像尺寸。 /// public (int Width, int Height) ImageSize { get; init; } /// /// 长宽比。 /// public double AspectRatio { get; init; } } /// /// 边界框。 /// public sealed class BoundingBox { /// /// X坐标。 /// public double X { get; init; } /// /// Y坐标。 /// public double Y { get; init; } /// /// 宽度。 /// public double Width { get; init; } /// /// 高度。 /// public double Height { get; init; } /// /// 置信度。 /// public double Confidence { get; init; } /// /// 类别。 /// public string Class { get; init; } = string.Empty; /// /// 中心点。 /// public (double X, double Y) Center => (X + Width / 2, Y + Height / 2); /// /// 面积。 /// public double Area => Width * Height; /// /// 长宽比。 /// public double AspectRatio => Width / Height; } /// /// 层级历史记录。 /// public sealed class LayerHistoryRecord { /// /// 记录ID。 /// public Guid RecordId { get; init; } /// /// 层级。 /// public int Layer { get; init; } /// /// 置信度。 /// public double Confidence { get; init; } /// /// 推理结果。 /// public InferenceResultDto Inference { get; init; } = new(); /// /// 层级特征。 /// public LayerFeatures Features { get; init; } = new(); /// /// 记录时间。 /// public DateTime RecordTimeUtc { get; init; } /// /// 是否为有效记录。 /// public bool IsValid { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级训练数据。 /// public sealed class LayerTrainingData { /// /// 数据ID。 /// public Guid DataId { get; init; } /// /// 目标层级。 /// public int TargetLayer { get; init; } /// /// 推理结果。 /// public InferenceResultDto Inference { get; init; } = new(); /// /// 层级特征。 /// public LayerFeatures Features { get; init; } = new(); /// /// 数据标签。 /// public string Label { get; init; } = string.Empty; /// /// 创建时间。 /// public DateTime CreatedAtUtc { get; init; } /// /// 是否为验证数据。 /// public bool IsValidationData { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级模型训练结果。 /// public sealed class LayerModelTrainingResult { /// /// 训练ID。 /// public Guid TrainingId { get; init; } /// /// 模型准确率。 /// public double Accuracy { get; init; } /// /// 模型精确率。 /// public double Precision { get; init; } /// /// 模型召回率。 /// public double Recall { get; init; } /// /// F1分数。 /// public double F1Score { get; init; } /// /// 训练样本数量。 /// public int TrainingSampleCount { get; init; } /// /// 验证样本数量。 /// public int ValidationSampleCount { get; init; } /// /// 训练耗时(毫秒)。 /// public long TrainingElapsedMs { get; init; } /// /// 模型版本。 /// public string ModelVersion { get; init; } = string.Empty; /// /// 训练时间。 /// public DateTime TrainingTimeUtc { get; init; } /// /// 详细信息。 /// public string? Details { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级模型数据。 /// public sealed class LayerModelData { /// /// 模型ID。 /// public Guid ModelId { get; init; } /// /// 模型版本。 /// public string Version { get; init; } = string.Empty; /// /// 模型类型。 /// public LayerModelType ModelType { get; init; } /// /// 模型数据。 /// public byte[] ModelData { get; init; } = Array.Empty(); /// /// 模型参数。 /// public Dictionary ModelParameters { get; init; } = new(); /// /// 特征提取器配置。 /// public Dictionary FeatureExtractorConfig { get; init; } = new(); /// /// 创建时间。 /// public DateTime CreatedAtUtc { get; init; } /// /// 是否启用。 /// public bool IsEnabled { get; init; } /// /// 扩展属性。 /// public Dictionary ExtendedProperties { get; init; } = new(); } /// /// 层级识别统计信息。 /// public sealed class LayerRecognitionStatistics { /// /// 总识别次数。 /// public int TotalRecognitions { get; init; } /// /// 成功识别次数。 /// public int SuccessfulRecognitions { get; init; } /// /// 失败识别次数。 /// public int FailedRecognitions { get; init; } /// /// 识别准确率。 /// public double Accuracy { get; init; } /// /// 平均置信度。 /// public double AverageConfidence { get; init; } /// /// 层级变化次数。 /// public int LayerTransitions { get; init; } /// /// 按层级分组的统计。 /// public Dictionary ByLayer { get; init; } = new(); /// /// 按方法分组的统计。 /// public Dictionary ByMethod { get; init; } = new(); /// /// 按时间分组的统计。 /// public Dictionary ByTime { get; init; } = new(); /// /// 统计开始时间。 /// public DateTime StartTimeUtc { get; init; } /// /// 统计结束时间。 /// public DateTime EndTimeUtc { get; init; } } /// /// 层级统计。 /// public sealed class LayerStatistics { /// /// 层级。 /// public int Layer { get; init; } /// /// 识别次数。 /// public int RecognitionCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 准确率。 /// public double Accuracy { get; init; } /// /// 平均置信度。 /// public double AverageConfidence { get; init; } } /// /// 方法统计。 /// public sealed class MethodStatistics { /// /// 识别方法。 /// public LayerRecognitionMethod Method { get; init; } /// /// 使用次数。 /// public int UsageCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 准确率。 /// public double Accuracy { get; init; } /// /// 平均耗时(毫秒)。 /// public double AverageElapsedMs { get; init; } } /// /// 时间统计。 /// public sealed class TimeStatistics { /// /// 时间。 /// public DateTime Time { get; init; } /// /// 识别次数。 /// public int RecognitionCount { get; init; } /// /// 成功次数。 /// public int SuccessCount { get; init; } /// /// 层级变化次数。 /// public int LayerTransitions { get; init; } } /// /// 层级识别方法。 /// public enum LayerRecognitionMethod { /// /// 基于规则。 /// RuleBased = 0, /// /// 基于机器学习。 /// MachineLearning = 1, /// /// 基于深度学习。 /// DeepLearning = 2, /// /// 基于特征匹配。 /// FeatureMatching = 3, /// /// 混合方法。 /// Hybrid = 4 } /// /// 层级变化类型。 /// public enum LayerTransitionType { /// /// 正常切层。 /// Normal = 0, /// /// 跳层。 /// Skip = 1, /// /// 回退。 /// Backward = 2, /// /// 重复。 /// Duplicate = 3, /// /// 异常。 /// Abnormal = 4 } /// /// 层级模型类型。 /// public enum LayerModelType { /// /// 决策树。 /// DecisionTree = 0, /// /// 随机森林。 /// RandomForest = 1, /// /// 支持向量机。 /// SupportVectorMachine = 2, /// /// 神经网络。 /// NeuralNetwork = 3, /// /// 卷积神经网络。 /// ConvolutionalNeuralNetwork = 4, /// /// 梯度提升。 /// GradientBoosting = 5 }