| 1220 | |
| 1221 | |
| 1222 | def prepare_image(image): |
| 1223 | if isinstance(image, torch.Tensor): |
| 1224 | # Batch single image |
| 1225 | if image.ndim == 3: |
| 1226 | image = image.unsqueeze(0) |
| 1227 | |
| 1228 | image = image.to(dtype=torch.float32) |
| 1229 | else: |
| 1230 | # preprocess image |
| 1231 | if isinstance(image, (PIL.Image.Image, np.ndarray)): |
| 1232 | image = [image] |
| 1233 | |
| 1234 | if isinstance(image, list) and isinstance(image[0], PIL.Image.Image): |
| 1235 | image = [np.array(i.convert("RGB"))[None, :] for i in image] |
| 1236 | image = np.concatenate(image, axis=0) |
| 1237 | elif isinstance(image, list) and isinstance(image[0], np.ndarray): |
| 1238 | image = np.concatenate([i[None, :] for i in image], axis=0) |
| 1239 | |
| 1240 | image = image.transpose(0, 3, 1, 2) |
| 1241 | image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0 |
| 1242 | |
| 1243 | return image |
| 1244 | |
| 1245 | |
| 1246 | class FrescoV2VPipeline(StableDiffusionControlNetImg2ImgPipeline): |