Spaces:
Sleeping
Sleeping
Delete enhance_utils.py
Browse files- enhance_utils.py +0 -54
enhance_utils.py
DELETED
|
@@ -1,54 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
import cv2
|
| 4 |
-
import numpy as np
|
| 5 |
-
import subprocess
|
| 6 |
-
|
| 7 |
-
from PIL import Image
|
| 8 |
-
from gfpgan.utils import GFPGANer
|
| 9 |
-
from basicsr.archs.srvgg_arch import SRVGGNetCompact
|
| 10 |
-
from realesrgan.utils import RealESRGANer
|
| 11 |
-
|
| 12 |
-
def runcmd(cmd, verbose = False, *args, **kwargs):
|
| 13 |
-
|
| 14 |
-
process = subprocess.Popen(
|
| 15 |
-
cmd,
|
| 16 |
-
stdout = subprocess.PIPE,
|
| 17 |
-
stderr = subprocess.PIPE,
|
| 18 |
-
text = True,
|
| 19 |
-
shell = True
|
| 20 |
-
)
|
| 21 |
-
std_out, std_err = process.communicate()
|
| 22 |
-
if verbose:
|
| 23 |
-
print(std_out.strip(), std_err)
|
| 24 |
-
pass
|
| 25 |
-
|
| 26 |
-
runcmd("pip freeze")
|
| 27 |
-
if not os.path.exists('GFPGANv1.4.pth'):
|
| 28 |
-
runcmd("wget https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth -P .")
|
| 29 |
-
if not os.path.exists('realesr-general-x4v3.pth'):
|
| 30 |
-
runcmd("wget https://github.com/xinntao/Real-ESRGAN/releases/download/v0.2.5.0/realesr-general-x4v3.pth -P .")
|
| 31 |
-
|
| 32 |
-
model = SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=32, upscale=4, act_type='prelu')
|
| 33 |
-
model_path = 'realesr-general-x4v3.pth'
|
| 34 |
-
half = True if torch.cuda.is_available() else False
|
| 35 |
-
upsampler = RealESRGANer(scale=4, model_path=model_path, model=model, tile=0, tile_pad=10, pre_pad=0, half=half)
|
| 36 |
-
|
| 37 |
-
face_enhancer = GFPGANer(model_path='GFPGANv1.4.pth', upscale=1, arch='clean', channel_multiplier=2)
|
| 38 |
-
|
| 39 |
-
def enhance_image(
|
| 40 |
-
pil_image: Image,
|
| 41 |
-
enhance_face: bool = False,
|
| 42 |
-
):
|
| 43 |
-
img = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
| 44 |
-
|
| 45 |
-
h, w = img.shape[0:2]
|
| 46 |
-
if h < 300:
|
| 47 |
-
img = cv2.resize(img, (w * 2, h * 2), interpolation=cv2.INTER_LANCZOS4)
|
| 48 |
-
if enhance_face:
|
| 49 |
-
_, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=True, paste_back=True)
|
| 50 |
-
else:
|
| 51 |
-
output, _ = upsampler.enhance(img, outscale=2)
|
| 52 |
-
pil_output = Image.fromarray(cv2.cvtColor(output, cv2.COLOR_BGR2RGB))
|
| 53 |
-
|
| 54 |
-
return pil_output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|