+
    {DìjŒ  ã                   óŠ   € R t ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 ]P                  ! ]4      tR tR tR tRR ltR# )	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                ó¬  €  V P                   R8w  Ed…   V P                  '       Edr   V P                  '       Ed_   \        P                  ! V P                  P
                  4      P                  R4      pV P                  w  w  r#w  r$w  r%w  r&W4,           ^,          WV,           ^,          r‡\        \        VP                  VR\        W‡,
          ^4      ,          ,           4      4      p	VP                  ^ ^ VP                  \        V	^4      34      p
\        P                  ! RV
P                  R4      pVP                  WªP!                  R4      R7       \"        P$                  ! RR7      w  rÍ\&        P(                  ! V4       VP+                  V^_R	7       VR
3# T P2                  P
                  R3#   \,         d    \.        P1                  R4        L:i ; i)ai  
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ÚRGBAg      Ð?ÚRGBÚA)Úmaskz.jpg)Úsuffix)ÚqualityTz.Could not prepare the upper-body garment imageF)éÿ   r   r   )Ú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   &             Útryon\services\engine_ai.pyÚprepare_garmentr3      s_  € ðKØ×Ñ Õ*¨u¯}¯}©}ÀÇÇÁÜ—*’*˜UŸ[™[×-Ñ-Ó.×6Ñ6°vÓ>ˆCØ5:·]±]Ñ2‰HˆQ‘h�q¡ ©(¨1Ø!$¥¨a¥°#µ)¸qµ˜Üœ˜SŸZ™Z¨°¼¸EÕ<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˜§?¡?°3Ó#7ˆKÔ8Ü#×+Ò+°6Ô:‰LˆFÜ�HŠH�VÔØ�J‰J�t RˆJÔ(Ø˜�:Ðð �?‰?×Ñ Ð&Ð&øô ô KÜ×ÑÐIÖJðKús   ‚$F0 §F0 ºEF0 Æ0 GÇGc                óÜ  € V P                   R,          w  r\        V^,          ^,          4      pW#8:  d   V # T^,          p ^RIHp V! V 4      pVe/   \	        VR,          R,          P                  4       4      V,          p\        \        P                  ! YC^,          ,
          ^ Y#,
          4      4      pT RYwT,           13,          #   \         d    \        P                  R4        Lii ; i)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.
ºNé   N)Údetect_bodyÚpointsz*Person detection failed; centring the crop:NNN)r5   é    )Úshaper   Úpose_detectorr7   ÚfloatÚmeanr"   r#   r$   ÚnpÚclip)Ú	photo_bgrÚhÚwÚtarget_wÚcentrer7   Ú	detectionÚx0s   &       r2   Úportrait_croprG   3   sÏ   € ð
 �?‰?˜2Õ�D€AÜ�1�q•5˜1•9‹~€HØ„}ØÐØ��U€FðGÝ.á 	Ó*ˆ	ØÒ Ü˜9 XÕ.¨uÕ5×:Ñ:Ó<Ó=ÀÕAˆFô 
ŒR�WŠW�V¨�lÕ*¨A¨q­|Ó<Ó	=€BØ�Q˜ �=Ð(Ð(Õ)Ð)øô ô GÜ×ÑÐEÖFðGús   ÁA C Ã C+Ã*C+c                ón   € V'       d   R F  p V ! V3/ W2/B u # 	  V ! V4      #   \          d     K(  i ; i)zYgradio_client versions name the login-token option differently (token / hf_token / none).)ÚtokenÚhf_token)Ú	TypeError)ÚClientÚspacerI   Úkeywords   &&& r2   Ú_make_clientrO   I   sG   € çÛ,ˆGðÙ˜eÑ8¨Ð'7Ñ8Ò8ñ -ñ
 �%‹=Ðøô ô Úðús   �	%¥4³4c                ó€  € ^ RI HpHp \        V\        P
                  \        P                  4      p\        P                  ! 4       ;_uu_ 4       p\        P                  P                  VR4      p\        P                  ! Wp\        P                  ^_.4       VP                  RV! V4      R. RR/V! \!        V4      4      VRR^^*R	R
7      p\#        V\$        \&        34      '       d
   V^ ,          MTp	\        P(                  ! \!        V	4      4      p
V
f   \+        R4      hV
uuRRR4       #   + '       g   i     R# ; i)z?Return the try-on result as a BGR image. Raises on any failure.)rL   Úhandle_filez
person.jpgÚ
backgroundÚlayersÚ	compositeNTFz/tryon)ÚdictÚgarm_imgÚgarment_desÚ
is_checkedÚis_checked_cropÚdenoise_stepsÚseedÚapi_namez!The AI service returned no image.)Úgradio_clientrL   rQ   rO   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ÚdescriptionrL   rQ   ÚclientÚtmpÚperson_pathÚresultÚout_pathr   s   &&&        r2   Úrenderrt   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Ò A�6˜!–9ÀvˆÜ—
’
œ3˜x›=Ó)ˆØŠ=ÜÐBÓCÐCØ÷% 
'×	&×	&Ó	&ús   ÁCD,Ä,D=	)z	a garment)Ú__doc__Úloggingr   r   rb   Únumpyr>   Údjango.confr   ÚPILr   Ú	getLoggerÚ__name__r#   r3   rG   rO   rt   © ó    r2   Ú<module>r~      sE   ðñó Û 	Û ã 
Û Ý  Ý à	×	Ò	˜8Ó	$€ò'ò2*ò,ör}   