§
    ezni«  ã                   ó�  — d dl Z d dlmZ d dlmZmZmZmZ d dlZ	d dl
Z
	 d%de
j        dee         dee         ded	e
j        f
d
„Zded	eee         ddf         fd„Zde
j        deded	e
j        fd„Zded	e	j        fd„Zdededed	ee	j                 fd„Zdeedf         deded	eeee                  ee         f         fd„Zde
j        dee         d	e
j        fd„Zde
j        dee         d	e
j        fd„Zde
j        dee         deded	e
j        f
d„Zd e	j        d!ed"ed	ee	j        ef         fd#„Zde
j        d	e
j        fd$„ZdS )&é    N)Úproduct)ÚAnyÚ	GeneratorÚListÚTupleç      4@ÚboxesÚcrop_boxÚorig_boxÚatolÚreturnc                 ó¾  — t          j        |t           j        | j        ¬¦  «        }t          j        |t           j        | j        ¬¦  «        }t	          | |¦  «                             ¦   «         } t          j        | |ddd…f         |d¬¦  «        }t          j        | |ddd…f         |d¬¦  «        }t          j        || ¦  «        }t          j        |d¬¦  «        S )zCReturn a boolean tensor indicating if boxes are near the crop edge.)ÚdtypeÚdeviceNr   )r   Úrtolé   ©Údim)ÚtorchÚ	as_tensorÚfloatr   Úuncrop_boxes_xyxyÚiscloseÚlogical_andÚany)r	   r
   r   r   Úcrop_box_torchÚorig_box_torchÚnear_crop_edgeÚnear_image_edges           úz/var/www/html/cool-property-finder/image-detection-backend/venv/lib/python3.11/site-packages/ultralytics/models/sam/amg.pyÚis_box_near_crop_edger!      sÉ   € õ
 ”_ XµU´[ÈÌÐVÑVÔV€NÝ”_ XµU´[ÈÌÐVÑVÔV€NÝ˜e XÑ.Ô.×4Ò4Ñ6Ô6€EÝ”] 5¨.¸¸q¸q¸q¸Ô*AÈÐSTÐUÑUÔU€NÝ”m E¨>¸$ÀÀÀ¸'Ô+BÈÐTUÐVÑVÔV€OÝÔ& ~¸Ð7GÑHÔH€NÝŒ9�^¨Ð+Ñ+Ô+Ð+ó    Ú
batch_sizec              '   ó(  ‡ ‡‡K  — ‰rt          ˆfd„‰D ¦   «         ¦  «        s
J d¦   «         ‚t          ‰d         ¦  «        ‰ z  t          t          ‰d         ¦  «        ‰ z  dk    ¦  «        z   }t          |¦  «        D ]Šˆˆ fd„‰D ¦   «         V — ŒdS )z/Yield batches of data from the input arguments.c              3   ód   •K  — | ]*}t          |¦  «        t          ‰d          ¦  «        k    V — Œ+dS )r   N)Úlen)Ú.0ÚaÚargss     €r    ú	<genexpr>z!batch_iterator.<locals>.<genexpr>   s8   øè è € Ð=Ð=°1�˜A™œ¥# d¨1¤g¡,¤,Ò.Ð=Ð=Ð=Ð=Ð=Ð=r"   z-Batched iteration must have same-size inputs.r   c                 ó6   •— g | ]}|‰‰z  ‰d z   ‰z  …         ‘ŒS ©r   © )r'   ÚargÚbr#   s     €€r    ú
<listcomp>z"batch_iterator.<locals>.<listcomp>   s0   ø€ ÐHÐHÐH¸Cˆs�1�z‘> 1 q¡5¨JÑ"6Ð6Ô7ÐHÐHÐHr"   N)Úallr&   ÚintÚrange)r#   r)   Ú	n_batchesr/   s   `` @r    Úbatch_iteratorr5      sÀ   øøøè è € àÐn•CÐ=Ð=Ð=Ð=¸Ð=Ñ=Ô=Ñ=Ô=ÐnÐnÐ?nÑnÔnÐ=Ý�D˜”G‘” 
Ñ*­Sµ°T¸!´W±´À
Ñ1JÈaÒ1OÑ-PÔ-PÑP€IÝ�9ÑÔð Ið IˆØHÐHÐHÐHÐHÀ4ÐHÑHÔHÐHÐHÐHÐHðIð Ir"   ÚmasksÚmask_thresholdÚthreshold_offsetc                 ó(  — | ||z   k                          dt          j        ¬¦  «                              dt          j        ¬¦  «        }| ||z
  k                          dt          j        ¬¦  «                              dt          j        ¬¦  «        }||z  S )zÈ
    Computes the stability score for a batch of masks.

    The stability score is the IoU between the binary masks obtained by thresholding the predicted mask logits at high
    and low values.
    éÿÿÿÿ)r   )Úsumr   Úint16Úint32)r6   r7   r8   ÚintersectionsÚunionss        r    Úcalculate_stability_scorer@   !   s”   € ð ˜~Ð0@Ñ@ÒA×FÒFÀrÕQVÔQ\ÐFÑ]Ô]×aÒaÐbdÝhmÔhsð bñ uô u€Mà˜Ð)9Ñ9Ò:×?Ò?ÀÍ%Ì+Ð?ÑVÔV×ZÒZÐ[]ÕejÔepÐZÑqÔq€FØ˜6Ñ!Ð!r"   Ú
n_per_sidec                 ó   — dd| z  z  }t          j        |d|z
  | ¦  «        }t          j        |ddd…f         | df¦  «        }t          j        |dd…df         d| f¦  «        }t          j        ||gd¬¦  «                             dd¦  «        S )zDGenerate a 2D grid of evenly spaced points in the range [0,1]x[0,1].r   é   Nr:   )Úaxis)ÚnpÚlinspaceÚtileÚstackÚreshape)rA   ÚoffsetÚpoints_one_sideÚpoints_xÚpoints_ys        r    Úbuild_point_gridrN   0   s•   € à�!�j‘.Ñ!€FÝ”k &¨!¨f©*°jÑAÔA€OÝŒw� t¨Q¨Q¨Q wÔ/°*¸a°ÑAÔA€HÝŒw� q q q¨$ wÔ/°!°Z°ÑAÔA€HÝŒ8�X˜xÐ(¨rÐ2Ñ2Ô2×:Ò:¸2¸qÑAÔAÐAr"   Ún_layersÚscale_per_layerc                 óD   ‡ ‡— ˆ ˆfd„t          |dz   ¦  «        D ¦   «         S )z)Generate point grids for all crop layers.c           	      óT   •— g | ]$}t          t          ‰‰|z  z  ¦  «        ¦  «        ‘Œ%S r-   )rN   r2   )r'   ÚirA   rP   s     €€r    r0   z/build_all_layer_point_grids.<locals>.<listcomp>;   s4   ø€ ÐdÐdÐdÈ1Õ�S ¨À!Ñ/CÑ!DÑEÔEÑFÔFÐdÐdÐdr"   r   )r3   )rA   rO   rP   s   ` `r    Úbuild_all_layer_point_gridsrT   9   s1   øø€ àdÐdÐdÐdÐdÕPUÐV^ÐabÑVbÑPcÔPcÐdÑdÔdÐdr"   Úim_size.Úoverlap_ratioc           	      ó‚  ‡‡‡— g g }}| \  }}t          ||¦  «        }|                     dd||g¦  «         |                     d¦  «         d„ }t          |¦  «        D ]ß}	d|	dz   z  }
t          ||z  d|
z  z  ¦  «        Š |||
‰¦  «        Š |||
‰¦  «        Šˆˆfd„t          |
¦  «        D ¦   «         }ˆˆfd„t          |
¦  «        D ¦   «         }t	          ||¦  «        D ]Z\  }}||t          |‰z   |¦  «        t          |‰z   |¦  «        g}|                     |¦  «         |                     |	dz   ¦  «         Œ[Œà||fS )zs
    Generates a list of crop boxes of different sizes.

    Each layer has (2**i)**2 boxes for the ith layer.
    r   c                 ó\   — t          t          j        ||dz
  z  | z   |z  ¦  «        ¦  «        S )z4Crops bounding boxes to the size of the input image.r   )r2   ÚmathÚceil)Úorig_lenÚn_cropsÚoverlaps      r    Úcrop_lenz%generate_crop_boxes.<locals>.crop_lenM   s-   € å•4”9˜g¨°1©Ñ5¸Ñ@ÀGÑKÑLÔLÑMÔMÐMr"   rC   r   c                 ó:   •— g | ]}t          ‰‰z
  |z  ¦  «        ‘ŒS r-   ©r2   )r'   rS   Úcrop_wr]   s     €€r    r0   z'generate_crop_boxes.<locals>.<listcomp>X   ó+   ø€ ÐTÐTÐT°q•s˜F WÑ,°Ñ1Ñ2Ô2ÐTÐTÐTr"   c                 ó:   •— g | ]}t          ‰‰z
  |z  ¦  «        ‘ŒS r-   r`   )r'   rS   Úcrop_hr]   s     €€r    r0   z'generate_crop_boxes.<locals>.<listcomp>Y   rb   r"   )ÚminÚappendr3   r2   r   )rU   rO   rV   Ú
crop_boxesÚ
layer_idxsÚim_hÚim_wÚ
short_sider^   Úi_layerÚn_crops_per_sideÚcrop_box_x0Úcrop_box_y0Úx0Úy0Úboxrd   ra   r]   s                   @@@r    Úgenerate_crop_boxesrs   >   sª  øøø€ ð   �
€JØ�J€Dˆ$Ý�T˜4‘”€Jð ×Ò�q˜!˜T 4Ð(Ñ)Ô)Ð)Ø×Ò�aÑÔÐðNð Nð Nõ ˜‘?”?ð +ð +ˆØ ¨1¡Ñ-ÐÝ�m jÑ0°AÐ8HÑ4HÑIÑJÔJˆà�˜$Ð 0°'Ñ:Ô:ˆØ�˜$Ð 0°'Ñ:Ô:ˆàTÐTÐTÐTÐT½EÐBRÑ<SÔ<SÐTÑTÔTˆØTÐTÐTÐTÐT½EÐBRÑ<SÔ<SÐTÑTÔTˆõ ˜k¨;Ñ7Ô7ð 	+ð 	+‰FˆB�Ø�r�3˜r F™{¨DÑ1Ô1µ3°r¸F±{ÀDÑ3IÔ3IÐJˆCØ×Ò˜cÑ"Ô"Ð"Ø×Ò˜g¨™kÑ*Ô*Ð*Ð*ð	+ð
 �zÐ!Ð!r"   c                 ó´   — |\  }}}}t          j        ||||gg| j        ¬¦  «        }t          | j        ¦  «        dk    r|                     d¦  «        }| |z   S )z4Uncrop bounding boxes by adding the crop box offset.©r   é   r   ©r   Útensorr   r&   ÚshapeÚ	unsqueeze)r	   r
   rp   rq   Ú_rJ   s         r    r   r   d   sc   € à�L€BˆˆAˆqÝŒ\˜B  B¨Ð+Ð,°U´\ÐBÑBÔB€Få
ˆ5Œ;ÑÔ˜1ÒÐØ×!Ò! !Ñ$Ô$ˆØ�6‰>Ðr"   Úpointsc                 ó°   — |\  }}}}t          j        ||gg| j        ¬¦  «        }t          | j        ¦  «        dk    r|                     d¦  «        }| |z   S )z,Uncrop points by adding the crop box offset.ru   rv   r   rw   )r|   r
   rp   rq   r{   rJ   s         r    Úuncrop_pointsr~   n   s]   € à�L€BˆˆAˆqÝŒ\˜B ˜8˜*¨V¬]Ð;Ñ;Ô;€Få
ˆ6Œ<ÑÔ˜AÒÐØ×!Ò! !Ñ$Ô$ˆØ�F‰?Ðr"   Úorig_hÚorig_wc                 óÊ   — |\  }}}}|dk    r|dk    r||k    r||k    r| S |||z
  z
  |||z
  z
  }	}|||z
  ||	|z
  f}
t           j        j                             | |
d¬¦  «        S )z8Uncrop masks by padding them to the original image size.r   )Úvalue)r   ÚnnÚ
functionalÚpad)r6   r
   r   r€   rp   rq   Úx1Úy1Úpad_xÚpad_yr…   s              r    Úuncrop_masksrŠ   x   s†   € à�N€BˆˆB�Ø	ˆQ‚w€w�2˜’7�7˜r Vš|˜|°°f²°Øˆà˜R "™WÑ% v°°b±Ñ'9ˆ5€EØˆu�r‰z˜2˜u r™zÐ
*€CÝŒ8Ô×"Ò" 5¨#°QÐ"Ñ7Ô7Ð7r"   ÚmaskÚarea_threshÚmodec                 óÌ  ‡‡— ddl }|dv sJ ‚|dk    }|| z                       t          j        ¦  «        }|                     |d¦  «        \  }}}}	|dd…df         dd…         }
ˆfd„t          |
¦  «        D ¦   «         }|s| d	fS dg|z   Š|s@ˆfd
„t          |¦  «        D ¦   «         p$t          t          j        |
¦  «        ¦  «        dz   gŠt          j	        |‰¦  «        } | dfS )zfRemove small disconnected regions or holes in a mask, returning the mask and a modification indicator.r   N>   ÚholesÚislandsr�   é   r:   r   c                 ó,   •— g | ]\  }}|‰k     ¯|d z   ‘ŒS r,   r-   )r'   rS   ÚsrŒ   s      €r    r0   z(remove_small_regions.<locals>.<listcomp>Œ   s&   ø€ ÐKÐKÐK™t˜q !¸1¸{º?¸?�Q˜‘U¸?¸?¸?r"   Fc                 ó   •— g | ]}|‰v¯|‘Œ	S r-   r-   )r'   rS   Úfill_labelss     €r    r0   z(remove_small_regions.<locals>.<listcomp>’   s#   ø€ ÐJÐJÐJ˜Q°Q¸kÐ5IÐ5I�qÐ5IÐ5IÐ5Ir"   T)
Úcv2ÚastyperE   Úuint8ÚconnectedComponentsWithStatsÚ	enumerater3   r2   ÚargmaxÚisin)r‹   rŒ   r�   r–   Úcorrect_holesÚworking_maskÚn_labelsÚregionsÚstatsr{   ÚsizesÚsmall_regionsr•   s    `          @r    Úremove_small_regionsr¤   ƒ   s   øø€ à€J€J€JàÐ'Ð'Ð'Ð'Ð'Ø˜G’O€MØ! DÑ(×0Ò0µ´Ñ:Ô:€LØ"%×"BÒ"BÀ<ÐQRÑ"SÔ"SÑ€Hˆg�u˜aØ�!�!�!�R�%ŒL˜˜˜Ô€EØKÐKÐKÐK¥y°Ñ'7Ô'7ÐKÑKÔK€MØð Ø�Uˆ{ÐØ�#˜Ñ%€KØð jàJÐJÐJÐJ¥%¨¡/¤/ÐJÑJÔJÐiÍsÕSUÔS\Ð]bÑScÔScÑOdÔOdÐghÑOhÐNiˆÝŒ7�7˜KÑ(Ô(€DØ�ˆ:Ðr"   c                 óú  — t          j        | ¦  «        dk    r(t          j        g | j        dd…         ¢d‘R d| j        iŽS | j        }|dd…         \  }}t          |¦  «        dk    r|                      dd¦  «        n|                      d¦  «        } t          j        | d¬	¦  «        \  }}|t          j	        ||j        ¬
¦  «        ddd…f         z  }t          j        |d¬	¦  «        \  }}||| z  z   }t          j
        |d¬	¦  «        \  }}t          j        | d¬	¦  «        \  }	}|	t          j	        ||	j        ¬
¦  «        ddd…f         z  }
t          j        |
d¬	¦  «        \  }}|
||	 z  z   }
t          j
        |
d¬	¦  «        \  }}||k     ||k     z  }t          j        ||||gd¬	¦  «        }||                      d¦  «        z  }t          |¦  «        dk    r |j        g |dd…         ¢d‘R Ž n|d         S )z 
    Calculates boxes in XYXY format around masks.

    Return [0,0,0,0] for an empty mask. For input shape C1xC2x...xHxW, the output shape is C1xC2x...x4.
    r   Néþÿÿÿé   r   rC   éýÿÿÿr:   r   ru   )r   ÚnumelÚzerosry   r   r&   Úflattenrz   ÚmaxÚarangere   rH   rI   )r6   ry   ÚhÚwÚ	in_heightr{   Úin_height_coordsÚbottom_edgesÚ	top_edgesÚin_widthÚin_width_coordsÚright_edgesÚ
left_edgesÚempty_filterÚouts                  r    Úbatched_mask_to_boxrº   —   s+  € õ „{�5ÑÔ˜QÒÐÝŒ{ÐE˜EœK¨¨¨Ô,ÐE¨aÐEÐEÐE¸¼ÐEÐEÐEð ŒK€EØ���Œ:�D€A€qÝ$'¨¡J¤J°¢N NˆE�MŠM˜!˜RÑ Ô Ð ¸¿ºÈÑ8JÔ8J€Eå”9˜U¨Ð+Ñ+Ô+�L€IˆqØ ¥5¤<°¸)Ô:JÐ#KÑ#KÔ#KÈDÐRSÐRSÐRSÈGÔ#TÑTÐÝ”iÐ 0°bÐ9Ñ9Ô9�O€L�!Ø'¨!°	¨zÑ*:Ñ:ÐÝ”9Ð-°2Ð6Ñ6Ô6�L€Iˆqõ ”)˜E rÐ*Ñ*Ô*�K€HˆaØ¥¤¨a¸¼Ð!HÑ!HÔ!HÈÈqÈqÈqÈÔ!QÑQ€OÝ”Y˜°BÐ7Ñ7Ô7�N€K�Ø%¨¨h¨Y©Ñ7€OÝ”I˜o°2Ð6Ñ6Ô6�M€J�ð   *Ò,°À	Ò1IÑJ€LÝ
Œ+�z 9¨k¸<ÐHÈbÐ
QÑ
QÔ
Q€CØ
�,�×)Ò)¨"Ñ-Ô-Ñ
-€Cõ +.¨e©*¬*°qª.¨.ˆ;ˆ3Œ;Ð&˜˜c˜r˜cœ
Ð& AÐ&Ð&Ð&Ð&¸cÀ!¼fÐDr"   )r   )rY   Ú	itertoolsr   Útypingr   r   r   r   ÚnumpyrE   r   ÚTensorr2   r   r!   r5   r@   ÚndarrayrN   rT   rs   r   r~   rŠ   ÚstrÚboolr¤   rº   r-   r"   r    ú<module>rÂ      sú  ðð €€€Ø Ð Ð Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ð .à Ð Ð Ð Ø €€€ð )-ð,ð , ¤ð ,Ø$(¨¤Ið,à$(¨¤Ið,ð !&ð,ð 27´ð,ð ,ð ,ð ,ðI˜sð I¨i¸¸S¼	À4ÈÐ8MÔ.Nð Ið Ið Ið Ið" U¤\ð "À5ð "Ð\að "ÐfkÔfrð "ð "ð "ð "ðB ð B¨¬ð Bð Bð Bð Bðe¨Cð e¸3ð eÐQTð eÐY]Ð^`Ô^hÔYið eð eð eð eð
#"  s¨C x¤ð #"¸Cð #"Ø',ð#"Ø16°t¸DÀ¼I´ÈÈSÌ	Ð7QÔ1Rð#"ð #"ð #"ð #"ðL˜Uœ\ð °T¸#´Yð À5Ä<ð ð ð ð ð˜%œ,ð °$°s´)ð ÀÄð ð ð ð ð8˜œð 8°°S´	ð 8À3ð 8ÐPSð 8ÐX]ÔXdð 8ð 8ð 8ð 8ð˜rœzð ¸ð ÀSð ÈUÐSUÔS]Ð_cÐScÔMdð ð ð ð ð(#E˜uœ|ð #E°´ð #Eð #Eð #Eð #Eð #Eð #Er"   