nihui / waifu2x-ncnn-vulkan

waifu2x converter ncnn version, runs fast on intel / amd / nvidia / apple-silicon GPU with vulkan
MIT License
3.02k stars 212 forks source link

Recursive processing #103

Open kattjevfel opened 4 years ago

kattjevfel commented 4 years ago

Could it be possible to add a recursive processing feature? So when you input a folder for processing, it will also process all folders under it.

I'm sure I could come up with a solution in bash, but that'd be rather ugly and unreliable :P

Rob2k9 commented 4 years ago
@echo off
title BATCH IMAGE UPSCAILER
FOR %%a IN (input/*.png) DO (
    CLS
    echo =========================================
    echo   STARTING UPSCALING OF %%a
    echo =========================================
    waifu2x-ncnn-vulkan.exe -j 4:4:4 -g 0 -s 2 -v -m models-upconv_7_anime_style_art_rgb -n 3 -i "input/%%a" -o "output/%%~na.png"
)
echo.
echo All done! Press any key to exit...
pause>nul
kattjevfel commented 4 years ago

I am fully aware that you can make a script for this, but it'd be nice to have it included in the program.

For anyone else reading this is how I've solved it:

#!/bin/bash

# Needs to be exactly 2 arguments, and they must be directories
if [ ! $# -eq 2 ] || [ ! -d "$1" ] || [ ! -d "$2" ]; then
    echo "Usage: ${0##*/} input_dir output_dir"
    exit
fi

# You can set these permanently and remove the above section
input_dir=${1%/}
output_dir=${2%/}

# Check if there's any files to process
filesfound=$(find "$input_dir" -type f)
if [ -z "$filesfound" ]; then
    echo 'No files to process, exiting!'
    exit
fi

while IFS= read -r fullfilepath; do
    # Path but without $input_dir
    shortfilepath="${fullfilepath#*$input_dir}"

    # Create directory (remove everything after /)
    mkdir -p "$output_dir/${shortfilepath%/*}"

    # Print current file being upscaled, without leading /
    echo -n "Upscaling ${shortfilepath#*/} "

    # Verbose, TTA, webp format
    fulloutput=$(
        waifu2x-ncnn-vulkan -v -x -f webp \
            -i "${input_dir}${shortfilepath}" \
            -o "${output_dir}${shortfilepath%.*}".webp 2>&1
    )

    if echo "$fulloutput" | grep -q "done"; then
        echo "done!"
        rm "${input_dir}${shortfilepath}"
    else
        echo -n 'failed: '
        # Print error in red and move on
        ERROR=$(echo "$fulloutput" | grep -v -e '^\[' -e 'Experimental compiler')
        echo -e "\033[0;31m$ERROR\033[0m"
        continue
    fi
done <<<"$filesfound"

# Remove any empty directories left behind
find "$input_dir" -mindepth 1 -type d -empty -delete

https://github.com/kattjevfel/scripts/blob/master/2x.sh

inikishev commented 2 years ago
@echo off
title BATCH IMAGE UPSCAILER
FOR %%a IN (input/*.png) DO (
    CLS
    echo =========================================
    echo   STARTING UPSCALING OF %%a
    echo =========================================
    waifu2x-ncnn-vulkan.exe -j 4:4:4 -g 0 -s 2 -v -m models-upconv_7_anime_style_art_rgb -n 3 -i "input/%%a" -o "output/%%~na.png"
)
echo.
echo All done! Press any key to exit...
pause>nul

this is not recursive. This is the first time I ever tried batch files, so the best i can do is this - it REPLACES all files in "input" folder with uspcaled, but at least it does that recursively. You need to open the bat file with text editor and change the folder path to yours (D:\Apps\waifu2x-ncnn-vulkan-20220419-windows\waifu2x-ncnn-vulkan.exe)

@echo off
title BATCH IMAGE UPSCAILER
forfiles /p input /m *.png /s /c "cmd /c D:\Apps\waifu2x-ncnn-vulkan-20220419-windows\waifu2x-ncnn-vulkan.exe -v -s 2 -n 3 -x -i @PATH -o @PATH"

echo.
echo All done! Press any key to exit...
pause>nul