o f!@sddlZddlmZddlmZmZddlmZddlm Z ddlZddl Z Gddde Z e Zdd d Z  dd ej j d edej j fddZde fddZdddZddZdS)N)do_resize_content)instantiate_from_configget_obj_from_str) OmegaConf)Imagec@sReZdZdejdf dddZ   dd d ZdddZddZdddZ dS)TwoStagePipelinecpureturnNc Cs||_tt|jj|_|jjtj|j dddd|j | ||_tt|jj|_ tj|j dd}|j j|dd|j | ||_ ||j_ ||j _ ||_ ||_ t|j|jf||d|j|_t|j|j f||d|j|_dS)aI only for two stage generate process. - the first stage was condition on single pixel image, gererate multi-view pixel image, based on the v2pp config - the second stage was condition on multiview pixel image generated by the first stage, generate the final image, based on the stage2-test config r) map_locationF)strict)devicedtypeN) resize_raterrloadconfigmodel stage1_modelload_state_dicttorchresumeto stage2_modelr rrtargetparamsstage1_samplerstage2_sampler) selfstage1_model_configstage2_model_configstage1_sampler_configstage2_sampler_configr rrsdr#)/home/gustavo/Documentos/CRM/pipelines.py__init__ s4 zTwoStagePipeline.__init__ 3D assetsuniform low no texture ugly, boring, bad anatomy, blurry, pixelated, obscure, unnatural colors, poor lighting, dull, and unclear.2c Cst|tkr t|}t|tjr.|jdkr(td|jd}t|| d}n| d}n|j j |g |j}|j j|j j |j j|f||j j||||j j||j j|j j|j j|j j|j jdk|j j|j jd} dd| D} | |j j| S)NRGBArrrrRGBpixel)ucsampleripstepscale batch_sizeddim_etarr camera num_frames pixel_control transform offset_noisecSg|]}t|qSr#r fromarray.0imgr#r#r$ Zz2TwoStagePipeline.stage1_sample..)typestrropen isinstancemodenewsizealpha_compositeconvertrrget_learned_conditioningrr i2ir0r4rr6r7image_transformr:pop ref_position) r pixel_imgprompt neg_textsr2r3r5 backgroundr/ stage1_imagesr#r#r$ stage1_sample1s@     zTwoStagePipeline.stage1_samplecCst|tkr t|}t|tjr.|jdkr(td|jd}t|| d}n| d}n|j j |j j |j jd|j j |j jf|||||j jd|j j|j j|j j|j j|j jdk|j j|j jd }dd |D}|S) Nr+r,r-r&r*r.) pixel_imagesr1r2r3r4r5rr r6r7r8r9r:cSr;r#r<r>r#r#r$rA~rBz2TwoStagePipeline.stage2_sample..)rCrDrrErFrGrHrIrJrKr i2iStage2rr/r0r4rr r6r7rNr:)rrQrUr3r2rT stage2_imagesr#r#r$ stage2_sample^s>      zTwoStagePipeline.stage2_samplecCs||j_||j_dSN)rseedr)rr\r#r#r$set_seeds zTwoStagePipeline.set_seedcCs<t||j}|j||||d}|j||||d}|||dS)N)r3r2)ref_imgrUrY)rrrVrZ)rrQrRr3r2rUrYr#r#r$__call__s zTwoStagePipeline.__call__)r N)r&r'r(r)r*)r)r()r&r)r() __name__ __module__ __qualname__rfloat16r%rVrZr]r_r#r#r#r$r s  ( -#rr,cCsh|j\}}||kr |St||t||f}td||}|d|d|d|df}||||S)Nr+rr )rImaxrrHpaste)imagebg_colorwidthheightnew_size new_imagepaste_positionr#r#r$expand_to_squares   rnFrgforcer cKspd}|jdkr%|dddkr%tdtd|jd}t||}d}|p(|}|r6tj|fd |i|}|S) NTr+rzKalhpa channl not enpty, skip remove background, using alpha channel as maskr,Fsession) rG getextremaprintrrHrIrJrembgremove)rg rembg_sessionro rembg_kwargs do_removerTr#r#r$remove_backgrounds rzoriginal_imagecsndkr5tfdd|jD}||}td|jd}|j|jd|j|jdf}||||S|S)Nr c3s|] }t|VqdSr[)int)r?dim scale_rater#r$ sz$do_resize_content..r+r,rd)tuplerIresizerrHrirjrf)r{rrk resized_image padded_imagermr#r~r$rs   rrqrqrqcCstd|j|}t||S)Nr+)rrHrIrJ)rgrhrTr#r#r$add_backgrounds rcCsbt||dkrtd|jd}t||}nt|tdd}t||}t|}t ||}| dS)z> input image is a pil image in RGBA, return RGB image z Alpha as maskr+r,T) force_remover-) rtrrHrIrJrzrwrrnrrK)rgbackground_choiceforeground_ratiobackgroud_colorrTr#r#r$preprocess_images   r)r,)NF)r)rlibs.base_utilsrimagedream.ldm.utilrr omegaconfrPILrruobjectr new_sessionrwrnboolrzrrr#r#r#r$s.