File size: 2,546 Bytes
cd9824b
 
76c9769
cd9824b
 
 
 
 
367f77f
592ba82
 
 
 
367f77f
592ba82
 
 
 
c40c37c
cd9824b
592ba82
16056cf
592ba82
 
 
 
 
cd9824b
592ba82
a12b379
592ba82
367f77f
592ba82
367f77f
592ba82
3dd4690
cd9824b
 
 
 
a12b379
5e60f3c
3dd4690
 
cd9824b
592ba82
cd9824b
 
592ba82
 
367f77f
592ba82
367f77f
cd9824b
 
 
 
 
 
 
 
 
 
592ba82
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
from basicsr.archs.rrdbnet_arch import RRDBNet
from web.log import logger
from realesrgan import RealESRGANer
from gfpgan import GFPGANer
import numpy as np

# restorer
def create_real_esrganer(model_path, num_block, scale=4):
    upsampler = RealESRGANer(
        scale=scale,
        model_path=model_path,
        dni_weight=None,
        model=RRDBNet(num_in_ch=3, num_out_ch=3, num_feat=64, num_block=num_block, num_grow_ch=32, scale=scale),
        tile=0,
        tile_pad=10,
        pre_pad=0,
        half=False,
        gpu_id=None)

    face_enhancer = GFPGANer(
        model_path='weights/GFPGANv1.4.pth',
        upscale=scale,
        arch='clean',
        channel_multiplier=2,
        bg_upsampler=upsampler)
    return upsampler, face_enhancer

def convert_img(path: str, origin: str, output_name: str, face_enhance_open:bool, amine:bool):
    scale = 4
    if amine:
        upsampler, face_enhancer = create_real_esrganer('weights/RealESRGAN_x4plus_anime_6B.pth', 6, scale)
    else:
        upsampler, face_enhancer = create_real_esrganer('weights/RealESRGAN_x4plus.pth', 23, scale)
    img = cv2.imread(f"{path}/{origin}", cv2.IMREAD_UNCHANGED)
    logger.info("convert_img img shape: %s", img.shape)
    try:
        if face_enhance_open:
            _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
        else:
            output, _ = upsampler.enhance(img, outscale=scale)
    except Exception as error:
        logger.info('Error %s', error)
        logger.info('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
    else:
        cv2.imwrite(f"{path}/{output_name}", output)


def convert_img_row(data: bytes, output_name: str, face_enhance_open:bool, amine:bool):
    if amine:
        upsampler, face_enhancer = create_real_esrganer('weights/RealESRGAN_x4plus_anime_6B.pth', 6)
    else:
        upsampler, face_enhancer = create_real_esrganer('weights/RealESRGAN_x4plus.pth', 23)
    img = cv2.imdecode(np.fromstring(data, np.uint8), cv2.IMREAD_UNCHANGED)
    try:
        if face_enhance_open:
            _, _, output = face_enhancer.enhance(img, has_aligned=False, only_center_face=False, paste_back=True)
        else:
            output, _ = upsampler.enhance(img, outscale=4)
    except RuntimeError as error:
        print('Error', error)
        print('If you encounter CUDA out of memory, try to set --tile with a smaller number.')
    else:
        return cv2.imencode(f'.{output_name}', output)[1].tobytes()