CoolFace
Apppublic

xdecoder/Instruct-X-Decoder

sourceHugging Faceafl-3.0updated 3y agoView on Hugging Face
163likes
visualizer.cpython-38.pyc551 linesDownload Raw Back to __pycache__
1U

2E$�c���@sNddlZddlZddlZddlZddlmZmZddlZddl	Z3ddlmZ
ddlmZddlmZddlZddlmZddlmZddlmZddlmZmZmZmZm Z m!Z!ddl"m#Z#ddl$m%Z%e�&e'�Z(d	d4dgZ)dZ*d
Z+dZ,dZ-dZ.dZ/eGdd	�d	e��Z0Gdd�d�Z1Gdd�d�Z2ddd�Z3Gdd5�d6�Z4Gdd�d�Z5dS)�N)�Enum�unique)�FigureCanvasAgg)�Image)�MetadataCatalog)�BitMasks�Boxes�BoxMode�	Keypoints�PolygonMasks�RotatedBoxes)�PathManager��random_color�	ColorMode�VisImage�7Visualizeri�i��)��?rg�?)rrr)rrrg�������?c@seZdZdZdZdZdZdS)rzK8    Enum of different color modes to use for instance visualizations.9    r��N)�__name__�10__module__�__qualname__�__doc__�IMAGE�SEGMENTATION�IMAGE_BW�rr�)/data/arXiv/demo/Demo/utils/visualizer.pyr#s11c@s\eZdZdZdd�Zedd��Zedd��Zedd	��Zd12d�Z	dd
�Z13dd�Zdd�ZdS)�GenericMaskz�14    Attribute:15        polygons (list[ndarray]): list[ndarray]: polygons for this mask.16            Each ndarray has format [x, y, x, y, ...]17        mask (ndarray): a binary mask18    cCs4d|_|_|_||_||_|}t|t�r�d|kr<d|ks@t�t|dt�r||d\}}||krj||ksnt�t	�19|||�}t	�|�dd�dd�f|_dSt|t�r�dd�|D�|_dSt|tj
��r|jddks�t|j��|j||fk�std|j�d|�d	|����|�d20�|_dStd�|t|����dS)N�counts�sizecSsg|]}t�|��d��qS)�����)�np�asarray�reshape��.0�xrrr�21<listcomp>Ssz(GenericMask.__init__.<locals>.<listcomp>rrzmask shape: z, target dims: z, �uint8z0GenericMask cannot handle object {} of type '{}')�_mask�	_polygons�22_has_holes�height�width�23isinstance�dict�AssertionError�list�	mask_util�frPyObjects�decoder#�ndarray�shape�astype�24ValueError�format�type)�selfZmask_or_polygonsr.r/�m�h�wrrr�__init__Bs42526�27�zGenericMask.__init__cCs|jdkr|�|j�|_|jS�N)r+�polygons_to_maskr,�r=rrr�maskas28zGenericMask.maskcCs$|jdkr|�|j�\|_|_|jSrB)r,�mask_to_polygonsr+r-rDrrr�polygonsgs29zGenericMask.polygonscCs6|jdkr0|jdk	r*|�|j�\|_|_nd|_|jS)NF)r-r+rFr,rDrrr�	has_holesms303132zGenericMask.has_holescCs�t�|�}t�|�d�tjtj�}|d}|dkr:gdfS|�dd�dd�dfdk��dk}|d}dd	�|D�}d33d	�|D�}||fS)Nr*r"F��r�����cSsg|]}|���qSr)�flattenr&rrrr)�sz0GenericMask.mask_to_polygons.<locals>.<listcomp>cSs g|]}t|�dkr|d�qS)���?)�lenr&rrrr)�s)	r#�ascontiguousarray�cv2�findContoursr9�34RETR_CCOMP�CHAIN_APPROX_NONEr%�sum)r=rE�resZ	hierarchyrHrrrrFvs35$zGenericMask.mask_to_polygonscCs6t�||j|j�}t�|�}t�|�dd�dd�fSrB)r4r5r.r/�merger6)r=rG�rlerrrrC�s36zGenericMask.polygons_to_maskcCs37|j��SrB)rErUrDrrr�area�szGenericMask.areacCsTt�|j|j|j�}t�|�}t�|�}|d|d7<|d|d7<|S)NrrrJr)r4r5rGr.r/rW�toBbox)r=�p�bboxrrrr\�s3839zGenericMask.bboxN)
rrrrrA�propertyrErGrHrFrCrYr\rrrrr:s404142rc@s2eZdZdZddd�Zdd�Zdd�Zd	d43�ZdS)�_PanopticPredictionz@44    Unify different panoptic annotation/prediction formats45    Nc
Cs46|dkrp|dk	st�|j}g}t�|���D]B}|dkr:q,||}||j��k}|�t|�t|�t	|�d��q,~||_47dd�|D�|_tj|ddd�\}}	|	��}	t�
|	�}48||49|	|50|_|_|j��|_t|j|j�D]$\}}||jkr�t|�|j|d<q�dS)Nr")�id�category_id�isthingcSsi|]}|d|�qS)r_r�r'�srrr�51<dictcomp>�sz0_PanopticPrediction.__init__.<locals>.<dictcomp>T)�sorted�
return_countsrY)r2�
label_divisorr#r�numpy�!thing_dataset_id_to_contiguous_id�values�append�int�bool�_seg�_sinfo�torch�argsort�_seg_idsZ52_seg_areas�tolist�zip�float)
r=�panoptic_seg�
segments_info�metadatargZpanoptic_label�53pred_classraZsegment_ids�areas�sorted_idxs�sidrYrrrrA�s4��54z_PanopticPrediction.__init__cCsrg}|jD]}||jkr55|�|�q56t|�dkrDtj|jjtjd�St|�dksXt	d��|j|dk�57��tj�S)za58        Returns:59            (H, W) array, a mask for all pixels that have a prediction60        r��dtyperz@>1 ids corresponds to no labels. This is currently not supported)
rrrorkrOr#�zerosrnr8r*r2rhr9rm)r=Z	empty_idsr_rrr�non_empty_mask�s616263��z"_PanopticPrediction.non_empty_maskccsJ|jD]>}|j�|�}|dks|dr(q|j|k���tj�|fVqdS)Nra)rrro�getrnrhr9r#rm)r=r|�sinforrr�semantic_masks�s6465z"_PanopticPrediction.semantic_masksccsZ|jD]N}|j�|�}|dks|ds(q|j|k���tj�}|��dkr||fVqdS)Nrar)	rrror�rnrhr9r#rmrU)r=r|r�rErrr�instance_masks�s66z"_PanopticPrediction.instance_masks)N)rrrrrAr�r�r�rrrrr^�s6768$r^cs�d}|dk	rB�dk	r4t��dkr4�fdd�|D�}ndd�|D�}|dk	rv|dkrbdd�|D�}ndd�t||�D�}|dk	r�|dk	r�dd�t||�D�}|S)	z�69    Args:70        classes (list[int] or None):71        scores (list[float] or None):72        class_names (list[str] or None):73        is_crowd (list[bool] or None):74 75    Returns:76        list[str] or None77    Nrcsg|]}�|�qSrr�r'�i��class_namesrrr)�sz'_create_text_labels.<locals>.<listcomp>cSsg|]}t|��qSr)�strr�rrrr)�scSsg|]}d�|d��qS)z{:.0f}%�d�r;rbrrrr)�scSs g|]\}}d�||d��qS)z78{} {:.0f}%r�r�)r'�lrcrrrr)�scSs g|]\}}||rdnd�qS)z|crowd�r)r'r�Zcrowdrrrr)�s)rOrt)�classes�scoresr��is_crowd�labelsrr�r�_create_text_labels�sr�c@s6eZdZd
dd�Zdd�Zdd�Zdd	�Zd79d�ZdS)rrcCs4||_||_|jd|jd|_|_|�|�dS)z�80        Args:81            img (ndarray): an RGB image of shape (H, W, 3) in range [0, 255].82            scale (float): scale the input image83        rrN)�img�scaler8r/r.�
_setup_figure)r=r�r�rrrrAszVisImage.__init__cCs�tjdd�}|��|_|�|j|jd|j|j|jd|j�t|�|_	|�84ddddg�}|�d�||_||_
|�|�dS)a85        Args:86            Same as in :meth:`__init__()`.87 88        Returns:89            fig (matplotlib.pyplot.figure): top level container for all the image plot elements.90            ax (matplotlib.pyplot.Axes): contains figure elements and sets the coordinate system.91        F)�frameong{�G�z�?�r�offN)�	mplfigure�Figure�get_dpi�dpi�set_size_inchesr/r�r.r�canvas�add_axes�axis�fig�ax�reset_image)r=r�r�r�rrrr�s	92�9394zVisImage._setup_figurecCs,|�d�}|jj|d|j|jdfdd�dS)z<95        Args:96            img: same as in __init__97        r*r�nearest)�extent�
interpolationN)r9r��imshowr/r.)r=r�rrrr�%s98zVisImage.reset_imagecCs|j�|�dS)z�99        Args:100            filepath (str): a string that contains the absolute path, including the file name, where101                the visualized image will be saved.102        N)r��savefig)r=�filepathrrr�save-sz
VisImage.savec	CsR|j}|��\}\}}tj|dd�}|�||d�}tj|dgdd�\}}|�d�S)z�103        Returns:104            ndarray:105                the visualized image of shape (H, W, 3) (RGB) in uint8 type.106                The shape is scaled w.r.t the input image using the given `scale` argument.107        r*r}rIrJr�r�)r�Zprint_to_bufferr#�108frombufferr%�splitr9)	r=r�rcr/r.�bufferZimg_rgba�rgb�alpharrr�	get_image5szVisImage.get_imageN)r)rrrrAr�r�r�r�rrrrrs109110c@s$eZdZdZddejfdd�Zdd�Zd@d	d111�ZdAdd�Z	e	Z112d
d�Zddddddd�dd�ZdBdd�Z
dd�Zddddd�dd�ZdCdd�ZdDd d!�ZdEd#d$�ZdFd%d&�ZdGdddd'd(�d)d*�ZdHddd+�d,d-�ZdId.d/�Zd0d1�ZdJd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<d=�Zd>d?�ZdS)Kra113    Visualizer that draws data about detection/segmentation on images.114 115    It contains methods like `draw_{text,box,circle,line,binary_mask,polygon}`116    that draw primitive objects to images, as well as high-level wrappers like117    `draw_{instance_predictions,sem_seg,panoptic_seg_predictions,dataset_dict}`118    that draw composite data in some pre-defined style.119 120    Note that the exact visualization style for the high-level wrappers are subject to change.121    Style such as color, opacity, label contents, visibility of labels, or even the visibility122    of objects themselves (e.g. when the object is too small) may change according123    to different heuristics, as long as the results still look visually reasonable.124 125    To obtain a consistent style, you can implement custom drawing functions with the126    abovementioned primitive methods instead. If you need more customized visualization127    styles, you can process the data yourself following their format documented in128    tutorials (:doc:`/tutorials/models`, :doc:`/tutorials/datasets`). This class does not129    intend to satisfy everyone's preference on drawing styles.130 131    This visualizer focuses on high rendering quality rather than performance. It is not132    designed to be used for real-time applications.133    NrcCs�t�|��dd��tj�|_|dkr.t�d�}||_t	|j|d�|_134t�d�|_
tt�|j135j|j136j�dd|�|_d	|_||_t|_dS)137ae138        Args:139            img_rgb: a numpy array of shape (H, W, C), where H and W correspond to140                the height and width of the image respectively. C is the number of141                color channels. The image is required to be in RGB format since that142                is a requirement of the Matplotlib library. The image is also expected143                to be in the range [0, 255].144            metadata (Metadata): dataset metadata (e.g. class names and colors)145            instance_mode (ColorMode): defines one of the pre-defined style for drawing146                instances on an image.147        r��NZ__nonexist__)r��cpu�Z�148�)r#r$�clipr9r*r�rr�rxr�outputrp�deviceZ149cpu_device�max�sqrtr.r/�_default_font_size�_instance_mode�_KEYPOINT_THRESHOLD�keypoint_threshold)r=Zimg_rgbrxr�Z
instance_moderrrrAds150�zVisualizer.__init__cs�|�d�r|jnd}|�d�r$|jnd}|�d�r<|j��nd}t||�j�dd��}|�d�rf|jnd}|dk�	�}||}||}t151�|�}|t152�|�}t153�|�}|t154�|�}|�d�r�t155�|j
�}|t156�|�}�fd	d157�|D�}nd}�jtjk�r&�j�d��r&�fdd158�|D�}	d
}159nd}	d
}160�jtjk�rt�j���|�d��rh|j
jdd�dk��nd��d}161�j|||||	|162d��jS)a�163        Draw instance-level prediction results on an image.164 165        Args:166            predictions (Instances): the output of an instance detection/segmentation167                model. Following fields will be used to draw:168                "pred_boxes", "pred_classes", "scores", "pred_masks" (or "pred_masks_rle").169 170        Returns:171            output (VisImage): image object with visualizations.172173pred_boxesNr��pred_classes�
thing_classes�pred_keypoints皙�����?�174pred_maskscs g|]}t|�jj�jj��qSr)rr�r.r/r&rDrrr)�sz8Visualizer.draw_instance_predictions.<locals>.<listcomp>�thing_colorscs(g|] }��dd��jj|D���qS)cSsg|]}|d�qS�r�rr&rrrr)�szCVisualizer.draw_instance_predictions.<locals>.<listcomp>.<listcomp>��_jitterrxr��r'�crDrrr)�s皙�����?r)�dimg333333�?)�masks�boxesr��	keypoints�assigned_colorsr�)�hasr�r�r�rsr�rxr�r�r�r#�arrayr$r�r�rrrr�r��_create_grayscale_image�anyrh�overlay_instances)r=�predictionsr�r�r�r�r��keepr��colorsr�rrDr�draw_instance_predictionssT175176177178�����z$Visualizer.draw_instance_predictions�ffffff�?c179s�t|tj�r|��}tj|dd�\}}t�|���}||}t�fdd�|�D]n}zdd��j	j180|D�}Wnttfk181r�d}YnX||k�
tj�}	�j	j|}182�j|	|t|183||d�qP�jS)	a�184        Draw semantic segmentation predictions/labels.185 186        Args:187            sem_seg (Tensor or ndarray): the segmentation of shape (H, W).188                Each value is the integer label of the pixel.189            area_threshold (int): segments with less than `area_threshold` are not drawn.190            alpha (float): the larger it is, the more opaque the segmentations are.191 192        Returns:193            output (VisImage): image object with visualizations.194        T)rfcs|t�jj�kSrB)rOrx�
stuff_classes)r�rDrr�<lambda>��z)Visualizer.draw_sem_seg.<locals>.<lambda>cSsg|]}|d�qSr�rr&rrrr)�sz+Visualizer.draw_sem_seg.<locals>.<listcomp>N��color�195edge_color�textr��area_threshold)r0rp�Tensorrhr#rrqrs�filterrx�stuff_colors�AttributeError�196IndexErrorr9r*r��draw_binary_mask�197_OFF_WHITEr�)r=�sem_segr�r�r�rzr{�label�198mask_color�binary_maskr�rrDr�draw_sem_seg�s*
199�zVisualizer.draw_sem_segc	spt||�j�}�jtjkr0�j���|����|�	�D]f\}}|d}zdd��jj200|D�}	Wntk201rzd}	YnX�jj|}202�j
||	t|203||d�q8t|���}t|�dkr��jStt|��\}}dd�|D�}
zdd�|D�}Wntk204�rd}YnXt|
|�jjd	d�|D��}z�fd205d�|
D�}Wntk206�rVd}YnX�j||||d��jS)a�207        Draw panoptic prediction annotations or results.208 209        Args:210            panoptic_seg (Tensor): of shape (height, width) where the values are ids for each211                segment.212            segments_info (list[dict] or None): Describe each segment in `panoptic_seg`.213                If it is a ``list[dict]``, each dict contains keys "id", "category_id".214                If None, category id of each pixel is computed by215                ``pixel // metadata.label_divisor``.216            area_threshold (int): stuff segments with less than `area_threshold` are not drawn.217 218        Returns:219            output (VisImage): image object with visualizations.220        r`cSsg|]}|d�qSr�rr&rrrr)�sz0Visualizer.draw_panoptic_seg.<locals>.<listcomp>Nr�rcSsg|]}|d�qS�r`rr&rrrr)scSsg|]}|d�qS)�scorerr&rrrr)scSsg|]}|�dd��qS��iscrowdr�r�r&rrrr)scs(g|] }��dd��jj|D���qS)cSsg|]}|d�qSr�rr&rrrr)sz;Visualizer.draw_panoptic_seg.<locals>.<listcomp>.<listcomp>r�r�rDrrr)s)r�r�r�r�)r^rxr�rrr�r�r�r�r�r�r�r�r�r�r3r�rOrt�KeyErrorr�r�r�)r=rvrwr�r��predrEr�Zcategory_idxr�r�Z
all_instancesr��category_idsr�r�r�rrDr�draw_panoptic_seg�sR221�222223�224�225226zVisualizer.draw_panoptic_segc	s�|�dd�}|r�d|dkr,dd�|D�}nd}d|dkrddd�|D�}t�|��t|�d	d227�}nd}dd�|D�}d}dd�|D�}�jtjkr��j�d
�r��fdd�|D�}�j�dd�}t	|d|dd�|D�d�}	�j228|	||||d�|�dd�}229|230dk�rBd|k�rBt�|dd��}t
�|�}231tj|232dd�}233W5QRX|234dk	�r\�j|235ddd�|�dd�}|dk�r�d|k�r�t�|dd��.}t
�|�}t�|�}ddlm}
|
|�}W5QRX|dk	�r�|d}t�|�}�j||ddd��jS)a236        Draw annotations/segmentaions in Detectron2 Dataset format.237 238        Args:239            dic (dict): annotation/segmentation data of one image, in Detectron2 Dataset format.240 241        Returns:242            output (VisImage): image object with visualizations.243        �annotationsN�segmentationrcSsg|]}|d�qS)r�rr&rrrr)1sz0Visualizer.draw_dataset_dict.<locals>.<listcomp>r�cSsg|]}|d�qS)r�rr&rrrr)5sr"rJcSs<g|]4}t|d�dkr0t�|d|dtj�n|d�qS)r\rI�	bbox_mode)rOr	�convert�XYXY_ABSr&rrrr):s��cSsg|]}|d�qSr�rr&rrrr)Bsr�cs(g|] }��dd��jj|D���qS)cSsg|]}|d�qSr�rr&rrrr)Esz;Visualizer.draw_dataset_dict.<locals>.<listcomp>.<listcomp>r�r�rDrrr)Ds�r�cSsg|]}|�dd��qSr�r�r&rrrr)Ms)r�r�r�)r�r�r�r�r�r��sem_seg_file_name�rbr*r}r�)r�r��pan_seg�pan_seg_file_name)�rgb2idrwr�)r�r#r�r%rOr�rrrxr�r�r
�openrr$r�Zpanopticapi.utilsrrp�tensorr�r�)r=�dic�annosr��keyptsr�r�r��namesr�r��frrrwrrDr�draw_dataset_dict$sf244�245���246247248249250251zVisualizer.draw_dataset_dictrN)r�r�r�r�r�r�cs�d}|dk	r|�|�}t|�}�dk	rN|����|rFt��|ksNt�nt��}|dk	r~|rlt|�|kstt�nt|�}|�|�}�dk	r�t��|ks�t��dkr�dd�t|�D��|dkr�|jS|dk	r�|jddkr�|j|��d�Sd}|dk	�r&t	j252|dd�dd�f|dd�dd�fdd	�}n�dk	�rDt	�d253d��D��}|dk	�r�t	�|��
�}	|dk	�rp||	nd}�dk	�r��fdd�|	D�nd��dk	�r��fdd�|	D�nd��fd
d�|	D��|dk	�r�||	nd}t|�D�]�}254�|255}|dk	�r|j||256|d��dk	�rB�|257jD]}|j|�dd�||d��q"�dk	�r�|dk	�rt||258\}
}}}|
|f}d}nb�dk	�r�t�|259j�dk�r��q�|260��\}
}}}t	j�|261j��dd	�ddd�}d}n�q�||||
}|t|jjk�s||d|jjk�r2||jjdk�r*||f}n|
|f}||t	�|jj|jj�}|j|dd�}t	�|ddddd�d|j}|j�|262||||d��q�|dk	�r�|D]}|�|��q�|jS)a263        Args:264            boxes (Boxes, RotatedBoxes or ndarray): either a :class:`Boxes`,265                or an Nx4 numpy array of XYXY_ABS format for the N objects in a single image,266                or a :class:`RotatedBoxes`,267                or an Nx5 numpy array of (x_center, y_center, width, height, angle_degrees) format268                for the N objects in a single image,269            labels (list[str]): the text to be displayed for each instance.270            masks (masks-like object): Supported types are:271 272                * :class:`detectron2.structures.PolygonMasks`,273                  :class:`detectron2.structures.BitMasks`.274                * list[list[ndarray]]: contains the segmentation masks for all objects in one image.275                  The first level of the list corresponds to individual instances. The second276                  level to all the polygon that compose the instance, and the third level277                  to the polygon coordinates. The third level should have the format of278                  [x0, y0, x1, y1, ..., xn, yn] (n >= 3).279                * list[ndarray]: each ndarray is a binary mask of shape (H, W).280                * list[dict]: each dict is a COCO-style RLE.281            keypoints (Keypoint or array like): an array-like object of shape (N, K, 3),282                where the N is the number of instances and K is the number of keypoints.283                The last dimension corresponds to (x, y, visibility or score).284            assigned_colors (list[matplotlib.colors]): a list of colors, where each color285                corresponds to each mask or box in the image. Refer to 'matplotlib.colors'286                for full list of formats that the colors are accepted in.287        Returns:288            output (VisImage): image object with visualizations.289        rNcSsg|]}tddd��qS�Tr�r��maximumr�r'�_rrrr)�sz0Visualizer.overlay_instances.<locals>.<listcomp>r�)r�r�r�rr�cSsg|]}|���qSr)rYr&rrrr)�scsg|]}�|�qSrr�r'�k�r�rrr)�scsg|]}�|�qSrr�r'�idx)r�rrr)�scsg|]}�|�qSrrr�r�rrr)�s)r�r")r��left�center�(r���brightness_factor�{�G�z�?�{�G�z�?�333333�?rN)r��horizontal_alignment�	font_size) �_convert_boxesrO�_convert_masksr2�_convert_keypoints�ranger�r8�overlay_rotated_instancesr#�prodr$rqrs�draw_boxrG�draw_polygonr%r\�medianrE�nonzero�_SMALL_OBJECT_AREA_THRESHr�r.r�r/�_change_color_brightnessr�r��	draw_text�draw_and_connect_keypoints)r=r�r�r�r�r�r��
num_instancesrzr{r�r��segment�x0�y0�x1�y1�text_posZhoriz_alignZ
instance_area�height_ratio�
lighter_colorr Zkeypoints_per_instancer)r�r�r�rr�is�&290291292�2934294295  296297298299300"��301����302	303zVisualizer.overlay_instancesc	s�t|�}�dkr"dd�t|�D��|dkr0|jS|dk	rX|dd�df|dd�df}t�|���}||}�dk	r��fdd�|D�nd��fdd�|D�}t|�D],}|j||||�dk	r̈|ndd	�q�|jS)304ag305        Args:306            boxes (ndarray): an Nx5 numpy array of307                (x_center, y_center, width, height, angle_degrees) format308                for the N objects in a single image.309            labels (list[str]): the text to be displayed for each instance.310            assigned_colors (list[matplotlib.colors]): a list of colors, where each color311                corresponds to each mask or box in the image. Refer to 'matplotlib.colors'312                for full list of formats that the colors are accepted in.313 314        Returns:315            output (VisImage): image object with visualizations.316        NcSsg|]}tddd��qSrrrrrrr)sz8Visualizer.overlay_rotated_instances.<locals>.<listcomp>rrrJcsg|]}�|�qSrrrrrrr)scsg|]}�|�qSrrrrrrr)s)r�r�)rOr$r�r#rqrs�draw_rotated_box_with_label)	r=r�r�r�r/rzr{r�r�r)r�r�rr%�s$ �z$Visualizer.overlay_rotated_instancescCs�i}|j�d�}t|�D]F\}}|\}}}||jkr|j||ftd�|r||}	||f||	<q|j�d�r�|jjD]\\}317}}|318|krt||krt||319\}
}||\}}tdd�|D��}|j|
|g||g|d�qtz6|d\}}|d\}}||d||d}}Wnt	k320�rYn�X|�d	d321�\}}|dk	�rR|j||g||gtd�z|d\}}|d
\}}Wnt	k322�r�Yn4X||d||d}}|j||g||gtd�|j323S)a�324        Draws keypoints of an instance and follows the rules for keypoint connections325        to draw lines between appropriate keypoints. This follows color heuristics for326        line color.327 328        Args:329            keypoints (Tensor): a tensor of shape (K, 3), where K is the number of keypoints330                and the last dimension corresponds to (x, y, probability).331 332        Returns:333            output (VisImage): image object with visualizations.334        �keypoint_names�r��keypoint_connection_rulescss|]}|dVqdS)g�o@Nrr&rrr�	<genexpr>;sz8Visualizer.draw_and_connect_keypoints.<locals>.<genexpr>�
left_shoulder�right_shoulderr�nose)NNN�left_hip�	right_hip)rxr��	enumerater��draw_circle�_REDr;�tuple�	draw_liner�r�)r=r��visibler9rZkeypointr(�y�probZ
keypoint_nameZkp0Zkp1r�r1r2r3r4Zls_xZls_yZrs_xZrs_yZmid_shoulder_xZmid_shoulder_yZnose_xZnose_yZlh_xZlh_yZrh_xZrh_yZ	mid_hip_xZ	mid_hip_yrrrr.sB
335336337z%Visualizer.draw_and_connect_keypoints�grr)r r�r�rotationc	
Cs~|s338|j}t�tt�|��d�}tdt�|��|t�|�<|\}}|jj	j339|||||jjdddddd�d||d	|d340�|jS)aw341        Args:342            text (str): class label343            position (tuple): a tuple of the x and y coordinates to place text on image.344            font_size (int, optional): font of the text. If not provided, a font size345                proportional to the image width is calculated and used.346            color: color of the text. Refer to `matplotlib.colors` for full list347                of formats that are accepted.348            horizontal_alignment (str): see `matplotlib.text.Text`349            rotation: rotation angle in degrees CCW350 351        Returns:352            output (VisImage): image object with text drawn.353        g�������?r�z354sans-serif�blackr��none)�	facecolorr��pad�	edgecolor�topr�)r!�familyr\�verticalalignment�horizontalalignmentr��zorderrK)r�r#r
r3�mplc�to_rgbr��argmaxr�r�r�r�)	r=r��positionr r�rrKr(rHrrrr-\s&355�
zVisualizer.draw_text�-cCsd|\}}}}||}	||}356t|jdd�}|jj�tjj||f|	|357d|||jj||d��|jS)a�358        Args:359            box_coord (tuple): a tuple containing x0, y0, x1, y1 coordinates, where x0 and y0360                are the coordinates of the image's top left corner. x1 and y1 are the361                coordinates of the image's bottom right corner.362            alpha (float): blending efficient. Smaller values lead to more transparent masks.363            edge_color: color of the outline of the box. Refer to `matplotlib.colors`364                for full list of formats that are accepted.365            line_style (string): the string to use to create the outline of the boxes.366 367        Returns:368            output (VisImage): image object with box drawn.369        rIrF)�fillrP�	linewidthr��	linestyle)	r�r�r�r��	add_patch�mpl�patches�	Rectangler�)r=Z	box_coordr�r��370line_styler1r2r3r4r/r.r\rrrr'�s"371��zVisualizer.draw_boxcs�|\��}}}||}	|j|	t|jjkr.dnd}372|tjd}t�|��t�|��|d|df|d|df|d|df|d|dfg}����fdd�|D�}
td�D]Z}|dd}|j	|
|d	|
|d	g|
|d|
|dg||dk�r373d374n||375d�q�|dk	�r�|
d}|t376�|jj|jj
�}|j|d
d�}t377�|ddddd�d|j}|j|||||d�|jS)a�378        Draw a rotated box with label on its top-left corner.379 380        Args:381            rotated_box (tuple): a tuple containing (cnt_x, cnt_y, w, h, angle),382                where cnt_x and cnt_y are the center coordinates of the box.383                w and h are the width and height of the box. angle represents how384                many degrees the box is rotated CCW with regard to the 0-degree box.385            alpha (float): blending efficient. Smaller values lead to more transparent masks.386            edge_color: color of the outline of the box. Refer to `matplotlib.colors`387                for full list of formats that are accepted.388            line_style (string): the string to use to create the outline of the boxes.389            label (string): label for rotated box. It will not be rendered when set to None.390 391        Returns:392            output (VisImage): image object with box drawn.393        rMrJg�f@rcs8g|]0\}}�|�|��|�|�f�qSrr)r'�xx�yy�r��cnt_x�cnt_yrcrrr)�sz:Visualizer.draw_rotated_box_with_label.<locals>.<listcomp>rIrrz--)r�r]r\Nr�rrrrrN)r�r rK)r�r+r�r��math�pi�cos�sinr$rFr#r�r.r/r,r�r-)r=Zrotated_boxr�r�rbr�r@r?�anglerYr\�theta�rectZrotated_rectr�jr5r6Zlabel_colorr rrerr8�s6�394395D�396"�z&Visualizer.draw_rotated_box_with_labelrJcCs,|\}}|jj�tjj||d|d��|jS)a�397        Args:398            circle_coord (list(int) or tuple(int)): contains the x and y coordinates399                of the center of the circle.400            color: color of the polygon. Refer to `matplotlib.colors` for a full list of401                formats that are accepted.402            radius (int): radius of the circle.403 404        Returns:405            output (VisImage): image object with box drawn.406        T)�radiusr[r�)r�r�r^r_r`�Circle)r=Zcircle_coordr�rpr(rHrrrrC�s407�zVisualizer.draw_circlec	CsJ|dkr|jd}t|d�}|jj�tjj||||jj||d��|jS)a/408        Args:409            x_data (list[int]): a list containing x values of all the points being drawn.410                Length of list should match the length of y_data.411            y_data (list[int]): a list containing y values of all the points being drawn.412                Length of list should match the length of x_data.413            color: color of the line. Refer to `matplotlib.colors` for a full list of414                formats that are accepted.415            linestyle: style of the line. Refer to `matplotlib.lines.Line2D`416                for a full list of formats that are accepted.417            linewidth (float or None): width of the line. When it's None,418                a default value will be computed and used.419 420        Returns:421            output (VisImage): image object with line drawn.422        NrJr)r\r�r])	r�r�r�r��add_liner_�lines�Line2Dr�)r=�x_dataZy_datar�r]r\rrrrF�s423424425��	zVisualizer.draw_liner�)r�r�r�r�c	Cs`|dkrtddd�}t�|�}d}|�d�}t||jj|jj�}|jd|jdf}	|j	s�|j426D]R}427t�t�
|428g|	d|	d��}||p�dkr�q`d}|429�dd	�}430|j|431|||d432�q`nxtj|	ddd
�}||dd�dd�dd�f<|jdk�d�||dd�dd�df<d}|jjj|d|jj|jjdfd�|dk	�rZ|�rZ|j|dd�}
|�|||
�|jS)an433        Args:434            binary_mask (ndarray): numpy array of shape (H, W), where H is the image height and435                W is the image width. Each value in the array is either a 0 or 1 value of uint8436                type.437            color: color of the mask. Refer to `matplotlib.colors` for a full list of438                formats that are accepted. If None, will pick a random color.439            edge_color: color of the polygon edges. Refer to `matplotlib.colors` for a440                full list of formats that are accepted.441            text (str): if None, will be drawn on the object442            alpha (float): blending efficient. Smaller values lead to more transparent masks.443            area_threshold (float): a connected component smaller than this area will not be shown.444 445        Returns:446            output (VisImage): image object with mask drawn.447        NTrrFr*rr"r)r�r�r��rI�float32r}rJ�r�r�r)rrVrWr9rr�r.r/r8rHrGr4rYr5r%r(r#rrEr�r�r,�_draw_text_in_mask)r=r�r�r�r�r�r�Zhas_valid_segmentrE�shape2dr0rY�rgbar7rrrr�s0448449450&"zVisualizer.draw_binary_mask)r�r�c	Cs�|dkrtddd�}t�|�}|jd|jdf}tj|ddd�}||dd�dd�dd	�f<|||dd�dd�d	f<|jjj|d|jj	|jj451dfd452�|dk	r�|j|dd�}|d
k�d�}|�
|||�|jS)a�453        Args:454            soft_mask (ndarray): float array of shape (H, W), each value in [0, 1].455            color: color of the mask. Refer to `matplotlib.colors` for a full list of456                formats that are accepted. If None, will pick a random color.457            text (str): if None, will be drawn on the object458            alpha (float): blending efficient. Smaller values lead to more transparent masks.459 460        Returns:461            output (VisImage): image object with mask drawn.462        NTrrrrvrwr}rJrxr�rrNr*)rrVrWr8r#rr�r�r�r/r.r,r9ry)	r=Z	soft_maskr�r�r�rzr{r7r�rrr�draw_soft_maskHs463"zVisualizer.draw_soft_maskcCs||dkr$|dkr |j|dd�}n|}t�|�d}tjj|dt�|�|f|t|jd|jj	d�d	�}|jj464�|�|jS)465a�466        Args:467            segment: numpy array of shape Nx2, containing all the points in the polygon.468            color: color of the polygon. Refer to `matplotlib.colors` for a full list of469                formats that are accepted.470            edge_color: color of the polygon edges. Refer to `matplotlib.colors` for a471                full list of formats that are accepted. If not provided, a darker shade472                of the polygon color will be used instead.473            alpha (float): blending efficient. Smaller values lead to more transparent masks.474 475        Returns:476            output (VisImage): image object with polygon drawn.477        Nr�gffffff�r)rT�r)r[rNrPr\)r,rVrWr_r`�Polygonr�r�r�r�r�r^)r=r0r�r�r��polygonrrrr(ds�zVisualizer.draw_polygoncCsDt�|�}tj�d�}|tj�|�d}t�||dd�}t|�S)a�478        Randomly modifies given color to produce a slightly different color than the color given.479 480        Args:481            color (tuple[double]): a tuple of 3 elements, containing the RGB values of the color482                picked. The values in the list are in the [0.0, 1.0] range.483 484        Returns:485            jittered_color (tuple[double]): a tuple of 3 elements, containing the RGB values of the486                color after being jittered. The values in the list are in the [0.0, 1.0] range.487        rJrNrr)	rVrWr#�random�rand�linalg�normr�rE)r=r��vecrVrrrr��s488489zVisualizer._jittercCsB|j�d�jdd�}tj|gddd�}|dk	r>|j|||<|S)z~490        Create a grayscale version of the original image.491        The colors in masked area, if given, will be kept.492        �f4rr�rJN)r�r9�meanr#�stack)r=rEZimg_bwrrrr��s493z"Visualizer._create_grayscale_imagecCs||dkr|dkst�t�|�}tjt�|��}|d||d}|dkrNdn|}|dkr^dn|}t�|d||d�}|S)a494        Depending on the brightness_factor, gives a lighter or darker color i.e. a color with495        less or more saturation than the original color.496 497        Args:498            color: color of the polygon. Refer to `matplotlib.colors` for a full list of499                formats that are accepted.500            brightness_factor (float): a value in [-1.0, 1.0] range. A lightness factor of501                0 will correspond to no change, a factor in [-1.0, 0) range will result in502                a darker color and a factor in (0, 1.0] range will result in a lighter color.503 504        Returns:505            modified_color (tuple[double]): a tuple containing the RGB values of the506                modified color. Each value in the tuple is in the [0.0, 1.0] range.507        g�rrr�rr)r2rVrW�colorsys�508rgb_to_hls�509hls_to_rgb)r=r�rZ
polygon_colorZmodified_lightnessZmodified_colorrrrr,�s510z#Visualizer._change_color_brightnesscCs0t|t�st|t�r"|j����St�|�SdS)zk511        Convert different format of boxes to an NxB array, where B = 4 or 5 is the box dimension.512        N)r0rrr�detachrhr#r$)r=r�rrrr!�szVisualizer._convert_boxescCs~|}t|t�r|j}t|t�r(|j��}t|tj�r<|��}g}|D]4}t|t�r^|�	|�qD|�	t||j513j|j514j��qD|S)z�515        Convert different format of masks or polygons to a tuple of masks and polygons.516 517        Returns:518            list[GenericMask]:519        )
r0rrGrrrhrpr�rrkr�r.r/)r=Zmasks_or_polygonsr>�retr(rrrr"�s520521522523zVisualizer._convert_maskscCs�t�|d�\}}}}|dd�dfjdkr.dSt�|dd�df�d}td|�D]L}	|	|ksn||	dftkrRtj||	k��dd�ddd�}524|j	||525|d�qRdS)zF526        Find proper places to draw text given a binary mask.527        �rNr"rr�r:)528rQ�connectedComponentsWithStatsr!r#rXr$�_LARGE_MASK_AREA_THRESHr)r*r-)r=r�r�r�Z_num_ccZ	cc_labels�stats�	centroidsZlargest_component_id�cidrrrrry�s zVisualizer._draw_text_in_maskcCst|t�r|j}t�|�}|SrB)r0r529rr#r$)r=r�rrrr#�s530531zVisualizer._convert_keypointscCs|jS)z�532        Returns:533            output (VisImage): the image output containing the visualizations added534            to the image.535        )r�rDrrr�536get_output�szVisualizer.get_output)Nr�)Nr�)NNN)rNrJrZ)rNrJrZN)rJ)rZN)N)N)NrN)N)rrrrrrrAr�r�r�Zdraw_panoptic_seg_predictionsr537r�r%r.r-r'r8rCrFr�r|r(r�r�r,r!r"ryr#r�rrrrrJs^?538$539@H�540&D�/541#�5427543544 ��3545$546	)N)6r��loggingrhrhr#�enumrrrQ�547matplotlibr_�matplotlib.colorsr�rV�matplotlib.figure�figurer��pycocotools.maskrEr4rpZmatplotlib.backends.backend_aggr�PILr�detectron2.datar�detectron2.structuresrrr	r548rr�detectron2.utils.file_ior
�detectron2.utils.colormapr�	getLoggerr�logger�__all__r+r�r�Z_BLACKrDr�rrr^r�rrrrrr�<module>s> 549550`K551J