示例#1
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);
        }
示例#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);
        }