CoolFace
Apppublic

ececleon/magic

sourceHugging Faceapache-2.0updated 4y agoView on Hugging Face
0likes
app.py63 linesDownload Raw Back to root
1from upcunet_v3 import RealWaifuUpScaler2import gradio as gr3import time4import logging5import os6from PIL import ImageOps7import numpy as np8import math9 10 11def greet(input_img, input_model_name, input_tile_mode):12    # if input_img.size[0] * input_img.size[1] > 256 * 256:13    #     y = int(math.sqrt(256*256/input_img.size[0]*input_img.size[1]))14    #     x = int(input_img.size[0]/input_img.size[1]*y)15    #     input_img = ImageOps.fit(input_img, (x, y))16    input_img = np.array(input_img)17    if input_model_name not in model_cache:18        t1 = time.time()19        upscaler = RealWaifuUpScaler(input_model_name[2], ModelPath + input_model_name, half=False, device="cpu")20        t2 = time.time()21        logger.info(f'load model time, {t2 - t1}')22        model_cache[input_model_name] = upscaler23    else:24        upscaler = model_cache[input_model_name]25        logger.info(f'load model from cache')26 27    start = time.time()28    result = upscaler(input_img, tile_mode=input_tile_mode)29    end = time.time()30    logger.info(f'input_model_name, {input_model_name}')31    logger.info(f'input_tile_mode, {input_tile_mode}')32    logger.info(f'input shape, {input_img.shape}')33    logger.info(f'output shape, {result.shape}')34    logger.info(f'speed time, {end - start}')35    return result36 37 38if __name__ == '__main__':39    logging.basicConfig(level=logging.INFO, format="[%(asctime)s] [%(process)d] [%(levelname)s] %(message)s")40    logger = logging.getLogger()41 42    ModelPath = "weights_v3/"43    model_cache = {}44 45    input_model_name = gr.inputs.Dropdown(os.listdir(ModelPath), default="up2x-latest-denoise2x.pth", label='选择model')46    input_tile_mode = gr.inputs.Dropdown([0, 1, 2, 3, 4], default=2, label='选择tile_mode')47    input_img = gr.inputs.Image(label='image', type='pil')48 49    inputs = [input_img, input_model_name, input_tile_mode]50    outputs = "image"51    iface = gr.Interface(fn=greet,52                         inputs=inputs,53                         outputs=outputs,54                         allow_screenshot=False,55                         allow_flagging='never',56                         examples=[['test-img.jpg', "up2x-latest-denoise2x.pth", 2]],57                         article='[https://github.com/bilibili/ailab/tree/main/Real-CUGAN](https://github.com/bilibili/ailab/tree/main/Real-CUGAN)<br>'58                                 '感谢b站开源的项目,图片过大会导致内存不足,所有我将图片裁剪小,想体验大图片的效果请自行前往上面的链接。<br>'59                                 '修改bbb'60                                 'The large image will lead to memory limit exceeded. So I crop and resize image. '61                                 'If you want to experience the large image, please go to the link above.')62    iface.launch()63