private static classificacao ClassifySingleImage(MLContext mlContext, string imagePath, string outputModelLocation, ITransformer model) { var imageData = new ImageData() { ImagePath = imagePath }; // Make prediction function (input = ImageData, output = ImagePrediction) var predictor = mlContext.Model.CreatePredictionEngine <ImageData, ImagePrediction>(model); var prediction = predictor.Predict(imageData); classificacao c = new classificacao(); c.tipo = prediction.PredictedLabelValue; c.porcentagem = prediction.Score.Max(); return(c); }
public List <classificacao> analisar(string path, int deltacor, int instancia) { List <classificacao> listaclassific = new List <classificacao>(); whidthoriginal = new Bitmap(path).Width; heightoriginal = new Bitmap(path).Height; Bitmap bitmap = new Bitmap(new Bitmap(path), 250, 250); List <Point> listap = new List <Point>(); //int deltanominal = 10; int deltanominal = deltacor; Bitmap b = new Bitmap(bitmap); for (int i = 0; i < 250; i++) { for (int i2 = 0; i2 < 250; i2++) { double media = (b.GetPixel(i, i2).R + b.GetPixel(i, i2).G + b.GetPixel(i, i2).B) / 3; int delta = Convert.ToInt32(media - deltanominal); if (delta < -1) { delta = delta * -1; } if (delta <= 30) { listap.Add(new Point(i, i2)); } } } List <pointtipe> lpt = new List <pointtipe>(); List <pointtipe> lptwatch = new List <pointtipe>(); List <Point> listap2 = new List <Point>(); for (int i = 0; i < 250; i += 3) { for (int i2 = 0; i2 < 250; i2 += 3) { if (listap.FirstOrDefault(x => x.X == i && x.Y == i2) != null) { listap2.Add(listap.FirstOrDefault(x => x.X == i && x.Y == i2)); } } } int deltad = 10; foreach (var item in listap2) { lpt.Add(new pointtipe(item, -1)); } int classeatual = 0; for (int i = 0; i < lpt.Count(); i++) { if (lpt[i].classe == -1) { lpt[i].classe = classeatual; lptwatch.Add(lpt[i]); do { for (int i2 = 0; i2 < lptwatch.Count(); i2++) { foreach (var item2 in lpt.Where(x => x.classe == -1 && x.ponto != lptwatch[i2].ponto)) { double distancia; double n1 = (lptwatch[i2].ponto.X - item2.ponto.X) * (lptwatch[i2].ponto.X - item2.ponto.X); double n2 = (lptwatch[i2].ponto.Y - item2.ponto.Y) * (lptwatch[i2].ponto.Y - item2.ponto.Y); distancia = Math.Sqrt(n1 + n2); if (distancia <= deltad && distancia >= deltad - 1) { item2.classe = classeatual; lptwatch.Add(item2); } else if (distancia <= deltad && distancia < deltad - 1) { item2.classe = classeatual; } } lptwatch.Remove(lptwatch.First()); } } while (lptwatch.Count() > 0); classeatual++; } } Bitmap b2 = new Bitmap(250, 250); List <pointtipe> listapontosreais = new List <pointtipe>(); foreach (var item in lpt) { if (lpt.Where(x => x.classe == item.classe).ToList().Count() > 7) { listapontosreais.Add(item); } } List <List <pointtipe> > llp = new List <List <pointtipe> >(); for (int i = 0; i < lpt.ToList().Count(); i++) { if (listapontosreais.Where(x => x.classe == i).ToList().Count() > 0) { llp.Add(listapontosreais.Where(x => x.classe == i).ToList()); } } int direc = 0; do { try { Directory.CreateDirectory($"imagensgrid{instancia}"); listaclassific = new List <classificacao>(); int n = 0; foreach (var item in llp) { pointtipe ltb = item.OrderBy(z => z.ponto.X).First(); ltb.ponto = new Point(ltb.ponto.X, item.OrderBy(z => z.ponto.Y).First().ponto.Y); pointtipe rbb = item.OrderByDescending(z => z.ponto.X).First(); rbb.ponto = new Point(rbb.ponto.X, item.OrderByDescending(z => z.ponto.Y).First().ponto.Y); if (ltb.ponto.X != rbb.ponto.X && ltb.ponto.Y != rbb.ponto.Y) { Bitmap bitb = new Bitmap((rbb.ponto.X - ltb.ponto.X), (rbb.ponto.Y - ltb.ponto.Y)); for (int i = ltb.ponto.X; i < rbb.ponto.X; i++) { for (int i2 = ltb.ponto.Y; i2 < rbb.ponto.Y; i2++) { bitb.SetPixel(i - ltb.ponto.X, i2 - ltb.ponto.Y, bitmap.GetPixel(i, i2)); } } bitb.Save($"imagensgrid{instancia}/imgdetect{n}.png"); classificacao c = new classificacao(); double mx = (double)whidthoriginal / (double)250; double my = (double)heightoriginal / (double)250; c.x = Convert.ToInt32(Math.Round((double)ltb.ponto.X * mx)); c.y = Convert.ToInt32(Math.Round((double)ltb.ponto.Y * my)); c.width = Convert.ToInt32(Math.Round((double)(rbb.ponto.X - ltb.ponto.X) * mx)); c.height = Convert.ToInt32(Math.Round((double)(rbb.ponto.Y - ltb.ponto.Y) * my)); c.path = $"imagensgrid{instancia}/imgdetect{n}.png"; listaclassific.Add(c); n++; } } break; } catch (Exception) { } } while (true); foreach (var item in listaclassific) { classificacao s = ClassifySingleImage(mlContext, Path.GetFullPath(item.path), _outputImageClassifierZip, model); item.porcentagem = s.porcentagem; item.tipo = s.tipo; } return(listaclassific); }