MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / preprocess

Method preprocess

src/diffusers/image_processor.py:789–886  ·  view source on GitHub ↗

Preprocess the image input. Accepted formats are PIL images, NumPy arrays or PyTorch tensors.

(
        self,
        rgb: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray],
        depth: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray],
        height: Optional[int] = None,
        width: Optional[int] = None,
        target_res: Optional[int] = None,
    )

Source from the content-addressed store, hash-verified

787 raise Exception(f"This type {output_type} is not supported")
788
789 def preprocess(
790 self,
791 rgb: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray],
792 depth: Union[torch.FloatTensor, PIL.Image.Image, np.ndarray],
793 height: Optional[int] = None,
794 width: Optional[int] = None,
795 target_res: Optional[int] = None,
796 ) -> torch.Tensor:
797 """
798 Preprocess the image input. Accepted formats are PIL images, NumPy arrays or PyTorch tensors.
799 """
800 supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor)
801
802 # Expand the missing dimension for 3-dimensional pytorch tensor or numpy array that represents grayscale image
803 if self.config.do_convert_grayscale and isinstance(rgb, (torch.Tensor, np.ndarray)) and rgb.ndim == 3:
804 raise Exception("This is not yet supported")
805
806 if isinstance(rgb, supported_formats):
807 rgb = [rgb]
808 depth = [depth]
809 elif not (isinstance(rgb, list) and all(isinstance(i, supported_formats) for i in rgb)):
810 raise ValueError(
811 f"Input is in incorrect format: {[type(i) for i in rgb]}. Currently, we only support {', '.join(supported_formats)}"
812 )
813
814 if isinstance(rgb[0], PIL.Image.Image):
815 if self.config.do_convert_rgb:
816 raise Exception("This is not yet supported")
817 # rgb = [self.convert_to_rgb(i) for i in rgb]
818 # depth = [self.convert_to_depth(i) for i in depth] #TODO define convert_to_depth
819 if self.config.do_resize or target_res:
820 height, width = self.get_default_height_width(rgb[0], height, width) if not target_res else target_res
821 rgb = [self.resize(i, height, width) for i in rgb]
822 depth = [self.resize(i, height, width) for i in depth]
823 rgb = self.pil_to_numpy(rgb) # to np
824 rgb = self.numpy_to_pt(rgb) # to pt
825
826 depth = self.depth_pil_to_numpy(depth) # to np
827 depth = self.numpy_to_pt(depth) # to pt
828
829 elif isinstance(rgb[0], np.ndarray):
830 rgb = np.concatenate(rgb, axis=0) if rgb[0].ndim == 4 else np.stack(rgb, axis=0)
831 rgb = self.numpy_to_pt(rgb)
832 height, width = self.get_default_height_width(rgb, height, width)
833 if self.config.do_resize:
834 rgb = self.resize(rgb, height, width)
835
836 depth = np.concatenate(depth, axis=0) if rgb[0].ndim == 4 else np.stack(depth, axis=0)
837 depth = self.numpy_to_pt(depth)
838 height, width = self.get_default_height_width(depth, height, width)
839 if self.config.do_resize:
840 depth = self.resize(depth, height, width)
841
842 elif isinstance(rgb[0], torch.Tensor):
843 raise Exception("This is not yet supported")
844 # rgb = torch.cat(rgb, axis=0) if rgb[0].ndim == 4 else torch.stack(rgb, axis=0)
845
846 # if self.config.do_convert_grayscale and rgb.ndim == 3:

Callers

nothing calls this directly

Calls 7

depth_pil_to_numpyMethod · 0.95
pil_to_numpyMethod · 0.80
binarizeMethod · 0.80
resizeMethod · 0.45
numpy_to_ptMethod · 0.45
normalizeMethod · 0.45

Tested by

no test coverage detected