public ActionResult StartLearning(LearningDTO simplepassword) { if (simplepassword.Password == "Flamingo007") { const string assetsRelativePath = @"assets"; string path = AITrainer.GetAbsolutePath(assetsRelativePath); AITrainer.Train(Path.Combine(path, "images")); return(new OkResult()); } return(new UnauthorizedResult()); }
public ActionResult GetCategories() { const string assetsRelativePath = @"assets"; string path = AITrainer.GetAbsolutePath(assetsRelativePath); Console.WriteLine(Directory.Exists(path)); Console.WriteLine(path); var names = Directory.GetDirectories(Path.Combine(path, "images")).Select(dir => Path.GetFileName(dir)); dynamic obj = new ExpandoObject(); obj.categories = names; return(new OkObjectResult(obj)); }
public bool testSimulation() { AI ai1 = new AI(0); AI ai2 = new AI(0); BotFightSim fightSim = new BotFightSim(); AI winnerAI = fightSim.getBestAI(ai1, ai2); AITrainer aITrainer = new AITrainer(); aITrainer.train(); return(true); }
public static PredictionDTO Recognize(string base64, string imagepath = null) { const string assetsRelativePath = @"assets"; var assetsPath = AITrainer.GetAbsolutePath(assetsRelativePath); string imageForPrediction; if (imagepath == null) { imageForPrediction = SaveTempImage(base64, assetsPath); } else { imageForPrediction = CopyTestImage(imagepath, assetsPath); } var imageClassifierModelZipFilePath = Path.Combine(assetsPath, "inputs", "model", "imageClassifier.zip"); try { var loadedModel = mlContext.Model.Load(imageClassifierModelZipFilePath, out var modelInputSchema); var predictionEngine = mlContext.Model.CreatePredictionEngine <InMemoryImageData, ImagePrediction>(loadedModel); var imagesToPredict = FileUtils.LoadInMemoryImagesFromDirectory(Path.Combine(assetsPath, "inputs", "toguess"), false); var imageToPredict = imagesToPredict.Where(i => i.ImageFileName == Path.Combine(assetsPath, "inputs", "toguess", imageForPrediction) || i.ImageFileName == imageForPrediction).FirstOrDefault(); if (imageToPredict != null) { var prediction = predictionEngine.Predict(imageToPredict); Console.WriteLine(prediction.PredictedLabel + "-----score---" + prediction.Score.Max()); if (prediction.Score.Max() * 100 > 69) { var maxindext = prediction.Score.ToList().IndexOf(prediction.Score.Max()); File.Delete(Path.Combine(assetsPath, "inputs", "toguess", imageForPrediction)); return(new PredictionDTO { Label = prediction.PredictedLabel, Percentage = prediction.Score.Max() * 100 }); } } File.Delete(Path.Combine(assetsPath, "inputs", "toguess", imageForPrediction)); } catch (Exception e) { } return(null); }
public ActionResult GuessImage(ImageDTO image) { if (image != null && !String.IsNullOrEmpty(image.Base64)) { var result = AIRecognize.Recognize(image.Base64); if (result != null) { const string assetsRelativePath = @"assets"; var assetsPath = AITrainer.GetAbsolutePath(assetsRelativePath); List <AnimalDTO> res = JsonConvert.DeserializeObject <List <AnimalDTO> >(System.IO.File.ReadAllText(Path.Combine(assetsPath, "animals.json"))); return(new OkObjectResult(res.Where(r => r.Name == result.Label).FirstOrDefault())); } else { return(new NotFoundResult()); } } return(new BadRequestObjectResult("This request had no valid image send with it!")); }
public ActionResult AddTrainingMaterial(List <ImageDTO> images) { if (images != null) { const string assetsRelativePath = @"assets"; string path = AITrainer.GetAbsolutePath(assetsRelativePath); foreach (var image in images) { if (!Directory.Exists(Path.Combine(path, "images", image.Category))) { Directory.CreateDirectory(Path.Combine(path, "images", image.Category)); } System.IO.File.WriteAllBytes(Path.Combine(path, "images", image.Category, Guid.NewGuid() + ".png"), Convert.FromBase64String(image.Base64)); } return(new OkResult()); } return(new BadRequestObjectResult("No Images Send")); }