Ë
    {DìjŒ  ã                   ó†   — d Z ddlZddlZddlZddlZddlZddlmZ ddl	m
Z
  ej                  e«      Zd„ Zd„ Zd„ Zd	d„Zy)
aâ  
"ai" engine: realistic try-on with an OPEN-SOURCE diffusion model (IDM-VTON by default),
called through a Hugging Face Space with `gradio_client` (pip install gradio_client).

IMPORTANT
- Needs the internet; the customer's photo is sent to that Space (a third-party server).
- Free Spaces are queued and rate-limited; one photo can take 20-90 seconds.
- Model licences differ (IDM-VTON is CC BY-NC-SA 4.0 = non-commercial). Check before selling.
- The Space's API can change. If it breaks, open the Space page -> "Use via API"
  and adjust the predict() call below. If anything fails, the view falls back to the
  "warp" engine automatically.
You can instead host the same model yourself on a GPU machine and point TRYON_AI_SPACE at it.
é    N)Úsettings)ÚImagec                 ó`  — 	 | j                   dk7  �rb| j                  �rU| j                  �rHt        j                  | j                  j
                  «      j                  d«      }| j                  \  \  }}\  }}\  }}\  }}||z   dz  ||z   dz  }}t        t        |j                  |dt        ||z
  d«      z  z   «      «      }	|j                  dd|j                  t        |	d«      f«      }
t        j                  d|
j                  d	«      }|j                  |
|
j!                  d
«      ¬«       t#        j$                  d¬«      \  }}t'        j(                  |«       |j+                  |d¬«       |dfS | j2                  j
                  dfS # t,        $ r t.        j1                  d«       Y Œ8w xY w)a}  
    IDM-VTON is built for UPPER-BODY clothes shown on a plain white background. So instead of the
    original upload (a full-length model photo with a head, legs and shoes) we send the cleaned
    garment cut-out, cropped from the collar down to just below the hips, on white.
    Returns (path, is_temporary_file). Falls back to the original upload if that is not possible.
    ÚbottomsÚRGBAé   g      Ð?é   r   é   ÚRGB)éÿ   r   r   ÚA)Úmaskz.jpg)Úsuffixé_   )ÚqualityTz.Could not prepare the upper-body garment imageF)Úgarment_typeÚanchorsÚimager   ÚopenÚpathÚconvertÚintÚminÚheightÚmaxÚcropÚwidthÚnewÚsizeÚpasteÚ
getchannelÚtempfileÚmkstempÚosÚcloseÚsaveÚ	ExceptionÚloggerÚ	exceptionÚ	raw_image)ÚdressÚimgÚ_ÚlsyÚrsyÚrhyÚlhyÚ
shoulder_yÚhip_yÚbottomr   ÚwhiteÚhandler   s                 úSC:\Users\Stabi\Downloads\virtual-tryon(1)\virtual-tryon\tryon\services\engine_ai.pyÚprepare_garmentr8      so  € ðKØ×Ñ Ó*¨u¯}«}ÀÇÃÜ—*‘*˜UŸ[™[×-Ñ-Ó.×6Ñ6°vÓ>ˆCØ5:·]±]Ñ2‰HˆQ�‘h�q˜#¡  C©(¨1¨cØ!$ s¡¨a¡°#¸±)¸q±˜ˆJÜœ˜SŸZ™Z¨°¼¸EÀJÑ<NÐPQÓ8RÑ1RÑ)RÓSÓTˆFØ—8‘8˜Q  3§9¡9¬c°&¸"«oÐ>Ó?ˆDÜ—I‘I˜e T§Y¡Y°Ó@ˆEØ�K‰K˜ 4§?¡?°3Ó#7ˆKÔ8Ü#×+Ñ+°6Ô:‰LˆF�DÜ�H‰H�VÔØ�J‰J�t RˆJÔ(Ø˜�:Ðð �?‰?×Ñ Ð&Ð&øô ò KÜ×ÑÐIÖJðKús   ‚E1F ÆF-Æ,F-c                 ó„  — | j                   dd \  }}t        |dz  dz  «      }||k  r| S |dz  }	 ddlm}  || «      }|�&t	        |d   dd…df   j                  «       «      |z  }t        t        j                  ||dz  z
  d||z
  «      «      }| dd…|||z   …f   S # t        $ r t        j                  d	«       Y ŒWw xY w)
zÂ
    The model works on 3:4 portrait pictures. A wide webcam frame is cropped to a portrait window
    centred on the person (found through their shoulders) so the person fills the picture.
    Nr   é   é   r	   )Údetect_bodyÚpointsr   z*Person detection failed; centring the crop)Úshaper   Úpose_detectorr<   ÚfloatÚmeanr'   r(   r)   ÚnpÚclip)Ú	photo_bgrÚhÚwÚtarget_wÚcentrer<   Ú	detectionÚx0s           r7   Úportrait_croprK   3   sã   € ð
 �?‰?˜2˜AÐ�D€A€qÜ�1�q‘5˜1‘9‹~€HØˆH‚}ØÐØ�‰U€FðGÝ.á 	Ó*ˆ	ØÐ Ü˜9 XÑ.¨r°¨r°1¨uÑ5×:Ñ:Ó<Ó=ÀÑAˆFô 
ŒR�W‰W�V˜h¨™lÑ*¨A¨q°8©|Ó<Ó	=€BØ’Q˜˜2 ™=Ð(Ð(Ñ)Ð)øô ò GÜ×ÑÐEÖFðGús   ±6B ÂB?Â>B?c                 ó\   — |rdD ]  }	  | |fi ||i¤Žc S   | |«      S # t         $ r Y Œ%w xY w)zYgradio_client versions name the login-token option differently (token / hf_token / none).)ÚtokenÚhf_token)Ú	TypeError)ÚClientÚspacerM   Úkeywords       r7   Ú_make_clientrS   I   sI   € áÛ,ˆGðÙ˜eÑ8¨°Ð'7Ñ8Ò8ð -ñ
 �%‹=Ðøô ò Ùðús   ‰
Ÿ	+ª+c                 ó>  — ddl m}m} t        |t        j
                  t        j                  «      }t        j                  «       5 }t        j                  j                  |d«      }t        j                  || t        j                  dg«       |j                   ||«      g ddœ |t!        |«      «      |ddd	d
d¬«      }t#        |t$        t&        f«      r|d   n|}	t        j(                  t!        |	«      «      }
|
€t+        d«      ‚|
cddd«       S # 1 sw Y   yxY w)z?Return the try-on result as a BGR image. Raises on any failure.r   )rP   Úhandle_filez
person.jpgr   N)Ú
backgroundÚlayersÚ	compositeTFé   é*   z/tryon)ÚdictÚgarm_imgÚgarment_desÚ
is_checkedÚis_checked_cropÚdenoise_stepsÚseedÚapi_namez!The AI service returned no image.)Úgradio_clientrP   rU   rS   r   ÚTRYON_AI_SPACEÚHF_TOKENr"   ÚTemporaryDirectoryr$   r   ÚjoinÚcv2ÚimwriteÚIMWRITE_JPEG_QUALITYÚpredictÚstrÚ
isinstanceÚlistÚtupleÚimreadÚRuntimeError)Ú
person_bgrÚgarment_pathÚdescriptionrP   rU   ÚclientÚtmpÚperson_pathÚresultÚout_pathr   s              r7   Úrenderrz   T   sê   € ç1ä˜&¤(×"9Ñ"9¼8×;LÑ;LÓM€Fä	×	$Ñ	$Ô	&¨#Ü—g‘g—l‘l 3¨Ó5ˆÜ�‰�K ¬c×.FÑ.FÈÐ-KÔLà—‘Ù +¨KÓ 8ÀBÐUYÑZÙ ¤ \Ó!2Ó3Ø#ØØ!ØØØð  ó 	
ˆô !+¨6´D¼%°=Ô A�6˜!’9ÀvˆÜ—
‘
œ3˜x›=Ó)ˆØˆ=ÜÐBÓCÐCØ÷% 
'×	&Ò	&ús   ÁCDÄD)z	a garment)Ú__doc__Úloggingr$   r"   rh   ÚnumpyrB   Údjango.confr   ÚPILr   Ú	getLoggerÚ__name__r(   r8   rK   rS   rz   © ó    r7   Ú<module>r„      sG   ðñó Û 	Û ã 
Û Ý  Ý à	ˆ×	Ñ	˜8Ó	$€ò'ò2*ò,ôrƒ   