Skip to content

Instantly share code, notes, and snippets.

@phhusson
Created October 24, 2022 08:44
Show Gist options
  • Save phhusson/861ee02024830d24d1d521000cd2f112 to your computer and use it in GitHub Desktop.
Save phhusson/861ee02024830d24d1d521000cd2f112 to your computer and use it in GitHub Desktop.
Upscaling/Improving BDs in CBZ/CBR format using Real-CUGAN
#!/bin/bash
set -ex
rm -Rf in out list-all-files global-dead
v="$(readlink -f -- "$1")"
ext="$(echo "$v" |rev | cut -d . -f 1 |rev)"
outdir="$(dirname "$v" |sed -E 's;/where/are/originals/;;g')"
mkdir -p new-bds/"$outdir"
outfile="new-bds/"$outdir"/$(basename "$v" .$ext)-new.cbz"
[ -f "$outfile" ] && exit 0
touch "$outfile"
outfile="$(readlink -f -- "$outfile")"
mkdir -p in out
cd in
if [ "$ext" = "cbr" ];then
unrar x "$1"
elif [ "$ext" = "cbz" ];then
unzip "$1"
else
exit 1
fi
find -type d -exec chmod 0755 '{}' \;
find -type f -exec chmod 0644 '{}' \;
find -type f -name \*.jpg -or -name \*.png > ../list-all-files
cd ..
if ! python3 -c 'import sys ; sys.stdin.readlines()' < list-all-files;then
rm -Rf in out list-all-files
exit 0
fi
cat list-all-files > phh-fifo
cd out
cat ../list-all-files |while read r;do
while [ ! -f "$r".ready ] && [ ! -f "$r".dead ] && [ ! -f ../global-dead ];do
echo "Waiting for $r to be ready"
sleep 1
done
if [ -f ../global-dead ];then
break
fi
if [ -f "$r".dead ];then
echo "$r failed processing !!!!!"
echo "Ignoring...."
rm -f "$r" "$r".dead
continue
fi
if [ -f "$r".ready ];then
cp "$r" ../in/"$r"
fi
done
if [ -f ../global-dead ];then exit 0 ;fi
cd ../in
ziptmp="$(mktemp)"
rm -f "$outfile" "$ziptmp"
zip -0 -r "$ziptmp" .
cd ..
cp -f "$ziptmp" "$outfile"
rm -Rf in out list-all-files "$ziptmp"
import sys
import pathlib
import fileinput
sys.path.append(".")
import torch
from torch import nn as nn
from torch.nn import functional as F
import os,sys,cv2
import numpy as np
from upcunet_v3 import RealWaifuUpScaler
from time import time as ttime
upscaler = RealWaifuUpScaler(2, "weights_v3/up2x-latest-denoise3x.pth", half=True, device="cuda:0")
for i in fileinput.input():
i=i.strip()
pathlib.Path('out/' + os.path.dirname(i)).mkdir(parents=True,exist_ok=True)
try:
print("Transforming", i)
img = cv2.imread('in/' + i)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = upscaler(img,tile_mode=5,cache_mode=2,alpha=1)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imwrite('out/' + i, img)
with open(os.path.join('out/', i + '.ready'), 'w') as fp:
pass
except Exception as e:
print("Got error", e)
with open(os.path.join('out/', i + '.dead'), 'w') as fp:
pass
git clone https://github.com/bilibili/ailab/
cd ailab/Real-CUGAN
# Download weights_v3/up2x-latest-denoise3x.pth from original website
mkfifo phh-fifo
# One shell runs
find /where/are/originals/ -name \*.cbz -or -name \*.cbr | while read r;do bash hello.sh "$r";done
# Another shell runs
(while true;do cat phh-fifo;done) | python3 phh.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment