Spaces:
Runtime error
Runtime error
Commit
·
32b650b
1
Parent(s):
8663a23
new image loading code
Browse files
app.py
CHANGED
@@ -94,24 +94,22 @@ def show_images(batch: th.Tensor):
|
|
94 |
reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
|
95 |
return Image.fromarray(reshaped.numpy())
|
96 |
|
97 |
-
def read_image(
|
98 |
-
pil_img =
|
99 |
pil_img = pil_img.resize((size, size), resample=Image.BICUBIC)
|
|
|
100 |
img = np.array(pil_img)
|
101 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 127.5 - 1
|
102 |
|
103 |
-
def read_mask(
|
104 |
-
|
105 |
-
pil_img_full = PIL.Image.open(path).convert('RGBA')
|
106 |
-
#image = Image.open( inputImagePath ).convert( 'RGBA' )
|
107 |
-
pil_img = pil_img_full.getchannel( 'A' ) # Mode 'L'
|
108 |
|
109 |
-
|
110 |
pil_img = pil_img.resize((size, size), resample=PIL.Image.BICUBIC)
|
|
|
111 |
img = np.array(pil_img)[..., np.newaxis]
|
112 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 255.0
|
113 |
|
114 |
-
|
115 |
def pil_to_numpy(pil_img: Image) -> Tuple[th.Tensor, th.Tensor]:
|
116 |
img = np.array(pil_img)
|
117 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 127.5 - 1
|
@@ -122,34 +120,44 @@ def inpaint(input_img, prompt):
|
|
122 |
print(prompt)
|
123 |
|
124 |
# Save as png for later mask detection :)
|
125 |
-
input_img_256 = input_img.convert('RGB').resize((256, 256), resample=Image.BICUBIC)
|
126 |
-
input_img_64 = input_img.convert('RGB').resize((64, 64), resample=Image.BICUBIC)
|
127 |
|
128 |
-
input_img_with_mask_converted = input_img.convert('RGBA').getchannel( 'A' ) # Mode 'L'
|
129 |
|
130 |
-
input_img_with_mask_64 = input_img_with_mask_converted.resize((64, 64), resample=Image.BICUBIC)
|
131 |
# TODO: make 256x256 mask more accurate when upscaling?
|
132 |
-
input_img_with_mask_256 = input_img_with_mask_converted.resize((256, 256), resample=Image.BICUBIC)
|
133 |
# return input_img, input_img_with_mask_64
|
134 |
# Source image we are inpainting
|
135 |
-
source_image_256 =
|
136 |
-
source_image_64 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
|
138 |
# Since gradio doesn't supply which pixels were drawn, we need to find it ourselves!
|
139 |
# Assuming that all black pixels are meant for inpainting.
|
140 |
# input_img_with_mask_64 = input_img_with_mask.convert('L').resize((64, 64), resample=Image.BICUBIC)
|
141 |
-
gray_scale_source_image_64 = image_to_tensor(input_img_with_mask_64)
|
142 |
-
gray_scale_source_image_256 = image_to_tensor(input_img_with_mask_256)
|
143 |
|
144 |
-
source_mask_64 = (gray_scale_source_image_64!=0).float()
|
145 |
-
source_mask_256 = (gray_scale_source_image_256!=0).float()
|
146 |
|
147 |
-
source_mask_64_img = tensor_to_image(source_mask_64)
|
148 |
|
149 |
# The mask should always be a boolean 64x64 mask, and then we
|
150 |
# can upsample it for the second stage.
|
151 |
-
source_mask_64 = source_mask_64.unsqueeze(0)
|
152 |
-
source_mask_256 = source_mask_256.unsqueeze(0)
|
153 |
# source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
|
154 |
|
155 |
|
|
|
94 |
reshaped = scaled.permute(2, 0, 3, 1).reshape([batch.shape[2], -1, 3])
|
95 |
return Image.fromarray(reshaped.numpy())
|
96 |
|
97 |
+
def read_image(pil_img, size: int = 256) -> Tuple[th.Tensor, th.Tensor]:
|
98 |
+
pil_img = pil_img.convert('RGB')
|
99 |
pil_img = pil_img.resize((size, size), resample=Image.BICUBIC)
|
100 |
+
|
101 |
img = np.array(pil_img)
|
102 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 127.5 - 1
|
103 |
|
104 |
+
def read_mask(pil_img_full, size: int = 256) -> Tuple[th.Tensor, th.Tensor]:
|
105 |
+
pil_img_full = pil_img_full.convert('RGBA')
|
|
|
|
|
|
|
106 |
|
107 |
+
pil_img = pil_img_full.getchannel( 'A' ) # Mode 'L'
|
108 |
pil_img = pil_img.resize((size, size), resample=PIL.Image.BICUBIC)
|
109 |
+
|
110 |
img = np.array(pil_img)[..., np.newaxis]
|
111 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 255.0
|
112 |
|
|
|
113 |
def pil_to_numpy(pil_img: Image) -> Tuple[th.Tensor, th.Tensor]:
|
114 |
img = np.array(pil_img)
|
115 |
return th.from_numpy(img)[None].permute(0, 3, 1, 2).float() / 127.5 - 1
|
|
|
120 |
print(prompt)
|
121 |
|
122 |
# Save as png for later mask detection :)
|
123 |
+
# input_img_256 = input_img.convert('RGB').resize((256, 256), resample=Image.BICUBIC)
|
124 |
+
# input_img_64 = input_img.convert('RGB').resize((64, 64), resample=Image.BICUBIC)
|
125 |
|
126 |
+
# input_img_with_mask_converted = input_img.convert('RGBA').getchannel( 'A' ) # Mode 'L'
|
127 |
|
128 |
+
# input_img_with_mask_64 = input_img_with_mask_converted.resize((64, 64), resample=Image.BICUBIC)
|
129 |
# TODO: make 256x256 mask more accurate when upscaling?
|
130 |
+
# input_img_with_mask_256 = input_img_with_mask_converted.resize((256, 256), resample=Image.BICUBIC)
|
131 |
# return input_img, input_img_with_mask_64
|
132 |
# Source image we are inpainting
|
133 |
+
source_image_256 = read_image(input_img, size=256)
|
134 |
+
source_image_64 = read_image(input_img, size=64)
|
135 |
+
|
136 |
+
source_mask_64 = read_mask(input_img, size=64)
|
137 |
+
source_mask_64 = (source_mask_64>0.5).float()
|
138 |
+
|
139 |
+
# these are better but will leave a "mark"
|
140 |
+
source_mask_256 = read_mask(input_img, size=256)
|
141 |
+
source_mask_256 = (source_mask_256>0.5).float()
|
142 |
+
# source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
|
143 |
+
# source_image_256 = pil_to_numpy(input_img_256)
|
144 |
+
# source_image_64 = pil_to_numpy(input_img_64)
|
145 |
|
146 |
# Since gradio doesn't supply which pixels were drawn, we need to find it ourselves!
|
147 |
# Assuming that all black pixels are meant for inpainting.
|
148 |
# input_img_with_mask_64 = input_img_with_mask.convert('L').resize((64, 64), resample=Image.BICUBIC)
|
149 |
+
# gray_scale_source_image_64 = image_to_tensor(input_img_with_mask_64)
|
150 |
+
# gray_scale_source_image_256 = image_to_tensor(input_img_with_mask_256)
|
151 |
|
152 |
+
# source_mask_64 = (gray_scale_source_image_64!=0).float()
|
153 |
+
# source_mask_256 = (gray_scale_source_image_256!=0).float()
|
154 |
|
155 |
+
# source_mask_64_img = tensor_to_image(source_mask_64)
|
156 |
|
157 |
# The mask should always be a boolean 64x64 mask, and then we
|
158 |
# can upsample it for the second stage.
|
159 |
+
# source_mask_64 = source_mask_64.unsqueeze(0)
|
160 |
+
# source_mask_256 = source_mask_256.unsqueeze(0)
|
161 |
# source_mask_256 = F.interpolate(source_mask_64, (256, 256), mode='nearest')
|
162 |
|
163 |
|