libcom 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libcom/__init__.py +23 -0
- libcom/color_transfer/__init__.py +3 -0
- libcom/color_transfer/reinhard.py +57 -0
- libcom/fopa_heat_map/__init__.py +4 -0
- libcom/fopa_heat_map/fopa_heat_map.py +212 -0
- libcom/fopa_heat_map/source/__init__.py +0 -0
- libcom/fopa_heat_map/source/backbone/ResNet.py +229 -0
- libcom/fopa_heat_map/source/backbone/__init__.py +0 -0
- libcom/fopa_heat_map/source/data/OBdataset.py +292 -0
- libcom/fopa_heat_map/source/data/__init__.py +0 -0
- libcom/fopa_heat_map/source/data/all_transforms.py +25 -0
- libcom/fopa_heat_map/source/network/BaseBlocks.py +26 -0
- libcom/fopa_heat_map/source/network/DynamicModules.py +43 -0
- libcom/fopa_heat_map/source/network/ObPlaNet_simple.py +112 -0
- libcom/fopa_heat_map/source/network/__init__.py +1 -0
- libcom/fopa_heat_map/source/network/tensor_ops.py +37 -0
- libcom/fopa_heat_map/source/prepare_multi_fg_scales.py +85 -0
- libcom/fos_score/__init__.py +4 -0
- libcom/fos_score/fos_score_prediction.py +218 -0
- libcom/fos_score/source/__init__.py +0 -0
- libcom/fos_score/source/config/__init__.py +1 -0
- libcom/fos_score/source/config/config.py +68 -0
- libcom/fos_score/source/config/config.sfosd.yaml +43 -0
- libcom/fos_score/source/config/config_rfosd.yaml +43 -0
- libcom/fos_score/source/loss_function.py +118 -0
- libcom/fos_score/source/networks.py +355 -0
- libcom/harmony_score/__init__.py +3 -0
- libcom/harmony_score/harmony_score_prediction.py +119 -0
- libcom/harmony_score/source/__init__.py +0 -0
- libcom/harmony_score/source/bargainnet.py +226 -0
- libcom/image_harmonization/__init__.py +4 -0
- libcom/image_harmonization/image_harmonization.py +193 -0
- libcom/image_harmonization/source/__init__.py +0 -0
- libcom/image_harmonization/source/functions.py +747 -0
- libcom/image_harmonization/source/pct_net.py +85 -0
- libcom/image_harmonization/source/src/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/config.py +141 -0
- libcom/image_harmonization/source/src/lbm/data/__init__.py +62 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/__init__.py +9 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/collation_fn.py +41 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/dataset.py +243 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/datasets_config.py +42 -0
- libcom/image_harmonization/source/src/lbm/data/filters/__init__.py +12 -0
- libcom/image_harmonization/source/src/lbm/data/filters/base.py +21 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filter_wrapper.py +36 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filters.py +33 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filters_config.py +32 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/__init__.py +19 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/base.py +26 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers.py +135 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers_config.py +109 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers_wrapper.py +31 -0
- libcom/image_harmonization/source/src/lbm/inference/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/inference/inference.py +70 -0
- libcom/image_harmonization/source/src/lbm/inference/utils.py +222 -0
- libcom/image_harmonization/source/src/lbm/models/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/models/base/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/base/base_model.py +66 -0
- libcom/image_harmonization/source/src/lbm/models/base/model_config.py +8 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/base_conditioner.py +60 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/base_conditioner_config.py +27 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/conditioners_wrapper.py +114 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/latents_concat_embedder_config.py +31 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/latents_concat_embedder_model.py +80 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/lbm_config.py +101 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/lbm_model.py +359 -0
- libcom/image_harmonization/source/src/lbm/models/unets/__init__.py +14 -0
- libcom/image_harmonization/source/src/lbm/models/unets/unet.py +148 -0
- libcom/image_harmonization/source/src/lbm/models/utils.py +377 -0
- libcom/image_harmonization/source/src/lbm/models/vae/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/vae/autoencoderKL.py +136 -0
- libcom/image_harmonization/source/src/lbm/models/vae/autoencoderKL_config.py +27 -0
- libcom/image_harmonization/source/src/lbm/trainer/__init__.py +85 -0
- libcom/image_harmonization/source/src/lbm/trainer/loggers.py +324 -0
- libcom/image_harmonization/source/src/lbm/trainer/trainer.py +199 -0
- libcom/image_harmonization/source/src/lbm/trainer/training_config.py +82 -0
- libcom/image_harmonization/source/src/lbm/trainer/utils.py +193 -0
- libcom/inharmonious_region_localization/__init__.py +4 -0
- libcom/inharmonious_region_localization/inharmonious_region_localization.py +97 -0
- libcom/inharmonious_region_localization/source/__init__.py +0 -0
- libcom/inharmonious_region_localization/source/blocks.py +368 -0
- libcom/inharmonious_region_localization/source/functions.py +679 -0
- libcom/inharmonious_region_localization/source/madis_net.py +79 -0
- libcom/kontext_blending_harmonization/__init__.py +3 -0
- libcom/kontext_blending_harmonization/kontext_blending_harmonization.py +148 -0
- libcom/kontext_blending_harmonization/source/__init__.py +0 -0
- libcom/kontext_blending_harmonization/source/pipeline.py +1135 -0
- libcom/naive_composition/__init__.py +3 -0
- libcom/naive_composition/generate_composite_image.py +116 -0
- libcom/opa_score/__init__.py +3 -0
- libcom/opa_score/opa_score_prediction.py +101 -0
- libcom/opa_score/source/__init__.py +3 -0
- libcom/opa_score/source/object_place_net.py +55 -0
- libcom/opa_score/source/oblect_place_config.py +66 -0
- libcom/opa_score/source/resnet_4ch.py +213 -0
- libcom/os_insert/__init__.py +3 -0
- libcom/os_insert/diffusers_osinsert/__init__.py +78 -0
- libcom/os_insert/diffusers_osinsert/_patched_pipeline_flux_fill.py +878 -0
- libcom/os_insert/os_insert.py +534 -0
- libcom/os_insert/source/__init__.py +1 -0
- libcom/os_insert/source/ia_utils.py +183 -0
- libcom/os_insert/source/insertanything_infer.py +529 -0
- libcom/os_insert/source/ldm/__init__.py +0 -0
- libcom/os_insert/source/ldm/data/__init__.py +0 -0
- libcom/os_insert/source/ldm/data/base.py +23 -0
- libcom/os_insert/source/ldm/data/datamodule.py +128 -0
- libcom/os_insert/source/ldm/data/imagenet.py +394 -0
- libcom/os_insert/source/ldm/data/lsun.py +92 -0
- libcom/os_insert/source/ldm/data/open_images.py +728 -0
- libcom/os_insert/source/ldm/lr_scheduler.py +98 -0
- libcom/os_insert/source/ldm/models/__init__.py +0 -0
- libcom/os_insert/source/ldm/models/autoencoder.py +443 -0
- libcom/os_insert/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/os_insert/source/ldm/models/diffusion/classifier.py +267 -0
- libcom/os_insert/source/ldm/models/diffusion/ddim.py +263 -0
- libcom/os_insert/source/ldm/models/diffusion/ddpm.py +1417 -0
- libcom/os_insert/source/ldm/models/diffusion/plms.py +252 -0
- libcom/os_insert/source/ldm/modules/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/attention.py +261 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/model.py +835 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/openaimodel.py +1108 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/util.py +267 -0
- libcom/os_insert/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/os_insert/source/ldm/modules/ema.py +76 -0
- libcom/os_insert/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/encoders/modules.py +190 -0
- libcom/os_insert/source/ldm/modules/encoders/xf.py +130 -0
- libcom/os_insert/source/ldm/modules/losses/__init__.py +1 -0
- libcom/os_insert/source/ldm/modules/losses/contperceptual.py +111 -0
- libcom/os_insert/source/ldm/modules/losses/vqperceptual.py +167 -0
- libcom/os_insert/source/ldm/modules/x_transformer.py +641 -0
- libcom/os_insert/source/ldm/util.py +203 -0
- libcom/os_insert/source/objectstitch_data.py +66 -0
- libcom/os_insert/source/objectstitch_infer.py +618 -0
- libcom/os_insert/source/sam_on_objectstitch.py +253 -0
- libcom/os_insert/source/utils.py +41 -0
- libcom/os_insert/source/v1.yaml +108 -0
- libcom/painterly_image_harmonization/__init__.py +4 -0
- libcom/painterly_image_harmonization/painterly_image_harmonization.py +215 -0
- libcom/painterly_image_harmonization/source/PHDNet/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDNet/networks.py +202 -0
- libcom/painterly_image_harmonization/source/PHDNet/phdnet.py +47 -0
- libcom/painterly_image_harmonization/source/PHDNet/phdnet_config.py +18 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/inference_base.py +300 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/lr_scheduler.py +98 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/autoencoder.py +211 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/__init__.py +3 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/ddim.py +336 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/ddpm.py +1400 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/dpm_solver.py +1217 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/plms.py +243 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/scheduling_pndm.py +518 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/attention.py +378 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/model.py +857 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/openaimodel.py +823 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/util.py +270 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/distributions/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/distributions/distributions.py +92 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/ema.py +80 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/adapter.py +213 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/modules.py +82 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/__init__.py +1 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/api.py +269 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/api.py +175 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/base_model.py +16 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/blocks.py +342 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/dpt_depth.py +109 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/midas_net.py +76 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/midas_net_custom.py +128 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/transforms.py +234 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/vit.py +491 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/utils.py +189 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/model_edge.py +653 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/api.py +35 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/body.py +209 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/hand.py +75 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/model.py +178 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/util.py +203 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/utils.py +72 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/fusion/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/fusion/dual_encoder_fusion.py +93 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/__init__.py +2 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/util.py +200 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/stable_diffusion.yaml +75 -0
- libcom/painterly_image_harmonization/source/__init__.py +0 -0
- libcom/reflection_generation/__init__.py +3 -0
- libcom/reflection_generation/reflection_generation.py +174 -0
- libcom/reflection_generation/source/PostProcessModel.py +513 -0
- libcom/reflection_generation/source/__init__.py +0 -0
- libcom/reflection_generation/source/cldm/__init__.py +0 -0
- libcom/reflection_generation/source/cldm/base_network.py +166 -0
- libcom/reflection_generation/source/cldm/cldm.py +509 -0
- libcom/reflection_generation/source/cldm/ddim_hacked.py +317 -0
- libcom/reflection_generation/source/cldm/hack.py +111 -0
- libcom/reflection_generation/source/cldm/logger.py +127 -0
- libcom/reflection_generation/source/cldm/model.py +26 -0
- libcom/reflection_generation/source/cldm_v15.yaml +81 -0
- libcom/reflection_generation/source/cluster_feature.py +109 -0
- libcom/reflection_generation/source/ldm/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/autoencoder.py +219 -0
- libcom/reflection_generation/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/diffusion/attention_processor.py +188 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ddim.py +340 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ddpm.py +1892 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/dpm_solver.py +1154 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ip_adapter.py +57 -0
- libcom/reflection_generation/source/ldm/models/diffusion/plms.py +244 -0
- libcom/reflection_generation/source/ldm/models/diffusion/sampling_util.py +22 -0
- libcom/reflection_generation/source/ldm/modules/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/attention.py +341 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/model.py +852 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/openaimodel.py +786 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/upscaling.py +81 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/util.py +280 -0
- libcom/reflection_generation/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/reflection_generation/source/ldm/modules/ema.py +80 -0
- libcom/reflection_generation/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/encoders/modules.py +214 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/reflection_generation/source/ldm/util.py +198 -0
- libcom/reflection_generation/source/local_clip.py +749 -0
- libcom/reflection_generation/source/tool_add_control.py +49 -0
- libcom/shadow_generation/__init__.py +3 -0
- libcom/shadow_generation/shadow_generation.py +754 -0
- libcom/shadow_generation/source/PostProcessModel.py +511 -0
- libcom/shadow_generation/source/__init__.py +0 -0
- libcom/shadow_generation/source/attention_processor.py +234 -0
- libcom/shadow_generation/source/cldm/__init__.py +0 -0
- libcom/shadow_generation/source/cldm/base_network.py +166 -0
- libcom/shadow_generation/source/cldm/cldm.py +524 -0
- libcom/shadow_generation/source/cldm/model.py +28 -0
- libcom/shadow_generation/source/ldm/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/autoencoder.py +219 -0
- libcom/shadow_generation/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/diffusion/attention_processor.py +188 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ddim.py +340 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ddpm.py +1893 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/dpm_solver.py +1154 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ip_adapter.py +57 -0
- libcom/shadow_generation/source/ldm/models/diffusion/plms.py +244 -0
- libcom/shadow_generation/source/ldm/models/diffusion/sampling_util.py +22 -0
- libcom/shadow_generation/source/ldm/modules/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/attention.py +341 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/model.py +852 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/openaimodel.py +786 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/upscaling.py +81 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/util.py +270 -0
- libcom/shadow_generation/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/shadow_generation/source/ldm/modules/ema.py +80 -0
- libcom/shadow_generation/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/encoders/modules.py +214 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/__init__.py +2 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/shadow_generation/source/ldm/modules/midas/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/midas/api.py +170 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/base_model.py +16 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/blocks.py +342 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/dpt_depth.py +109 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/midas_net.py +76 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/midas_net_custom.py +128 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/transforms.py +234 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/vit.py +491 -0
- libcom/shadow_generation/source/ldm/modules/midas/utils.py +189 -0
- libcom/shadow_generation/source/ldm/util.py +199 -0
- libcom/utils/__init__.py +0 -0
- libcom/utils/environment.py +12 -0
- libcom/utils/model_download.py +67 -0
- libcom/utils/process_image.py +108 -0
- libcom-0.1.8.dist-info/METADATA +157 -0
- libcom-0.1.8.dist-info/RECORD +310 -0
- libcom-0.1.8.dist-info/WHEEL +5 -0
- libcom-0.1.8.dist-info/licenses/LICENSE +201 -0
- libcom-0.1.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,534 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from typing import Literal
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
import cv2
|
|
11
|
+
import numpy as np
|
|
12
|
+
|
|
13
|
+
from libcom.utils.model_download import download_pretrained_model, download_entire_folder
|
|
14
|
+
from .source.insertanything_infer import InsertAnythingModel, insertanything_infer, run_insertanything
|
|
15
|
+
from .source.objectstitch_infer import (
|
|
16
|
+
ObjectStitchConfig,
|
|
17
|
+
run_objectstitch_single_image,
|
|
18
|
+
run_objectstitch_single_image_from_images,
|
|
19
|
+
load_objectstitch_model_and_sampler,
|
|
20
|
+
run_objectstitch_single_image_from_images_cached,
|
|
21
|
+
)
|
|
22
|
+
from .source.sam_on_objectstitch import (
|
|
23
|
+
SamOnObjectStitchConfig,
|
|
24
|
+
build_sam_predictor,
|
|
25
|
+
run_sam_on_objectstitch,
|
|
26
|
+
run_sam_on_objectstitch_with_predictor,
|
|
27
|
+
)
|
|
28
|
+
from .source.utils import load_bbox_txt, make_rect_mask_from_bbox
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
@dataclass
|
|
32
|
+
class OSInsertConfig:
|
|
33
|
+
model_dir: Path
|
|
34
|
+
device: str = "cuda:0"
|
|
35
|
+
objectstitch_ckpt_path: Path | None = None
|
|
36
|
+
objectstitch_config_path: Path | None = None
|
|
37
|
+
objectstitch_clip_dir: Path | None = None
|
|
38
|
+
sam_checkpoint: Path | None = None
|
|
39
|
+
flux_fill_path: Path | None = None
|
|
40
|
+
flux_redux_path: Path | None = None
|
|
41
|
+
ia_lora_path: Path | None = None
|
|
42
|
+
|
|
43
|
+
class OSInsertModel:
|
|
44
|
+
"""
|
|
45
|
+
High-level OSInsert interface.
|
|
46
|
+
|
|
47
|
+
This model provides a unified interface for object insertion with two modes:
|
|
48
|
+
conservative and aggressive. It internally combines multiple sub-models such
|
|
49
|
+
as InsertAnything, ObjectStitch, and SAM.
|
|
50
|
+
|
|
51
|
+
Modes
|
|
52
|
+
-----
|
|
53
|
+
- ``aggressive``:
|
|
54
|
+
ObjectStitch + SAM + InsertAnything pipeline.
|
|
55
|
+
Suitable for more complex and flexible compositions.
|
|
56
|
+
|
|
57
|
+
- ``conservative``:
|
|
58
|
+
Directly uses background + bbox to generate mask,
|
|
59
|
+
then performs insertion via InsertAnything.
|
|
60
|
+
Faster and more stable.
|
|
61
|
+
|
|
62
|
+
Args:
|
|
63
|
+
device (str): Device to run the model on (e.g., "cuda:0", "cpu").
|
|
64
|
+
model_dir (str | Path | None): Root directory of all model checkpoints.
|
|
65
|
+
eager_aggressive_init (bool):
|
|
66
|
+
If True, preload ObjectStitch and SAM models at initialization.
|
|
67
|
+
Otherwise, they will be lazily loaded when first used.
|
|
68
|
+
objectstitch_ckpt_path (str | Path | None):
|
|
69
|
+
Path to ObjectStitch checkpoint.
|
|
70
|
+
objectstitch_config_path (str | Path | None):
|
|
71
|
+
Path to ObjectStitch config file.
|
|
72
|
+
objectstitch_clip_dir (str | Path | None):
|
|
73
|
+
Path to CLIP model directory used by ObjectStitch.
|
|
74
|
+
sam_checkpoint (str | Path | None):
|
|
75
|
+
Path to SAM (Segment Anything Model) checkpoint.
|
|
76
|
+
flux_fill_path (str | Path | None):
|
|
77
|
+
Path to Flux Fill model directory.
|
|
78
|
+
flux_redux_path (str | Path | None):
|
|
79
|
+
Path to Flux Redux model directory.
|
|
80
|
+
ia_lora_path (str | Path | None):
|
|
81
|
+
Path to LoRA weights for InsertAnything.
|
|
82
|
+
|
|
83
|
+
Notes
|
|
84
|
+
-----
|
|
85
|
+
- InsertAnything is initialized during class construction.
|
|
86
|
+
- ObjectStitch and SAM are lazily initialized (unless
|
|
87
|
+
``eager_aggressive_init=True``), and then cached for reuse.
|
|
88
|
+
- Conservative mode does not require ObjectStitch.
|
|
89
|
+
|
|
90
|
+
Examples:
|
|
91
|
+
>>> import cv2
|
|
92
|
+
>>> from libcom import OSInsertModel
|
|
93
|
+
|
|
94
|
+
>>> model = OSInsertModel(
|
|
95
|
+
>>> device="cuda:0"
|
|
96
|
+
>>> )
|
|
97
|
+
|
|
98
|
+
>>> bg = cv2.imread("tests/osinsert/background/Demo_0.png")
|
|
99
|
+
>>> fg = cv2.imread("tests/osinsert/foreground/Demo_0.png")
|
|
100
|
+
>>> fg_mask = cv2.imread(
|
|
101
|
+
>>> "tests/osinsert/foreground_mask/Demo_0.png",
|
|
102
|
+
>>> cv2.IMREAD_GRAYSCALE
|
|
103
|
+
>>> )
|
|
104
|
+
|
|
105
|
+
>>> bbox = (175, 184, 363, 372)
|
|
106
|
+
|
|
107
|
+
>>> result = model.infer_images(
|
|
108
|
+
>>> background=bg,
|
|
109
|
+
>>> foreground=fg,
|
|
110
|
+
>>> foreground_mask=fg_mask,
|
|
111
|
+
>>> bbox_xyxy=bbox,
|
|
112
|
+
>>> mode="conservative", # or "aggressive"
|
|
113
|
+
>>> verbose=False,
|
|
114
|
+
>>> seed=123,
|
|
115
|
+
>>> strength=1.0,
|
|
116
|
+
>>> split_ratio=0.33,
|
|
117
|
+
>>> save_path="result_dir/conservative",
|
|
118
|
+
>>> )
|
|
119
|
+
|
|
120
|
+
Expected result:
|
|
121
|
+
The foreground object is inserted into the background image
|
|
122
|
+
at the specified bounding box, with realistic blending.
|
|
123
|
+
|
|
124
|
+
.. image:: _static/image/os_insert_result.jpg
|
|
125
|
+
|
|
126
|
+
"""
|
|
127
|
+
def __init__(
|
|
128
|
+
self,
|
|
129
|
+
device: str = "cuda:0",
|
|
130
|
+
model_dir: str | Path | None = None,
|
|
131
|
+
*,
|
|
132
|
+
eager_aggressive_init: bool = False,
|
|
133
|
+
objectstitch_ckpt_path: str | Path | None = None,
|
|
134
|
+
objectstitch_config_path: str | Path | None = None,
|
|
135
|
+
objectstitch_clip_dir: str | Path | None = None,
|
|
136
|
+
sam_checkpoint: str | Path | None = None,
|
|
137
|
+
flux_fill_path: str | Path | None = None,
|
|
138
|
+
flux_redux_path: str | Path | None = None,
|
|
139
|
+
ia_lora_path: str | Path | None = None,
|
|
140
|
+
) -> None:
|
|
141
|
+
# 设置模型目录
|
|
142
|
+
cur_dir = os.path.dirname(os.path.abspath(__file__))
|
|
143
|
+
self.model_dir = os.environ.get('LIBCOM_MODEL_DIR', cur_dir)
|
|
144
|
+
|
|
145
|
+
# 设置默认路径
|
|
146
|
+
if model_dir is None:
|
|
147
|
+
model_dir = self.model_dir
|
|
148
|
+
|
|
149
|
+
# 下载必要的模型权重
|
|
150
|
+
self._download_model_weights()
|
|
151
|
+
|
|
152
|
+
self.config = OSInsertConfig(
|
|
153
|
+
model_dir=Path(model_dir),
|
|
154
|
+
device=device,
|
|
155
|
+
objectstitch_ckpt_path=Path(objectstitch_ckpt_path) if objectstitch_ckpt_path is not None else None,
|
|
156
|
+
objectstitch_config_path=Path(objectstitch_config_path) if objectstitch_config_path is not None else None,
|
|
157
|
+
objectstitch_clip_dir=Path(objectstitch_clip_dir) if objectstitch_clip_dir is not None else None,
|
|
158
|
+
sam_checkpoint=Path(sam_checkpoint) if sam_checkpoint is not None else None,
|
|
159
|
+
flux_fill_path=Path(flux_fill_path) if flux_fill_path is not None else None,
|
|
160
|
+
flux_redux_path=Path(flux_redux_path) if flux_redux_path is not None else None,
|
|
161
|
+
ia_lora_path=Path(ia_lora_path) if ia_lora_path is not None else None,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
self._ia_net = InsertAnythingModel(
|
|
165
|
+
model_dir=self.config.model_dir,
|
|
166
|
+
flux_fill_path=self.config.flux_fill_path,
|
|
167
|
+
flux_redux_path=self.config.flux_redux_path,
|
|
168
|
+
ia_lora_path=self.config.ia_lora_path,
|
|
169
|
+
device=self.config.device,
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
self._os_model = None
|
|
173
|
+
self._os_sampler = None
|
|
174
|
+
self._sam_predictor = None
|
|
175
|
+
|
|
176
|
+
if eager_aggressive_init:
|
|
177
|
+
self._get_objectstitch_model_and_sampler()
|
|
178
|
+
self._get_sam_predictor()
|
|
179
|
+
|
|
180
|
+
def _download_model_weights(self):
|
|
181
|
+
"""下载所有必要的模型权重"""
|
|
182
|
+
# ObjectStitch 模型权重
|
|
183
|
+
objectstitch_ckpt_path = os.path.join(self.model_dir, 'pretrained_models', 'ObjectStitch.pth')
|
|
184
|
+
download_pretrained_model(objectstitch_ckpt_path)
|
|
185
|
+
|
|
186
|
+
# ObjectStitch 配置文件 - 使用 source 目录中的本地文件
|
|
187
|
+
objectstitch_config_path = os.path.join(os.path.dirname(__file__), 'source', 'v1.yaml')
|
|
188
|
+
if not os.path.exists(objectstitch_config_path):
|
|
189
|
+
print(f'v1.yaml not found at {objectstitch_config_path}')
|
|
190
|
+
raise Exception('v1.yaml not found')
|
|
191
|
+
print('Using local v1.yaml configuration file from source directory')
|
|
192
|
+
|
|
193
|
+
# CLIP 模型
|
|
194
|
+
clip_dir = os.path.join(self.model_dir, 'shared_pretrained_models', 'openai-clip-vit-large-patch14')
|
|
195
|
+
download_entire_folder(clip_dir)
|
|
196
|
+
|
|
197
|
+
# SAM 模型权重
|
|
198
|
+
sam_ckpt_path = os.path.join(self.model_dir, 'pretrained_models', 'sam_vit_h_4b8939.pth')
|
|
199
|
+
download_pretrained_model(sam_ckpt_path)
|
|
200
|
+
|
|
201
|
+
# InsertAnything LoRA 权重
|
|
202
|
+
ia_lora_path = os.path.join(self.model_dir, 'pretrained_models', 'insert_anything_lora.safetensors')
|
|
203
|
+
download_pretrained_model(ia_lora_path)
|
|
204
|
+
|
|
205
|
+
def _get_objectstitch_model_and_sampler(self):
|
|
206
|
+
if self._os_model is not None and self._os_sampler is not None:
|
|
207
|
+
return self._os_model, self._os_sampler
|
|
208
|
+
|
|
209
|
+
objectstitch_ckpt = self.config.objectstitch_ckpt_path
|
|
210
|
+
if objectstitch_ckpt is None:
|
|
211
|
+
objectstitch_ckpt = os.path.join(self.model_dir, 'pretrained_models', 'ObjectStitch.pth')
|
|
212
|
+
|
|
213
|
+
objectstitch_cfg = self.config.objectstitch_config_path
|
|
214
|
+
if objectstitch_cfg is None:
|
|
215
|
+
objectstitch_cfg = os.path.join(os.path.dirname(__file__), 'source', 'v1.yaml')
|
|
216
|
+
|
|
217
|
+
clip_dir = self.config.objectstitch_clip_dir
|
|
218
|
+
if clip_dir is None:
|
|
219
|
+
clip_dir = os.path.join(self.model_dir, 'shared_pretrained_models', 'openai-clip-vit-large-patch14')
|
|
220
|
+
|
|
221
|
+
os_cfg = ObjectStitchConfig(
|
|
222
|
+
ckpt_path=objectstitch_ckpt,
|
|
223
|
+
config_path=objectstitch_cfg,
|
|
224
|
+
clip_dir=clip_dir,
|
|
225
|
+
device=self.config.device,
|
|
226
|
+
)
|
|
227
|
+
self._os_model, self._os_sampler = load_objectstitch_model_and_sampler(config=os_cfg)
|
|
228
|
+
return self._os_model, self._os_sampler
|
|
229
|
+
|
|
230
|
+
def _get_sam_predictor(self):
|
|
231
|
+
if self._sam_predictor is not None:
|
|
232
|
+
return self._sam_predictor
|
|
233
|
+
|
|
234
|
+
sam_ckpt = self.config.sam_checkpoint
|
|
235
|
+
if sam_ckpt is None:
|
|
236
|
+
sam_ckpt = os.path.join(self.model_dir, 'pretrained_models', 'sam_vit_h_4b8939.pth')
|
|
237
|
+
|
|
238
|
+
sam_cfg = SamOnObjectStitchConfig(
|
|
239
|
+
sam_checkpoint=sam_ckpt,
|
|
240
|
+
device=self.config.device,
|
|
241
|
+
)
|
|
242
|
+
self._sam_predictor = build_sam_predictor(config=sam_cfg)
|
|
243
|
+
return self._sam_predictor
|
|
244
|
+
|
|
245
|
+
def __call__(
|
|
246
|
+
self,
|
|
247
|
+
background_path: str | Path,
|
|
248
|
+
foreground_path: str | Path,
|
|
249
|
+
foreground_mask_path: str | Path,
|
|
250
|
+
bbox: list[int],
|
|
251
|
+
result_dir: str | Path,
|
|
252
|
+
mode: Literal["aggressive", "conservative"] = "conservative",
|
|
253
|
+
cleanup_intermediate: bool = True,
|
|
254
|
+
verbose: bool = False,
|
|
255
|
+
seed: int = 123,
|
|
256
|
+
strength: float = 1.0,
|
|
257
|
+
split_ratio: float = 0.5,
|
|
258
|
+
) -> np.ndarray | None:
|
|
259
|
+
"""Run a single OSInsert inference.
|
|
260
|
+
|
|
261
|
+
Parameters
|
|
262
|
+
----------
|
|
263
|
+
background_path:
|
|
264
|
+
Path to the background image.
|
|
265
|
+
foreground_path:
|
|
266
|
+
Path to the foreground image used as the InsertAnything reference
|
|
267
|
+
image.
|
|
268
|
+
foreground_mask_path:
|
|
269
|
+
Binary mask for the foreground image.
|
|
270
|
+
bbox:
|
|
271
|
+
List containing ``[x1, y1, x2, y2]``, specifying the
|
|
272
|
+
insertion region on the background image.
|
|
273
|
+
result_dir:
|
|
274
|
+
Directory where the final composed image will be written.
|
|
275
|
+
mode:
|
|
276
|
+
- ``"conservative"``: background + bbox -> mask -> InsertAnything.
|
|
277
|
+
- ``"aggressive"``: ObjectStitch + SAM -> combined source/mask ->
|
|
278
|
+
InsertAnything.
|
|
279
|
+
cleanup_intermediate:
|
|
280
|
+
Deprecated. Present for backward compatibility.
|
|
281
|
+
verbose:
|
|
282
|
+
If True, save intermediate artifacts into ``result_dir/intermediates``.
|
|
283
|
+
Default False (do not save intermediates).
|
|
284
|
+
seed:
|
|
285
|
+
Random seed for InsertAnything.
|
|
286
|
+
strength:
|
|
287
|
+
InsertAnything strength parameter.
|
|
288
|
+
|
|
289
|
+
Returns:
|
|
290
|
+
Generated composited image (np.array): The inserted result.
|
|
291
|
+
"""
|
|
292
|
+
|
|
293
|
+
if mode not in {"aggressive", "conservative"}:
|
|
294
|
+
raise ValueError(f"Unsupported mode: {mode}")
|
|
295
|
+
|
|
296
|
+
# ------------------------------------------------------------------
|
|
297
|
+
# Path normalization and output directory.
|
|
298
|
+
# ------------------------------------------------------------------
|
|
299
|
+
background_path = Path(background_path)
|
|
300
|
+
foreground_path = Path(foreground_path)
|
|
301
|
+
foreground_mask_path = Path(foreground_mask_path)
|
|
302
|
+
result_dir = Path(result_dir)
|
|
303
|
+
|
|
304
|
+
os.makedirs(result_dir, exist_ok=True)
|
|
305
|
+
|
|
306
|
+
intermediates_dir = result_dir / "intermediates"
|
|
307
|
+
if verbose:
|
|
308
|
+
os.makedirs(intermediates_dir, exist_ok=True)
|
|
309
|
+
|
|
310
|
+
# InsertAnything expects a list of seeds.
|
|
311
|
+
seeds = [seed]
|
|
312
|
+
|
|
313
|
+
# Load background once; used by both modes.
|
|
314
|
+
bg = cv2.imread(str(background_path))
|
|
315
|
+
if bg is None:
|
|
316
|
+
raise FileNotFoundError(background_path)
|
|
317
|
+
h, w = bg.shape[:2]
|
|
318
|
+
|
|
319
|
+
fg = cv2.imread(str(foreground_path))
|
|
320
|
+
if fg is None:
|
|
321
|
+
raise FileNotFoundError(foreground_path)
|
|
322
|
+
fg_mask = cv2.imread(str(foreground_mask_path))
|
|
323
|
+
if fg_mask is None:
|
|
324
|
+
raise FileNotFoundError(foreground_mask_path)
|
|
325
|
+
if fg_mask.ndim == 3:
|
|
326
|
+
fg_mask = fg_mask[:, :, 0]
|
|
327
|
+
|
|
328
|
+
# 直接使用传入的 bbox 列表
|
|
329
|
+
assert len(bbox) == 4, f"bbox should be a list of 4 integers: [x1, y1, x2, y2], got {bbox}"
|
|
330
|
+
|
|
331
|
+
# ------------------------------------------------------------------
|
|
332
|
+
# Aggressive mode: ObjectStitch + SAM + InsertAnything.
|
|
333
|
+
# ------------------------------------------------------------------
|
|
334
|
+
if mode == "aggressive":
|
|
335
|
+
# 1) ObjectStitch coarse composite (cached weights).
|
|
336
|
+
os_model, os_sampler = self._get_objectstitch_model_and_sampler()
|
|
337
|
+
os_rgb = run_objectstitch_single_image_from_images_cached(
|
|
338
|
+
background=cv2.cvtColor(bg, cv2.COLOR_BGR2RGB),
|
|
339
|
+
foreground=cv2.cvtColor(fg, cv2.COLOR_BGR2RGB),
|
|
340
|
+
foreground_mask=fg_mask,
|
|
341
|
+
bbox_xyxy=tuple(bbox),
|
|
342
|
+
model=os_model,
|
|
343
|
+
sampler=os_sampler,
|
|
344
|
+
device=self.config.device,
|
|
345
|
+
seed=seed,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
# 2) SAM mask on top of ObjectStitch composite (in-memory).
|
|
349
|
+
predictor = self._get_sam_predictor()
|
|
350
|
+
sam_mask = run_sam_on_objectstitch_with_predictor(
|
|
351
|
+
predictor=predictor,
|
|
352
|
+
os_image=cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR),
|
|
353
|
+
bg_shape_hw=(h, w),
|
|
354
|
+
bbox_xyxy_bg=tuple(bbox),
|
|
355
|
+
)
|
|
356
|
+
|
|
357
|
+
# 3) Construct InsertAnything source & mask following
|
|
358
|
+
# exp/run_insertanything_strength_sweep_dispatch.py::make_source_and_mask
|
|
359
|
+
bg_bgr = bg # already read above
|
|
360
|
+
os_bgr = cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR)
|
|
361
|
+
|
|
362
|
+
hh, ww = bg_bgr.shape[:2]
|
|
363
|
+
os_bgr = cv2.resize(os_bgr, (ww, hh), interpolation=cv2.INTER_AREA)
|
|
364
|
+
if sam_mask.shape[:2] != (hh, ww):
|
|
365
|
+
sam_mask = cv2.resize(sam_mask, (ww, hh), interpolation=cv2.INTER_NEAREST)
|
|
366
|
+
|
|
367
|
+
m = (sam_mask > 127).astype(np.float32)
|
|
368
|
+
m3 = np.stack([m, m, m], axis=-1)
|
|
369
|
+
|
|
370
|
+
src_bgr = bg_bgr.astype(np.float32) * (1.0 - m3) + os_bgr.astype(np.float32) * m3
|
|
371
|
+
src_bgr = np.clip(src_bgr, 0, 255).astype(np.uint8)
|
|
372
|
+
|
|
373
|
+
# BBox-based mask for InsertAnything second-stage (bbox mask)
|
|
374
|
+
bbox_mask = make_rect_mask_from_bbox(h, w, bbox)
|
|
375
|
+
|
|
376
|
+
if verbose:
|
|
377
|
+
cv2.imwrite(str(intermediates_dir / "objectstitch_coarse_rgb.png"), cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR))
|
|
378
|
+
cv2.imwrite(str(intermediates_dir / "sam_mask.png"), sam_mask)
|
|
379
|
+
cv2.imwrite(str(intermediates_dir / "blended_source.png"), src_bgr)
|
|
380
|
+
cv2.imwrite(str(intermediates_dir / "bbox_mask.png"), bbox_mask)
|
|
381
|
+
|
|
382
|
+
# 4) InsertAnything refinement using blended source, bbox mask
|
|
383
|
+
# (for the second half of denoising) and SAM mask
|
|
384
|
+
# (for the first half, wired via sam_mask_path).
|
|
385
|
+
result = insertanything_infer(
|
|
386
|
+
source_image=cv2.cvtColor(src_bgr, cv2.COLOR_BGR2RGB),
|
|
387
|
+
mask_image=bbox_mask,
|
|
388
|
+
ref_image=cv2.cvtColor(fg, cv2.COLOR_BGR2RGB),
|
|
389
|
+
ref_mask=fg_mask,
|
|
390
|
+
sam_mask=sam_mask,
|
|
391
|
+
seeds=seeds,
|
|
392
|
+
strength=strength,
|
|
393
|
+
split_ratio=split_ratio,
|
|
394
|
+
save_path=str(result_dir),
|
|
395
|
+
filename_suffix="",
|
|
396
|
+
net=self._ia_net,
|
|
397
|
+
return_image=True,
|
|
398
|
+
)
|
|
399
|
+
|
|
400
|
+
return result
|
|
401
|
+
|
|
402
|
+
# ------------------------------------------------------------------
|
|
403
|
+
# Conservative mode: background + bbox -> mask -> InsertAnything.
|
|
404
|
+
# ------------------------------------------------------------------
|
|
405
|
+
mask = make_rect_mask_from_bbox(h, w, bbox)
|
|
406
|
+
|
|
407
|
+
if verbose:
|
|
408
|
+
cv2.imwrite(str(intermediates_dir / "bbox_mask.png"), mask)
|
|
409
|
+
|
|
410
|
+
result = insertanything_infer(
|
|
411
|
+
source_image=cv2.cvtColor(bg, cv2.COLOR_BGR2RGB),
|
|
412
|
+
mask_image=mask,
|
|
413
|
+
ref_image=cv2.cvtColor(fg, cv2.COLOR_BGR2RGB),
|
|
414
|
+
ref_mask=fg_mask,
|
|
415
|
+
seeds=seeds,
|
|
416
|
+
strength=strength,
|
|
417
|
+
split_ratio=split_ratio,
|
|
418
|
+
save_path=str(result_dir),
|
|
419
|
+
filename_suffix="",
|
|
420
|
+
net=self._ia_net,
|
|
421
|
+
return_image=True,
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
return result
|
|
425
|
+
|
|
426
|
+
def infer_images(
|
|
427
|
+
self,
|
|
428
|
+
*,
|
|
429
|
+
background: np.ndarray,
|
|
430
|
+
foreground: np.ndarray,
|
|
431
|
+
foreground_mask: np.ndarray,
|
|
432
|
+
bbox_xyxy: tuple[int, int, int, int],
|
|
433
|
+
mode: Literal["aggressive", "conservative"] = "conservative",
|
|
434
|
+
verbose: bool = False,
|
|
435
|
+
seed: int = 123,
|
|
436
|
+
strength: float = 1.0,
|
|
437
|
+
split_ratio: float = 0.5,
|
|
438
|
+
save_path: str | Path | None = None,
|
|
439
|
+
filename_suffix: str = "",
|
|
440
|
+
) -> np.ndarray | None:
|
|
441
|
+
if background.ndim != 3:
|
|
442
|
+
raise ValueError("background must be HxWx3")
|
|
443
|
+
h, w = background.shape[:2]
|
|
444
|
+
|
|
445
|
+
out_dir = str(save_path) if save_path is not None else "./result"
|
|
446
|
+
|
|
447
|
+
if verbose and save_path is not None:
|
|
448
|
+
inter_dir = Path(save_path) / "intermediates"
|
|
449
|
+
os.makedirs(inter_dir, exist_ok=True)
|
|
450
|
+
|
|
451
|
+
# InsertAnything expects a list of seeds.
|
|
452
|
+
seeds = [seed]
|
|
453
|
+
|
|
454
|
+
if mode == "aggressive":
|
|
455
|
+
os_model, os_sampler = self._get_objectstitch_model_and_sampler()
|
|
456
|
+
os_rgb = run_objectstitch_single_image_from_images_cached(
|
|
457
|
+
background=background[:, :, ::-1],
|
|
458
|
+
foreground=foreground[:, :, ::-1],
|
|
459
|
+
foreground_mask=foreground_mask,
|
|
460
|
+
bbox_xyxy=tuple(bbox_xyxy),
|
|
461
|
+
model=os_model,
|
|
462
|
+
sampler=os_sampler,
|
|
463
|
+
device=self.config.device,
|
|
464
|
+
seed=seed,
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
predictor = self._get_sam_predictor()
|
|
468
|
+
sam_mask = run_sam_on_objectstitch_with_predictor(
|
|
469
|
+
predictor=predictor,
|
|
470
|
+
os_image=cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR),
|
|
471
|
+
bg_shape_hw=(h, w),
|
|
472
|
+
bbox_xyxy_bg=tuple(bbox_xyxy),
|
|
473
|
+
)
|
|
474
|
+
|
|
475
|
+
bg_bgr = background
|
|
476
|
+
os_bgr = cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR)
|
|
477
|
+
hh, ww = bg_bgr.shape[:2]
|
|
478
|
+
os_bgr = cv2.resize(os_bgr, (ww, hh), interpolation=cv2.INTER_AREA)
|
|
479
|
+
if sam_mask.shape[:2] != (hh, ww):
|
|
480
|
+
sam_mask = cv2.resize(sam_mask, (ww, hh), interpolation=cv2.INTER_NEAREST)
|
|
481
|
+
|
|
482
|
+
m = (sam_mask > 127).astype(np.float32)
|
|
483
|
+
m3 = np.stack([m, m, m], axis=-1)
|
|
484
|
+
src_bgr = bg_bgr.astype(np.float32) * (1.0 - m3) + os_bgr.astype(np.float32) * m3
|
|
485
|
+
src_bgr = np.clip(src_bgr, 0, 255).astype(np.uint8)
|
|
486
|
+
|
|
487
|
+
bbox_mask = make_rect_mask_from_bbox(h, w, list(bbox_xyxy))
|
|
488
|
+
|
|
489
|
+
if verbose and save_path is not None:
|
|
490
|
+
inter_dir = Path(save_path) / "intermediates"
|
|
491
|
+
prefix = f"{filename_suffix}_" if filename_suffix else ""
|
|
492
|
+
cv2.imwrite(str(inter_dir / f"{prefix}objectstitch_coarse_rgb.png"), cv2.cvtColor(os_rgb, cv2.COLOR_RGB2BGR))
|
|
493
|
+
cv2.imwrite(str(inter_dir / f"{prefix}sam_mask.png"), sam_mask)
|
|
494
|
+
cv2.imwrite(str(inter_dir / f"{prefix}blended_source.png"), src_bgr)
|
|
495
|
+
cv2.imwrite(str(inter_dir / f"{prefix}bbox_mask.png"), bbox_mask)
|
|
496
|
+
|
|
497
|
+
return insertanything_infer(
|
|
498
|
+
source_image=cv2.cvtColor(src_bgr, cv2.COLOR_BGR2RGB),
|
|
499
|
+
mask_image=bbox_mask,
|
|
500
|
+
ref_image=cv2.cvtColor(foreground, cv2.COLOR_BGR2RGB),
|
|
501
|
+
ref_mask=foreground_mask,
|
|
502
|
+
sam_mask=sam_mask,
|
|
503
|
+
seeds=seeds,
|
|
504
|
+
strength=strength,
|
|
505
|
+
split_ratio=split_ratio,
|
|
506
|
+
save_path=out_dir,
|
|
507
|
+
filename_suffix=filename_suffix,
|
|
508
|
+
net=self._ia_net,
|
|
509
|
+
return_image=True,
|
|
510
|
+
)
|
|
511
|
+
|
|
512
|
+
if mode != "conservative":
|
|
513
|
+
raise ValueError(f"Unsupported mode: {mode}")
|
|
514
|
+
|
|
515
|
+
mask = make_rect_mask_from_bbox(h, w, list(bbox_xyxy))
|
|
516
|
+
|
|
517
|
+
if verbose and save_path is not None:
|
|
518
|
+
inter_dir = Path(save_path) / "intermediates"
|
|
519
|
+
prefix = f"{filename_suffix}_" if filename_suffix else ""
|
|
520
|
+
cv2.imwrite(str(inter_dir / f"{prefix}bbox_mask.png"), mask)
|
|
521
|
+
|
|
522
|
+
return insertanything_infer(
|
|
523
|
+
source_image=cv2.cvtColor(background, cv2.COLOR_BGR2RGB),
|
|
524
|
+
mask_image=mask,
|
|
525
|
+
ref_image=cv2.cvtColor(foreground, cv2.COLOR_BGR2RGB),
|
|
526
|
+
ref_mask=foreground_mask,
|
|
527
|
+
seeds=seeds,
|
|
528
|
+
strength=strength,
|
|
529
|
+
split_ratio=split_ratio,
|
|
530
|
+
save_path=out_dir,
|
|
531
|
+
filename_suffix=filename_suffix,
|
|
532
|
+
net=self._ia_net,
|
|
533
|
+
return_image=True,
|
|
534
|
+
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Utility subpackage for OSInsert."""
|