
    ^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MI%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GE#G,00299;AACJJ4P'1557>>@FFHOOPST	+e1D.3.?E*U*8488:AACIIK+Ix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18 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    $ (8MXX}EGww'EFF]K''*-KLI,^<@@BIIKQQSHH<IJ	wt}	,JfTl 
 &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    " (8M$\2JNW'-0NOI9ZPHH<IJ	wt}	,JfTl 
 &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HH8ABH	x	'BfTl 
 &i2>D\%(uyt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     JJrzz*-.E88E*DC $q!Qw-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    & |hh9Mx 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   