Exemplo n.º 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);
        }
Exemplo n.º 2
0
        private async void saveMergedPreviewBtn_Click(object sender, EventArgs e)
        {
            DialogForm loadingForm = new DialogForm("Post-Processing And Saving...");
            await Task.Delay(50);

            Upscale.currentMode = Upscale.UpscaleMode.Single;
            string ext     = Path.GetExtension(Program.lastFilename);
            string outPath = Path.ChangeExtension(Program.lastFilename, null) + "[temp]" + ext + ".tmp";

            previewImg.Image.Save(outPath);
            await Upscale.PostprocessingSingle(outPath, true);

            string outFilename = Upscale.FilenamePostprocess(MainUIHelper.lastOutfile);
            string finalPath   = IOUtils.ReplaceInFilename(outFilename, "[temp]", "");

            loadingForm.Close();
            Program.ShowMessage("Saved to " + finalPath + ".", "Message");
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        public static async Task ProcessQueue()
        {
            Stopwatch sw = new Stopwatch();

            while (run || AnyFilesLeft())
            {
                if (outputFileQueue.Count > 0)
                {
                    string file = outputFileQueue.Dequeue();
                    Logger.Log("[Queue] Post-Processing " + Path.GetFileName(file));
                    sw.Restart();
                    await Upscale.PostprocessingSingle(file, false);

                    while (IOUtils.IsFileLocked(lastOutfile))
                    {
                        Logger.Log($"{file} appears to be locked - waiting 500ms...");
                        await Task.Delay(500);
                    }

                    string outFilename = Upscale.FilenamePostprocess(lastOutfile);
                    outputFiles.Add(outFilename);
                    Logger.Log("[Queue] Done Post-Processing " + Path.GetFileName(file) + " in " + sw.ElapsedMilliseconds + "ms");

                    try
                    {
                        if (Upscale.overwriteMode == Upscale.Overwrite.Yes)
                        {
                            string suffixToRemove = "-" + Program.lastModelName.Replace(":", ".").Replace(">>", "+");

                            if (copyMode == CopyMode.KeepStructure)
                            {
                                string combinedPath = currentOutPath + outFilename.Replace(Paths.imgOutPath, "");
                                Directory.CreateDirectory(combinedPath.GetParentDir());
                                File.Copy(outFilename, combinedPath.ReplaceInFilename(suffixToRemove, "", true), true);
                            }
                            if (copyMode == CopyMode.CopyToRoot)
                            {
                                File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename).Replace(suffixToRemove, "")), true);
                            }

                            File.Delete(outFilename);
                        }
                        else
                        {
                            if (copyMode == CopyMode.KeepStructure)
                            {
                                string combinedPath = currentOutPath + outFilename.Replace(Paths.imgOutPath, "");
                                Directory.CreateDirectory(combinedPath.GetParentDir());
                                File.Copy(outFilename, combinedPath, true);
                            }

                            if (copyMode == CopyMode.CopyToRoot)
                            {
                                File.Copy(outFilename, Path.Combine(currentOutPath, Path.GetFileName(outFilename)), true);
                            }

                            File.Delete(outFilename);
                        }
                    }
                    catch (Exception e)
                    {
                        Logger.Log("Error trying to copy post-processed file back: " + e.Message + "\n" + e.StackTrace);
                    }

                    BatchUpscaleUI.upscaledImages++;
                }

                await Task.Delay(200);
            }
        }