o
    ߥiC                     @   s>   d dl Zd dlZd dlmZ dddZ					d	ddZdS )
    N)bbox2resultP   c                 C   s   t | }g }g }|D ]}|dks||k rq|| || |k qt|dkr4t |}t |}n| jdgd}| jd| jd | jd gd}| |	 fS )a  Convert semantic segmentation mask to binary masks

    Args:
        gt_sem_seg (torch.Tensor): Semantic masks to be converted.
            [0, num_thing_classes-1] is the classes of things,
            [num_thing_classes:] is the classes of stuff.
        num_thing_classes (int, optional): Number of thing classes.
            Defaults to 80.

    Returns:
        tuple[torch.Tensor]: (mask_labels, bin_masks).
            Mask labels and binary masks of stuff classes.
       r   )size)
torchuniqueappendlenstackcat	new_zerosshapelongfloat)
gt_sem_segnum_thing_classesclassesmaskslabelsi r   j/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/video_instance_segmentation/utils.pysem2ins_masks   s    


r   c                    sT  dusJ |dusJ t  }durdk}| |  durldurb |   jd dkr<dd t|D }n,t tjrT         fddt|D }nt |}||d< |durdurx|| }t|tjr|	   }dd t|D }	t jd D ]}
|	|
  
||
  q|	|d	< |S )
a@  Convert tracking/detection results to a list of numpy arrays.
    Args:
        bboxes (torch.Tensor | np.ndarray): shape (n, 5)
        labels (torch.Tensor | np.ndarray): shape (n, )
        masks (torch.Tensor | np.ndarray): shape (n, h, w)
        ids (torch.Tensor | np.ndarray): shape (n, )
        num_classes (int): class number, not including background class
    Returns:
        dict[str : list(ndarray) | list[list[np.ndarray]]]: tracking/detection
        results of each class. It may contain keys as belows:
        - bbox_results (list[np.ndarray]): Each list denotes bboxes of one
            category.
        - mask_results (list[list[np.ndarray]]): Each outer list denotes masks
            of one category. Each inner list denotes one mask belonging to
            the category. Each mask has shape (h, w).
    Nr   r   c                 S   s   g | ]
}t jd t jdqS ))r      )dtype)npzerosfloat32.0r   r   r   r   
<listcomp>T   s    z outs2results.<locals>.<listcomp>c                    s:   g | ]}t j|kd f  |kd d f fddqS )N   )axis)r   concatenater    bboxesidsr   r   r   r"   ]   s    "bbox_resultsc                 S   s   g | ]}g qS r   r   )r!   _r   r   r   r"   k   s    mask_results)dictr   range
isinstancer   Tensorcpunumpyr   detachr
   )r'   r   r   r(   num_classeskwargsresults
valid_indsr)   masks_resultsr   r   r&   r   outs2results0   sB   r8   )r   )NNNNN)r1   r   r   
mmdet.corer   r   r8   r   r   r   r   <module>   s   
(