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> >(); var features = IndexImages.GetFeatures(); OpenCvSharp.AKAZE kaze = OpenCvSharp.AKAZE.Create(); KeyPoint[] keyPoints; Mat desc = new Mat(); kaze.DetectAndCompute(img, null, out 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.OrderBy(x => (x.Value)).ToList()[0].Key : "Unknown"); }
static void Main(string[] args) { string command = ""; string folder = ""; if (args.Length == 0) { Console.WriteLine("Use command train or predict along with path to folder as second args."); } if (args.Length == 1) { Console.WriteLine("Missing path as second argument"); } command = args[0].ToLower(); folder = args[1]; DateTime start = DateTime.Now; switch (command) { case "train": IndexImages.Execute(folder); Console.WriteLine("Indexing completed."); Console.WriteLine("Time taken (in Mins): " + (DateTime.Now - start).TotalMinutes); Console.ReadLine(); break; case "predict": string prediction = Predictor.Execute(folder); Console.WriteLine("Prediction: " + prediction); Console.WriteLine("Time taken (in sec): " + (DateTime.Now - start).TotalSeconds); Console.ReadLine(); break; case "test": int match = 0; int total = 0; PredictCategory(folder, "cat", ref match, ref total); PredictCategory(folder, "dog", ref match, ref total); Console.WriteLine("Accuracy score: " + (match == 0 ? 0 : (match * 100) / total)); Console.WriteLine("Time taken (in sec): " + (DateTime.Now - start).TotalSeconds); Console.ReadLine(); break; default: break; } }