c# - 由于没有“获取枚举器”,C# 对于每个语句都无法操作“列表”,我该如何修复此代码?
问题描述
我有一些方法和类,但它会引发错误,因为
“列表”不包含“GetEnumerator”的公共实例定义
下面的第一种方法显示了正在使用的 foreach:
public static string Execute(string imagePath)
{
var img = Cv2.ImRead(imagePath);
List<string> result = new List<string>();
List<KeyValuePair<string, double>> featureScores = new List<KeyValuePair<string, double>>();
List features = GetFeatures();
AKAZE kaze = AKAZE.Create();
Mat desc = new Mat();
kaze.DetectAndCompute(img, null, out KeyPoint[] keyPoints, desc);
foreach (var feature in features)
{
var train = new Mat(feature.Rows, feature.Cols, feature.ImgType, feature.ImgData);
var matches = GetMatches(desc, train);
var score = GetScore(matches);
if (score == double.MaxValue)
continue;
featureScores.Add(new KeyValuePair<string, double>(feature.Category, score));
}
return featureScores.Count > 0 ? featureScores.OrderByDescending(x => (x.Value)).ToList()[0].Key : "Unknown";
}
下面的GetFeatures()
方法:
public static List GetFeatures()
{
SharpSerializer serializer = new SharpSerializer(true);
FileStream fs = File.Open(@"D:\NCBC\OpenCV\CustomCV\CustomCV\bin\Debug\features.bin", FileMode.Open);
object obj = serializer.Deserialize(fs);
List result = (List)obj;
fs.Flush();
fs.Close();
fs.Dispose();
return result;
}
以及已存储的信息
private static ExtractedFeature ExtractFeatures(string image)
{
Mat img = Cv2.ImRead(image);
string category = Path.GetFileNameWithoutExtension(image).Split('.')[0];
Console.WriteLine(category);
OpenCvSharp.AKAZE kaze = OpenCvSharp.AKAZE.Create();
Mat desc = new Mat();
kaze.DetectAndCompute(img, null, out KeyPoint[] keyPoints, desc);
ExtractedFeature result = new ExtractedFeature()
{
ImgData = desc.ToBytes(),
Cols = desc.Cols,
Rows = desc.Rows,
ImgType = desc.Type(),
Category = category
};
return result;
}
解决方案
GetFeatures() 返回 aList
不是 aList<T>
您应该给出与列表包含的内容相对应List
的类型,例如 string 或int
推荐阅读
- c# - 在谓词中动态选择参数的通用方法
- javascript - 识别元素中的随机背景梯度函数问题
- java - 等待多个线程完成
- python - python中的“具有多个元素的数组的真值不明确”
- python-3.x - Pandas 0.25.0 无法为 read_sql 创建游标 - 抛出错误
- javascript - 选择日期后模拟输入键(jquery)
- object-detection - 带有 TensorFlow Objection Detection API V2 的多通道输入
- linqpad - 以编程方式确定 linqpad 脚本是否针对生产连接字符串运行
- powershell - 如何创建签名自己的 exe/ps1 文件以与 applocker 一起使用
- visual-studio-2019 - “无法在设置文档缓冲区之前设置数据缓冲区。” 对于 IVsTextLines