| 179 | |
| 180 | |
| 181 | def prepare_image(image): |
| 182 | if isinstance(image, torch.Tensor): |
| 183 | # Batch single image |
| 184 | if image.ndim == 3: |
| 185 | image = image.unsqueeze(0) |
| 186 | |
| 187 | image = image.to(dtype=torch.float32) |
| 188 | else: |
| 189 | # preprocess image |
| 190 | if isinstance(image, (PIL.Image.Image, np.ndarray)): |
| 191 | image = [image] |
| 192 | |
| 193 | if isinstance(image, list) and isinstance(image[0], PIL.Image.Image): |
| 194 | image = [np.array(i.convert("RGB"))[None, :] for i in image] |
| 195 | image = np.concatenate(image, axis=0) |
| 196 | elif isinstance(image, list) and isinstance(image[0], np.ndarray): |
| 197 | image = np.concatenate([i[None, :] for i in image], axis=0) |
| 198 | |
| 199 | image = image.transpose(0, 3, 1, 2) |
| 200 | image = torch.from_numpy(image).to(dtype=torch.float32) / 127.5 - 1.0 |
| 201 | |
| 202 | return image |
| 203 | |
| 204 | |
| 205 | class TensorRTStableDiffusionControlNetImg2ImgPipeline(DiffusionPipeline): |