+
    zDìjŽ)  ã                   óÔ   € R t ^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RIH	t	H
t
Ht ]P                  ! ]4      t/ tR tR tR tR tR tR	 tR
 tR tR tRR R lltRR R lltR R ltR# )a¤  
Image helpers: turn an uploaded dress photo into a clean, transparent GARMENT image.

Pipeline
--------
1. rembg (u2net) removes the background (skipped when the photo is already a transparent PNG).
2. If the photo shows a model, we also remove the model's head, neck, arms and
   skin so ONLY the clothing remains:
     a) clothing-segmentation model (USE_CLOTH_SEG, on by default), else
     b) MediaPipe Pose finds the shoulders -> everything above is cut off, then
        skin-coloured pixels are removed and the largest piece is kept.
   Photos with no person in them (flat-lay / mannequin) are left untouched.
rembg and mediapipe are imported lazily (they are heavy).
N)Úsettings)ÚImageÚ
ImageChopsÚImageOpsc                 ó\   € V \         9  d   ^ RIHp V! V 4      \         V &   \         V ,          # )é    )Únew_session)Ú	_sessionsÚrembgr   )Únamer   s   & Úcatalog\services\image_utils.pyÚ_get_sessionr      s%   € Ø”9ÔÝ%á% dÓ+Œ	�$‰Ü�T�?Ðó    c                ód   € \         P                  V R4      e   \        P                  ! 4        R# R# )zWFree a model's memory once it is no longer needed (dress uploads are rare, RAM is not).N)r	   ÚpopÚgcÚcollect)r   s   &r   Ú_releaser   $   s!   € ä‡}�}�T˜4Ó Ò,Ü
�
Š
Žñ -r   c                óX  € W,
          p\        \        P                  P                  V4      4      pV^8  d   W.# \        P                  ! V^,          ) V^ ,          .4      V,          pW,           ^,          WC,          R,          ,           pV^,          R,          pWV,
          WV,           .# )zFGuess hip positions (pixels) ~1.6 shoulder-widths below the shoulders.gš™™™™™ù?gÍÌÌÌÌÌì?)ÚfloatÚnpÚlinalgÚnormÚarray)ÚlsÚrsÚvecÚwidthÚdownÚmidÚhalfs   &&     r   Ú_estimate_hipsr!   *   sƒ   € à
�'€CÜ”"—)‘)—.‘. Ó%Ó&€EØˆq„yØˆxˆÜ�8Š8�c˜!•f�W˜c !�fÐ%Ó&¨Õ.€DØ�7�a�-˜$�,¨Õ,Õ
,€CØ��7�S�=€DØ�J˜�
Ð#Ð#r   c                óP  aa	a
€ ^ RI pV P                  w  o
oVP                  P                  P	                  R^R7      ;_uu_ 4       pVP                  \        P                  ! V 4      4      pRRR4       XP                  '       g   R# VP                  P                  o	\        S	^,          P                  S	^,          P                  4      R8  d   R# VV	V
3R lp\        V! ^4      V! ^4      .R R7      p\        S	^,          P                  S	^,          P                  4      R8¬  ;'       d2    \        S	^,          P                  S	^,          P                  4      R8*  pV'       d   \        V! ^4      V! ^4      .R	 R7      M\        V!  pR
VRV/#   + '       g   i     EL1; i)z�
Shoulders and hips of the person in the photo, in PIXELS, as
{"shoulders": [left, right], "hips": [left, right]} (left = smaller x), or None.
NT)Ústatic_image_modeÚmodel_complexityg      à?c                 ó´   <€ \         P                  ! SV ,          P                  S,          SV ,          P                  S,          .\         P                  R 7      # )©Údtype)r   r   ÚxÚyÚfloat64)ÚiÚhÚlmÚws   &€€€r   ÚptÚ_pose_points.<locals>.ptF   s5   ø€ Ü�xŠx˜˜A�Ÿ™ 1� b¨¥e§g¡g°¥kÐ2¼"¿*¹*ÔEÐEr   c                 ó   € V ^ ,          # ©r   © ©Úps   &r   Ú<lambda>Ú_pose_points.<locals>.<lambda>I   s   € °q¸¶tr   )Úkeyg      ð?c                 ó   € V ^ ,          # r2   r3   r4   s   &r   r6   r7   K   s   € °!°A¶$r   Ú	shouldersÚhips)Ú	mediapipeÚsizeÚ	solutionsÚposeÚPoseÚprocessr   r   Úpose_landmarksÚlandmarkÚminÚ
visibilityÚsortedÚmaxr)   r!   )Úimg_rgbÚmpr?   Úresr/   r:   Úhips_visibler;   r,   r-   r.   s   &       @@@r   Ú_pose_pointsrL   6   sI  ú€ ó
 à�<‰<�D€A€qØ	�‰×	Ñ	×	Ñ	°$ÈÐ	×	KÔ	KÈtØ�l‰lœ2Ÿ8š8 GÓ,Ó-ˆ÷ 
Là××ÐÙØ	×	Ñ	×	$Ñ	$€BÜ
ˆ2ˆb�6×Ñ˜b �f×/Ñ/Ó0°3Ô6Ù÷Fô ™˜2›¡ 2£Ð'©^Ô<€IÜ�r˜"•v×(Ñ(¨"¨R­&×*;Ñ*;Ó<ÀÑC×fÐfÌÈBÈrÍFÏHÉHÐVXÐY[ÕV\×V^ÑV^ÓH_ÐcfÑHf€Lß;GŒ6‘2�b“6™2˜b›6Ð"©Õ7Ì^Ð]fÑMg€DØ˜ F¨DÐ1Ð1÷ 
L×	KÐ	Kús   Á&FÆF%	c                ó  € \        R4      P                  V 4      p\        R4       V^ ,          pVR,           F  p\        P                  ! W#4      pK  	  \
        P                  ! VP                  V P                  4      4      # )zDMerge the 3 masks (upper / lower / full body) of the clothing model.Úu2net_cloth_segºé   NN)	r   Úpredictr   r   Úlighterr   r   Úresizer=   )rH   ÚmasksÚmergedÚms   &   r   Ú_cloth_maskrW   O   sf   € äÐ*Ó+×3Ñ3°GÓ<€EÜÐÔØ�1�X€FØ�2�YˆYˆÜ×#Ò# FÓ.Šñ ä�8Š8�F—M‘M '§,¡,Ó/Ó0Ð0r   c                ó  € V ^€8„  P                  \        P                  4      p\        P                  ! V\        P
                  \        P                  ! \        P                  R4      4      p\        P                  ! V4      w  r#rEV^8”  d�   VR\        P                  3,          p\        V4       UUu. uF,  w  rxVRVP                  4       ,          8¼  g   K#  ^V,           NK.  	  p	pp\        P                  ! W94      P                  \        P                  4      p\        P                  ! V\        P                  \        P                  4      w  r«\        ^\!        VP#                  4       4      4      pVej   \%        W«^ ,          4       FS  w  rÞV^,          R8w  g   K  \        P&                  ! V4      RV,          8  g   K9  \        P(                  ! W.R^R4       KU  	  \        P*                  ! V^ÿ,          R^ 4      # u uppi )zWClean a clothing mask: fill pinholes (sheer fabric), drop specks, keep the main pieces.rO   çš™™™™™©?g¸…ëQ¸Ž?©é   r[   éÿÿÿÿ©é   r^   )Úastyper   Úuint8Úcv2ÚmorphologyExÚMORPH_CLOSEÚgetStructuringElementÚMORPH_ELLIPSEÚconnectedComponentsWithStatsÚCC_STAT_AREAÚ	enumeraterG   ÚisinÚfindContoursÚ
RETR_CCOMPÚCHAIN_APPROX_SIMPLEÚintÚsumÚzipÚcontourAreaÚdrawContoursÚGaussianBlur)ÚmaskÚbinaryÚnÚlabelsÚstatsÚ_Úareasr+   ÚaÚkeepÚcontoursÚ	hierarchyÚtotalÚcntÚhiers   &              r   Ú
_tidy_maskr�   Y   su  € à�S‰j× Ñ ¤§¡Ó*€FÜ×Ò˜f¤c§o¡o´s×7PÒ7PÔQT×QbÑQbÐdjÓ7kÓl€FÜ×:Ò:¸6ÓBÑ€AˆuØˆ1„uØ�bœ#×*Ñ*Ð*Õ+ˆÜ"+¨EÔ"2ÔNÑ"2™$˜!°a¸4À%Ç)Á)Ã+Õ;MÑ6M”��A—�Ñ"2ˆÑNÜ—’˜Ó&×-Ñ-¬b¯h©hÓ7ˆä×*Ò*¨6´3·>±>Ä3×CZÑCZÓ[Ñ€HÜ�”3�v—z‘z“|Ó$Ó%€EØÒÜ˜X°¥|Ö4‰IˆCØ�A�w˜"Ž}¤§¢°Ó!5¸À½Ö!EÜ× Ò  ¨°°A°rÖ:ñ 5ô ×Ò˜F S�L¨&°!Ó4Ð4ùó Os   Â3 H ÃH c           	     ó$  € VR,          w  rEVR,          w  rg\         P                  ! V P                  \         P                  4      p\        P
                  ! V\         P                  ! WEWv.\         P                  R7      ^ÿ4       \        P                  ! \        P                  ! V\        P                  4      \        P                  4      p	\        P                  ! V	RR4      ^ 8„  p
V P                  4       p^ÿW±^€8„  V^ 8„  ,          V
( ,          &   V# )z­Ties, scarves, belts and shirt fronts are often not "clothing" for the segmentation model
and leave holes. Add back every non-skin foreground pixel inside the model's torso.r:   r;   r&   )r   é…   éM   )éÿ   é¹   éŒ   )r   ÚzerosÚshaper`   ra   ÚfillConvexPolyr   Úint32ÚcvtColorÚCOLOR_RGB2BGRÚCOLOR_BGR2YCrCbÚinRangeÚcopy)rs   ÚalphaÚrgbÚptsr   r   ÚlhÚrhÚtorsoÚyccÚskinÚouts   &&&&        r   Ú_add_torso_fillrš   l   sÃ   € ð �Õ�F€BØ��[�F€BÜ�HŠH�T—Z‘Z¤§¡Ó*€EÜ×Ò�uœbŸhšh¨°Ð'7¼r¿x¹xÔHÈ#ÔNÜ
�,Š,”s—|’| C¬×):Ñ):Ó;¼S×=PÑ=PÓ
Q€CÜ�;Š;�s˜L¨/Ó:¸QÑ>€DØ
�)‰)‹+€CØ/2€C�‰˜ ™Õ# t eÕ+Ñ,Ø€Jr   c           
     óÚ  € \        ^\        V^€8„  P                  4       4      4      pVfá   \        \        RR4      '       dÈ    \        \        V 4      4      p\        P                  ! V^€8„  P                  ^R7      4      ^ ,          p\        V4      '       dp   V^€8„  P                  4       V,          R8”  dQ   \        ^ \        VP                  4       RV P                  ,          ,
          4      4      pVP                  4       p^ VRV% V# V# \        \        RR4      '       dY    \        V 4      pV^€8„  P                  4       V,          R8”  d-   \#        \        V4      W!V4      p\        P$                  ! W(4      #  \        ^ \        \        R
 VR,           4       4      RV P                  ,          ,
          4      4      pVP                  4       p^ VRV% \&        P(                  ! \&        P(                  ! V\&        P*                  4      \&        P,                  4      p	\&        P.                  ! V	RR4      ^ 8„  p
^ W*&   \&        P0                  ! V\&        P2                  \&        P4                  ! \&        P6                  R4      4      p\&        P8                  ! V^8„  P;                  \        P<                  4      4      w  r¼rÞV^8”  dE   ^\        \        P>                  ! VR\&        P@                  3,          4      4      ,           p^ W,V8g  &   \&        PB                  ! VR^ 4      #   \         d    \        P!                  R4        T# i ; i  \         d    \        P!                  R	4        ELïi ; i)z@Return a new alpha channel with the model's head / skin removed.NÚUSE_CLOTH_SEGT)Úaxisg333333Ã?g{®Gáz„?z4Cloth segmentation failed; keeping the plain cut-outg333333Ó?z5Cloth segmentation failed, using pose + skin fallbackc              3   ó2   "  € T F  q^,          x € K  	  R# 5i)rP   Nr3   )Ú.0r5   s   & r   Ú	<genexpr>Ú_strip_model.<locals>.<genexpr>˜   s   é € Ð8Ñ'7 !˜1ŸšÓ'7ùs   ‚r:   ç{®Gáz”?rO   )r   é‡   éU   )r…   é´   r£   )é   r¦   r]   )"rG   rm   rn   Úgetattrr   r�   rW   r   ÚwhereÚanyÚlenrD   Úheightr�   Ú	ExceptionÚloggerÚ	exceptionrš   Úminimumra   rŒ   r�   rŽ   r�   rb   Ú
MORPH_OPENrd   re   rf   r_   r`   Úargmaxrg   rr   )rH   r’   r‘   r“   Úfg_areaÚclothÚrowsÚcutÚtidyr—   r˜   ru   rv   rw   rx   Úbiggests   &&&&            r   Ú_strip_modelr¸   z   sµ  € ä�!”S˜% #™+×*Ñ*Ó,Ó-Ó.€Gà
‚{ô ”8˜_¨d×3Ò3ðYÜ"¤;¨wÓ#7Ó8�Ü—x’x ¨¡× 1Ñ 1°qÐ 1Ó 9Ó:¸1Õ=�Ü�t—9’9 %¨#¡+×!2Ñ!2Ó!4°wÕ!>ÀÔ!EÜ˜a¤ T§X¡X£Z°$¸¿¹Õ2GÕ%GÓ!HÓI�CØ!ŸJ™J›L�EØ"#�E˜$˜3�Kð ˆˆuˆäŒx˜¨$×/Ò/ð	VÜ Ó(ˆEØ˜‘× Ñ Ó" WÕ,¨tÔ3Ü&¤z°%Ó'8¸%ÀcÓJ�Ü—z’z %Ó.Ð.ð 4ô ˆa””SÑ8 s¨;Ö'7Ó8Ó8¸4À'Ç.Á.Õ;PÕPÓQÓ
R€CØ�J‰J‹L€EØ€Eˆ$ˆ3€Kô �,Š,”s—|’| C¬×):Ñ):Ó;¼S×=PÑ=PÓ
Q€CÜ�;Š;�s˜L¨/Ó:¸QÑ>€DØ€E�Kô ×Ò˜U¤C§N¡N´C×4MÒ4MÌc×N_ÑN_ÐagÓ4hÓi€EÜ×:Ò:¸EÀB¹J×;NÑ;NÌrÏxÉxÓ;XÓYÑ€AˆuØˆ1„uØ”cœ"Ÿ)š) E¨"¬c×.>Ñ.>Ð*>Õ$?Ó@ÓAÕAˆØ#$ˆ˜ÑÑ Ü×Ò˜E 6¨1Ó-Ð-øô; ô YÜ× Ñ Ð!WÕXØˆðYûô ô 	VÜ×ÑÐT×Uð	Vús&   ÁCL Ä#AM Ì MÍMÍ M*Í)M*c           	     óÄ  € \         P                  ! V^€8„  P                  \        P                  4      \        P
                  ! R\        P                  4      ^R7      pVP                  \        P                  4      pV P                  \        P                  4      p\        ^4       F¯  p\         P                  ! WCR,          ,          R4      p\         P                  ! VR4      pV\        P                  ! VR,          R4      ,          pV^ 8H  VR8„  ,          p	W‰,          WI&   \        P                  ! W9P                  \        P                  4      4      pK±  	  \         P                  ! \         P                  ! V\        P
                  ! R\        P                  4      ^R7      R^ 4      p
\        P                  ! V^ ^ÿ4      P                  \        P                  4      V
3# )z³Cut-outs keep a thin light halo of the old background along their edge (it shows up as
white dotted lines on the customer). Trim ~2 px and repaint the edge from the fabric inside.)Ú
iterationsgü©ñÒMbP?rY   r]   ).NrZ   )ra   Úeroder_   r   r`   ÚonesÚfloat32ÚrangeÚblurÚmaximumrr   Úclip)r’   r‘   ÚinnerÚweightÚrgb_frx   ÚnumÚdenÚfillÚedgeÚ	new_alphas   &&         r   Ú_remove_fringerÊ   ª   s>  € ô �IŠI�u˜s‘{×*Ñ*¬2¯8©8Ó4´b·g²g¸fÄbÇhÁhÓ6OÐ\]Ô^€EØ�\‰\œ"Ÿ*™*Ó%€FØ�J‰J”r—z‘zÓ"€EÜ�1ŽXˆÜ�hŠh�u iÕ0Õ0°&Ó9ˆÜ�hŠh�v˜vÓ&ˆØ”R—Z’Z  I¥°Ó5Õ5ˆØ˜‘
˜s T™zÕ*ˆØ•jˆ‰Ü—’˜F§K¡K´·
±
Ó$;Ó<Šñ ô × Ò ¤§¢¨5´"·'²'¸&Ä"Ç(Á(Ó2KÐXYÔ!ZÐ\bÐdeÓf€IÜ�7Š7�5˜!˜SÓ!×(Ñ(¬¯©Ó2°IÐ=Ð=r   c                ó0   € V ^8„  d   QhR\         R\        /# )é   Ú	raw_bytesÚremove_model)ÚbytesÚbool)Úformats   "r   Ú__annotate__rÒ   »   s   € ÷ :ñ :œuð :´Dñ :r   c           
     ó  € ^ RI p^ RIHp \        P                  ! \
        P                  ! VP                  V 4      4      4      pVP                  R\
        P                  4       RpVP                  R9   g   RVP                  9   dQ   VP                  R4      p\        P                  ! VP                  R4      4      pV^ú8  P!                  4       R8”  d   TpVee   \
        P"                  ! RXP$                  R4      pVP'                  WfP                  R4      R7       Tp	\        P                  ! V4      R,          TrºMeVP                  R4      p	V! V	\)        R	4      R
7      P                  R4      p\+        R	4       \        P                  ! V4      pVR,          VR,          rº\-        V	4      pV'       dN   \/        WšW¾4      pV^€8„  P1                  4       RVP$                  ,          8¼  d   TpM\2        P5                  R4       \7        W«4      w  r«\
        P8                  ! \        P:                  ! W«.4      R4      pVP                  R4      P=                  4       pV'       d   V^ ,          V^,          3MRw  ppV'       d   VP?                  V4      pVP@                  pVP                  R\
        P                  4       VP@                  V,          pRpV'       d¢   VR,          ^ ,          VR,          ^,          VR,          ^,          VR,          ^ ,          .pV Uu. uFW  p\C        \E        V^ ,          V,
          V,          4      ^4      \C        \E        V^,          V,
          V,          4      ^4      .NKY  	  ppVV3# u upi )a  
Return (garment_rgba_image, anchors).
anchors = [[x, y] x4] = model's left shoulder, right shoulder, right hip, left hip
in the coordinates of the returned image (or None if no person was found).
The try-on maps these 4 points onto the customer's body for a natural fit.
N)ÚremoveÚRGBAÚtransparencyÚArY   ÚRGB)rs   Úu2net)Úsessionr¢   z=Model removal left almost nothing; keeping the plain cut-out.r:   r;   )é°  rÛ   )rÕ   ÚLAÚPA)r…   r…   r…   )ºNNNrÞ   :Nr^   N)rÞ   rÞ   r^   )r   r   )éè  rß   )#Úior
   rÔ   r   Úexif_transposer   ÚopenÚBytesIOÚ	thumbnailÚLANCZOSÚmodeÚinfoÚconvertr   r   Ú
getchannelÚmeanÚnewr=   Úpaster   r   rL   r¸   rn   r­   ÚwarningrÊ   Ú	fromarrayÚdstackÚgetbboxÚcropr   Úroundr   )rÍ   rÎ   rà   rÔ   ÚsrcÚ	own_alphaÚrgbarz   ÚflatÚimgr’   r‘   ÚfgÚarrr“   Ústrippedr™   ÚbboxÚoxÚoyÚwidth_beforeÚscaleÚanchorsÚorderr5   s   &&                       r   Úextract_garmentr  »   s§  € ó åä
×
!Ò
!¤%§*¢*¨R¯Z©Z¸	Ó-BÓ"CÓ
D€CØ‡M�M�,¤§¡Ô.à€IØ
‡x�xÐ'Ô'¨>¸S¿X¹XÔ+EØ�{‰{˜6Ó"ˆÜ�HŠH�T—_‘_ SÓ)Ó*ˆØ�‰G�>‰>Ó˜dÔ"ØˆIàÒÜ�yŠy˜ §	¡	¨?Ó;ˆØ�
‰
�4Ÿo™o¨cÓ2ˆ
Ô3ØˆÜ—X’X˜d“^ HÕ-¨y‰Uà�k‰k˜%Ó ˆÙ�C¤¨gÓ!6Ô7×?Ñ?ÀÓGˆÜ�ÔÜ�hŠh�r‹lˆØ˜•] C¨¥LˆUä
�sÓ
€CçÜ ¨%Ó5ˆà�s‰N×ÑÓ! T¨H¯M©MÕ%9Ô9Ø‰Eä�N‰NÐZÔ[ä Ó+�J€Cä
�/Š/œ"Ÿ)š) S LÓ1°6Ó
:€CØ�>‰>˜#Ó×&Ñ&Ó(€Dß#'ˆd�1�g�t˜A•wÑ¨V�F€BˆßØ�h‰h�t‹nˆØ—9‘9€LØ‡M�M�,¤§¡Ô.Ø�I‰I˜Õ$€Eà€Gß
Ø�[Õ! !Õ$ c¨+Õ&6°qÕ&9¸3¸v½;Àq½>È3ÈvÍ;ÐWXÍ>ÐZˆÙinÓoÑinÐde”Eœ%  1¥¨¥¨eÕ 3Ó4°aÓ8¼%ÄÀqÈÅtÈbÅyÐTYÕFYÓ@ZÐ\]Ó:^Ó_ÑinˆÐoØ�ˆ<Ðùò ps   Ì ANc                ód   € V ^8„  d   QhR\         P                   R\        R\         P                   /# )rÌ   r÷   r=   Úreturn)r   rm   )rÑ   s   "r   rÒ   rÒ   ø   s)   € ÷ ñ œŸ™ð ¬3ð ¼¿¹ñ r   c                 óh   € V P                  4       pVP                  W3\        P                  4       V# )N)r�   rä   r   rå   )r÷   r=   Úthumbs   && r   Úmake_thumbnailr  ø   s&   € Ø�H‰H‹J€EØ	‡O�O�T�L¤%§-¡-Ô0Ø€Lr   c                óD   € V ^8„  d   QhR\         P                   R\        /# )rÌ   r÷   r  )r   rÏ   )rÑ   s   "r   rÒ   rÒ   þ   s   € ÷ ñ ”e—k‘kð ¤eñ r   c                 ór   € ^ RI pVP                  4       pV P                  VRRR7       VP                  4       # )r   NÚPNGT)rÑ   Úoptimize)rà   rã   ÚsaveÚgetvalue)r÷   rà   Úbufs   &  r   Úto_png_bytesr  þ   s.   € Ûà
�*‰*‹,€CØ‡H�HˆS˜¨€HÔ.Ø�<‰<‹>Ðr   )T)éÈ   )Ú__doc__r   Úloggingra   Únumpyr   Údjango.confr   ÚPILr   r   r   Ú	getLoggerÚ__name__r­   r	   r   r   r!   rL   rW   r�   rš   r¸   rÊ   r  r  r  r3   r   r   Ú<module>r     st   ðñó 
Û ã 
Û Ý  ß +Ñ +à	×	Ò	˜8Ó	$€à€	òòò	$ò2ò21ò5ò&ò-.ò`>÷":÷z÷r   