public MainMod() { Post("/api/inpaint", async x => { Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Incomming request from " + this.Request.UserHostAddress); if (this.Request.Files.Count() < 2) { Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + $"] Error, {this.Request.Files.Count()} files found"); return("Err"); } Bitmap BitmapImg; var imageFile = this.Request.Files.First(); byte[] ByteImg = new byte[imageFile.Value.Length]; imageFile.Value.Read(ByteImg, 0, (int)imageFile.Value.Length); using (MemoryStream ms = new MemoryStream(ByteImg)) BitmapImg = new Bitmap(ms); Bitmap BitmapMask; var maskFile = this.Request.Files.Last(); byte[] ByteMask = new byte[maskFile.Value.Length]; maskFile.Value.Read(ByteMask, 0, (int)maskFile.Value.Length); using (MemoryStream ms = new MemoryStream(ByteMask)) BitmapMask = new Bitmap(ms); var imageArgb = ConvertToArgbImage(BitmapImg); var markupArgb = ConvertToArgbImage(BitmapMask); var inpainter = new Inpainter(); var settings = new InpaintSettings { //MaxInpaintIterations = 15, MaxInpaintIterations = 5, //less iterations for debugging PatchDistanceCalculator = ImagePatchDistance.Cie76 }; Image finalResult = null; inpainter.IterationFinished += (sender, eventArgs) => { Bitmap iterationResult = eventArgs.InpaintedLabImage .FromLabToRgb() .FromRgbToBitmap(); finalResult = iterationResult; Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] call on inpainter.IterationFinished"); //Debugging }; await Task.Factory.StartNew(() => inpainter.Inpaint(imageArgb, markupArgb, settings)); Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Processing finished"); finalResult.Save(@"..\..\TESTAPP.PNG"); //Debugging Stream stream = new MemoryStream(finalResult.GetBytes()); //return this.Response.FromStream(stream, "image/png"); return(Convert.ToBase64String(finalResult.GetBytes())); //this does the job ¯\_(ツ)_/¯ }); Get("/", _ => View["TestWebsite/index"]); }
private async void OnInpaint(object s, EventArgs e) { btnInpaint.Enabled = false; var imageArgb = ConvertToArgbImage((Bitmap)pbMarkup.Image); var markupArgb = ConvertToArgbImage((Bitmap)pbMarkup.RemoveMarkup); var markupArea = markupArgb.FromArgbToArea2D(); if (markupArea.IsEmpty) { MessageBox.Show("No area to remove!"); } else { var inpainter = new Inpainter(); var settings = new InpaintSettings { MaxInpaintIterations = 15, PatchDistanceCalculator = ImagePatchDistance.Cie76 }; inpainter.IterationFinished += (sender, eventArgs) => { pbMarkup.Image = eventArgs.InpaintedLabImage .FromLabToRgb() .FromRgbToBitmap(); }; await Task.Factory.StartNew(() => inpainter.Inpaint(imageArgb, markupArgb, settings)); MessageBox.Show("Finished"); } btnInpaint.Enabled = true; }
static void Main(string[] args) { var watch = System.Diagnostics.Stopwatch.StartNew(); const string imagesPath = "../../../../images"; const string outputPath = "../../../out"; string resultPath = Path.Combine(outputPath, "result.png"); const string imageName = "t067.jpg"; const string markupName = "m067.png"; var donorNames = new string [0];// { "d0231.png", "d0612.png" }; if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } // open an image and an image with a marked area to inpaint var imageArgb = OpenArgbImage(Path.Combine(imagesPath, imageName)); var markupArgb = OpenArgbImage(Path.Combine(imagesPath, markupName)); var donors = new List <ZsImage>(); if (donorNames.Any()) { donors.AddRange(donorNames.Select(donorName => OpenArgbImage(Path.Combine(imagesPath, donorName)))); } var inpainter = new Inpainter(); inpainter.IterationFinished += (sender, eventArgs) => { Console.WriteLine($"Level:{eventArgs.LevelIndex}\tIteration: {eventArgs.InpaintIteration}"); eventArgs.InpaintedLabImage .FromLabToRgb() .FromRgbToBitmap() .CloneWithScaleTo(imageArgb.Width, imageArgb.Height, InterpolationMode.HighQualityBilinear) .SaveTo(Path.Combine(outputPath, $"r{eventArgs.LevelIndex}_{eventArgs.InpaintIteration}.png"), ImageFormat.Png); }; Console.WriteLine($"Begin processing ..."); var result = inpainter.Inpaint(imageArgb, markupArgb, donors); result .FromArgbToBitmap() .SaveTo(resultPath, ImageFormat.Png) .ShowFile(); watch.Stop(); var elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine($"Done in {elapsedMs}ms"); }
public MainMod() { // initialize default settings ApiSettings.Initialize(); Post("/api/inpaint", async x => { try { Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Incomming request from " + this.Request.UserHostAddress); if (this.Request.Files.Count() < 2) { Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + $"] Error, {this.Request.Files.Count()} files found"); return("Err"); } Bitmap BitmapImg = null, BitmapMask = null; var donors = new List <ZsImage>(); foreach (var file in this.Request.Files) { Bitmap tempBitmap; byte[] ByteImg = new byte[file.Value.Length]; file.Value.Read(ByteImg, 0, (int)file.Value.Length); using (MemoryStream ms = new MemoryStream(ByteImg)) tempBitmap = new Bitmap(ms); if (BitmapImg == null) { BitmapImg = tempBitmap; } else if (BitmapMask == null) { BitmapMask = tempBitmap; } else { donors.Add(tempBitmap.ToArgbImage()); } } var imageArgb = ConvertToArgbImage(BitmapImg); var markupArgb = ConvertToArgbImage(BitmapMask); var inpainter = new Inpainter(); // Convert body request to settings ApiSettings userSettings = new ApiSettings(); try { //Console.WriteLine(Request.B .Form['body']); var data = this.Request.Form["body"]; if (data != null) { userSettings = JsonConvert.DeserializeObject <ApiSettings>(data); } } catch (Exception e) { Console.WriteLine(e.ToString()); } // Now merge then, giving priority to the default if (userSettings.InpaintSettings.PatchSize > ApiSettings._Instance.InpaintSettings.PatchSize) { userSettings.InpaintSettings.PatchSize = ApiSettings._Instance.InpaintSettings.PatchSize; } if (userSettings.InpaintSettings.MaxInpaintIterations > ApiSettings._Instance.InpaintSettings.MaxInpaintIterations) { userSettings.InpaintSettings.MaxInpaintIterations = ApiSettings._Instance.InpaintSettings.MaxInpaintIterations; } Image finalResult = null; inpainter.IterationFinished += (sender, eventArgs) => { Bitmap iterationResult = eventArgs.InpaintedLabImage .FromLabToRgb() .FromRgbToBitmap(); finalResult = iterationResult; Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] call on inpainter.IterationFinished (Level " + eventArgs.LevelIndex + ", Iteration " + eventArgs.InpaintIteration + ")"); //Debugging }; await Task.Factory.StartNew(() => inpainter.Inpaint(imageArgb, markupArgb, userSettings.InpaintSettings, donors)); Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss.fffff") + "] Processing finished"); #if DEBUG finalResult.Save(@"..\..\TESTAPP.PNG"); //Debugging #endif MemoryStream stream = new MemoryStream(); finalResult.Save(stream, ImageFormat.Png); return(Convert.ToBase64String(stream.ToArray())); //this does the job ¯\_(ツ)_/¯ } catch (Exception ex) { Console.WriteLine(ex.Message); return(null); } }); Get("/api/settings", async x => { return(Response.AsJson <ApiSettings>(ApiSettings._Instance)); }); Get(@"/", _ => { return(Response.AsFile("TestWebsite/index.html", "text/html")); }); Get("/ping", _ => "Ping is successful"); }