U x8d@sJddlZddlZddlZddlmZddlmZmZm Z GdddZ dS)N)Image)!StableDiffusionControlNetPipelineControlNetModelUniPCMultistepSchedulerc@s8eZdZddZddZeddZddZd d Zd S) TextToImagecCs||_dS)N)initialize_modelmodel)selfr ;/mnt/home/wangjp/Code/Image2Text/models/controlnet_model.py__init__ szTextToImage.__init__cCs@tjdtjd}tjd|dtjd}t|jj|_| |S)Nz-fusing/stable-diffusion-v1-5-controlnet-canny) torch_dtypezrunwayml/stable-diffusion-v1-5) controlnetZsafety_checkerr ) rfrom_pretrainedtorchfloat16rr from_config schedulerconfigZenable_model_cpu_offload)r rpipeliner r r rszTextToImage.initialize_modelcCsBt|}d}d}t|||}tj|||gdd}t|}|S)Nd)axis)nparraycv2Cannystackr fromarray)image low_thresholdhigh_thresholdr r r preprocess_image"s  zTextToImage.preprocess_imagecCs$||}|j||ddjd}|S)N)Znum_inference_stepsr)r#rimages)r textr generated_imager r r text_to_image,s zTextToImage.text_to_imagecCs td|S)Ntext_to_image_debug)print)r r&r r r r r)1szTextToImage.text_to_image_debugN) __name__ __module__ __qualname__r r staticmethodr#r(r)r r r r r s  r) rrnumpyrPILrZ diffusersrrrrr r r r s