I(eddlmZddlmZddlmZddlZddlZddlZddl Z ddlm Z m Z m Z m Z mZddlmZddlZddlmZddlmZmZddlZddlZddlZejejejeddd lmZmZmZd Z d Z!e!Z"ed Z#d Z$dZ%dZ&dZ'dS))device_encoding)StableDiffusionInpaintPipeline)ImageN)StableDiffusionPipelineStableDiffusionImg2ImgPipeline$StableDiffusionInpaintPipelineLegacy DDIMScheduler AutoencoderKL)remove) BlipProcessorBlipForConditionalGenerationzhuggingface-cloth-segmentation)load_seg_model get_palette generate_maskcpuc6d}t|t}|S)Nzmodel/cloth_segm.pthdevice)rr)checkpoint_pathnets 4C:\Users\Admin\source\gen_ai_haute-u-AR\app\genai.pyinitialize_and_load_modelsrs,O  8 8 8C JcJt|ttt}|S)N)rpaletter)rrrr)img cloth_segs rrunr*scsGFKKKI rc~tjtjrdnd}t jd}t jd|}tj | d}|dkr%t|}td|j d}|||d |}|jdi|d d i}t||d d ||d d } | S)Ncudarznoamrot/FuseCapRGBdressz bg removedz a picture of pt)return_tensors num_beamsrT)skip_special_tokens)torchrr! is_availabler from_pretrainedr toropenconvertr printshowgeneratedecode) image_pathimg_typer processormodel raw_imagetextinputsoutcaptions r image_captionr=/s5 \EJ$;$;$=$=H&&5 I IF-.?@@I ( 89J K K N Nv V VE :&&..u55I79%%  l D Yy$t < < < ? ? G GF %. 1 16 1 1q 1 1 1C )  3q6t  < <===s1v4@@G Nrc tjdtj}|}t j|}t |d}d}d}td|r|}n|r|}n|}|} t j|} t j|} | d} | d} t|d} t| d } t| t| d }d }d }d }d}tj d}|||| | ||||j }|d d|d ddS)Nz$runwayml/stable-diffusion-inpainting) torch_dtypez3./huggingface-cloth-segmentation/output/alpha/1.pngz3./huggingface-cloth-segmentation/output/alpha/2.pngz3./huggingface-cloth-segmentation/output/alpha/3.pngmask_generatedrBuserr#z# a human wearing a {dress_caption} z{user_caption}g@g?rr)promptnegative_promptimage mask_imageguidance_scaledenoising_strength generatornum_images_per_promptrz#./processed_images/output_image.jpgz%./processed_images/output_image_1.jpg) rr,r*float32rr.rr0resizer= Generatorimagessave) image_input dress_inputpiper4img_open gen_mask_1 gen_mask_2 gen_mask_3 mask_path dress_pathrGmask user_caption dress_captionrE neg_promptrIrJ num_samplesrKrQs rgen_vtonraCs ) 9.M   D Jz*%%HMMMFJFJFJ    J Jz " "E :i D LL $ $E ;;z " "D!V44L!*g66M , - 2F!J NKu---I T"%-)     . 1INN8999 1INN:;;;;;rc|ddd}|ddd}tdS)NrGr"rAr\r)r/rOrQ)dictrErGrHs rpredictrdsV =  ' ' . .z : :%F|##E**11*==* r)(osr diffusersrPILrr*numpynpgcrrrr r requestsrembgr transformersr r sys subprocesspathappendjoindirname__file__processrrrrrrrrr=rardr)rrrvs 444444  RRRRRRRRRRRRRRDDDDDDDD GLL**,LMMOOO?>>>>>>>>> ! "" +a.. (R<R<R<~r