§
    >²¸j‘*  ã                  ó¦   — d dl mZ d dlZd dlmZmZ d dlZd dlmZ	 d dl
mZ d dlmZ d dlmZ dd„Zdd„Zdd„Zdd„Zdd„Zd d„Zd!d„Z	 d"d#d„ZdS )$é    )ÚannotationsN)ÚAnyÚcast)ÚImage)ÚCLASS_NAME_DATA_FIELD)Úmask_to_xyxyÚdetection_resultúdict[str, Any]Úid2labelúdict[int, str] | NoneÚreturnc                óæ  — | d                               ¦   «                              ¦   «                              ¦   «                              t          ¦  «        }t          ||i ¦  «        }t          | d                               ¦   «                              ¦   «                              ¦   «         | d                               ¦   «                              ¦   «                              ¦   «         ||¬¦  «        S )a[  
    Process the result of Transformers object detection functions such as
    `post_process` (v4) and `post_process_detection` (v5).

    Args:
        detection_result: Dictionary containing detection results with keys
            'boxes', 'labels', and 'scores'.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed detection result including bounding boxes, confidence scores,
            class IDs, and data.
    ÚlabelsÚboxesÚscores)ÚxyxyÚ
confidenceÚclass_idÚdata)ÚcpuÚdetachÚnumpyÚastypeÚintÚappend_class_names_to_dataÚdict)r	   r   Ú	class_idsr   s       úx/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/supervision/detection/tools/transformers.pyÚ%process_transformers_detection_resultr      sÆ   € ð$ ! Ô*×.Ò.Ñ0Ô0×7Ò7Ñ9Ô9×?Ò?ÑAÔA×HÒHÍÑMÔM€IÝ% i°¸2Ñ>Ô>€DåØ˜gÔ&×*Ò*Ñ,Ô,×3Ò3Ñ5Ô5×;Ò;Ñ=Ô=Ø# HÔ-×1Ò1Ñ3Ô3×:Ò:Ñ<Ô<×BÒBÑDÔDØØð	ñ ô ð ó    Úsegmentation_resultc                ó  — d| v rt          | |¦  «        S d}d| v r>| d                              ¦   «                              ¦   «                              ¦   «         }| d                              ¦   «                              ¦   «                              ¦   «                              t
          ¦  «        }| d                              ¦   «                              ¦   «                              ¦   «                              t          ¦  «        }t          |�|nt          |¦  «        |�t          j
        |d¬¦  «        n|| d                              ¦   «                              ¦   «                              ¦   «         |t          ||i ¦  «        ¬	¦  «        S )
aŒ  
    Process the result of Transformers segmentation functions such as
    `post_process_panoptic`, `post_process_segmentation`, and `post_process_instance`
    (v4).

    Args:
        segmentation_result: Dictionary containing segmentation results with keys
            'masks', 'labels', and 'scores'.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed segmentation result including bounding boxes, masks, confidence
            scores, class IDs, and data.
    Ú
png_stringNr   Úmasksr   é   ©Úaxisr   ©r   Úmaskr   r   r   )Ú4process_transformers_v4_panoptic_segmentation_resultr   r   r   r   Úboolr   r   r   ÚnpÚsqueezer   )r!   r   r   r$   r   s        r   Ú+process_transformers_v4_segmentation_resultr.   +   sf  € ð& Ð*Ð*Ð*ÝCØ ñ
ô 
ð 	
ð ˆØÐ)Ð)Ð)Ø'¨Ô0×4Ò4Ñ6Ô6×=Ò=Ñ?Ô?×EÒEÑGÔGˆEØ# GÔ,×0Ò0Ñ2Ô2×9Ò9Ñ;Ô;×AÒAÑCÔC×JÒJÍ4ÑPÔPˆØ'¨Ô1×5Ò5Ñ7Ô7×>Ò>Ñ@Ô@×FÒFÑHÔH×OÒOÕPSÑTÔTˆ	åØÐ+��µ¸eÑ1DÔ1DØ.3Ð.?•”˜E¨Ð*Ñ*Ô*Ð*ÀUØ*¨8Ô4×8Ò8Ñ:Ô:×AÒAÑCÔC×IÒIÑKÔKØÝ+¨I°xÀÑDÔDð
ñ 
ô 
ð 	
r    r   c                ó  — | j         j        dk    rH|                      ¦   «                              ¦   «                              ¦   «         }t          ||¦  «        S t          t          t          t          t          f         | ¦  «        |¦  «        S )aû  
    Process the result of Transformers segmentation functions such as
    `post_process_semantic_segmentation`, `post_process_instance_segmentation`, and
    `post_process_panoptic_segmentation` (v5).

    Args:
        segmentation_result: Either a dictionary containing segmentation results
            (`segments_info` and `segmentation`) or a tensor object
            representing a semantic segmentation map.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed segmentation result including bounding boxes, masks, confidence
            scores, class IDs, and data.
    ÚTensor)Ú	__class__Ú__name__r   r   r   Ú4process_transformers_v5_panoptic_segmentation_resultÚ@process_transformers_v5_semantic_or_instance_segmentation_resultr   r   Ústrr   )r!   r   Úsegmentation_arrays      r   Ú+process_transformers_v5_segmentation_resultr7   R   sƒ   € ð( Ô$Ô-°Ò9Ð9Ø0×4Ò4Ñ6Ô6×=Ò=Ñ?Ô?×EÒEÑGÔGÐÝCØ ñ
ô 
ð 	
õ LÝ�T•#•s�(Œ^Ð0Ñ1Ô1°8ñô ð r    c                óØ  ‡— | d         }| d                               ¦   «                              ¦   «                              ¦   «         Št          j        d„ |D ¦   «         t
          ¬¦  «        }t          j        d„ |D ¦   «         t          ¬¦  «        }t          |¦  «        dk    r%t          j        dg‰j	        ¢R t          ¬¦  «        }np‰j        dk    r-d„ |D ¦   «         }‰|                              t          ¦  «        }n8t          j        ˆfd	„|D ¦   «         ¦  «                             t          ¦  «        }t          ||i ¦  «        }t          t          |¦  «        ||||¬
¦  «        S )aj  
    Process the result of Transformers segmentation functions such as
    `post_process_semantic_segmentation` and `post_process_instance_segmentation` (v5).

    Args:
        segmentation_result: Dictionary containing segmentation results with keys
            `segments_info` and `segmentation`. `segmentation` is either a
            `(height, width)` map of segment ids or, for
            `post_process_instance_segmentation(return_binary_maps=True)`, a
            `(num_segments, height, width)` stack of binary maps.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed segmentation result including bounding boxes, masks, confidence
            scores, class IDs, and data.
    Úsegments_infoÚsegmentationc                ó   — g | ]
}|d          ‘ŒS )Úscore© ©Ú.0Úsegments     r   ú
<listcomp>zTprocess_transformers_v5_semantic_or_instance_segmentation_result.<locals>.<listcomp>‡   s   € ÐEÐEÐE¨G�w˜wÔ'ÐEÐEÐEr    ©Údtypec                ó   — g | ]
}|d          ‘ŒS )Úlabel_idr=   r>   s     r   rA   zTprocess_transformers_v5_semantic_or_instance_segmentation_result.<locals>.<listcomp>ˆ   s   € ÐKÐKÐK°'˜' *Ô-ÐKÐKÐKr    r   é   c                ó   — g | ]
}|d          ‘ŒS ©Úidr=   r>   s     r   rA   zTprocess_transformers_v5_semantic_or_instance_segmentation_result.<locals>.<listcomp>�   s   € ÐBÐBÐB¨�w˜t”}ÐBÐBÐBr    c                ó(   •— g | ]}‰|d          k    ‘ŒS rH   r=   ©r?   r@   r6   s     €r   rA   zTprocess_transformers_v5_semantic_or_instance_segmentation_result.<locals>.<listcomp>“   ó#   ø€ ÐNÐNÐN°WÐ 7¨4¤=Ò0ÐNÐNÐNr    r(   )r   r   r   r,   ÚarrayÚfloatr   ÚlenÚemptyÚshaper+   Úndimr   r   r   r   )	r!   r   r9   r   r   r$   Úsegment_idsr   r6   s	           @r   r4   r4   p   sr  ø€ ð* (¨Ô8€MØ,¨^Ô<×@Ò@ÑBÔB×IÒIÑKÔK×QÒQÑSÔSÐÝŒXÐEÐE°}ÐEÑEÔEÍUÐSÑSÔS€FÝ”ÐKÐK¸]ÐKÑKÔKÕSVÐWÑWÔW€IÝ
ˆ=ÑÔ˜QÒÐÝ”˜!Ð7Ð0Ô6Ð7Ð7½tÐDÑDÔDˆˆØ	Ô	  AÒ	%Ð	%ð CÐB°MÐBÑBÔBˆØ" ;Ô/×6Ò6µtÑ<Ô<ˆˆå”ØNÐNÐNÐNÀÐNÑNÔNñ
ô 
ç
Š&•‰,Œ,ð 	õ & i°¸2Ñ>Ô>€DåÝ˜%Ñ Ô ØØØØðñ ô ð r    c                óÐ  ‡— | d         }| d         }t          j        d„ |D ¦   «         t          ¬¦  «        }t          |¬¦  «        Št	          |¦  «        dk    r%t          j        dg‰j        ¢R t          ¬¦  «        }n8t          j        ˆfd„|D ¦   «         ¦  «                             t          ¦  «        }t          ||i ¦  «        }t          t          |¦  «        |||¬¦  «        S )	a.  
    Process the result of the Transformers function `post_process_panoptic` (v4).

    Args:
        segmentation_result: Dictionary containing segmentation results with keys
            such as 'png_string' and 'segments_info'.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed segmentation result including bounding boxes, masks,
            class IDs, and data.
    r9   r#   c                ó   — g | ]
}|d          ‘ŒS )Úcategory_idr=   r>   s     r   rA   zHprocess_transformers_v4_panoptic_segmentation_result.<locals>.<listcomp>´   s   € Ð=Ð=Ð= Gˆ�Ô	Ð=Ð=Ð=r    rB   )r#   r   c                ó(   •— g | ]}‰|d          k    ‘ŒS rH   r=   rK   s     €r   rA   zHprocess_transformers_v4_panoptic_segmentation_result.<locals>.<listcomp>»   rL   r    ©r   r)   r   r   )r,   rM   r   Ú png_string_to_segmentation_arrayrO   rP   rQ   r+   r   r   r   r   )r!   r   r9   r#   r   r$   r   r6   s          @r   r*   r*       s  ø€ ð" (¨Ô8€MØ$ \Ô2€JÝ”Ø=Ð=¨}Ð=Ñ=Ô=ÅSðñ ô €Iõ :ÀZÐPÑPÔPÐÝ
ˆ=ÑÔ˜QÒÐÝ”˜!Ð7Ð0Ô6Ð7Ð7½tÐDÑDÔDˆˆå”ØNÐNÐNÐNÀÐNÑNÔNñ
ô 
ç
Š&•‰,Œ,ð 	õ & i°¸2Ñ>Ô>€DåÝ˜%Ñ Ô ØØØð	ñ ô ð r    r6   únpt.NDArray[Any]c                ó¢  ‡ — t          j        ‰ ¦  «                             t          ¦  «        }t	          |¦  «        dk    r%t          j        dg‰ j        ¢R t          ¬¦  «        }n:t          j        ˆ fd„|D ¦   «         d¬¦  «                             t          ¦  «        }t          ||i ¦  «        }t          t          |¦  «        |||¬¦  «        S )aâ  
    Process a v5 Transformers semantic segmentation tensor.

    Args:
        segmentation_array: Segmentation array where unique values are class IDs.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.

    Returns:
        Processed segmentation result including bounding boxes, masks,
            class IDs, and data.
    r   rB   c                ó   •— g | ]}‰|k    ‘Œ	S r=   r=   )r?   r   r6   s     €r   rA   zHprocess_transformers_v5_panoptic_segmentation_result.<locals>.<listcomp>Ü   s   ø€ ÐFÐFÐF°Ð 8Ò+ÐFÐFÐFr    r&   rX   )r,   Úuniquer   r   rO   rP   rQ   r+   Ústackr   r   r   )r6   r   r   r$   r   s   `    r   r3   r3   Ç   sÈ   ø€ õ  ”	Ð,Ñ-Ô-×4Ò4µSÑ9Ô9€IÝ
ˆ9�~„~˜ÒÐÝ”˜!Ð7Ð0Ô6Ð7Ð7½tÐDÑDÔDˆˆå”ØFÐFÐFÐF¸IÐFÑFÔFÈQð
ñ 
ô 
ç
Š&•‰,Œ,ð 	õ & i°¸2Ñ>Ô>€DÝ•\ %Ñ(Ô(¨u¸yÈtÐTÑTÔTÐTr    r#   Úbytesc                ó„  — t          j        t          j        | ¦  «        ¦  «        }t	          j        |t          j        ¬¦  «        }|j        dk    r|                     t          j	        ¦  «        S |j
        d         dk     rt          d¦  «        ‚|dd…dd…df                              t          j	        ¦  «        |dd…dd…df                              t          j	        ¦  «        dz  z   |dd…dd…df                              t          j	        ¦  «        d	z  z   }t          t          j        t                   |¦  «        S )
a„  
    Convert a PNG byte string to a panoptic segmentation array.

    Args:
        png_string: A byte string representing the PNG image.

    Returns:
        A segmentation ID array with shape (H, W), where each unique value
            represents a different object or category. RGB-encoded panoptic
            PNGs are decoded as little-endian 24-bit integers; alpha is ignored.
    rB   é   rF   z1Panoptic PNG masks must have at least 3 channels.Nr   r%   é   é   )r   ÚopenÚioÚBytesIOr,   rM   Úuint8rR   r   Úuint32rQ   Ú
ValueErrorr   ÚnptÚNDArrayr   )r#   Úimager)   r:   s       r   rY   rY   â   s  € õ ŒJ•r”z *Ñ-Ô-Ñ.Ô.€EÝŒ8�E¥¤Ð*Ñ*Ô*€DØ„y�A‚~€~Ø�{Š{�2œ9Ñ%Ô%Ð%Ø„z�!„}�qÒÐÝÐLÑMÔMÐMð 	ˆQˆQˆQ����1ˆWŒ×Ò�RœYÑ'Ô'Ø����1�1�1�a�Œ=×Ò¥¤	Ñ*Ô*¨aÑ/ñ	1à����1�1�1�a�Œ=×Ò¥¤	Ñ*Ô*¨bÑ0ñ	2ð õ
 •”�CÔ  ,Ñ/Ô/Ð/r    r   r   údict[str, Any] | Nonec                óh   ‡— |€i }‰�*t          j        ˆfd„| D ¦   «         ¦  «        }||t          <   |S )aî  
    Helper function to create or append to a data dictionary with class names if
    available.

    Args:
        class_ids: Array of class IDs.
        id2label: A dictionary mapping class IDs to labels,
            typically part of the `transformers` model configuration. If provided, the
            resulting dictionary will include class names.
        data: An existing data dictionary to append to.

    Returns:
        Dictionary containing class names if id2label is provided.
    Nc                ó    •— g | ]
}‰|         ‘ŒS r=   r=   )r?   r   r   s     €r   rA   z.append_class_names_to_data.<locals>.<listcomp>  s   ø€ ÐMÐMÐM°x ¨Ô 2ÐMÐMÐMr    )r,   rM   r   )r   r   r   Úclass_namess    `  r   r   r   ý   sI   ø€ ð& €|ØˆàÐÝ”hÐMÐMÐMÐMÀ9ÐMÑMÔMÑNÔNˆØ&1ˆÕ"Ñ#à€Kr    )r	   r
   r   r   r   r
   )r!   r
   r   r   r   r
   )r!   r   r   r   r   r
   )r6   rZ   r   r   r   r
   )r#   r_   r   rZ   )N)r   rZ   r   r   r   rm   r   r
   )Ú
__future__r   re   Útypingr   r   r   r,   Únumpy.typingrj   ÚPILr   Úsupervision.configr   Ú&supervision.detection.utils.convertersr   r   r.   r7   r4   r*   r3   rY   r   r=   r    r   ú<module>rw      sG  ðØ "Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø Ð Ð Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø ?Ð ?Ð ?Ð ?Ð ?Ð ?ðð ð ð ð:$
ð $
ð $
ð $
ðNð ð ð ð<-ð -ð -ð -ð`$ð $ð $ð $ðNUð Uð Uð Uð60ð 0ð 0ð 0ð< #'ðð ð ð ð ð ð r    