Ë
    ¼Ó^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y)é    )ÚannotationsN)ÚAnyÚcast)ÚImage)ÚCLASS_NAME_DATA_FIELD)Úmask_to_xyxyc                ór  — | d   j                  «       j                  «       j                  «       j                  t        «      }t        ||i «      }t        | d   j                  «       j                  «       j                  «       | d   j                  «       j                  «       j                  «       ||¬«      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)Údetection_resultÚid2labelÚ	class_idsr   s       ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/supervision/detection/tools/transformers.pyÚ%process_transformers_detection_resultr      s›   € ð$ ! Ñ*×.Ñ.Ó0×7Ñ7Ó9×?Ñ?ÓA×HÑHÌÓM€IÜ% i°¸2Ó>€DäØ˜gÑ&×*Ñ*Ó,×3Ñ3Ó5×;Ñ;Ó=Ø# HÑ-×1Ñ1Ó3×:Ñ:Ó<×BÑBÓDØØô	ð ó    c                óp  — d| v rt        | |«      S d}d| v r/| d   j                  «       j                  «       j                  «       }| d   j                  «       j                  «       j                  «       j	                  t
        «      }| d   j                  «       j                  «       j                  «       j	                  t        «      }t        |�|n
t        |«      |�t        j                  |d¬«      n|| d   j                  «       j                  «       j                  «       |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   )Úsegmentation_resultr   r   r    r   s        r   Ú+process_transformers_v4_segmentation_resultr+   +   s  € ð& Ð*Ñ*ÜCØ ó
ð 	
ð ˆØÐ)Ñ)Ø'¨Ñ0×4Ñ4Ó6×=Ñ=Ó?×EÑEÓGˆEØ# GÑ,×0Ñ0Ó2×9Ñ9Ó;×AÑAÓC×JÑJÌ4ÓPˆØ'¨Ñ1×5Ñ5Ó7×>Ñ>Ó@×FÑFÓH×OÑOÔPSÓTˆ	äØÐ+‘´¸eÓ1DØ.3Ð.?”—‘˜E¨Õ*ÀUØ*¨8Ñ4×8Ñ8Ó:×AÑAÓC×IÑIÓKØÜ+¨I°xÀÓDô
ð 	
r   c                óò   — | j                   j                  dk(  r8| j                  «       j                  «       j	                  «       }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 panoptic 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_resultr4   R   sp   € ð( ×$Ñ$×-Ñ-°Ò9Ø0×4Ñ4Ó6×=Ñ=Ó?×EÑEÓGÐÜCØ ó
ð 	
ô LÜŒT”#”s�(‰^Ð0Ó1°8óð r   c                óä  — | d   }t        j                  |D �cg c]  }|d   ‘Œ	 c}«      }t        j                  |D �cg c]  }|d   ‘Œ	 c}«      }| d   j                  «       j                  «       j	                  «       }t        j                  |D �cg c]
  }||d   k(  ‘Œ c}«      j                  t        «      }t        ||i «      }t        t        |«      ||||¬«      S c c}w c c}w c c}w )a‡  
    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`.
        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ÚscoreÚlabel_idÚsegmentationÚidr$   )
r(   Úarrayr   r   r   r   r'   r   r   r   )	r*   r   r6   Úsegmentr   r   r3   r    r   s	            r   r1   r1   p   sâ   € ð$ (¨Ñ8€MÜ�X‰X°}ÖE¨G�w˜wÓ'ÒEÓF€FÜ—‘¸]ÖK°'˜' *Ó-ÒKÓL€IØ,¨^Ñ<×@Ñ@ÓB×IÑIÓK×QÑQÓSÐÜ�H‰HØ<IÖJ°Ð	˜w t™}Ó	,ÒJóç�fŒTƒlð 
ô & i°¸2Ó>€DäÜ˜%Ó ØØØØôð ùò FùÚKùò 	Ks   ™C#¿C(ÂC-c                óP  — | d   }| d   }t        j                  |D �cg c]  }|d   ‘Œ	 c}«      }t        |¬«      }t        j                  |D �cg c]
  }||d   k(  ‘Œ c}«      j                  t        «      }t        ||i «      }t        t        |«      |||¬«      S c c}w c c}w )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.
    r6   r   Úcategory_id)r   r:   ©r   r%   r   r   )r(   r;   Ú png_string_to_segmentation_arrayr   r'   r   r   r   )	r*   r   r6   r   r<   r   r3   r    r   s	            r   r&   r&   ”   s­   € ð" (¨Ñ8€MØ$ \Ñ2€JÜ—‘ÀÖN°W˜' -Ó0ÒNÓO€IÜ9ÀZÔPÐÜ�H‰HØ<IÖJ°Ð	˜w t™}Ó	,ÒJóç�fŒTƒlð 
ô & i°¸2Ó>€DäÜ˜%Ó ØØØô	ð ùò Oùò 	Ks   žBÁB#c                óö   — t        j                  | «      }t        j                  |D �cg c]  }| |k(  ‘Œ	 c}d¬«      j                  t        «      }t        ||i «      }t        t        |«      |||¬«      S c c}w )aç  
    Process the result of the Transformers function
    `post_process_panoptic_segmentation` (v5).

    Args:
        segmentation_array: Segmentation array.
        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   r"   r?   )r(   ÚuniqueÚstackr   r'   r   r   r   )r3   r   r   r   r    r   s         r   r0   r0   ¶   so   € ô" —	‘	Ð,Ó-€IÜ�H‰HØ8AÖB¨HÐ	˜xÓ	'ÒBÈôç�fŒTƒlð 
ô & i°¸2Ó>€DÜ”\ %Ó(¨u¸yÈtÔTÐTùò 	Cs   ©A6c                óò   — t        j                  t        j                  | «      «      }t	        j
                  |t        j                  ¬«      }t        t        j                  t           |dd…dd…df   «      S )aV  
    Convert a PNG byte string to a label mask array.

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

    Returns:
        A label mask array with shape (H, W), where H and W
            are the height and width of the image. Each unique value in the array
            represents a different object or category.
    )ÚdtypeNr   )r   ÚopenÚioÚBytesIOr(   r;   Úuint8r   ÚnptÚNDArrayr   )r   Úimager%   s      r   r@   r@   Ï   sO   € ô �J‰J”r—z‘z *Ó-Ó.€EÜ�8‰8�E¤§¡Ô*€DÜ”—‘œCÑ  $¢qª!¨Q w¡-Ó0Ð0r   c                óz   — |€i }|�/t        j                  | D �cg c]  }||   ‘Œ	 c}«      }||t        <   |S c c}w )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.
    )r(   r;   r   )r   r   r   r   Úclass_namess        r   r   r   à   sH   € ð& €|ØˆàÐÜ—h‘hÀ9ÖM°x ¨Ó 2ÒMÓNˆØ&1ˆÔ"Ñ#à€Kùò  Ns   š8)r   údict[str, Any]r   údict[int, str] | NoneÚreturnrO   )r*   rO   r   rP   rQ   rO   )r*   r   r   rP   rQ   rO   )r3   únpt.NDArray[Any]r   rP   rQ   rO   )r   ÚbytesrQ   rR   )N)r   rR   r   rP   r   zdict[str, Any] | NonerQ   rO   )Ú
__future__r   rG   Útypingr   r   r   r(   Únumpy.typingrJ   ÚPILr   Úsupervision.configr   Ú&supervision.detection.utils.convertersr   r   r+   r4   r1   r&   r0   r@   r   © r   r   ú<module>r[      s  ðÝ "ã 	ß ã Ý Ý å 4Ý ?ðØ$ðØ0Eðàóð:$
Ø'ð$
Ø3Hð$
àó$
ðNØðØ(=ðàóð<!Ø'ð!Ø3Hð!àó!ðHØ'ðØ3HðàóðDUØ(ðUØ4IðUàóUó21ð( #'ðØðà#ðð  ðð ô	r   