o 0yh @sddlZdZdZejeddejeddeejd<eejd<ddlmZddlZddl Z dd l m Z ddl Z dd l mZdd lmZmZddlZd Zed ZedZejde jdedddZdddZddZdS)Nz/data/.cache/huggingfacez/data/ultralyticsT)exist_okHF_HOMEZYOLO_CONFIG_DIR)YOLO)Image) StableDiffusionXLInpaintPipeline) pil_to_cv2 cv2_to_pilizdeepfashion2_yolov8s-seg.ptzyolov8x-seg.ptz0diffusers/stable-diffusion-xl-1.0-inpainting-0.1HF_TOKEN) torch_dtypeuse_safetensorsuse_auth_tokencuda cCs|rtj|s tdt|d}t|}t |}|r.|dj r.t |dj j dkr3td|dj j d }|dktj}d|} tdtj} tj| | dd} | d tj} t| ttfd } |ttfd}t||p}d || |d d jd}|S)Nz.No valid image found. Please run Step 1 first.RGBrz@No subject detected in the image. Please upload a clearer photo.?)r) iterationsL(promptnegative_promptimage mask_imageguidance_scalenum_inference_steps)ospathisfilegrErrorropenconvertryolomaskslendatacpunumpyastypenpuint8onescv2dilaterresize INPAINT_SIZE inpaint_pipeimages) image_pathrrrrimg_cvresultsmaskbinaryZbackground_maskkerneldilatedZ inpaint_maskmask_pilZimg_pilresultr?!/home/user/app/background_edit.py"run_background_removal_and_inpaint(s4 "  rAc Cstzytdddt|dddddf}|jdd\}}t|}|djj }t |dkr8t d |d} t j| ||ft jd } | d ktjd } t| d ttf} |dttf} t||| | |ddjd}|dfWSt j y}zddt|fWYd}~Sd}~wty}ztddt|jdt|fWYd}~Sd}~ww)Nz6[INFO] Step 3: Clothing segmentation and inpainting...T)flushr.rz,No clothing detected. Try a different image.) interpolationrrr2rru🛑 u❌ Unexpected Error: z: )printr-arrayr%shapeclothing_modelr'r)r*r+r(r"r#r0r2Z INTER_NEARESTr,r.r fromarrayr3r4r5str Exception traceback print_exctype__name__)rrrguidancer7hwr8r'r9Z resized_maskZ binary_maskr= resized_imager>er?r?r@run_clothing_inpaintHs@    *rW)r)rZhf_homeZyolo_cfgmakedirsenvironZ ultralyticsrr+r-torchPILrr0 diffusersrutilsrrgradior"r3rJr&from_pretrainedfloat16getenvtor4rArWr?r?r?r@s8