Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added image resizing to resolution size. Now accept input images of v… #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions scripts/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

warnings.filterwarnings("ignore")


def read_mask(mask_path, invert=False):
mask = Image.open(mask_path)
mask = resize(mask, max_size=512, interpolation=Image.NEAREST)
Expand Down Expand Up @@ -54,12 +53,12 @@ def resize(image, max_size, interpolation=Image.BICUBIC):


def preprocess(img: Image, mask: Image, resolution: int) -> torch.Tensor:
img = img.resize((resolution, resolution), Image.BICUBIC)
mask = mask.resize((resolution, resolution), Image.NEAREST)

img = np.array(img)
mask = np.array(mask)[:, :, np.newaxis] // 255
img = torch.Tensor(img).float() * 2 / 255 - 1
mask = torch.Tensor(mask).float()

img = img.permute(2, 0, 1).unsqueeze(0)
mask = mask.permute(2, 0, 1).unsqueeze(0)
x = torch.cat([mask - 0.5, img * mask], dim=1)
Expand Down Expand Up @@ -123,9 +122,10 @@ def main():
mask_path = os.path.join(args.masks_dir, "".join(os.path.basename(img_path).split('.')[:-1]) + ".png")

img = Image.open(img_path).convert("RGB")
img_resized = resize(img, max_size=resolution)
h, w = img.size
img_resized = img.resize((resolution, resolution), Image.BICUBIC)
mask = read_mask(mask_path, invert=args.invert_mask)
mask_resized = resize(mask, max_size=resolution, interpolation=Image.NEAREST)
mask_resized = mask.resize((resolution, resolution), Image.NEAREST)

x = preprocess(img_resized, mask_resized, resolution)
if cuda:
Expand All @@ -137,8 +137,9 @@ def main():

result_image = cv2.resize(result_image, dsize=img_resized.size, interpolation=cv2.INTER_CUBIC)
mask_resized = np.array(mask_resized)[:, :, np.newaxis] // 255

composed_img = img_resized * mask_resized + result_image * (1 - mask_resized)
composed_img = Image.fromarray(composed_img)
composed_img = Image.fromarray(composed_img).resize((h, w))
composed_img.save(args.output_dir / f"{Path(img_path).stem}.png")


Expand Down