diff --git a/internal_controlnet/external_code.py b/internal_controlnet/external_code.py index 8b631b069..157f08efc 100644 --- a/internal_controlnet/external_code.py +++ b/internal_controlnet/external_code.py @@ -115,154 +115,7 @@ def pixel_perfect_resolution( return int(np.round(estimation)) -<<<<<<< HEAD def to_base64_nparray(encoding: str) -> np.ndarray: -======= -InputImage = Union[np.ndarray, str] -InputImage = Union[Dict[str, InputImage], Tuple[InputImage, InputImage], InputImage] - - -@dataclass -class ControlNetUnit: - """ - Represents an entire ControlNet processing unit. - """ - - enabled: bool = True - module: str = "none" - model: str = "None" - weight: float = 1.0 - image: Optional[Union[InputImage, List[InputImage]]] = None - resize_mode: Union[ResizeMode, int, str] = ResizeMode.INNER_FIT - low_vram: bool = False - processor_res: int = -1 - threshold_a: float = -1 - threshold_b: float = -1 - guidance_start: float = 0.0 - guidance_end: float = 1.0 - pixel_perfect: bool = False - control_mode: Union[ControlMode, int, str] = ControlMode.BALANCED - # Whether to crop input image based on A1111 img2img mask. This flag is only used when `inpaint area` - # in A1111 is set to `Only masked`. In API, this correspond to `inpaint_full_res = True`. - inpaint_crop_input_image: bool = True - # If hires fix is enabled in A1111, how should this ControlNet unit be applied. - # The value is ignored if the generation is not using hires fix. - hr_option: Union[HiResFixOption, int, str] = HiResFixOption.BOTH - - # Whether save the detected map of this unit. Setting this option to False prevents saving the - # detected map or sending detected map along with generated images via API. - # Currently the option is only accessible in API calls. - save_detected_map: bool = True - - # Weight for each layer of ControlNet params. - # For ControlNet: - # - SD1.5: 13 weights (4 encoder block * 3 + 1 middle block) - # - SDXL: 10 weights (3 encoder block * 3 + 1 middle block) - # For T2IAdapter - # - SD1.5: 5 weights (4 encoder block + 1 middle block) - # - SDXL: 4 weights (3 encoder block + 1 middle block) - # For IPAdapter - # - SD15: 16 (6 input blocks + 9 output blocks + 1 middle block) - # - SDXL: 11 weights (4 input blocks + 6 output blocks + 1 middle block) - # Note1: Setting advanced weighting will disable `soft_injection`, i.e. - # It is recommended to set ControlMode = BALANCED when using `advanced_weighting`. - # Note2: The field `weight` is still used in some places, e.g. reference_only, - # even advanced_weighting is set. - advanced_weighting: Optional[List[float]] = None - - # The effective region mask that unit's effect should be restricted to. - effective_region_mask: Optional[np.ndarray] = None - - # Whether to crop image to effective mask's bounding box. - resize_to_effective_region: bool = False - - # The tensor input for ipadapter. When this field is set in the API, - # the base64string will be interpret by torch.load to reconstruct ipadapter - # preprocessor output. - # Currently the option is only accessible in API calls. - ipadapter_input: Optional[List[Any]] = None - - def __eq__(self, other): - if not isinstance(other, ControlNetUnit): - return False - - return vars(self) == vars(other) - - def accepts_multiple_inputs(self) -> bool: - """This unit can accept multiple input images.""" - return self.module in ( - "ip-adapter_clip_sdxl", - "ip-adapter_clip_sdxl_plus_vith", - "ip-adapter_clip_sd15", - "ip-adapter_face_id", - "ip-adapter_face_id_plus", - "instant_id_face_embedding", - ) - - @staticmethod - def infotext_excluded_fields() -> List[str]: - return [ - "image", - "enabled", - # API-only fields. - "advanced_weighting", - "ipadapter_input", - # End of API-only fields. - # Note: "inpaint_crop_image" is img2img inpaint only flag, which does not - # provide much information when restoring the unit. - "inpaint_crop_input_image", - "effective_region_mask", - "resize_to_effective_region", - ] - - @property - def is_animate_diff_batch(self) -> bool: - return getattr(self, "animatediff_batch", False) - - @property - def uses_clip(self) -> bool: - """Whether this unit uses clip preprocessor.""" - return any( - ( - ("ip-adapter" in self.module and "face_id" not in self.module), - self.module - in ("clip_vision", "revision_clipvision", "revision_ignore_prompt"), - ) - ) - - @property - def is_inpaint(self) -> bool: - return "inpaint" in self.module - - def bound_check_params(self) -> None: - """ - Checks and corrects negative parameters in ControlNetUnit 'unit' in place. - Parameters 'processor_res', 'threshold_a', 'threshold_b' are reset to - their default values if negative. - """ - preprocessor = Preprocessor.get_preprocessor(self.module) - for unit_param, param in zip( - ("processor_res", "threshold_a", "threshold_b"), - ("slider_resolution", "slider_1", "slider_2"), - ): - value = getattr(self, unit_param) - cfg: PreprocessorParameter = getattr(preprocessor, param) - if value < 0: - setattr(self, unit_param, cfg.value) - logger.info( - f"[{self.module}.{unit_param}] Invalid value({value}), using default value {cfg.value}." - ) - - def get_actual_preprocessor(self) -> Preprocessor: - if self.module == "ip-adapter-auto": - return Preprocessor.get_preprocessor(self.module).get_preprocessor_by_model( - self.model - ) - return Preprocessor.get_preprocessor(self.module) - - -def to_base64_nparray(encoding: str): ->>>>>>> 0365a14 (Plumbing) """ Convert a base64 image into the image type the extension uses """