学习目标
通过本章学习,你将掌握:
机器学习基础
- ML.NET框架使用
- 数据预处理和特征工程
- 模型训练和评估
- 模型部署和推理
深度学习应用
- TensorFlow.NET集成
- 神经网络构建
- 图像识别和处理
- 自然语言处理
AI服务集成
- Azure认知服务
- OpenAI API集成
- 语音识别和合成
- 计算机视觉
24.3 AI服务集成
Azure认知服务集成
// 安装NuGet包:Microsoft.Azure.CognitiveServices.Vision.ComputerVision
// Microsoft.Azure.CognitiveServices.Language.TextAnalytics
// Microsoft.CognitiveServices.Speech
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Azure.AI.TextAnalytics;
using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
using System.Collections.Generic;
using System.Linq;
// Azure认知服务配置
public class AzureCognitiveServicesConfig
{
public string ComputerVisionKey { get; set; }
预测分析
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Forecasting;
// 时间序列数据
public class TimeSeriesData
{
[LoadColumn(0)]
public DateTime Date { get; set; }
[LoadColumn(1)]
public float Value { get; set; }
}
24.5 实践练习
练习1:构建智能客服系统
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// 智能客服系统
public class IntelligentCustomerService
{
private readonly OpenAIService _openAIService;
private readonly AzureTextAnalyticsService _textAnalytics;
private readonly Dictionary<string, string> _knowledgeBase;
private readonly List<CustomerInteraction> _interactions;
public IntelligentCustomerService(
OpenAIService openAIService,
AzureTextAnalyticsService textAnalytics)
{
_openAIService = openAIService;
_textAnalytics = textAnalytics;
_knowledgeBase = new Dictionary<string, string>();
_interactions = new List<CustomerInteraction>();
InitializeKnowledgeBase();
}
private void InitializeKnowledgeBase()
{
_knowledgeBase["退款"] = "退款政策:购买后30天内可申请退款,需提供订单号和退款原因。";
_knowledgeBase["配送"] = "配送时间:标准配送3-5个工作日,加急配送1-2个工作日。";
_knowledgeBase["售后"] = "售后服务:提供1年质保,支持维修和更换服务。";
_knowledgeBase["账户"] = "账户问题:可通过邮箱或手机号重置密码,联系客服协助处理。";
}
public async Task<CustomerServiceResponse> ProcessCustomerInquiry(string customerMessage, string customerId)
{
// 1. 情感分析
var sentiment = await _textAnalytics.AnalyzeSentimentAsync(customerMessage);
// 2. 关键词提取
var keyPhrases = await _textAnalytics.ExtractKeyPhrasesAsync(customerMessage);
// 3. 意图识别
var intent = IdentifyIntent(customerMessage, keyPhrases.ToList());
// 4. 生成回复
var response = await GenerateResponse(customerMessage, intent, sentiment.Sentiment.ToString());
// 5. 记录交互
var interaction = new CustomerInteraction
{
CustomerId = customerId,
Message = customerMessage,
Response = response,
Sentiment = sentiment.Sentiment.ToString(),
Intent = intent,
Timestamp = DateTime.Now
};
_interactions.Add(interaction);
return new CustomerServiceResponse
{
Message = response,
Sentiment = sentiment.Sentiment.ToString(),
Intent = intent,
Confidence = CalculateConfidence(intent, keyPhrases.Count),
SuggestedActions = GetSuggestedActions(intent)
};
}
private string IdentifyIntent(string message, List<string> keyPhrases)
{
var message_lower = message.ToLower();
if (keyPhrases.Any(kp => kp.Contains("退款")) || message_lower.Contains("退款"))
return "退款";
if (keyPhrases.Any(kp => kp.Contains("配送")) || message_lower.Contains("配送"))
return "配送";
if (keyPhrases.Any(kp => kp.Contains("售后")) || message_lower.Contains("售后"))
return "售后";
if (keyPhrases.Any(kp => kp.Contains("账户")) || message_lower.Contains("账户"))
return "账户";
return "一般咨询";
}
private async Task<string> GenerateResponse(string customerMessage, string intent, string sentiment)
{
// 首先尝试从知识库获取答案
if (_knowledgeBase.ContainsKey(intent))
{
var baseResponse = _knowledgeBase[intent];
// 根据情感调整回复语调
if (sentiment == "Negative")
{
return $"我理解您的困扰。{baseResponse} 如需进一步帮助,请联系人工客服。";
}
else
{
return $"很高兴为您服务!{baseResponse} 还有其他问题吗?";
}
}
// 使用AI生成回复
var prompt = $"作为客服代表,请回复客户的问题:{customerMessage}。客户情绪:{sentiment}";
return await _openAIService.GenerateTextAsync(prompt, 200);
}
private float CalculateConfidence(string intent, int keyPhraseCount)
{
if (intent == "一般咨询") return 0.5f;
return Math.Min(0.9f, 0.6f + (keyPhraseCount * 0.1f));
}
private List<string> GetSuggestedActions(string intent)
{
return intent switch
{
"退款" => new List<string> { "查看退款政策", "提交退款申请", "联系人工客服" },
"配送" => new List<string> { "查询订单状态", "修改配送地址", "选择配送方式" },
"售后" => new List<string> { "提交售后申请", "查看保修信息", "预约维修" },
"账户" => new List<string> { "重置密码", "修改个人信息", "账户安全设置" },
_ => new List<string> { "浏览帮助中心", "联系人工客服" }
};
}
public List<CustomerInteraction> GetCustomerHistory(string customerId)
{
return _interactions.Where(i => i.CustomerId == customerId)
.OrderByDescending(i => i.Timestamp)
.ToList();
}
public Dictionary<string, int> GetIntentStatistics()
{
return _interactions.GroupBy(i => i.Intent)
.ToDictionary(g => g.Key, g => g.Count());
}
}
// 客户交互记录
public class CustomerInteraction
{
public string CustomerId { get; set; }
public string Message { get; set; }
public string Response { get; set; }
public string Sentiment { get; set; }
public string Intent { get; set; }
public DateTime Timestamp { get; set; }
}
// 客服响应
public class CustomerServiceResponse
{
public string Message { get; set; }
public string Sentiment { get; set; }
public string Intent { get; set; }
public float Confidence { get; set; }
public List<string> SuggestedActions { get; set; }
}
练习2:智能数据分析平台
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// 智能数据分析平台
public class IntelligentDataAnalyticsPlatform
{
private readonly CollaborativeFilteringRecommender _recommender;
private readonly TimeSeriesForecaster _forecaster;
private readonly AnomalyDetector _anomalyDetector;
private readonly SalesPredictor _salesPredictor;
public IntelligentDataAnalyticsPlatform()
{
_recommender = new CollaborativeFilteringRecommender();
_forecaster = new TimeSeriesForecaster();
_anomalyDetector = new AnomalyDetector();
_salesPredictor = new SalesPredictor();
}
public async Task<AnalyticsReport> GenerateComprehensiveReport(
IEnumerable<UserRating> userRatings,
IEnumerable<TimeSeriesData> timeSeriesData,
IEnumerable<SalesData> salesData)
{
var report = new AnalyticsReport
{
GeneratedAt = DateTime.Now,
Sections = new List<ReportSection>()
};
// 1. 推荐系统分析
_recommender.TrainModel(userRatings);
var recommendationInsights = AnalyzeRecommendations(userRatings);
report.Sections.Add(new ReportSection
{
Title = "推荐系统分析",
Content = recommendationInsights,
Charts = GenerateRecommendationCharts(userRatings)
});
// 2. 时间序列预测
_forecaster.TrainModel(timeSeriesData);
var forecast = _forecaster.ForecastWithDates(DateTime.Now, 30);
report.Sections.Add(new ReportSection
{
Title = "未来30天预测",
Content = GenerateForecastInsights(forecast),
Charts = GenerateForecastCharts(forecast)
});
// 3. 异常检测
var values = timeSeriesData.Select(d => d.Value);
_anomalyDetector.TrainModel(timeSeriesData.Select(d => new AnomalyData { Value = d.Value }));
var anomalies = _anomalyDetector.DetectAnomalies(values);
report.Sections.Add(new ReportSection
{
Title = "异常检测报告",
Content = GenerateAnomalyInsights(anomalies),
Charts = GenerateAnomalyCharts(anomalies)
});
// 4. 销售预测分析
_salesPredictor.TrainModel(salesData);
var featureImportance = _salesPredictor.AnalyzeFeatureImportance(salesData);
report.Sections.Add(new ReportSection
{
Title = "销售影响因素分析",
Content = GenerateSalesInsights(featureImportance),
Charts = GenerateFeatureImportanceCharts(featureImportance)
});
// 5. 综合建议
report.Recommendations = GenerateActionableRecommendations(report);
return report;
}
private string AnalyzeRecommendations(IEnumerable<UserRating> ratings)
{
var ratingsList = ratings.ToList();
var userCount = ratingsList.Select(r => r.UserId).Distinct().Count();
var productCount = ratingsList.Select(r => r.ProductId).Distinct().Count();
var avgRating = ratingsList.Average(r => r.Rating);
return $"分析了{userCount}个用户对{productCount}个产品的{ratingsList.Count}条评分数据。" +
$"平均评分为{avgRating:F2}。推荐系统已训练完成,可为用户提供个性化推荐。";
}
private string GenerateForecastInsights(List<(DateTime Date, float PredictedValue, float LowerBound, float UpperBound)> forecast)
{
var avgPrediction = forecast.Average(f => f.PredictedValue);
var trend = forecast.Last().PredictedValue > forecast.First().PredictedValue ? "上升" : "下降";
return $"未来30天预测显示{trend}趋势,平均预测值为{avgPrediction:F2}。" +
$"预测区间为{forecast.Min(f => f.LowerBound):F2}到{forecast.Max(f => f.UpperBound):F2}。";
}
private string GenerateAnomalyInsights(List<(int Index, bool IsAnomaly, float Score)> anomalies)
{
var anomalyCount = anomalies.Count(a => a.IsAnomaly);
var totalCount = anomalies.Count;
var anomalyRate = (double)anomalyCount / totalCount * 100;
return $"在{totalCount}个数据点中检测到{anomalyCount}个异常点,异常率为{anomalyRate:F1}%。" +
$"建议重点关注异常值较高的时间段。";
}
private string GenerateSalesInsights(Dictionary<string, float> featureImportance)
{
var mostImportant = featureImportance.OrderByDescending(kv => kv.Value).First();
return $"销售预测分析显示,{mostImportant.Key}是最重要的影响因素(重要性:{mostImportant.Value:F2})。" +
$"建议在制定销售策略时重点考虑此因素。";
}
private List<ChartData> GenerateRecommendationCharts(IEnumerable<UserRating> ratings)
{
// 生成推荐系统相关图表数据
return new List<ChartData>
{
new ChartData
{
Type = "bar",
Title = "评分分布",
Data = ratings.GroupBy(r => (int)r.Rating)
.ToDictionary(g => g.Key.ToString(), g => (double)g.Count())
}
};
}
private List<ChartData> GenerateForecastCharts(List<(DateTime Date, float PredictedValue, float LowerBound, float UpperBound)> forecast)
{
return new List<ChartData>
{
new ChartData
{
Type = "line",
Title = "预测趋势",
Data = forecast.ToDictionary(f => f.Date.ToString("yyyy-MM-dd"), f => (double)f.PredictedValue)
}
};
}
private List<ChartData> GenerateAnomalyCharts(List<(int Index, bool IsAnomaly, float Score)> anomalies)
{
return new List<ChartData>
{
new ChartData
{
Type = "scatter",
Title = "异常检测结果",
Data = anomalies.Where(a => a.IsAnomaly)
.ToDictionary(a => a.Index.ToString(), a => (double)a.Score)
}
};
}
private List<ChartData> GenerateFeatureImportanceCharts(Dictionary<string, float> featureImportance)
{
return new List<ChartData>
{
new ChartData
{
Type = "bar",
Title = "特征重要性",
Data = featureImportance.ToDictionary(kv => kv.Key, kv => (double)kv.Value)
}
};
}
private List<string> GenerateActionableRecommendations(AnalyticsReport report)
{
return new List<string>
{
"基于用户行为数据优化推荐算法,提高用户满意度",
"根据预测趋势调整库存和资源配置",
"建立异常监控机制,及时发现和处理异常情况",
"重点关注高影响因素,制定针对性的业务策略",
"定期更新模型,确保预测准确性"
};
}
}
// 分析报告
public class AnalyticsReport
{
public DateTime GeneratedAt { get; set; }
public List<ReportSection> Sections { get; set; }
public List<string> Recommendations { get; set; }
}
// 报告章节
public class ReportSection
{
public string Title { get; set; }
public string Content { get; set; }
public List<ChartData> Charts { get; set; }
}
// 图表数据
public class ChartData
{
public string Type { get; set; }
public string Title { get; set; }
public Dictionary<string, double> Data { get; set; }
}
24.6 本章总结
本章深入介绍了C#在人工智能和机器学习领域的应用,涵盖了从基础概念到高级应用的完整知识体系。
核心概念
机器学习基础
- ML.NET框架的使用和配置
- 数据预处理和特征工程
- 回归、分类和聚类模型的实现
- 模型训练、评估和部署
深度学习应用
- TensorFlow.NET集成
- 神经网络构建和训练
- 图像分类和计算机视觉
- 自然语言处理和文本分析
AI服务集成
- Azure认知服务的集成和使用
- OpenAI API的调用和应用
- 语音识别和合成
- 计算机视觉和文本分析
智能应用开发
- 推荐系统的设计和实现
- 预测分析和时间序列预测
- 异常检测和监控
- 智能客服和数据分析平台
高级技术
算法优化
- 模型性能调优
- 特征选择和降维
- 超参数优化
- 模型集成和融合
实时处理
- 流式数据处理
- 在线学习和模型更新
- 实时预测和决策
- 异常实时监控
系统集成
- 微服务架构中的AI组件
- API设计和服务化
- 数据管道和ETL
- 模型版本管理
实际应用
企业级AI应用
- 智能客服系统
- 业务预测分析
- 风险评估和控制
- 个性化推荐
数据驱动决策
- 商业智能分析
- 用户行为分析
- 市场趋势预测
- 运营优化建议
自动化和智能化
- 流程自动化
- 智能监控和告警
- 自适应系统
- 决策支持系统
重要技能
技术能力
- 掌握ML.NET和相关框架
- 理解机器学习算法原理
- 具备数据处理和分析能力
- 熟悉AI服务集成
工程实践
- 模型开发和部署流程
- 性能监控和优化
- 数据质量管理
- 系统架构设计
业务理解
- 问题定义和建模
- 业务价值评估
- 用户需求分析
- 效果评估和改进
通过本章的学习,您已经掌握了使用C#开发人工智能和机器学习应用的核心技能。这些知识将帮助您构建智能化的应用系统,为业务创造更大价值。
下一章我们将探讨C#的未来发展趋势和新兴技术,包括云原生开发、边缘计算等前沿领域的应用。
// 预测结果 public class ForecastResult { public float[] ForecastedValues { get; set; } public float[] LowerBounds { get; set; } public float[] UpperBounds { get; set; } }
// 销售预测数据 public class SalesData { public DateTime Date { get; set; } public float Sales { get; set; } public float Temperature { get; set; } public bool IsHoliday { get; set; } public float Promotion { get; set; } }
// 销售预测 public class SalesPrediction { public float PredictedSales { get; set; } }
// 时间序列预测器 public class TimeSeriesForecaster { private readonly MLContext _mlContext; private ITransformer _model;
public TimeSeriesForecaster()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(IEnumerable<TimeSeriesData> data, int horizon = 7)
{
var dataView = _mlContext.Data.LoadFromEnumerable(data);
var forecastingPipeline = _mlContext.Forecasting.ForecastBySsa(
outputColumnName: "ForecastedValues",
inputColumnName: nameof(TimeSeriesData.Value),
windowSize: 30,
seriesLength: data.Count(),
trainSize: data.Count(),
horizon: horizon,
confidenceLevel: 0.95f,
confidenceLowerBoundColumn: "LowerBounds",
confidenceUpperBoundColumn: "UpperBounds");
_model = forecastingPipeline.Fit(dataView);
}
public ForecastResult Forecast(int horizon = 7)
{
var forecastEngine = _model.CreateTimeSeriesEngine<TimeSeriesData, ForecastResult>(_mlContext);
return forecastEngine.Predict();
}
public List<(DateTime Date, float PredictedValue, float LowerBound, float UpperBound)>
ForecastWithDates(DateTime startDate, int horizon = 7)
{
var forecast = Forecast(horizon);
var results = new List<(DateTime, float, float, float)>();
for (int i = 0; i < horizon; i++)
{
results.Add((
startDate.AddDays(i + 1),
forecast.ForecastedValues[i],
forecast.LowerBounds[i],
forecast.UpperBounds[i]
));
}
return results;
}
}
// 销售预测器 public class SalesPredictor { private readonly MLContext _mlContext; private ITransformer _model;
public SalesPredictor()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(IEnumerable<SalesData> data)
{
var dataView = _mlContext.Data.LoadFromEnumerable(data);
var pipeline = _mlContext.Transforms.CopyColumns(
outputColumnName: "Label",
inputColumnName: nameof(SalesData.Sales))
.Append(_mlContext.Transforms.Categorical.OneHotEncoding(
outputColumnName: "IsHolidayEncoded",
inputColumnName: nameof(SalesData.IsHoliday)))
.Append(_mlContext.Transforms.Concatenate(
"Features",
nameof(SalesData.Temperature),
"IsHolidayEncoded",
nameof(SalesData.Promotion)))
.Append(_mlContext.Regression.Trainers.Sdca());
_model = pipeline.Fit(dataView);
}
public float PredictSales(float temperature, bool isHoliday, float promotion)
{
var predictionEngine = _mlContext.Model.CreatePredictionEngine<SalesData, SalesPrediction>(_model);
var prediction = predictionEngine.Predict(new SalesData
{
Temperature = temperature,
IsHoliday = isHoliday,
Promotion = promotion
});
return prediction.PredictedSales;
}
public Dictionary<string, float> AnalyzeFeatureImportance(IEnumerable<SalesData> testData)
{
var importance = new Dictionary<string, float>();
// 简单的特征重要性分析
var baselinePredictions = new List<float>();
var temperaturePredictions = new List<float>();
var holidayPredictions = new List<float>();
var promotionPredictions = new List<float>();
foreach (var data in testData)
{
baselinePredictions.Add(PredictSales(data.Temperature, data.IsHoliday, data.Promotion));
temperaturePredictions.Add(PredictSales(0, data.IsHoliday, data.Promotion));
holidayPredictions.Add(PredictSales(data.Temperature, false, data.Promotion));
promotionPredictions.Add(PredictSales(data.Temperature, data.IsHoliday, 0));
}
importance["Temperature"] = CalculateVariance(baselinePredictions) - CalculateVariance(temperaturePredictions);
importance["Holiday"] = CalculateVariance(baselinePredictions) - CalculateVariance(holidayPredictions);
importance["Promotion"] = CalculateVariance(baselinePredictions) - CalculateVariance(promotionPredictions);
return importance;
}
private float CalculateVariance(List<float> values)
{
var mean = values.Average();
return values.Sum(v => (v - mean) * (v - mean)) / values.Count;
}
}
### 异常检测
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
// 异常检测数据
public class AnomalyData
{
[LoadColumn(0)]
public float Value { get; set; }
}
// 异常检测结果
public class AnomalyPrediction
{
[VectorType(3)]
public double[] Prediction { get; set; }
public bool IsAnomaly => Prediction[0] == 1;
public float AnomalyScore => (float)Prediction[1];
public float Magnitude => (float)Prediction[2];
}
// 网络流量数据
public class NetworkTrafficData
{
public DateTime Timestamp { get; set; }
public float BytesPerSecond { get; set; }
public float PacketsPerSecond { get; set; }
public float ConnectionCount { get; set; }
public float ErrorRate { get; set; }
}
// 异常检测器
public class AnomalyDetector
{
private readonly MLContext _mlContext;
private ITransformer _model;
public AnomalyDetector()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(IEnumerable<AnomalyData> data)
{
var dataView = _mlContext.Data.LoadFromEnumerable(data);
var pipeline = _mlContext.AnomalyDetection.Trainers.RandomizedPca(
featureColumnName: nameof(AnomalyData.Value),
rank: 1,
ensureZeroMean: true);
_model = pipeline.Fit(dataView);
}
public AnomalyPrediction DetectAnomaly(float value)
{
var predictionEngine = _mlContext.Model.CreatePredictionEngine<AnomalyData, AnomalyPrediction>(_model);
return predictionEngine.Predict(new AnomalyData { Value = value });
}
public List<(int Index, bool IsAnomaly, float Score)> DetectAnomalies(IEnumerable<float> values)
{
var results = new List<(int, bool, float)>();
var index = 0;
foreach (var value in values)
{
var prediction = DetectAnomaly(value);
results.Add((index, prediction.IsAnomaly, prediction.AnomalyScore));
index++;
}
return results;
}
}
// 网络异常检测器
public class NetworkAnomalyDetector
{
private readonly Dictionary<string, (double Mean, double StdDev)> _baselineStats;
private readonly double _threshold;
public NetworkAnomalyDetector(double threshold = 2.0)
{
_baselineStats = new Dictionary<string, (double, double)>();
_threshold = threshold;
}
public void EstablishBaseline(IEnumerable<NetworkTrafficData> historicalData)
{
var data = historicalData.ToList();
_baselineStats["BytesPerSecond"] = CalculateStats(data.Select(d => (double)d.BytesPerSecond));
_baselineStats["PacketsPerSecond"] = CalculateStats(data.Select(d => (double)d.PacketsPerSecond));
_baselineStats["ConnectionCount"] = CalculateStats(data.Select(d => (double)d.ConnectionCount));
_baselineStats["ErrorRate"] = CalculateStats(data.Select(d => (double)d.ErrorRate));
}
public NetworkAnomalyResult DetectAnomaly(NetworkTrafficData currentData)
{
var anomalies = new List<string>();
var scores = new Dictionary<string, double>();
// 检查每个指标
CheckMetric("BytesPerSecond", currentData.BytesPerSecond, anomalies, scores);
CheckMetric("PacketsPerSecond", currentData.PacketsPerSecond, anomalies, scores);
CheckMetric("ConnectionCount", currentData.ConnectionCount, anomalies, scores);
CheckMetric("ErrorRate", currentData.ErrorRate, anomalies, scores);
var maxScore = scores.Values.DefaultIfEmpty(0).Max();
var isAnomaly = anomalies.Any() || maxScore > _threshold;
return new NetworkAnomalyResult
{
IsAnomaly = isAnomaly,
AnomalyScore = maxScore,
AnomalousMetrics = anomalies,
MetricScores = scores,
Timestamp = currentData.Timestamp,
Severity = DetermineSeverity(maxScore)
};
}
private void CheckMetric(string metricName, float value, List<string> anomalies, Dictionary<string, double> scores)
{
if (!_baselineStats.ContainsKey(metricName)) return;
var (mean, stdDev) = _baselineStats[metricName];
var zScore = Math.Abs((value - mean) / stdDev);
scores[metricName] = zScore;
if (zScore > _threshold)
{
anomalies.Add(metricName);
}
}
private (double Mean, double StdDev) CalculateStats(IEnumerable<double> values)
{
var valueList = values.ToList();
var mean = valueList.Average();
var variance = valueList.Sum(v => Math.Pow(v - mean, 2)) / valueList.Count;
var stdDev = Math.Sqrt(variance);
return (mean, stdDev);
}
private string DetermineSeverity(double score)
{
if (score > 4.0) return "Critical";
if (score > 3.0) return "High";
if (score > 2.0) return "Medium";
return "Low";
}
}
// 网络异常检测结果
public class NetworkAnomalyResult
{
public bool IsAnomaly { get; set; }
public double AnomalyScore { get; set; }
public List<string> AnomalousMetrics { get; set; }
public Dictionary<string, double> MetricScores { get; set; }
public DateTime Timestamp { get; set; }
public string Severity { get; set; }
}
// 实时异常监控器
public class RealTimeAnomalyMonitor
{
private readonly NetworkAnomalyDetector _detector;
private readonly Queue<NetworkTrafficData> _dataWindow;
private readonly int _windowSize;
private readonly List<NetworkAnomalyResult> _recentAnomalies;
public event Action<NetworkAnomalyResult> AnomalyDetected;
public RealTimeAnomalyMonitor(int windowSize = 100)
{
_detector = new NetworkAnomalyDetector();
_dataWindow = new Queue<NetworkTrafficData>();
_windowSize = windowSize;
_recentAnomalies = new List<NetworkAnomalyResult>();
}
public void ProcessDataPoint(NetworkTrafficData data)
{
_dataWindow.Enqueue(data);
if (_dataWindow.Count > _windowSize)
{
_dataWindow.Dequeue();
}
// 重新建立基线(使用滑动窗口)
if (_dataWindow.Count >= 50)
{
_detector.EstablishBaseline(_dataWindow.Take(_dataWindow.Count - 1));
}
// 检测异常
var result = _detector.DetectAnomaly(data);
if (result.IsAnomaly)
{
_recentAnomalies.Add(result);
AnomalyDetected?.Invoke(result);
// 保持最近异常记录的数量
if (_recentAnomalies.Count > 1000)
{
_recentAnomalies.RemoveAt(0);
}
}
}
public List<NetworkAnomalyResult> GetRecentAnomalies(TimeSpan timeSpan)
{
var cutoffTime = DateTime.Now - timeSpan;
return _recentAnomalies.Where(a => a.Timestamp >= cutoffTime).ToList();
}
public Dictionary<string, int> GetAnomalyStatistics(TimeSpan timeSpan)
{
var recentAnomalies = GetRecentAnomalies(timeSpan);
return new Dictionary<string, int>
{
["Total"] = recentAnomalies.Count,
["Critical"] = recentAnomalies.Count(a => a.Severity == "Critical"),
["High"] = recentAnomalies.Count(a => a.Severity == "High"),
["Medium"] = recentAnomalies.Count(a => a.Severity == "Medium"),
["Low"] = recentAnomalies.Count(a => a.Severity == "Low")
};
}
}
public string ComputerVisionEndpoint { get; set; }
public string TextAnalyticsKey { get; set; }
public string TextAnalyticsEndpoint { get; set; }
public string SpeechKey { get; set; }
public string SpeechRegion { get; set; }
}
// 计算机视觉服务
public class AzureComputerVisionService
{
private readonly ComputerVisionClient _client;
public AzureComputerVisionService(AzureCognitiveServicesConfig config)
{
_client = new ComputerVisionClient(new ApiKeyServiceClientCredentials(config.ComputerVisionKey))
{
Endpoint = config.ComputerVisionEndpoint
};
}
public async Task<ImageAnalysis> AnalyzeImageAsync(string imageUrl)
{
var features = new List<VisualFeatureTypes?>
{
VisualFeatureTypes.Categories,
VisualFeatureTypes.Description,
VisualFeatureTypes.Faces,
VisualFeatureTypes.Objects,
VisualFeatureTypes.Tags
};
return await _client.AnalyzeImageAsync(imageUrl, features);
}
public async Task<ImageAnalysis> AnalyzeImageAsync(Stream imageStream)
{
var features = new List<VisualFeatureTypes?>
{
VisualFeatureTypes.Categories,
VisualFeatureTypes.Description,
VisualFeatureTypes.Faces,
VisualFeatureTypes.Objects,
VisualFeatureTypes.Tags
};
return await _client.AnalyzeImageInStreamAsync(imageStream, features);
}
public async Task<ReadResult> ExtractTextAsync(string imageUrl)
{
var textHeaders = await _client.ReadAsync(imageUrl);
var operationId = textHeaders.OperationLocation.Split('/').Last();
ReadOperationResult results;
do
{
await Task.Delay(1000);
results = await _client.GetReadResultAsync(Guid.Parse(operationId));
}
while (results.Status == OperationStatusCodes.Running ||
results.Status == OperationStatusCodes.NotStarted);
return results.AnalyzeResult;
}
public async Task<string> GenerateImageDescriptionAsync(string imageUrl)
{
var analysis = await AnalyzeImageAsync(imageUrl);
return analysis.Description.Captions.FirstOrDefault()?.Text ?? "无法生成描述";
}
}
// 文本分析服务
public class AzureTextAnalyticsService
{
private readonly TextAnalyticsClient _client;
public AzureTextAnalyticsService(AzureCognitiveServicesConfig config)
{
var credentials = new AzureKeyCredential(config.TextAnalyticsKey);
_client = new TextAnalyticsClient(new Uri(config.TextAnalyticsEndpoint), credentials);
}
public async Task<DocumentSentiment> AnalyzeSentimentAsync(string text)
{
var response = await _client.AnalyzeSentimentAsync(text);
return response.Value;
}
public async Task<CategorizedEntityCollection> RecognizeEntitiesAsync(string text)
{
var response = await _client.RecognizeEntitiesAsync(text);
return response.Value;
}
public async Task<KeyPhraseCollection> ExtractKeyPhrasesAsync(string text)
{
var response = await _client.ExtractKeyPhrasesAsync(text);
return response.Value;
}
public async Task<DetectedLanguage> DetectLanguageAsync(string text)
{
var response = await _client.DetectLanguageAsync(text);
return response.Value;
}
public async Task<string> SummarizeTextAsync(string text, int maxSentences = 3)
{
// 使用关键短语提取来创建摘要
var keyPhrases = await ExtractKeyPhrasesAsync(text);
var sentences = text.Split('.', StringSplitOptions.RemoveEmptyEntries);
var scoredSentences = sentences.Select(sentence => new
{
Sentence = sentence.Trim(),
Score = keyPhrases.Count(kp => sentence.Contains(kp, StringComparison.OrdinalIgnoreCase))
})
.OrderByDescending(s => s.Score)
.Take(maxSentences)
.Select(s => s.Sentence);
return string.Join(". ", scoredSentences) + ".";
}
}
// 语音服务
public class AzureSpeechService
{
private readonly SpeechConfig _speechConfig;
public AzureSpeechService(AzureCognitiveServicesConfig config)
{
_speechConfig = SpeechConfig.FromSubscription(config.SpeechKey, config.SpeechRegion);
}
public async Task<string> RecognizeSpeechAsync(string audioFilePath)
{
using var audioConfig = AudioConfig.FromWavFileInput(audioFilePath);
using var recognizer = new SpeechRecognizer(_speechConfig, audioConfig);
var result = await recognizer.RecognizeOnceAsync();
return result.Reason == ResultReason.RecognizedSpeech
? result.Text
: $"语音识别失败: {result.Reason}";
}
public async Task<bool> SynthesizeSpeechAsync(string text, string outputFilePath)
{
using var audioConfig = AudioConfig.FromWavFileOutput(outputFilePath);
using var synthesizer = new SpeechSynthesizer(_speechConfig, audioConfig);
var result = await synthesizer.SpeakTextAsync(text);
return result.Reason == ResultReason.SynthesizingAudioCompleted;
}
public async Task<string> TranslateTextAsync(string text, string targetLanguage)
{
var translationConfig = SpeechTranslationConfig.FromSubscription(
_speechConfig.SubscriptionKey, _speechConfig.Region);
translationConfig.SpeechRecognitionLanguage = "zh-CN";
translationConfig.AddTargetLanguage(targetLanguage);
using var audioConfig = AudioConfig.FromDefaultMicrophoneInput();
using var translator = new TranslationRecognizer(translationConfig, audioConfig);
var result = await translator.RecognizeOnceAsync();
if (result.Reason == ResultReason.TranslatedSpeech)
{
return result.Translations[targetLanguage];
}
return $"翻译失败: {result.Reason}";
}
}
OpenAI API集成
// 安装NuGet包:OpenAI
using OpenAI_API;
using OpenAI_API.Chat;
using OpenAI_API.Completions;
using OpenAI_API.Images;
using OpenAI_API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
// OpenAI服务配置
public class OpenAIConfig
{
public string ApiKey { get; set; }
public string Organization { get; set; }
}
// OpenAI服务
public class OpenAIService
{
private readonly OpenAIAPI _api;
public OpenAIService(OpenAIConfig config)
{
_api = new OpenAIAPI(config.ApiKey);
if (!string.IsNullOrEmpty(config.Organization))
{
_api.Auth.Organization = config.Organization;
}
}
public async Task<string> GenerateTextAsync(string prompt, int maxTokens = 150)
{
var request = new CompletionRequest
{
Prompt = prompt,
MaxTokens = maxTokens,
Temperature = 0.7,
TopP = 1.0,
FrequencyPenalty = 0.0,
PresencePenalty = 0.0
};
var result = await _api.Completions.CreateCompletionAsync(request);
return result.Completions.FirstOrDefault()?.Text?.Trim() ?? "";
}
public async Task<string> ChatAsync(List<ChatMessage> messages, string model = "gpt-3.5-turbo")
{
var request = new ChatRequest
{
Model = model,
Messages = messages,
Temperature = 0.7,
MaxTokens = 1000
};
var result = await _api.Chat.CreateChatCompletionAsync(request);
return result.Choices.FirstOrDefault()?.Message?.Content ?? "";
}
public async Task<string> GenerateImageAsync(string prompt, string size = "1024x1024")
{
var request = new ImageGenerationRequest
{
Prompt = prompt,
Size = size,
NumImages = 1,
ResponseFormat = ImageResponseFormat.Url
};
var result = await _api.ImageGenerations.CreateImageAsync(request);
return result.Data.FirstOrDefault()?.Url ?? "";
}
public async Task<string> AnalyzeCodeAsync(string code, string language)
{
var prompt = $"请分析以下{language}代码,提供代码质量评估、潜在问题和改进建议:\n\n{code}";
return await GenerateTextAsync(prompt, 500);
}
public async Task<string> TranslateTextAsync(string text, string targetLanguage)
{
var prompt = $"请将以下文本翻译成{targetLanguage}:\n\n{text}";
return await GenerateTextAsync(prompt, 200);
}
public async Task<string> SummarizeTextAsync(string text)
{
var prompt = $"请总结以下文本的主要内容:\n\n{text}";
return await GenerateTextAsync(prompt, 300);
}
}
// 智能聊天机器人
public class IntelligentChatBot
{
private readonly OpenAIService _openAIService;
private readonly List<ChatMessage> _conversationHistory;
private readonly string _systemPrompt;
public IntelligentChatBot(OpenAIService openAIService, string systemPrompt = null)
{
_openAIService = openAIService;
_conversationHistory = new List<ChatMessage>();
_systemPrompt = systemPrompt ?? "你是一个有用的AI助手,请友好、准确地回答用户的问题。";
// 添加系统消息
_conversationHistory.Add(new ChatMessage(ChatMessageRole.System, _systemPrompt));
}
public async Task<string> SendMessageAsync(string userMessage)
{
// 添加用户消息
_conversationHistory.Add(new ChatMessage(ChatMessageRole.User, userMessage));
// 获取AI回复
var response = await _openAIService.ChatAsync(_conversationHistory);
// 添加AI回复到历史记录
_conversationHistory.Add(new ChatMessage(ChatMessageRole.Assistant, response));
// 限制历史记录长度
if (_conversationHistory.Count > 20)
{
// 保留系统消息和最近的对话
var systemMessage = _conversationHistory.First();
var recentMessages = _conversationHistory.Skip(_conversationHistory.Count - 15).ToList();
_conversationHistory.Clear();
_conversationHistory.Add(systemMessage);
_conversationHistory.AddRange(recentMessages);
}
return response;
}
public void ClearHistory()
{
_conversationHistory.Clear();
_conversationHistory.Add(new ChatMessage(ChatMessageRole.System, _systemPrompt));
}
public List<ChatMessage> GetConversationHistory()
{
return _conversationHistory.ToList();
}
}
// AI内容生成器
public class AIContentGenerator
{
private readonly OpenAIService _openAIService;
public AIContentGenerator(OpenAIService openAIService)
{
_openAIService = openAIService;
}
public async Task<string> GenerateBlogPostAsync(string topic, int wordCount = 500)
{
var prompt = $"请写一篇关于'{topic}'的博客文章,大约{wordCount}字。文章应该包含引言、主要内容和结论。";
return await _openAIService.GenerateTextAsync(prompt, wordCount * 2);
}
public async Task<string> GenerateProductDescriptionAsync(string productName, string features)
{
var prompt = $"请为产品'{productName}'写一个吸引人的产品描述。产品特点:{features}";
return await _openAIService.GenerateTextAsync(prompt, 200);
}
public async Task<List<string>> GenerateKeywordsAsync(string content, int count = 10)
{
var prompt = $"请从以下内容中提取{count}个最重要的关键词:\n\n{content}";
var result = await _openAIService.GenerateTextAsync(prompt, 100);
return result.Split(new[] { ',', '\n', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(k => k.Trim())
.Where(k => !string.IsNullOrEmpty(k))
.Take(count)
.ToList();
}
public async Task<string> GenerateEmailAsync(string purpose, string tone, string recipient)
{
var prompt = $"请写一封{tone}的邮件给{recipient},目的是{purpose}。";
return await _openAIService.GenerateTextAsync(prompt, 300);
}
public async Task<string> GenerateCodeAsync(string description, string language)
{
var prompt = $"请用{language}编写代码来实现以下功能:{description}。请包含注释说明。";
return await _openAIService.GenerateTextAsync(prompt, 500);
}
}
- 智能应用开发
- 推荐系统
- 聊天机器人
- 预测分析
- 异常检测
24.4 智能应用开发
推荐系统
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Trainers;
// 用户评分数据
public class UserRating
{
[LoadColumn(0)]
public float UserId { get; set; }
[LoadColumn(1)]
public float ProductId { get; set; }
[LoadColumn(2)]
public float Rating { get; set; }
}
// 推荐预测结果
public class RatingPrediction
{
public float Score { get; set; }
}
// 产品信息
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
public string[] Tags { get; set; }
public double AverageRating { get; set; }
}
// 用户信息
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string[] Preferences { get; set; }
public Dictionary<int, float> Ratings { get; set; }
}
// 推荐结果
public class Recommendation
{
public Product Product { get; set; }
public float PredictedRating { get; set; }
public string Reason { get; set; }
}
// 协同过滤推荐系统
public class CollaborativeFilteringRecommender
{
private readonly MLContext _mlContext;
private ITransformer _model;
private readonly List<Product> _products;
private readonly List<User> _users;
public CollaborativeFilteringRecommender()
{
_mlContext = new MLContext(seed: 0);
_products = new List<Product>();
_users = new List<User>();
}
public void TrainModel(IEnumerable<UserRating> ratings)
{
var dataView = _mlContext.Data.LoadFromEnumerable(ratings);
var options = new MatrixFactorizationTrainer.Options
{
MatrixColumnIndexColumnName = nameof(UserRating.UserId),
MatrixRowIndexColumnName = nameof(UserRating.ProductId),
LabelColumnName = nameof(UserRating.Rating),
NumberOfIterations = 20,
ApproximationRank = 100
};
var trainer = _mlContext.Recommendation().Trainers.MatrixFactorization(options);
_model = trainer.Fit(dataView);
}
public float PredictRating(float userId, float productId)
{
var predictionEngine = _mlContext.Model.CreatePredictionEngine<UserRating, RatingPrediction>(_model);
var prediction = predictionEngine.Predict(new UserRating
{
UserId = userId,
ProductId = productId
});
return prediction.Score;
}
public List<Recommendation> GetRecommendations(int userId, int count = 10)
{
var user = _users.FirstOrDefault(u => u.Id == userId);
if (user == null) return new List<Recommendation>();
var recommendations = new List<Recommendation>();
foreach (var product in _products)
{
// 跳过用户已评分的产品
if (user.Ratings.ContainsKey(product.Id)) continue;
var predictedRating = PredictRating(userId, product.Id);
var reason = GenerateRecommendationReason(user, product);
recommendations.Add(new Recommendation
{
Product = product,
PredictedRating = predictedRating,
Reason = reason
});
}
return recommendations.OrderByDescending(r => r.PredictedRating)
.Take(count)
.ToList();
}
private string GenerateRecommendationReason(User user, Product product)
{
// 基于用户偏好生成推荐理由
var commonPreferences = user.Preferences.Intersect(product.Tags).ToList();
if (commonPreferences.Any())
{
return $"因为您喜欢 {string.Join(", ", commonPreferences)}";
}
if (product.AverageRating > 4.0)
{
return "高评分产品";
}
return "为您推荐";
}
public void AddProduct(Product product)
{
_products.Add(product);
}
public void AddUser(User user)
{
_users.Add(user);
}
}
// 基于内容的推荐系统
public class ContentBasedRecommender
{
private readonly List<Product> _products;
private readonly Dictionary<string, double> _categoryWeights;
public ContentBasedRecommender()
{
_products = new List<Product>();
_categoryWeights = new Dictionary<string, double>();
}
public List<Recommendation> GetRecommendations(User user, int count = 10)
{
var userProfile = BuildUserProfile(user);
var recommendations = new List<Recommendation>();
foreach (var product in _products)
{
if (user.Ratings.ContainsKey(product.Id)) continue;
var similarity = CalculateContentSimilarity(userProfile, product);
recommendations.Add(new Recommendation
{
Product = product,
PredictedRating = (float)similarity * 5, // 转换为1-5评分
Reason = GenerateContentReason(userProfile, product)
});
}
return recommendations.OrderByDescending(r => r.PredictedRating)
.Take(count)
.ToList();
}
private Dictionary<string, double> BuildUserProfile(User user)
{
var profile = new Dictionary<string, double>();
// 基于用户评分的产品构建偏好档案
foreach (var rating in user.Ratings)
{
var product = _products.FirstOrDefault(p => p.Id == rating.Key);
if (product == null) continue;
var weight = rating.Value / 5.0; // 归一化评分
// 更新类别权重
if (profile.ContainsKey(product.Category))
profile[product.Category] += weight;
else
profile[product.Category] = weight;
// 更新标签权重
foreach (var tag in product.Tags)
{
if (profile.ContainsKey(tag))
profile[tag] += weight * 0.5; // 标签权重较低
else
profile[tag] = weight * 0.5;
}
}
return profile;
}
private double CalculateContentSimilarity(Dictionary<string, double> userProfile, Product product)
{
double similarity = 0.0;
// 类别相似度
if (userProfile.ContainsKey(product.Category))
{
similarity += userProfile[product.Category] * 0.6;
}
// 标签相似度
foreach (var tag in product.Tags)
{
if (userProfile.ContainsKey(tag))
{
similarity += userProfile[tag] * 0.4;
}
}
return Math.Min(similarity, 1.0);
}
private string GenerateContentReason(Dictionary<string, double> userProfile, Product product)
{
var reasons = new List<string>();
if (userProfile.ContainsKey(product.Category))
{
reasons.Add($"您喜欢{product.Category}类产品");
}
var matchingTags = product.Tags.Where(tag => userProfile.ContainsKey(tag)).ToList();
if (matchingTags.Any())
{
reasons.Add($"匹配您的兴趣: {string.Join(", ", matchingTags)}");
}
return reasons.Any() ? string.Join("; ", reasons) : "为您推荐";
}
public void AddProduct(Product product)
{
_products.Add(product);
}
}
24.1 机器学习基础
ML.NET框架介绍
ML.NET是微软开源的机器学习框架,专为.NET开发者设计。
// 安装NuGet包:Microsoft.ML
using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
// 数据模型定义
public class HouseData
{
[LoadColumn(0)]
public float Size { get; set; }
[LoadColumn(1)]
public float Bedrooms { get; set; }
[LoadColumn(2)]
public float Bathrooms { get; set; }
[LoadColumn(3)]
public float Age { get; set; }
[LoadColumn(4)]
public float Location { get; set; }
[LoadColumn(5)]
public float Price { get; set; }
}
public class HousePrediction
{
[ColumnName("Score")]
public float Price { get; set; }
}
// 房价预测模型
public class HousePricePredictor
{
private readonly MLContext _mlContext;
private ITransformer _model;
private PredictionEngine<HouseData, HousePrediction> _predictionEngine;
public HousePricePredictor()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(string dataPath)
{
// 加载数据
IDataView dataView = _mlContext.Data.LoadFromTextFile<HouseData>(
path: dataPath,
hasHeader: true,
separatorChar: ',');
// 数据预处理和特征工程
var pipeline = _mlContext.Transforms.Concatenate("Features",
new[] { "Size", "Bedrooms", "Bathrooms", "Age", "Location" })
.Append(_mlContext.Transforms.NormalizeMinMax("Features"))
.Append(_mlContext.Regression.Trainers.Sdca(labelColumnName: "Price", maximumNumberOfIterations: 100));
// 训练模型
_model = pipeline.Fit(dataView);
// 创建预测引擎
_predictionEngine = _mlContext.Model.CreatePredictionEngine<HouseData, HousePrediction>(_model);
Console.WriteLine("模型训练完成");
}
public float PredictPrice(HouseData house)
{
if (_predictionEngine == null)
throw new InvalidOperationException("模型尚未训练");
var prediction = _predictionEngine.Predict(house);
return prediction.Price;
}
public void EvaluateModel(string testDataPath)
{
if (_model == null)
throw new InvalidOperationException("模型尚未训练");
// 加载测试数据
IDataView testDataView = _mlContext.Data.LoadFromTextFile<HouseData>(
path: testDataPath,
hasHeader: true,
separatorChar: ',');
// 进行预测
IDataView predictions = _model.Transform(testDataView);
// 评估模型
var metrics = _mlContext.Regression.Evaluate(predictions, labelColumnName: "Price");
Console.WriteLine($"R²: {metrics.RSquared:0.##}");
Console.WriteLine($"均方根误差: {metrics.RootMeanSquaredError:0.##}");
Console.WriteLine($"平均绝对误差: {metrics.MeanAbsoluteError:0.##}");
}
public void SaveModel(string modelPath)
{
if (_model == null)
throw new InvalidOperationException("模型尚未训练");
_mlContext.Model.Save(_model, null, modelPath);
Console.WriteLine($"模型已保存到: {modelPath}");
}
public void LoadModel(string modelPath)
{
_model = _mlContext.Model.Load(modelPath, out var modelInputSchema);
_predictionEngine = _mlContext.Model.CreatePredictionEngine<HouseData, HousePrediction>(_model);
Console.WriteLine($"模型已从 {modelPath} 加载");
}
}
// 使用示例
public class HousePricePredictionExample
{
public static void RunExample()
{
var predictor = new HousePricePredictor();
// 训练模型
predictor.TrainModel("house_data.csv");
// 评估模型
predictor.EvaluateModel("house_test_data.csv");
// 进行预测
var newHouse = new HouseData
{
Size = 1500,
Bedrooms = 3,
Bathrooms = 2,
Age = 10,
Location = 8.5f
};
var predictedPrice = predictor.PredictPrice(newHouse);
Console.WriteLine($"预测房价: ${predictedPrice:N0}");
// 保存模型
predictor.SaveModel("house_price_model.zip");
}
}
分类模型实现
// 情感分析数据模型
public class SentimentData
{
[LoadColumn(0)]
public string SentimentText { get; set; }
[LoadColumn(1), ColumnName("Label")]
public bool Sentiment { get; set; }
}
public class SentimentPrediction : SentimentData
{
[ColumnName("PredictedLabel")]
public bool Prediction { get; set; }
public float Probability { get; set; }
public float Score { get; set; }
}
// 情感分析分类器
public class SentimentAnalyzer
{
private readonly MLContext _mlContext;
private ITransformer _model;
private PredictionEngine<SentimentData, SentimentPrediction> _predictionEngine;
public SentimentAnalyzer()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(string dataPath)
{
// 加载数据
IDataView dataView = _mlContext.Data.LoadFromTextFile<SentimentData>(
path: dataPath,
hasHeader: false,
separatorChar: '\t');
// 数据分割
var splitDataView = _mlContext.Data.TrainTestSplit(dataView, testFraction: 0.2);
// 构建训练管道
var estimator = _mlContext.Transforms.Text.FeaturizeText(
outputColumnName: "Features",
inputColumnName: nameof(SentimentData.SentimentText))
.Append(_mlContext.BinaryClassification.Trainers.SdcaLogisticRegression(
labelColumnName: "Label",
featureColumnName: "Features"));
// 训练模型
_model = estimator.Fit(splitDataView.TrainSet);
// 评估模型
var predictions = _model.Transform(splitDataView.TestSet);
var metrics = _mlContext.BinaryClassification.Evaluate(
data: predictions,
labelColumnName: "Label",
scoreColumnName: "Score");
Console.WriteLine($"准确率: {metrics.Accuracy:P2}");
Console.WriteLine($"AUC: {metrics.AreaUnderRocCurve:P2}");
Console.WriteLine($"F1分数: {metrics.F1Score:P2}");
// 创建预测引擎
_predictionEngine = _mlContext.Model.CreatePredictionEngine<SentimentData, SentimentPrediction>(_model);
}
public SentimentPrediction PredictSentiment(string text)
{
if (_predictionEngine == null)
throw new InvalidOperationException("模型尚未训练");
var sampleStatement = new SentimentData { SentimentText = text };
return _predictionEngine.Predict(sampleStatement);
}
public List<SentimentPrediction> BatchPredict(IEnumerable<string> texts)
{
if (_model == null)
throw new InvalidOperationException("模型尚未训练");
var sentimentData = texts.Select(text => new SentimentData { SentimentText = text });
var dataView = _mlContext.Data.LoadFromEnumerable(sentimentData);
var predictions = _model.Transform(dataView);
return _mlContext.Data.CreateEnumerable<SentimentPrediction>(predictions, reuseRowObject: false).ToList();
}
}
// 使用示例
public class SentimentAnalysisExample
{
public static void RunExample()
{
var analyzer = new SentimentAnalyzer();
// 训练模型
analyzer.TrainModel("sentiment_data.txt");
// 单个预测
var result = analyzer.PredictSentiment("这个产品真的很棒!");
Console.WriteLine($"文本: 这个产品真的很棒!");
Console.WriteLine($"情感: {(result.Prediction ? "积极" : "消极")}");
Console.WriteLine($"概率: {result.Probability:P2}");
// 批量预测
var texts = new[]
{
"我喜欢这个电影",
"这个服务太糟糕了",
"还不错的体验",
"完全不推荐"
};
var batchResults = analyzer.BatchPredict(texts);
foreach (var (text, prediction) in texts.Zip(batchResults))
{
Console.WriteLine($"文本: {text} => {(prediction.Prediction ? "积极" : "消极")} ({prediction.Probability:P2})");
}
}
}
聚类分析
// 客户数据模型
public class CustomerData
{
[LoadColumn(0)]
public float Age { get; set; }
[LoadColumn(1)]
public float Income { get; set; }
[LoadColumn(2)]
public float SpendingScore { get; set; }
[LoadColumn(3)]
public float PurchaseFrequency { get; set; }
}
public class CustomerClusterPrediction
{
[ColumnName("PredictedLabel")]
public uint PredictedClusterId { get; set; }
[ColumnName("Score")]
public float[] Distances { get; set; }
}
// 客户聚类分析器
public class CustomerClusterAnalyzer
{
private readonly MLContext _mlContext;
private ITransformer _model;
private PredictionEngine<CustomerData, CustomerClusterPrediction> _predictionEngine;
public CustomerClusterAnalyzer()
{
_mlContext = new MLContext(seed: 0);
}
public void TrainModel(string dataPath, int numberOfClusters = 4)
{
// 加载数据
IDataView dataView = _mlContext.Data.LoadFromTextFile<CustomerData>(
path: dataPath,
hasHeader: true,
separatorChar: ',');
// 特征工程
string featuresColumnName = "Features";
var pipeline = _mlContext.Transforms.Concatenate(featuresColumnName,
"Age", "Income", "SpendingScore", "PurchaseFrequency")
.Append(_mlContext.Transforms.NormalizeMinMax(featuresColumnName))
.Append(_mlContext.Clustering.Trainers.KMeans(
featuresColumnName,
numberOfClusters: numberOfClusters));
// 训练模型
_model = pipeline.Fit(dataView);
// 创建预测引擎
_predictionEngine = _mlContext.Model.CreatePredictionEngine<CustomerData, CustomerClusterPrediction>(_model);
// 评估聚类质量
var predictions = _model.Transform(dataView);
var metrics = _mlContext.Clustering.Evaluate(
predictions,
scoreColumnName: "Score",
featureColumnName: featuresColumnName);
Console.WriteLine($"平均距离: {metrics.AverageDistance:F2}");
Console.WriteLine($"戴维斯-鲍尔丁指数: {metrics.DaviesBouldinIndex:F2}");
// 分析聚类结果
AnalyzeClusters(dataView, numberOfClusters);
}
private void AnalyzeClusters(IDataView dataView, int numberOfClusters)
{
var predictions = _model.Transform(dataView);
var clusteredData = _mlContext.Data.CreateEnumerable<CustomerClusterData>(predictions, reuseRowObject: false);
for (uint clusterId = 0; clusterId < numberOfClusters; clusterId++)
{
var clusterData = clusteredData.Where(x => x.PredictedClusterId == clusterId).ToList();
if (clusterData.Any())
{
Console.WriteLine($"\n聚类 {clusterId} (共 {clusterData.Count} 个客户):");
Console.WriteLine($" 平均年龄: {clusterData.Average(x => x.Age):F1}");
Console.WriteLine($" 平均收入: {clusterData.Average(x => x.Income):F0}");
Console.WriteLine($" 平均消费评分: {clusterData.Average(x => x.SpendingScore):F1}");
Console.WriteLine($" 平均购买频率: {clusterData.Average(x => x.PurchaseFrequency):F1}");
}
}
}
public CustomerClusterPrediction PredictCluster(CustomerData customer)
{
if (_predictionEngine == null)
throw new InvalidOperationException("模型尚未训练");
return _predictionEngine.Predict(customer);
}
public Dictionary<uint, List<CustomerData>> SegmentCustomers(IEnumerable<CustomerData> customers)
{
if (_model == null)
throw new InvalidOperationException("模型尚未训练");
var dataView = _mlContext.Data.LoadFromEnumerable(customers);
var predictions = _model.Transform(dataView);
var results = _mlContext.Data.CreateEnumerable<CustomerClusterData>(predictions, reuseRowObject: false);
return results.GroupBy(x => x.PredictedClusterId)
.ToDictionary(
g => g.Key,
g => g.Select(x => new CustomerData
{
Age = x.Age,
Income = x.Income,
SpendingScore = x.SpendingScore,
PurchaseFrequency = x.PurchaseFrequency
}).ToList());
}
}
public class CustomerClusterData : CustomerData
{
public uint PredictedClusterId { get; set; }
}
// 使用示例
public class CustomerClusteringExample
{
public static void RunExample()
{
var analyzer = new CustomerClusterAnalyzer();
// 训练模型
analyzer.TrainModel("customer_data.csv", numberOfClusters: 4);
// 预测新客户的聚类
var newCustomer = new CustomerData
{
Age = 35,
Income = 75000,
SpendingScore = 80,
PurchaseFrequency = 12
};
var clusterPrediction = analyzer.PredictCluster(newCustomer);
Console.WriteLine($"\n新客户属于聚类: {clusterPrediction.PredictedClusterId}");
// 客户分群
var customers = GenerateSampleCustomers();
var segments = analyzer.SegmentCustomers(customers);
foreach (var (clusterId, customerList) in segments)
{
Console.WriteLine($"\n聚类 {clusterId}: {customerList.Count} 个客户");
}
}
private static List<CustomerData> GenerateSampleCustomers()
{
var random = new Random(42);
var customers = new List<CustomerData>();
for (int i = 0; i < 100; i++)
{
customers.Add(new CustomerData
{
Age = random.Next(18, 70),
Income = random.Next(20000, 150000),
SpendingScore = random.Next(1, 100),
PurchaseFrequency = random.Next(1, 50)
});
}
return customers;
}
}
24.2 深度学习应用
自然语言处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Transforms.Text;
// 文本数据模型
public class TextData
{
public string Text { get; set; }
public string Label { get; set; }
}
// 文本特征
public class TextFeatures
{
[VectorType(10000)]
public float[] Features { get; set; }
}
// 文本预处理器
public class TextPreprocessor
{
private readonly MLContext _mlContext;
private ITransformer _preprocessor;
public TextPreprocessor()
{
_mlContext = new MLContext(seed: 0);
}
public void BuildPreprocessor(IEnumerable<TextData> data)
{
var dataView = _mlContext.Data.LoadFromEnumerable(data);
var pipeline = _mlContext.Transforms.Text.FeaturizeText(
outputColumnName: "Features",
inputColumnName: nameof(TextData.Text))
.Append(_mlContext.Transforms.NormalizeMinMax("Features"));
_preprocessor = pipeline.Fit(dataView);
}
public TextFeatures[] PreprocessTexts(string[] texts)
{
var textData = texts.Select(t => new TextData { Text = t }).ToArray();
var dataView = _mlContext.Data.LoadFromEnumerable(textData);
var transformedData = _preprocessor.Transform(dataView);
return _mlContext.Data.CreateEnumerable<TextFeatures>(
transformedData, reuseRowObject: false).ToArray();
}
public string CleanText(string text)
{
// 移除HTML标签
text = Regex.Replace(text, @"<[^>]+>", "");
// 移除特殊字符
text = Regex.Replace(text, @"[^\w\s]", "");
// 转换为小写
text = text.ToLower();
// 移除多余空格
text = Regex.Replace(text, @"\s+", " ").Trim();
return text;
}
public string[] Tokenize(string text)
{
return text.Split(' ', StringSplitOptions.RemoveEmptyEntries);
}
public Dictionary<string, int> GetWordFrequency(string[] tokens)
{
return tokens.GroupBy(t => t)
.ToDictionary(g => g.Key, g => g.Count());
}
}
// 实体数据模型
public class EntityData
{
public string Text { get; set; }
public string[] Entities { get; set; }
public string[] Labels { get; set; }
}
// 实体预测结果
public class EntityPrediction
{
public string Entity { get; set; }
public string Label { get; set; }
public float Confidence { get; set; }
public int StartPosition { get; set; }
public int EndPosition { get; set; }
}
// 命名实体识别器
public class NamedEntityRecognizer
{
private readonly MLContext _mlContext;
private ITransformer _model;
private readonly Dictionary<string, string[]> _entityPatterns;
public NamedEntityRecognizer()
{
_mlContext = new MLContext(seed: 0);
InitializeEntityPatterns();
}
private void InitializeEntityPatterns()
{
_entityPatterns = new Dictionary<string, string[]>
{
["PERSON"] = new[] { @"\b[A-Z][a-z]+ [A-Z][a-z]+\b" },
["EMAIL"] = new[] { @"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b" },
["PHONE"] = new[] { @"\b\d{3}-\d{3}-\d{4}\b", @"\b\(\d{3}\)\s*\d{3}-\d{4}\b" },
["DATE"] = new[] { @"\b\d{1,2}/\d{1,2}/\d{4}\b", @"\b\d{4}-\d{2}-\d{2}\b" },
["URL"] = new[] { @"https?://[^\s]+" },
["MONEY"] = new[] { @"\$\d+(?:,\d{3})*(?:\.\d{2})?" }
};
}
public void TrainModel(IEnumerable<EntityData> trainingData)
{
var dataView = _mlContext.Data.LoadFromEnumerable(trainingData);
var pipeline = _mlContext.Transforms.Text.FeaturizeText(
outputColumnName: "Features",
inputColumnName: nameof(EntityData.Text))
.Append(_mlContext.MulticlassClassification.Trainers
.SdcaMaximumEntropy(labelColumnName: "Label", featureColumnName: "Features"));
_model = pipeline.Fit(dataView);
}
public List<EntityPrediction> RecognizeEntities(string text)
{
var entities = new List<EntityPrediction>();
// 使用正则表达式识别实体
foreach (var entityType in _entityPatterns.Keys)
{
foreach (var pattern in _entityPatterns[entityType])
{
var matches = Regex.Matches(text, pattern);
foreach (Match match in matches)
{
entities.Add(new EntityPrediction
{
Entity = match.Value,
Label = entityType,
Confidence = 0.9f, // 基于规则的置信度
StartPosition = match.Index,
EndPosition = match.Index + match.Length
});
}
}
}
return entities.OrderBy(e => e.StartPosition).ToList();
}
public string AnnotateText(string text)
{
var entities = RecognizeEntities(text);
var annotatedText = text;
// 从后往前替换,避免位置偏移
foreach (var entity in entities.OrderByDescending(e => e.StartPosition))
{
var annotation = $"[{entity.Entity}]({entity.Label})";
annotatedText = annotatedText.Remove(entity.StartPosition,
entity.EndPosition - entity.StartPosition)
.Insert(entity.StartPosition, annotation);
}
return annotatedText;
}
public Dictionary<string, int> GetEntityStatistics(string text)
{
var entities = RecognizeEntities(text);
return entities.GroupBy(e => e.Label)
.ToDictionary(g => g.Key, g => g.Count());
}
}
TensorFlow.NET集成
// 安装NuGet包:TensorFlow.NET, TensorFlow.Keras
using Tensorflow;
using Tensorflow.Keras;
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine;
using NumSharp;
using System;
using System.Linq;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
// 图像分类神经网络
public class ImageClassificationModel
{
private IModel _model;
private readonly int _imageSize = 224;
private readonly int _numClasses;
private readonly string[] _classNames;
public ImageClassificationModel(string[] classNames)
{
_classNames = classNames ?? throw new ArgumentNullException(nameof(classNames));
_numClasses = classNames.Length;
// 启用eager execution
tf.enable_eager_execution();
}
public void BuildModel()
{
// 构建卷积神经网络
var inputs = keras.Input(shape: (_imageSize, _imageSize, 3));
// 卷积层
var x = keras.layers.Conv2D(32, (3, 3), activation: "relu").Apply(inputs);
x = keras.layers.MaxPooling2D((2, 2)).Apply(x);
x = keras.layers.Conv2D(64, (3, 3), activation: "relu").Apply(x);
x = keras.layers.MaxPooling2D((2, 2)).Apply(x);
x = keras.layers.Conv2D(128, (3, 3), activation: "relu").Apply(x);
x = keras.layers.MaxPooling2D((2, 2)).Apply(x);
// 全连接层
x = keras.layers.Flatten().Apply(x);
x = keras.layers.Dense(512, activation: "relu").Apply(x);
x = keras.layers.Dropout(0.5f).Apply(x);
var outputs = keras.layers.Dense(_numClasses, activation: "softmax").Apply(x);
_model = keras.Model(inputs, outputs);
// 编译模型
_model.compile(
optimizer: keras.optimizers.Adam(learning_rate: 0.001f),
loss: keras.losses.CategoricalCrossentropy(),
metrics: new[] { "accuracy" });
Console.WriteLine("模型构建完成");
_model.summary();
}
public void TrainModel(NDArray trainImages, NDArray trainLabels,
NDArray validationImages, NDArray validationLabels,
int epochs = 10, int batchSize = 32)
{
if (_model == null)
throw new InvalidOperationException("模型尚未构建");
// 数据预处理
trainImages = trainImages / 255.0f;
validationImages = validationImages / 255.0f;
// 训练模型
var history = _model.fit(
trainImages, trainLabels,
batch_size: batchSize,
epochs: epochs,
validation_data: (validationImages, validationLabels),
verbose: 1);
Console.WriteLine("模型训练完成");
// 显示训练历史
var finalAccuracy = history.history["accuracy"].Last();
var finalValAccuracy = history.history["val_accuracy"].Last();
Console.WriteLine($"最终训练准确率: {finalAccuracy:P2}");
Console.WriteLine($"最终验证准确率: {finalValAccuracy:P2}");
}
public (string className, float confidence) PredictImage(NDArray image)
{
if (_model == null)
throw new InvalidOperationException("模型尚未构建");
// 预处理图像
image = image / 255.0f;
if (image.ndim == 3)
image = np.expand_dims(image, axis: 0);
// 进行预测
var predictions = _model.predict(image);
var predictedClassIndex = np.argmax(predictions[0]).numpy();
var confidence = predictions[0][predictedClassIndex].numpy();
return (_classNames[predictedClassIndex], confidence);
}
public void SaveModel(string modelPath)
{
if (_model == null)
throw new InvalidOperationException("模型尚未构建");
_model.save(modelPath);
Console.WriteLine($"模型已保存到: {modelPath}");
}
public void LoadModel(string modelPath)
{
_model = keras.models.load_model(modelPath);
Console.WriteLine($"模型已从 {modelPath} 加载");
}
}
// 文本生成RNN模型
public class TextGenerationModel
{
private IModel _model;
private Dictionary<char, int> _charToIndex;
private Dictionary<int, char> _indexToChar;
private int _sequenceLength;
private int _vocabSize;
public void PrepareData(string text, int sequenceLength = 100)
{
_sequenceLength = sequenceLength;
// 构建字符映射
var uniqueChars = text.Distinct().OrderBy(c => c).ToArray();
_vocabSize = uniqueChars.Length;
_charToIndex = uniqueChars.Select((c, i) => new { c, i })
.ToDictionary(x => x.c, x => x.i);
_indexToChar = _charToIndex.ToDictionary(kvp => kvp.Value, kvp => kvp.Key);
Console.WriteLine($"词汇表大小: {_vocabSize}");
Console.WriteLine($"文本长度: {text.Length}");
}
public void BuildModel(int rnnUnits = 256)
{
if (_vocabSize == 0)
throw new InvalidOperationException("请先准备数据");
var inputs = keras.Input(shape: (_sequenceLength,));
// 嵌入层
var x = keras.layers.Embedding(_vocabSize, 256).Apply(inputs);
// LSTM层
x = keras.layers.LSTM(rnnUnits, return_sequences: true).Apply(x);
x = keras.layers.Dropout(0.2f).Apply(x);
x = keras.layers.LSTM(rnnUnits, return_sequences: true).Apply(x);
x = keras.layers.Dropout(0.2f).Apply(x);
// 输出层
var outputs = keras.layers.Dense(_vocabSize, activation: "softmax").Apply(x);
_model = keras.Model(inputs, outputs);
// 编译模型
_model.compile(
optimizer: keras.optimizers.Adam(learning_rate: 0.001f),
loss: keras.losses.SparseCategoricalCrossentropy(),
metrics: new[] { "accuracy" });
Console.WriteLine("文本生成模型构建完成");
_model.summary();
}
public (NDArray X, NDArray y) PrepareTrainingData(string text)
{
var sequences = new List<int[]>();
var nextChars = new List<int>();
for (int i = 0; i <= text.Length - _sequenceLength - 1; i++)
{
var sequence = text.Substring(i, _sequenceLength)
.Select(c => _charToIndex[c]).ToArray();
var nextChar = _charToIndex[text[i + _sequenceLength]];
sequences.Add(sequence);
nextChars.Add(nextChar);
}
var X = np.array(sequences.ToArray());
var y = np.array(nextChars.ToArray());
Console.WriteLine($"训练序列数量: {sequences.Count}");
return (X, y);
}
public void TrainModel(NDArray X, NDArray y, int epochs = 10, int batchSize = 64)
{
if (_model == null)
throw new InvalidOperationException("模型尚未构建");
var history = _model.fit(
X, y,
batch_size: batchSize,
epochs: epochs,
validation_split: 0.1f,
verbose: 1);
Console.WriteLine("文本生成模型训练完成");
}
public string GenerateText(string seedText, int length = 500, float temperature = 1.0f)
{
if (_model == null)
throw new InvalidOperationException("模型尚未构建");
var generated = seedText;
var currentSequence = seedText.TakeLast(_sequenceLength)
.Select(c => _charToIndex.GetValueOrDefault(c, 0)).ToArray();
for (int i = 0; i < length; i++)
{
var input = np.array(new[] { currentSequence });
var predictions = _model.predict(input)[0];
var lastPrediction = predictions[predictions.shape[0] - 1];
// 应用温度参数
if (temperature != 1.0f)
{
lastPrediction = lastPrediction / temperature;
var exp = np.exp(lastPrediction);
lastPrediction = exp / np.sum(exp);
}
// 采样下一个字符
var nextCharIndex = SampleFromProbabilities(lastPrediction.numpy());
var nextChar = _indexToChar[nextCharIndex];
generated += nextChar;
// 更新序列
currentSequence = currentSequence.Skip(1).Append(nextCharIndex).ToArray();
}
return generated;
}
private int SampleFromProbabilities(float[] probabilities)
{
var random = new Random();
var randomValue = random.NextDouble();
var cumulativeProb = 0.0;
for (int i = 0; i < probabilities.Length; i++)
{
cumulativeProb += probabilities[i];
if (randomValue <= cumulativeProb)
return i;
}
return probabilities.Length - 1;
}
}
// 使用示例
public class DeepLearningExample
{
public static void RunImageClassificationExample()
{
var classNames = new[] { "猫", "狗", "鸟" };
var model = new ImageClassificationModel(classNames);
// 构建模型
model.BuildModel();
// 这里应该加载实际的图像数据
// var (trainImages, trainLabels, testImages, testLabels) = LoadImageData();
// 训练模型
// model.TrainModel(trainImages, trainLabels, testImages, testLabels, epochs: 20);
// 保存模型
// model.SaveModel("image_classifier.h5");
Console.WriteLine("图像分类示例完成");
}
public static void RunTextGenerationExample()
{
var model = new TextGenerationModel();
// 示例文本(实际应用中应使用更大的文本语料库)
var sampleText = "这是一个用于训练文本生成模型的示例文本。" +
"模型将学习文本的模式并生成类似的内容。" +
"深度学习在自然语言处理领域有着广泛的应用。";
// 准备数据
model.PrepareData(sampleText, sequenceLength: 50);
// 构建模型
model.BuildModel(rnnUnits: 128);
// 准备训练数据
var (X, y) = model.PrepareTrainingData(sampleText);
// 训练模型
model.TrainModel(X, y, epochs: 50, batchSize: 32);
// 生成文本
var generatedText = model.GenerateText("这是", length: 200, temperature: 0.8f);
Console.WriteLine($"生成的文本: {generatedText}");
}
}
计算机视觉应用
// 安装NuGet包:OpenCvSharp4, OpenCvSharp4.runtime.win
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Drawing;
using System.Drawing.Imaging;
// 图像处理和分析
public class ComputerVisionProcessor
{
private readonly CascadeClassifier _faceCascade;
private readonly CascadeClassifier _eyeCascade;
public ComputerVisionProcessor()
{
// 加载预训练的级联分类器
_faceCascade = new CascadeClassifier("haarcascade_frontalface_alt.xml");
_eyeCascade = new CascadeClassifier("haarcascade_eye.xml");
}
public List<Rect> DetectFaces(Mat image)
{
var grayImage = new Mat();
Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);
Cv2.EqualizeHist(grayImage, grayImage);
var faces = _faceCascade.DetectMultiScale(
grayImage,
scaleFactor: 1.1,
minNeighbors: 3,
flags: HaarDetectionTypes.ScaleImage,
minSize: new OpenCvSharp.Size(30, 30));
return faces.ToList();
}
public List<Rect> DetectEyes(Mat image, Rect faceRegion)
{
var grayImage = new Mat();
Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);
var faceROI = new Mat(grayImage, faceRegion);
var eyes = _eyeCascade.DetectMultiScale(faceROI);
// 调整眼睛坐标到原图像坐标系
return eyes.Select(eye => new Rect(
eye.X + faceRegion.X,
eye.Y + faceRegion.Y,
eye.Width,
eye.Height)).ToList();
}
public Mat DrawDetections(Mat image, List<Rect> faces, List<Rect> eyes)
{
var result = image.Clone();
// 绘制人脸框
foreach (var face in faces)
{
Cv2.Rectangle(result, face, Scalar.Green, 2);
Cv2.PutText(result, "Face", new OpenCvSharp.Point(face.X, face.Y - 10),
HersheyFonts.HersheySimplex, 0.7, Scalar.Green, 2);
}
// 绘制眼睛框
foreach (var eye in eyes)
{
Cv2.Rectangle(result, eye, Scalar.Blue, 2);
Cv2.PutText(result, "Eye", new OpenCvSharp.Point(eye.X, eye.Y - 5),
HersheyFonts.HersheySimplex, 0.5, Scalar.Blue, 1);
}
return result;
}
public Mat ApplyImageFilters(Mat image, ImageFilter filter)
{
var result = new Mat();
switch (filter)
{
case ImageFilter.Blur:
Cv2.GaussianBlur(image, result, new OpenCvSharp.Size(15, 15), 0);
break;
case ImageFilter.EdgeDetection:
var gray = new Mat();
Cv2.CvtColor(image, gray, ColorConversionCodes.BGR2GRAY);
Cv2.Canny(gray, result, 100, 200);
Cv2.CvtColor(result, result, ColorConversionCodes.GRAY2BGR);
break;
case ImageFilter.Sharpen:
var kernel = new Mat(3, 3, MatType.CV_32F, new float[]
{
0, -1, 0,
-1, 5, -1,
0, -1, 0
});
Cv2.Filter2D(image, result, -1, kernel);
break;
case ImageFilter.Emboss:
var embossKernel = new Mat(3, 3, MatType.CV_32F, new float[]
{
-2, -1, 0,
-1, 1, 1,
0, 1, 2
});
Cv2.Filter2D(image, result, -1, embossKernel);
break;
default:
result = image.Clone();
break;
}
return result;
}
public ObjectDetectionResult DetectObjects(Mat image, string modelPath, string configPath, string classNamesPath)
{
// 加载YOLO模型
var net = CvDnn.ReadNetFromDarknet(configPath, modelPath);
var classNames = File.ReadAllLines(classNamesPath);
// 预处理图像
var blob = CvDnn.BlobFromImage(image, 1.0 / 255.0, new OpenCvSharp.Size(416, 416), new Scalar(), true, false);
net.SetInput(blob);
// 获取输出层名称
var outputNames = net.GetUnconnectedOutLayersNames();
// 前向传播
var outputs = outputNames.Select(name => new Mat()).ToArray();
net.Forward(outputs, outputNames);
// 解析检测结果
var detections = new List<Detection>();
float confidenceThreshold = 0.5f;
float nmsThreshold = 0.4f;
foreach (var output in outputs)
{
for (int i = 0; i < output.Rows; i++)
{
var scores = output.Row(i).ColRange(5, output.Cols);
Cv2.MinMaxLoc(scores, out _, out double confidence, out _, out OpenCvSharp.Point classIdPoint);
if (confidence > confidenceThreshold)
{
var centerX = (int)(output.At<float>(i, 0) * image.Width);
var centerY = (int)(output.At<float>(i, 1) * image.Height);
var width = (int)(output.At<float>(i, 2) * image.Width);
var height = (int)(output.At<float>(i, 3) * image.Height);
var left = centerX - width / 2;
var top = centerY - height / 2;
detections.Add(new Detection
{
BoundingBox = new Rect(left, top, width, height),
Confidence = (float)confidence,
ClassId = classIdPoint.X,
ClassName = classNames[classIdPoint.X]
});
}
}
}
// 非极大值抑制
var indices = CvDnn.NMSBoxes(
detections.Select(d => d.BoundingBox).ToArray(),
detections.Select(d => d.Confidence).ToArray(),
confidenceThreshold,
nmsThreshold);
var finalDetections = indices.Select(i => detections[i]).ToList();
return new ObjectDetectionResult
{
Detections = finalDetections,
ProcessedImage = DrawObjectDetections(image, finalDetections)
};
}
private Mat DrawObjectDetections(Mat image, List<Detection> detections)
{
var result = image.Clone();
var random = new Random();
foreach (var detection in detections)
{
var color = new Scalar(random.Next(0, 256), random.Next(0, 256), random.Next(0, 256));
Cv2.Rectangle(result, detection.BoundingBox, color, 2);
var label = $"{detection.ClassName}: {detection.Confidence:P1}";
var labelSize = Cv2.GetTextSize(label, HersheyFonts.HersheySimplex, 0.5, 1, out int baseline);
var labelRect = new Rect(
detection.BoundingBox.X,
detection.BoundingBox.Y - labelSize.Height - baseline,
labelSize.Width,
labelSize.Height + baseline);
Cv2.Rectangle(result, labelRect, color, -1);
Cv2.PutText(result, label,
new OpenCvSharp.Point(detection.BoundingBox.X, detection.BoundingBox.Y - baseline),
HersheyFonts.HersheySimplex, 0.5, Scalar.White, 1);
}
return result;
}
public void Dispose()
{
_faceCascade?.Dispose();
_eyeCascade?.Dispose();
}
}
public enum ImageFilter
{
None,
Blur,
EdgeDetection,
Sharpen,
Emboss
}
public class Detection
{
public Rect BoundingBox { get; set; }
public float Confidence { get; set; }
public int ClassId { get; set; }
public string ClassName { get; set; }
}
public class ObjectDetectionResult
{
public List<Detection> Detections { get; set; } = new();
public Mat ProcessedImage { get; set; }
}
// 使用示例
public class ComputerVisionExample
{
public static void RunFaceDetectionExample()
{
using var processor = new ComputerVisionProcessor();
// 加载图像
var image = Cv2.ImRead("sample_image.jpg");
if (image.Empty())
{
Console.WriteLine("无法加载图像");
return;
}
// 检测人脸
var faces = processor.DetectFaces(image);
Console.WriteLine($"检测到 {faces.Count} 个人脸");
// 检测眼睛
var allEyes = new List<Rect>();
foreach (var face in faces)
{
var eyes = processor.DetectEyes(image, face);
allEyes.AddRange(eyes);
}
Console.WriteLine($"检测到 {allEyes.Count} 个眼睛");
// 绘制检测结果
var resultImage = processor.DrawDetections(image, faces, allEyes);
// 保存结果
Cv2.ImWrite("face_detection_result.jpg", resultImage);
// 显示结果
Cv2.ImShow("Face Detection", resultImage);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
public static void RunImageFilterExample()
{
using var processor = new ComputerVisionProcessor();
var image = Cv2.ImRead("sample_image.jpg");
if (image.Empty())
{
Console.WriteLine("无法加载图像");
return;
}
// 应用不同的滤镜
var filters = new[] { ImageFilter.Blur, ImageFilter.EdgeDetection, ImageFilter.Sharpen, ImageFilter.Emboss };
foreach (var filter in filters)
{
var filteredImage = processor.ApplyImageFilters(image, filter);
Cv2.ImWrite($"filtered_{filter}.jpg", filteredImage);
Console.WriteLine($"已应用 {filter} 滤镜");
}
}
public static void RunObjectDetectionExample()
{
using var processor = new ComputerVisionProcessor();
var image = Cv2.ImRead("sample_image.jpg");
if (image.Empty())
{
Console.WriteLine("无法加载图像");
return;
}
// 注意:需要下载YOLO模型文件
var modelPath = "yolov3.weights";
var configPath = "yolov3.cfg";
var classNamesPath = "coco.names";
if (File.Exists(modelPath) && File.Exists(configPath) && File.Exists(classNamesPath))
{
var result = processor.DetectObjects(image, modelPath, configPath, classNamesPath);
Console.WriteLine($"检测到 {result.Detections.Count} 个对象:");
foreach (var detection in result.Detections)
{
Console.WriteLine($" {detection.ClassName}: {detection.Confidence:P1}");
}
Cv2.ImWrite("object_detection_result.jpg", result.ProcessedImage);
Cv2.ImShow("Object Detection", result.ProcessedImage);
Cv2.WaitKey(0);
Cv2.DestroyAllWindows();
}
else
{
Console.WriteLine("YOLO模型文件不存在,请下载相关文件");
}
}
}