-
Notifications
You must be signed in to change notification settings - Fork 0
/
scene_text_eraser.py
56 lines (40 loc) · 1.53 KB
/
scene_text_eraser.py
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
from diffusers import (
EulerAncestralDiscreteScheduler,
StableDiffusionControlNetSceneTextErasingPipeline,
)
import torch
from PIL import Image
import os
from tqdm import tqdm
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--folder", type=str, required=True)
args = parser.parse_args()
INPUT_IMAGE_PATH = args.folder
MASK_IMAGE_PATH = "tmp/masks"
SAVE_IMAGE_PATH = "tmp/steo"
os.makedirs(SAVE_IMAGE_PATH,exist_ok = True)
model_path = "onkarsus13/controlnet_stablediffusion_scenetextEraser"
device = torch.device(device="cuda" if torch.cuda.is_available() else "cpu")
pipe = StableDiffusionControlNetSceneTextErasingPipeline.from_pretrained(model_path)
pipe.scheduler = EulerAncestralDiscreteScheduler.from_config(pipe.scheduler.config)
pipe.to(device)
generator = torch.Generator(device).manual_seed(1)
for image_name in tqdm(os.listdir(MASK_IMAGE_PATH)):
image = Image.open(os.path.join(INPUT_IMAGE_PATH,image_name))
mask_image = Image.open(os.path.join(MASK_IMAGE_PATH,image_name))
original_image_size = image.size
new_image_size = (512, 512)
image = image.resize(new_image_size)
mask_image = mask_image.resize(new_image_size)
result_image = pipe(
image,
mask_image,
[mask_image],
num_inference_steps=40,
generator=generator,
controlnet_conditioning_scale=1.0,
guidance_scale=1.0
).images[0]
result_image = result_image.resize(original_image_size)
result_image.save(os.path.join(SAVE_IMAGE_PATH,image_name))