Exemplo n.º 1
0
        private async void runBtn_Click(object sender, EventArgs e)
        {
            if (PreviewUI.previewImg.Image == null || !File.Exists(Paths.tempImgPath))
            {
                Program.ShowMessage("No image loaded!", "Error");
                return;
            }
            Enabled    = false;
            cutoutMode = cropMode.SelectedIndex == 1;
            if (cutoutMode)
            {
                IOUtils.ClearDir(Paths.previewPath);
                PreviewUI.SaveCurrentCutout();
                currentSourcePath = Path.Combine(Paths.previewPath, "preview.png");
            }
            else
            {
                currentSourcePath = Paths.tempImgPath;
            }
            string[] lines = Regex.Split(modelPathsBox.Text, "\r\n|\r|\n");
            if (comparisonMode.SelectedIndex == 0)
            {
                string outpath = Path.Combine(Paths.imgOutPath, "!Original.png");
                await ImageProcessing.ConvertImage(currentSourcePath, GetSaveFormat(), false, ImageProcessing.ExtMode.UseNew, false, outpath);
                await ProcessImage(outpath, "Original");
            }
            for (int i = 0; i < lines.Length; i++)
            {
                if (!File.Exists(lines[i]))
                {
                    continue;
                }
                ModelData mdl = new ModelData(lines[i], null, ModelData.ModelMode.Single);
                await DoUpscale(i, mdl, !cutoutMode);
            }
            bool        vert       = compositionMode.SelectedIndex == 1;
            MagickImage merged     = ImgUtils.MergeImages(Directory.GetFiles(Paths.imgOutPath, "*.png", SearchOption.AllDirectories), vert, true);
            string      mergedPath = Path.Combine(Paths.imgOutPath, Path.GetFileNameWithoutExtension(Program.lastImgPath) + "-composition");

            mergedPath = Path.ChangeExtension(mergedPath, GetSaveExt());
            merged.Write(mergedPath);
            await Upscale.CopyImagesTo(Program.lastImgPath.GetParentDir());

            IOUtils.ClearDir(Paths.previewPath);
            Enabled = true;
            Program.ShowMessage("Saved model composition to " + Program.lastImgPath.GetParentDir() + "\\" + Path.GetFileName(mergedPath), "Message");
        }
Exemplo n.º 2
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);
        }