Beispiel #1
0
        async Task DoUpscale(int index, ModelData mdl, bool fullImage)
        {
            if (PreviewUI.previewImg.Image == null)
            {
                Program.ShowMessage("Please load an image first!", "Error");
                return;
            }
            Program.mainForm.SetBusy(true);

            Upscale.currentMode = Upscale.UpscaleMode.Composition;
            //await ImageProcessing.PreProcessImages(Paths.previewPath, !bool.Parse(Config.Get("alpha")));
            //if (cutoutMode)
            //currentSourcePath += ".png";
            string outImg = null;

            try
            {
                bool           useNcnn = (Config.Get("cudaFallback").GetInt() == 2 || Config.Get("cudaFallback").GetInt() == 3);
                bool           useCpu  = (Config.Get("cudaFallback").GetInt() == 1);
                ESRGAN.Backend backend = ESRGAN.Backend.CUDA;
                if (useCpu)
                {
                    backend = ESRGAN.Backend.CPU;
                }
                if (useNcnn)
                {
                    backend = ESRGAN.Backend.NCNN;
                }
                string inpath = Paths.previewPath;
                if (fullImage)
                {
                    inpath = Paths.tempImgPath.GetParentDir();
                }
                await ESRGAN.DoUpscale(inpath, Paths.compositionOut, mdl, false, Config.GetBool("alpha"), ESRGAN.PreviewMode.None, backend);

                if (backend == ESRGAN.Backend.NCNN)
                {
                    outImg = Directory.GetFiles(Paths.compositionOut, "*.png*", SearchOption.AllDirectories)[0];
                }
                else
                {
                    outImg = Directory.GetFiles(Paths.compositionOut, "*.tmp", SearchOption.AllDirectories)[0];
                }
                await Upscale.PostprocessingSingle(outImg, false);
                await ProcessImage(PreviewUI.lastOutfile, mdl.model1Name);

                IOUtils.TryCopy(PreviewUI.lastOutfile, Path.Combine(Paths.imgOutPath, $"{index}-{mdl.model1Name}.png"), true);
            }
            catch (Exception e)
            {
                if (e.StackTrace.Contains("Index"))
                {
                    Program.ShowMessage("The upscale process seems to have exited before completion!", "Error");
                }
                Logger.ErrorMessage("An error occured during upscaling:", e);
                Program.mainForm.SetProgress(0f, "Cancelled.");
            }
            Program.mainForm.SetProgress(0, "Done.");
            Program.mainForm.SetBusy(false);
        }
Beispiel #2
0
        private async void offlineInterpBtn_Click(object sender, EventArgs e)
        {
            if (MainUIHelper.currentMode == Mode.Interp)
            {
                try
                {
                    string mdl1 = Program.currentModel1;
                    string mdl2 = Program.currentModel2;
                    if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2))
                    {
                        return;
                    }
                    ModelData  mdl         = new ModelData(mdl1, mdl2, ModelData.ModelMode.Interp, interpValue);
                    DialogForm loadingForm = new DialogForm("Interpolating...");
                    await Task.Delay(50);

                    string outPath = ESRGAN.Interpolate(mdl);
                    loadingForm.Close();
                    Program.ShowMessage("Saved interpolated model to:\n\n" + outPath);
                }
                catch (Exception interpException)
                {
                    Logger.ErrorMessage("Error trying to create an interpolated model:", interpException);
                    Program.CloseTempForms();
                }
            }
            else
            {
                Program.ShowMessage("Please select \"Interpolate Between Two Models\" and select two models.");
            }
        }
Beispiel #3
0
        public static async Task Run(bool preprocess, bool postProcess = true, string overrideOutDir = "")
        {
            int  cudaFallback = Config.Get("cudaFallback").GetInt();
            bool useNcnn      = (cudaFallback == 2 || cudaFallback == 3);
            bool useCpu       = (cudaFallback == 1);

            string imgOutDir = outDir.Text.Trim();

            if (!string.IsNullOrWhiteSpace(overrideOutDir))
            {
                imgOutDir = overrideOutDir;
            }

            if (useNcnn && !Program.mainForm.HasValidNcnnModelSelection())
            {
                Program.ShowMessage("Invalid model selection - NCNN does not support interpolation or chaining.", "Error");
                return;
            }
            if (string.IsNullOrWhiteSpace(currentInDir) && (currentInFiles == null || currentInFiles.Length < 1))
            {
                Program.ShowMessage("No directory or files loaded.", "Error");
                return;
            }
            if (!IOUtils.HasEnoughDiskSpace(8192, imgOutDir.Substring(0, 2), 2.0f))
            {
                Program.ShowMessage($"Not enough disk space on {IOUtils.GetAppDataDir().Substring(0, 3)} to store temporary files!", "Error");
                return;
            }
            Upscale.currentMode = Upscale.UpscaleMode.Batch;
            Program.mainForm.SetBusy(true);
            Program.mainForm.SetProgress(2f, "Loading images...");
            await Task.Delay(20);

            Directory.CreateDirectory(imgOutDir);
            await CopyCompatibleImagesToTemp();

            Program.mainForm.SetProgress(3f, "Pre-Processing...");
            if (preprocess)
            {
                await ImageProcessing.PreProcessImages(Paths.imgInPath, !bool.Parse(Config.Get("alpha")));
            }
            else
            {
                IOUtils.AppendToFilenames(Paths.imgInPath, ".png");
            }
            ModelData mdl = Upscale.GetModelData();

            GetProgress(Paths.imgOutPath, IOUtils.GetAmountOfFiles(Paths.imgInPath, true));

            if (postProcess)
            {
                PostProcessingQueue.Start(imgOutDir);
            }

            List <Task> tasks = new List <Task>();

            ESRGAN.Backend backend = ESRGAN.Backend.CUDA;
            if (useCpu)
            {
                backend = ESRGAN.Backend.CPU;
            }
            if (useNcnn)
            {
                backend = ESRGAN.Backend.NCNN;
            }
            tasks.Add(ESRGAN.DoUpscale(Paths.imgInPath, Paths.imgOutPath, mdl, Config.Get("tilesize"), bool.Parse(Config.Get("alpha")), ESRGAN.PreviewMode.None, backend, false));
            if (postProcess)
            {
                tasks.Add(PostProcessingQueue.Update());
                tasks.Add(PostProcessingQueue.ProcessQueue());
            }
            sw.Restart();
            await Task.WhenAll(tasks);

            if (!Program.cancelled)
            {
                Program.mainForm.SetProgress(0, $"Done - Upscaling took {(sw.ElapsedMilliseconds / 1000f).ToString("0")}s");
            }
            Program.mainForm.SetBusy(false);
        }
Beispiel #4
0
        public static async Task UpscaleImage()
        {
            if (previewImg.Image == null)
            {
                Program.ShowMessage("Please load an image first!", "Error");
                return;
            }

            Program.mainForm.SetBusy(true);
            IOUtils.ClearDir(Paths.imgInPath);
            IOUtils.ClearDir(Paths.imgOutPath);
            Program.mainForm.SetProgress(3f, "Preprocessing...");
            string inImg = CopyImage();

            if (inImg == null)  // Try to copy/move image to input folder, return if failed
            {
                Cancel("I/O Error");
                return;
            }

            Upscale.currentMode = Upscale.UpscaleMode.Single;
            await ImageProcessing.PreProcessImage(inImg, !Config.GetBool("alpha"));

            ModelData mdl    = Upscale.GetModelData();
            string    outImg = null;

            sw.Restart();

            try
            {
                bool           useNcnn = (Config.Get("cudaFallback").GetInt() == 2 || Config.Get("cudaFallback").GetInt() == 3);
                bool           useCpu  = (Config.Get("cudaFallback").GetInt() == 1);
                ESRGAN.Backend backend = ESRGAN.Backend.CUDA;
                if (useCpu)
                {
                    backend = ESRGAN.Backend.CPU;
                }
                if (useNcnn)
                {
                    backend = ESRGAN.Backend.NCNN;
                }
                await ESRGAN.DoUpscale(Paths.imgInPath, Paths.imgOutPath, mdl, false, Config.GetBool("alpha"), ESRGAN.PreviewMode.None, backend);

                if (backend == ESRGAN.Backend.NCNN)
                {
                    outImg = Directory.GetFiles(Paths.imgOutPath, "*.png*", SearchOption.AllDirectories)[0];
                }
                else
                {
                    outImg = Directory.GetFiles(Paths.imgOutPath, "*.tmp", SearchOption.AllDirectories)[0];
                }
                Program.mainForm.SetProgress(100f, "Post-Processing...");
                await Task.Delay(50);

                await Upscale.PostprocessingSingle(outImg, false);

                string outFilename = Upscale.FilenamePostprocess(lastOutfile);
                await Upscale.CopyImagesTo(Path.GetDirectoryName(Program.lastImgPath));
            }
            catch (Exception e)
            {
                Program.mainForm.SetProgress(0f, "Cancelled.");
                if (Program.cancelled)
                {
                    return;
                }
                if (e.StackTrace.Contains("Index"))
                {
                    Program.ShowMessage("The upscale process seems to have exited before completion!", "Error");
                }
                Logger.ErrorMessage("An error occured during upscaling:", e);
            }

            if (!Program.cancelled)
            {
                Program.mainForm.SetProgress(0, $"Done - Upscaling took {(sw.ElapsedMilliseconds / 1000f).ToString("0.0")}s");
            }

            Program.mainForm.SetBusy(false);
        }
Beispiel #5
0
        public static async void UpscalePreview(bool fullImage = false)
        {
            if (!HasValidModelSelection())
            {
                Program.ShowMessage("Invalid model selection.\nMake sure you have selected a model and that the file still exists.", "Error");
                return;
            }
            if (Config.Get("cudaFallback").GetInt() == 3 && !Program.mainForm.HasValidNcnnModelSelection())
            {
                Program.ShowMessage("Invalid model selection - NCNN does not support interpolation or chaining.", "Error");
                return;
            }
            Upscale.currentMode = Upscale.UpscaleMode.Preview;
            Program.mainForm.SetBusy(true);
            Program.mainForm.SetProgress(2f, "Preparing...");
            Program.mainForm.resetState = new Cupscale.PreviewState(previewImg.Image, previewImg.Zoom, previewImg.AutoScrollPosition);
            await Task.Delay(20);

            ResetCachedImages();
            IOUtils.ClearDir(Paths.imgInPath);
            IOUtils.ClearDir(Paths.previewPath);
            IOUtils.ClearDir(Paths.previewOutPath);
            ESRGAN.PreviewMode prevMode = ESRGAN.PreviewMode.Cutout;
            if (fullImage)
            {
                prevMode = ESRGAN.PreviewMode.FullImage;
                if (!IOUtils.TryCopy(Paths.tempImgPath, Path.Combine(Paths.previewPath, "preview.png"), true))
                {
                    return;
                }
            }
            else
            {
                SaveCurrentCutout();
            }
            ClipboardComparison.originalPreview = (Bitmap)ImgUtils.GetImage(Directory.GetFiles(IO.Paths.previewPath, "*.png.*", SearchOption.AllDirectories)[0]);
            await ImageProcessing.PreProcessImages(Paths.previewPath, !bool.Parse(Config.Get("alpha")));

            string tilesize = Config.Get("tilesize");
            bool   alpha    = bool.Parse(Config.Get("alpha"));

            ESRGAN.Backend backend = ESRGAN.Backend.CUDA;
            if (Config.Get("cudaFallback").GetInt() == 1)
            {
                backend = ESRGAN.Backend.CPU;
            }
            if (Config.Get("cudaFallback").GetInt() == 3)
            {
                backend = ESRGAN.Backend.NCNN;
            }

            sw.Restart();

            if (currentMode == Mode.Single)
            {
                string mdl1 = Program.currentModel1;
                if (string.IsNullOrWhiteSpace(mdl1))
                {
                    return;
                }
                ModelData mdl = new ModelData(mdl1, null, ModelData.ModelMode.Single);
                await ESRGAN.DoUpscale(Paths.previewPath, Paths.previewOutPath, mdl, false, alpha, prevMode, backend);
            }
            if (currentMode == Mode.Interp)
            {
                string mdl1 = Program.currentModel1;
                string mdl2 = Program.currentModel2;
                if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2))
                {
                    return;
                }
                ModelData mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Interp, interpValue);
                await ESRGAN.DoUpscale(Paths.previewPath, Paths.previewOutPath, mdl, false, alpha, prevMode, backend);
            }
            if (currentMode == Mode.Chain)
            {
                string mdl1 = Program.currentModel1;
                string mdl2 = Program.currentModel2;
                if (string.IsNullOrWhiteSpace(mdl1) || string.IsNullOrWhiteSpace(mdl2))
                {
                    return;
                }
                ModelData mdl = new ModelData(mdl1, mdl2, ModelData.ModelMode.Chain);
                await ESRGAN.DoUpscale(Paths.previewPath, Paths.previewOutPath, mdl, false, alpha, prevMode, backend);
            }
            if (currentMode == Mode.Advanced)
            {
                ModelData mdl = new ModelData(null, null, ModelData.ModelMode.Advanced);
                await ESRGAN.DoUpscale(Paths.previewPath, Paths.previewOutPath, mdl, false, alpha, prevMode, backend);
            }
            if (!Program.cancelled)
            {
                Program.mainForm.SetProgress(0, $"Done - Upscaling took {(sw.ElapsedMilliseconds / 1000f).ToString("0.0")}s");
            }
            Program.mainForm.SetBusy(false);
        }