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"]); }
public static async Task Orchestrate( [OrchestrationTrigger] DurableOrchestrationContext ctx) { var inpaintRequest = ctx.GetInput <InpaintRequest>(); // TODO: get the setting from parameters var settings = new InpaintSettings(); // TODO: downscale the input image // TODO: crop the input image var pyramid = await ctx.CallActivityAsync <CloudPyramid>(PyramidsGenerateActivity.Name, inpaintRequest); settings.MaxInpaintIterations = 20; //settings.PatchMatch.IterationsAmount = 2; //var kStep = settings.MeanShift.KDecreaseStep; //var minK = settings.MeanShift.MinK; for (byte levelIndex = 0; levelIndex < pyramid.LevelsAmount; levelIndex++) { var imageName = pyramid.GetImageName(levelIndex); var mapping = pyramid.GetMapping(levelIndex); var nnf = pyramid.GetNnf(levelIndex); var inpaintArea = pyramid.GetInpaintArea(levelIndex); var mappings = pyramid.GetSplittedMappings(levelIndex); var nnfs = pyramid.GetSplittedNnfs(levelIndex); // TODO: this looks ugly var input = NnfInputData.From(nnf, inpaintRequest.Container, imageName, settings, mapping, inpaintArea, false, levelIndex, settings.MeanShift.K, nnfs, mappings); await ctx.CallSubOrchestratorAsync(InpaintLevelFunction.Name, input); } }
private void InitInpaintSettings() { // init defaultSettings with environment variables for Inpainting Settings this.InpaintSettings = new InpaintSettings(); int maxInpaintIterations = 0; if (!Int32.TryParse(Environment.GetEnvironmentVariable("MAX_INPAINT_ITERATIONS"), out maxInpaintIterations) && maxInpaintIterations != 0) { this.InpaintSettings.MaxInpaintIterations = maxInpaintIterations; } string patchDistanceEnvVar = Environment.GetEnvironmentVariable("PATCH_DISTANCE_CALCULATOR"); if (patchDistanceEnvVar != null && patchDistanceEnvVar.Equals("Cie2000", StringComparison.OrdinalIgnoreCase)) { this.InpaintSettings.PatchDistanceCalculator = ImagePatchDistance.Cie2000; } int patchSize; if (!Int32.TryParse(Environment.GetEnvironmentVariable("PATCH_SIZE"), out patchSize) && patchSize != 0) { this.InpaintSettings.PatchSize = (byte)patchSize; } }
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; }
public static NnfInputData From(string nnf, string container, string image, InpaintSettings settings, string mapping, string inpaintAreaName, bool isForward, byte levelIndex, double meanShiftK, string[] splittedNnfs, string[] mappings) { return(new NnfInputData { NnfName = nnf, Container = container, Image = image, Settings = settings, Mapping = mapping, InpaintAreaName = inpaintAreaName, IsForward = isForward, IsCie79Calc = settings.PatchDistanceCalculator == ImagePatchDistance.Cie76, LevelIndex = levelIndex, K = meanShiftK, SplittedNnfNames = splittedNnfs, Mappings = mappings }); }
public static async Task<CloudPyramid> GeneratePyramids([ActivityTrigger] InpaintRequest inpaintRequest) { var levelDetector = new PyramidLevelsDetector(); var pyramidBuilder = new PyramidBuilder(); var settings = new InpaintSettings(); var container = BlobHelper.OpenBlobContainer(inpaintRequest.Container); var imageBlob = container.GetBlockBlobReference(inpaintRequest.Image); var removeMaskBlob = container.GetBlockBlobReference(inpaintRequest.RemoveMask); var imageArgb = await BlobHelper.ConvertBlobToArgbImage(imageBlob); var removeMaskArgb = await BlobHelper.ConvertBlobToArgbImage(removeMaskBlob); var levelsAmount = levelDetector.CalculateLevelsAmount(imageArgb, removeMaskArgb, settings.PatchSize); pyramidBuilder.Init(imageArgb, removeMaskArgb); var pyramid = pyramidBuilder.Build(levelsAmount, settings.PatchSize); var cloudPyramid = new CloudPyramid { Levels = new CloudPyramidLevel[pyramid.LevelsAmount] }; for (byte levelIndex = 0; levelIndex < pyramid.LevelsAmount; levelIndex++) { var image = pyramid.GetImage(levelIndex); var fileName = $"{levelIndex}.png"; await BlobHelper.SaveImageLabToBlob(image, container, fileName); cloudPyramid.Levels[levelIndex].ImageName = fileName; var inpaintArea = pyramid.GetInpaintArea(levelIndex); var inpaintAreaState = inpaintArea.GetState(); var inpaintAreaFileName = $"ia{levelIndex}.json"; var inpaintAreaData = JsonConvert.SerializeObject(inpaintAreaState); BlobHelper.SaveJsonToBlob(inpaintAreaData, container, inpaintAreaFileName); cloudPyramid.Levels[levelIndex].InpaintArea = inpaintAreaFileName; cloudPyramid.Levels[levelIndex].Nnf = $"nnf{levelIndex}.json"; var mapping = pyramid.GetMapping(levelIndex); var mappingFileName = $"map{levelIndex}.json"; var mapState = mapping.GetState(); var mappingData = JsonConvert.SerializeObject(mapState); BlobHelper.SaveJsonToBlob(mappingData, container, mappingFileName); cloudPyramid.Levels[levelIndex].Mapping = mappingFileName; var mappings = SplitMapping(mapping, inpaintRequest.Settings.MaxPointsAmountPerFunction, settings.PatchSize).ToArray(); cloudPyramid.Levels[levelIndex].SplittedMappings = new string[mappings.Length]; cloudPyramid.Levels[levelIndex].SplittedNnfs = new string[mappings.Length]; for (var i = 0; i < mappings.Length; i++) { var map = mappings[i]; mappingFileName = $"map{levelIndex}_p{i}.json"; mapState = map.GetState(); mappingData = JsonConvert.SerializeObject(mapState); BlobHelper.SaveJsonToBlob(mappingData, container, mappingFileName); cloudPyramid.Levels[levelIndex].SplittedMappings[i] = mappingFileName; cloudPyramid.Levels[levelIndex].SplittedNnfs[i] = $"nnf{levelIndex}_p{i}.json"; } } return cloudPyramid; }
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 InpaintSettings userSettings = new InpaintSettings(); try { userSettings = JsonConvert.DeserializeObject <InpaintSettings>(Request.Body.AsString()); } catch { } // Now merge then, giving priority to the default if (userSettings.PatchSize > ApiSettings._Instance.InpaintSettings.PatchSize) { userSettings.PatchSize = ApiSettings._Instance.InpaintSettings.PatchSize; } if (userSettings.MaxInpaintIterations > ApiSettings._Instance.InpaintSettings.MaxInpaintIterations) { userSettings.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, 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"); }