public static void Main() { var assetsRelativePath = @"assets"; string assetsPath = GetAbsolutePath(assetsRelativePath); var tagsTsv = Path.Combine(assetsPath, "images", "tags.tsv"); var modelFilePath = Path.Combine(assetsPath, "resnet152v2", "resnet152v2.onnx"); var labelsTxt = Path.Combine(assetsPath, "resnet152v2", "synset_text.txt"); var imagesFolder = Path.Combine(assetsPath, "images"); var outputFolder = Path.Combine(assetsPath, "images", "output"); // Initialize MLContext MLContext mlContext = new MLContext(); try { // Load Data IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder); IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images); // Create instance of model scorer var modelScorer = new OnnxModelScorer(tagsTsv, imagesFolder, modelFilePath, labelsTxt); // Use model to score data modelScorer.Score(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("========= End of Process..Hit any Key ========"); Console.ReadLine(); }
public JsonResult CheckImage() { string isPersonOnImage = "no"; var file = Request.Files[0]; var currentPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath; var modelFilePath = currentPath + @"\Content\Model\TinyYolo2_model.onnx"; //var modelFilePath = @"C:\Users\Ragnus\Desktop\120819\praca\Thesis\Thesis\Content\Model\TinyYolo2_model.onnx"; var sourceImage = Image.FromStream(file.InputStream); var fileExt = Path.GetExtension(file.FileName); var filename = Guid.NewGuid() + fileExt; // Unikalny identyfikator + rozszerzenie var outputFolder = currentPath + @"\Content\ImageToReturn\"; //var outputFolder = @"C:\Users\Ragnus\Desktop\120819\praca\Thesis\Thesis\Content\ImageToReturn\"; var path = Path.Combine(Server.MapPath(AppConfig.ImagesUserFolder), filename); sourceImage.Save(path); MLContext mlContext = new MLContext(); IEnumerable <ImageNetData> images = ImageNetData.ReadFrom1File(path); IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images); var modelScorer = new OnnxModelScorer(path, modelFilePath, mlContext); IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView); YoloOutputParser parser = new YoloOutputParser(); var boundingBoxes = probabilities .Select(probability => parser.ParseOutputs(probability)) .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); // Draw bounding boxes for detected objects in each of the images for (var i = 0; i < images.Count(); i++) { string imageFileName = images.ElementAt(i).Label; IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); if (detectedObjects.Count > 0 && detectedObjects[0].Label == "person") { isPersonOnImage = "yes"; } DrawBoundingBox(path, outputFolder, imageFileName, detectedObjects); LogDetectedObjects(imageFileName, detectedObjects); } var img = Image.FromFile(outputFolder + filename); System.GC.Collect(); System.GC.WaitForPendingFinalizers(); System.IO.File.Delete(path); img.Dispose(); System.IO.File.Delete(outputFolder + filename); return(Json(new { isPerson = isPersonOnImage } , JsonRequestBehavior.AllowGet)); }
public List <List <Predict> > Pipe(string imgPath, IWebHostEnvironment _env) { string assetsPath = GetAbsolutePath("assets"); var modelFilePath = Path.Combine(assetsPath, "Model", "Model.onnx"); var absolutePath = Path.Combine(_env.WebRootPath, "assets", imgPath); var imagesFolder = Path.Combine(assetsPath, "images"); var outputFolder = Path.Combine(assetsPath, "images", "output"); var predicted = new List <List <Predict> >(); MLContext mlContext = new MLContext(); try { ImageNetData[] img = new ImageNetData[] { new ImageNetData { ImagePath = absolutePath, Label = imgPath } }; IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder); IDataView imageDataView = mlContext.Data.LoadFromEnumerable(img); var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext); // Use model to score data IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView); YoloOutputParser parser = new YoloOutputParser(); var boundingBoxes = probabilities .Select(probability => parser.ParseOutputs(probability)) .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); for (var i = 0; i < images.Count(); i++) { string imageFileName = images.ElementAt(i).Label; IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); //DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects); predicted.Add(LogDetectedObjects(imageFileName, detectedObjects)); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("========= End of Process..Hit any Key ========"); //Console.ReadLine(); return(predicted); }
public List <string> GenerateAnalysis(string guidName) { List <string> stringsToReturn = new List <string>(); var assetsRelativePath = @"../../../../DemoMLNet.Task.ObjectDetection.Console/assets"; string assetsPath = ProgramService.GetAbsolutePath(assetsRelativePath); var modelFilePath = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx"); var imagesFolder = Path.Combine(assetsPath, "images"); var outputFolder = Path.Combine(assetsPath, "images", "output"); MLContext mlContext = new MLContext(); try { IEnumerable <ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder); IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images); var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext); // Use model to score data IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView); YoloOutputParser parser = new YoloOutputParser(); var boundingBoxes = probabilities .Select(probability => parser.ParseOutputs(probability)) .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); for (var i = 0; i < images.Count(); i++) { if (images.ElementAt(i).Label == guidName + ".jpg") { string imageFileName = images.ElementAt(i).Label; IList <YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); ProgramService.DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects); stringsToReturn = ProgramService.LogDetectedObjects(imageFileName, detectedObjects); } //Console.WriteLine("========= End of Process..Hit any Key ========"); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } return(stringsToReturn); }
static void Main(string[] args) { try { var relativePath = @"../../../assets"; var filehelper = new FileHelper(relativePath); var images = ImageNetData.ReadFromFile(filehelper.ImagesFolder); var mlContext = new MLContext(); // Загруэаем данные var imageDataView = mlContext.Data.LoadFromEnumerable(images); // Создаем экземпляр OnnxModelScorer и используем его для оценки загруженных данных var modelScorer = new OnnxModelScorer(filehelper.ImagesFolder, filehelper.ModelFilePath, mlContext); var probabilities = modelScorer.Score(imageDataView); // Создаем экземпляр YoloOutputParser и используем его для обработки выходных данных модели YoloOutputParser parser = new YoloOutputParser(); var boundingBoxes = probabilities.Select(probability => parser.ParseOutputs(probability)) .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); for (var i = 0; i < images.Count(); i++) { string imageFileName = images.ElementAt(i).Label; var detectedObjects = boundingBoxes.ElementAt(i); var imageWithLabels = ImageHelper.DrawBoundingBox(filehelper.ImagesFolder, filehelper.OutputFolder, imageFileName, detectedObjects); if (!Directory.Exists(filehelper.OutputFolder)) { Directory.CreateDirectory(filehelper.OutputFolder); } imageWithLabels.Save(Path.Combine(filehelper.OutputFolder, imageFileName)); LogDetectedObjects(imageFileName, detectedObjects); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("========= End of Process..Hit any Key ========"); Console.ReadLine(); }
public byte[] Classify(string imagePath, string imageName) { var modelFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..\\..\\..", "assets", "Model", "TinyYolo2_model.onnx"); MLContext mlContext = new MLContext(); try { // Load Data ImageNetData data = new ImageNetData() { ImagePath = Path.Combine(imagePath, imageName), Label = imageName }; IEnumerable <ImageNetData> image = new List <ImageNetData>() { data }; IDataView imageDataView = mlContext.Data.LoadFromEnumerable(image); // Create instance of model scorer var modelScorer = new OnnxModelScorer(modelFilePath, mlContext); // Use model to score data IEnumerable <float[]> probabilities = modelScorer.Score(imageDataView); // Post-process model output YoloOutputParser parser = new YoloOutputParser(); var boundingBoxes = probabilities .Select(probability => parser.ParseOutputs(probability)) .Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)).First(); // Draw bounding boxes for detected objects in each of the images IList <YoloBoundingBox> detectedObjects = boundingBoxes; return(DrawBoundingBox(imagePath, imageName, detectedObjects)); } catch (Exception) { return(null); //TODO } }