open-mmlab / PowerPaint

[ECCV 2024] PowerPaint, a versatile image inpainting model that supports text-guided object inpainting, object removal, image outpainting and shape-guided object inpainting with only a single model. 一个高质量多功能的图像修补模型,可以同时支持插入物体、移除物体、图像扩展、形状可控的物体生成,只需要一个模型
https://powerpaint.github.io/
MIT License
553 stars 36 forks source link

How to do outpainting with specified ratio locally and save the result locally? #71

Closed ustczhouyu closed 1 week ago

ustczhouyu commented 2 weeks ago

Help, I wonder how to do outpainting with specified ratio locally and save the result locally? Has anyone ever done that?

lucDoom commented 2 weeks ago

try this: `def inference_dir(no_padding=True): torch.set_grad_enabled(False) weight_dtype = torch.float16 controller = PowerPaintController(weight_dtype, '/ProjectRoot/image_outpainting/PowerPaint-main/official_pretrained/PowerPaint_v2', False, 'ppt-v2')

lines = open('/ProjectRoot/image_outpainting/data/240821_outpaintig/make_dataset/anno.txt','r').readlines()
for line in lines:
    try:
        url = line.strip().split(',')[0]
        img_name = url.split('/')[-1].split('?')[0]
        input_image_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/ori_imgs_500/"+img_name  # Replace with your image path
        image = Image.open(input_image_path)
        w,h = image.size
        if w < h :
            scc = h / w
            if scc > 1.3:
                vertical_expansion_ratio = 1
                new_w = 3* h / 4  
                horizontal_expansion_ratio = new_w / w
                new_size = (int(new_w),h)
            else:
                vertical_expansion_ratio = 1
                horizontal_expansion_ratio = scc
                new_size = (h,h)   
        else:
            scc = w / h 
            if scc >1.3:
                horizontal_expansion_ratio = 1
                new_h =  3 * w /4
                vertical_expansion_ratio = new_h / h 
                new_size = (w,int(new_h))
            else:
                vertical_expansion_ratio = scc
                horizontal_expansion_ratio = 1
                new_size = (w,w)
        diff = abs(new_size[0] - w) + abs(new_size[1] - h)
        if diff<100:
            continue

        input_image = {"image": image}
        # Set the parameters for inference
        prompt = ""  # Your prompt here
        negative_prompt = "NSFW, (word:1.5), watermark, (wall:1.5), blurry, monochrome, missing body, amputation"  # Your negative prompt here
        fitting_degree = 0.5  # Adjust as needed
        ddim_steps = 25  # Number of inference steps
        scale = 20  # Guidance scale
        seed = 42  # Set a random seed for reproducibility
        task= "image-outpainting"
        dict_out, dict_res = controller.predict(
                        input_image,
                        prompt,
                        fitting_degree,
                        ddim_steps,
                        scale,
                        seed,
                        negative_prompt,
                        task,
                        vertical_expansion_ratio,
                        horizontal_expansion_ratio)
        res1 = dict_out[0]
        save_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/nega_res_500_43/" + img_name +'_res.jpg'
        if no_padding:
            res1 = res1.resize(new_size, Image.BICUBIC) 
            res1.save(save_path)
        else:
            if w > h:
                new_image.paste(image, (0, (w - h) // 2))

                new_image.paste(generated_part, (w, (max_side - generated_part.size[1]) // 2))
            else:
                new_image.paste(image, ((h - w) // 2, 0))

                left_part = res1.crop((0, 0, w + generated_part_width, max_side))  # 左侧部分
                right_part = res1.crop((0, 0, generated_part_width, max_side))     # 右侧部分
    except:
        print('error one!')

`

ustczhouyu commented 2 weeks ago

lines = open('/ProjectRoot/image_outpainting/data/240821_outpaintig/make_dataset/anno.txt','r').readlines()

Thank you for your prompt reply. I also want to know what anno.txt in "lines = open ('/ProjectRoot/image_outpainting/data/240821_outpaintig/make_dataset/anno.txt', 'r').readlines ()" contains and what is its content format?

lucDoom commented 2 weeks ago

just img path, nothing else

ustczhouyu commented 2 weeks ago

lines = open('/ProjectRoot/image_outpainting/data/240821_outpaintig/make_dataset/anno.txt','r').readlines() for line in lines: try: url = line.strip().split(',')[0] img_name = url.split('/')[-1].split('?')[0] input_image_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/ori_imgs_500/"+img_name # Replace with your image path image = Image.open(input_image_path) w,h = image.size if w < h : scc = h / w if scc > 1.3: vertical_expansion_ratio = 1 new_w = 3 h / 4
horizontal_expansion_ratio = new_w / w new_size = (int(new_w),h) else: vertical_expansion_ratio = 1 horizontal_expansion_ratio = scc new_size = (h,h)
else: scc = w / h if scc >1.3: horizontal_expansion_ratio = 1 new_h = 3
w /4 vertical_expansion_ratio = new_h / h new_size = (w,int(new_h)) else: vertical_expansion_ratio = scc horizontal_expansion_ratio = 1 new_size = (w,w) diff = abs(new_size[0] - w) + abs(new_size[1] - h) if diff<100: continue

    input_image = {"image": image}
    # Set the parameters for inference
    prompt = ""  # Your prompt here
    negative_prompt = "NSFW, (word:1.5), watermark, (wall:1.5), blurry, monochrome, missing body, amputation"  # Your negative prompt here
    fitting_degree = 0.5  # Adjust as needed
    ddim_steps = 25  # Number of inference steps
    scale = 20  # Guidance scale
    seed = 42  # Set a random seed for reproducibility
    task= "image-outpainting"
    dict_out, dict_res = controller.predict(
                    input_image,
                    prompt,
                    fitting_degree,
                    ddim_steps,
                    scale,
                    seed,
                    negative_prompt,
                    task,
                    vertical_expansion_ratio,
                    horizontal_expansion_ratio)
    res1 = dict_out[0]
    save_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/nega_res_500_43/" + img_name +'_res.jpg'
    if no_padding:
        res1 = res1.resize(new_size, Image.BICUBIC) 
        res1.save(save_path)
    else:
        if w > h:
            new_image.paste(image, (0, (w - h) // 2))

            new_image.paste(generated_part, (w, (max_side - generated_part.size[1]) // 2))
        else:
            new_image.paste(image, ((h - w) // 2, 0))

            left_part = res1.crop((0, 0, w + generated_part_width, max_side))  # 左侧部分
            right_part = res1.crop((0, 0, generated_part_width, max_side))     # 右侧部分
except:
    print('error one!')

It works, thank you very much!

ustczhouyu commented 2 weeks ago

try this: `def inference_dir(no_padding=True): torch.set_grad_enabled(False) weight_dtype = torch.float16 controller = PowerPaintController(weight_dtype, '/ProjectRoot/image_outpainting/PowerPaint-main/official_pretrained/PowerPaint_v2', False, 'ppt-v2')

lines = open('/ProjectRoot/image_outpainting/data/240821_outpaintig/make_dataset/anno.txt','r').readlines()
for line in lines:
    try:
        url = line.strip().split(',')[0]
        img_name = url.split('/')[-1].split('?')[0]
        input_image_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/ori_imgs_500/"+img_name  # Replace with your image path
        image = Image.open(input_image_path)
        w,h = image.size
        if w < h :
            scc = h / w
            if scc > 1.3:
                vertical_expansion_ratio = 1
                new_w = 3* h / 4  
                horizontal_expansion_ratio = new_w / w
                new_size = (int(new_w),h)
            else:
                vertical_expansion_ratio = 1
                horizontal_expansion_ratio = scc
                new_size = (h,h)   
        else:
            scc = w / h 
            if scc >1.3:
                horizontal_expansion_ratio = 1
                new_h =  3 * w /4
                vertical_expansion_ratio = new_h / h 
                new_size = (w,int(new_h))
            else:
                vertical_expansion_ratio = scc
                horizontal_expansion_ratio = 1
                new_size = (w,w)
        diff = abs(new_size[0] - w) + abs(new_size[1] - h)
        if diff<100:
            continue

        input_image = {"image": image}
        # Set the parameters for inference
        prompt = ""  # Your prompt here
        negative_prompt = "NSFW, (word:1.5), watermark, (wall:1.5), blurry, monochrome, missing body, amputation"  # Your negative prompt here
        fitting_degree = 0.5  # Adjust as needed
        ddim_steps = 25  # Number of inference steps
        scale = 20  # Guidance scale
        seed = 42  # Set a random seed for reproducibility
        task= "image-outpainting"
        dict_out, dict_res = controller.predict(
                        input_image,
                        prompt,
                        fitting_degree,
                        ddim_steps,
                        scale,
                        seed,
                        negative_prompt,
                        task,
                        vertical_expansion_ratio,
                        horizontal_expansion_ratio)
        res1 = dict_out[0]
        save_path = "/GlobalData/dongcun.cqs/image_outpainting/test_data/240821_test_outpainting/nega_res_500_43/" + img_name +'_res.jpg'
        if no_padding:
            res1 = res1.resize(new_size, Image.BICUBIC) 
            res1.save(save_path)
        else:
            if w > h:
                new_image.paste(image, (0, (w - h) // 2))

                new_image.paste(generated_part, (w, (max_side - generated_part.size[1]) // 2))
            else:
                new_image.paste(image, ((h - w) // 2, 0))

                left_part = res1.crop((0, 0, w + generated_part_width, max_side))  # 左侧部分
                right_part = res1.crop((0, 0, generated_part_width, max_side))     # 右侧部分
    except:
        print('error one!')

`

Hi, I found another problem, that is the saved picture is much blurrier than the original one, do you know what causes that? How to solve this problem?

lucDoom commented 2 weeks ago

resize opreation in PowerPaintController. read the code

ustczhouyu commented 2 weeks ago

resize opreation in PowerPaintController. read the code

ok, thanks a lot