o
    _{jÈò  ã                   @   s¢  d dl Z d dlZd dlmZ d dlZd dlZd dlZd dlZ	d dl
Z
d dlmZ d dlmZ ddlmZmZmZ ddlmZ d dlZe ¡ G dd	„ d	ƒƒZe ¡ G d
d„ dƒƒZe ¡ G dd„ dƒƒZe ¡ G dd„ deƒƒZe ¡ G dd„ deƒƒZe ¡ G dd„ deƒƒZe ¡ G dd„ dƒƒZe ¡ G dd„ dƒƒZe ¡ G dd„ deƒƒZe ¡ G dd„ dƒƒZ e ¡ G dd„ dƒƒZ!e ¡ G dd„ dƒƒZ"e ¡ G d d!„ d!ƒƒZ#e ¡ G d"d#„ d#ƒƒZ$e ¡ G d$d%„ d%ƒƒZ%e ¡ G d&d'„ d'ƒƒZ&e ¡ G d(d)„ d)ƒƒZ'e ¡ G d*d+„ d+ƒƒZ(e ¡ G d,d-„ d-ƒƒZ)e ¡ G d.d/„ d/e$ƒƒZ*e ¡ G d0d1„ d1ƒƒZ+e ¡ G d2d3„ d3ƒƒZ,e ¡ G d4d5„ d5ƒƒZ-e ¡ G d6d7„ d7ƒƒZ.dS )8é    N)Ú
FileClient)Ú_pairé   )Úget_random_stringÚget_shm_dirÚget_thread_idé   )Ú	PIPELINESc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚLoadHVULabelzÁConvert the HVU label from dictionaries to torch tensors.

    Required keys are "label", "categories", "category_nums", added or modified
    keys are "label", "mask" and "category_mask".
    c                 K   s   d| _ || _d S )NF)Úhvu_initializedÚkwargs)Úselfr   © r   ú9/root/disk_3tb/HAC/mmaction/datasets/pipelines/loading.pyÚ__init__   s   
zLoadHVULabel.__init__c                 C   sœ   t |ƒt |ƒks
J ‚|| _|| _t | jƒ| _t| jƒ| _tt||ƒƒ| _dg| _	t
| jd ƒD ]}| j	 | j	d | j|  ¡ q/tt|| j	ƒƒ| _d| _d S )Nr   é   éÿÿÿÿT)ÚlenÚ
categoriesÚcategory_numsÚnum_categoriesÚsumÚnum_tagsÚdictÚzipÚcategory2numÚ	start_idxÚrangeÚappendÚcategory2startidxr   )r   r   r   Úir   r   r   Úinit_hvu_info   s   
zLoadHVULabel.init_hvu_infoc                    s¾   | j s|  |d |d ¡ t | j¡}t | j¡}t | j¡}|d  ¡ D ]+\}}d|| j |¡< | j	| ‰ | j
| }‡ fdd„|D ƒ}d||< d|ˆ |ˆ  …< q%||d< ||d< ||d< |S )	zëConvert the label dictionary to 3 tensors: "label", "mask" and
        "category_mask".

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        r   r   Úlabelç      ð?c                    s   g | ]}|ˆ  ‘qS r   r   ©Ú.0Úidx©r   r   r   Ú
<listcomp>>   ó    z)LoadHVULabel.__call__.<locals>.<listcomp>ÚmaskÚcategory_mask)r   r!   ÚtorchÚzerosr   r   Úitemsr   Úindexr   r   )r   ÚresultsÚonehotZonehot_maskr+   ÚcategoryÚtagsZcategory_numr   r'   r   Ú__call__*   s    	

zLoadHVULabel.__call__c                 C   ó   | j j› d| j› d�}|S )Nz(hvu_initialized=ú))Ú	__class__Ú__name__r   ©r   Úrepr_strr   r   r   Ú__repr__G   ó   
ÿzLoadHVULabel.__repr__N)r8   Ú
__module__Ú__qualname__Ú__doc__r   r!   r4   r;   r   r   r   r   r
      s    r
   c                   @   sP   e Zd ZdZ							ddd„Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Z	dS )ÚSampleFramesaß  Sample frames from the video.

    Required keys are "filename", "total_frames", "start_index" , added or
    modified keys are "frame_inds", "frame_interval" and "num_clips".

    Args:
        clip_len (int): Frames of each sampled output clip.
        frame_interval (int): Temporal interval of adjacent sampled frames.
            Default: 1.
        num_clips (int): Number of clips to be sampled. Default: 1.
        temporal_jitter (bool): Whether to apply temporal jittering.
            Default: False.
        twice_sample (bool): Whether to use twice sample when testing.
            If set to True, it will sample frames with and without fixed shift,
            which is commonly used for testing in TSM model. Default: False.
        out_of_bound_opt (str): The way to deal with out of bounds frame
            indexes. Available options are 'loop', 'repeat_last'.
            Default: 'loop'.
        test_mode (bool): Store True when building test or validation dataset.
            Default: False.
        start_index (None): This argument is deprecated and moved to dataset
            class (``BaseDataset``, ``VideoDatset``, ``RawframeDataset``, etc),
            see this: https://github.com/open-mmlab/mmaction2/pull/89.
    r   FÚloopNc	           	      C   sR   || _ || _|| _|| _|| _|| _|| _| jdv sJ ‚|d ur't d¡ d S d S )N)rA   Úrepeat_lastú’No longer support "start_index" in "SampleFrames", it should be set in dataset class, see this pr: https://github.com/open-mmlab/mmaction2/pull/89)	Úclip_lenÚframe_intervalÚ	num_clipsÚtemporal_jitterÚtwice_sampleÚout_of_bound_optÚ	test_modeÚwarningsÚwarn)	r   rD   rE   rF   rG   rH   rI   rJ   Ústart_indexr   r   r   r   h   s   
ÿzSampleFrames.__init__c                 C   sÐ   | j | j }|| d | j }|dkr(t | j¡| }|tjj|| jd� }|S |t| j|ƒkrBt tjj|| d | jd�¡}|S |dkr\|| d | j }t 	t | j¡| ¡}|S tj
| jftjd�}|S )aÙ  Get clip offsets in train mode.

        It will calculate the average interval for selected frames,
        and randomly shift them within offsets between [0, avg_interval].
        If the total number of frames is smaller than clips num or origin
        frames length, it will return all zero indices.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   ©Úsizer#   ©Údtype)rD   rE   rF   ÚnpÚarangeÚrandomÚrandintÚmaxÚsortÚaroundr-   Úint)r   Ú
num_framesÚori_clip_lenÚavg_intervalÚbase_offsetsÚclip_offsetsÚratior   r   r   Ú_get_train_clips€   s*   ÿöÿÿ	úþzSampleFrames._get_train_clipsc                 C   s‚   | j | j }|| d t| jƒ }||d kr5t | j¡| }||d   tj¡}| jr3t 	||g¡}|S tj
| jftjd�}|S )aá  Get clip offsets in test mode.

        Calculate the average interval for selected frames, and shift them
        fixedly by avg_interval/2. If set twice_sample True, it will sample
        frames together without fixed shift. If the total number of frames is
        not enough, it will return all zero indices.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in test mode.
        r   ç       @rP   )rD   rE   ÚfloatrF   rR   rS   ÚastyperY   rH   Úconcatenater-   )r   rZ   r[   r\   r]   r^   r   r   r   Ú_get_test_clips¡   s   ÿzSampleFrames._get_test_clipsc                 C   s"   | j r
|  |¡}|S |  |¡}|S )zÍChoose clip offsets for the video in a given mode.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices.
        )rJ   re   r`   )r   rZ   r^   r   r   r   Ú_sample_clipsº   s
   	

þzSampleFrames._sample_clipsc                 C   s,  |d }|   |¡}|dd…df t | j¡ddd…f | j  }t |¡}| jr7tjj| jt	|ƒd�}||7 }| 
d| jf¡}| jdkrKt ||¡}n'| jdkrn||k }d| }tj|| dd�}|| |j| j }	|	}ntd	ƒ‚|d
 }
t |¡|
 }| tj¡|d< | j|d< | j|d< | j|d< |S )úµPerform the SampleFrames loading.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        Útotal_framesNrN   r   rA   rB   r   )ÚaxiszIllegal out_of_bound option.rM   Ú
frame_indsrD   rE   rF   )rf   rR   rS   rD   rE   rd   rG   rT   rU   r   ÚreshaperI   ÚmodrV   ÚTÚ
ValueErrorrc   rY   rF   )r   r0   rh   r^   rj   Úperframe_offsetsZ	safe_indsZunsafe_indsÚlast_indZnew_indsrM   r   r   r   r4   Ê   s@   
ÿ
ÿÿ

ÿ




zSampleFrames.__call__c                 C   sJ   | j j› d| j› d| j› d| j› d| j› d| j› d| j› d| j› d�}|S )	Nú
(clip_len=ú, frame_interval=ú, num_clips=ú, temporal_jitter=z, twice_sample=ú, out_of_bound_opt=ú, test_mode=r6   )	r7   r8   rD   rE   rF   rG   rH   rI   rJ   r9   r   r   r   r;   ñ   s    
ÿþýüûúùzSampleFrames.__repr__)r   r   FFrA   FN)
r8   r=   r>   r?   r   r`   re   rf   r4   r;   r   r   r   r   r@   M   s    
ø!'r@   c                   @   s*   e Zd ZdZddd„Zdd„ Zd	d
„ ZdS )ÚUntrimmedSampleFramesaC  Sample frames from the untrimmed video.

    Required keys are "filename", "total_frames", added or modified keys are
    "frame_inds", "frame_interval" and "num_clips".

    Args:
        clip_len (int): The length of sampled clips. Default: 1.
        frame_interval (int): Temporal interval of adjacent sampled frames.
            Default: 16.
        start_index (None): This argument is deprecated and moved to dataset
            class (``BaseDataset``, ``VideoDatset``, ``RawframeDataset``, etc),
            see this: https://github.com/open-mmlab/mmaction2/pull/89.
    r   é   Nc                 C   s&   || _ || _|d urt d¡ d S d S )NrC   )rD   rE   rK   rL   )r   rD   rE   rM   r   r   r   r     s
   ÿzUntrimmedSampleFrames.__init__c                 C   s¼   |d }|d }t  | jd || j¡}|jd }|dd…df t  | jd  | j| jd  ¡ddd…f  }t  |d|d ¡}t  |¡| }| t j¡|d< | j|d< | j|d	< ||d
< |S )rg   rh   rM   r   r   Nr   rj   rD   rE   rF   )	rR   rS   rE   ÚshaperD   Úcliprd   rc   rY   )r   r0   rh   rM   Zclip_centersrF   rj   r   r   r   r4     s(   ÿ
ÿÿ
þ

zUntrimmedSampleFrames.__call__c                 C   ó"   | j j› d| j› d| j› d�}|S )Nrq   rr   r6   )r7   r8   rD   rE   r9   r   r   r   r;   1  ó   
ÿþzUntrimmedSampleFrames.__repr__)r   rx   N©r8   r=   r>   r?   r   r4   r;   r   r   r   r   rw   ý   s
    

rw   c                       sH   e Zd ZdZ							d‡ fdd„	Zd	d
„ Zdd„ Zdd„ Z‡  ZS )ÚDenseSampleFramesa¹  Select frames from the video by dense sample strategy.

    Required keys are "filename", added or modified keys are "total_frames",
    "frame_inds", "frame_interval" and "num_clips".

    Args:
        clip_len (int): Frames of each sampled output clip.
        frame_interval (int): Temporal interval of adjacent sampled frames.
            Default: 1.
        num_clips (int): Number of clips to be sampled. Default: 1.
        sample_range (int): Total sample range for dense sample.
            Default: 64.
        num_sample_positions (int): Number of sample start positions, Which is
            only used in test mode. Default: 10. That is to say, by default,
            there are at least 10 clips for one input sample in test mode.
        temporal_jitter (bool): Whether to apply temporal jittering.
            Default: False.
        test_mode (bool): Store True when building test or validation dataset.
            Default: False.
    r   é@   é
   FrA   c	           	         s(   t ƒ j||||||d� || _|| _d S )N)rI   rJ   )Úsuperr   Úsample_rangeÚnum_sample_positions)	r   rD   rE   rF   r‚   rƒ   rG   rI   rJ   ©r7   r   r   r   O  s   	ú
zDenseSampleFrames.__init__c                 C   s^   t dd| | j ƒ}| j| j }|dkrdntj d|d ¡}t | j¡| }|| | }|S )aÊ  Get clip offsets by dense sample strategy in train mode.

        It will calculate a sample position and sample interval and set
        start index 0 when sample_pos == 1 or randomly choose from
        [0, sample_pos - 1]. Then it will shift the start index by each
        base offset.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   )rV   r‚   rF   rR   rT   rU   rS   )r   rZ   Úsample_positionÚintervalr   r]   r^   r   r   r   r`   b  s   ÿz"DenseSampleFrames._get_train_clipsc                 C   sx   t dd| | j ƒ}| j| j }tjd|d | jtd�}t | j¡| }tƒ }|D ]}| 	|| | ¡ q)t 
|¡}|S )aÍ  Get clip offsets by dense sample strategy in test mode.

        It will calculate a sample position and sample interval and evenly
        sample several start indexes as start positions between
        [0, sample_position-1]. Then it will shift each start index by the
        base offsets.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   )ÚnumrQ   )rV   r‚   rF   rR   Úlinspacerƒ   rY   rS   ÚlistÚextendÚarray)r   rZ   r…   r†   Ú
start_listr]   r^   r   r   r   r   re   x  s   ÿ
z!DenseSampleFrames._get_test_clipsc                 C   óR   | j j› d| j› d| j› d| j› d| j› d| j› d| j› d| j› d| j	› d	�}|S )
Nrq   rr   rs   z, sample_range=z, num_sample_positions=rt   ru   rv   r6   )
r7   r8   rD   rE   rF   r‚   rƒ   rG   rI   rJ   r9   r   r   r   r;   ‘  ó$   
ÿþýüûúùø	zDenseSampleFrames.__repr__)r   r   r   r€   FrA   F)	r8   r=   r>   r?   r   r`   re   r;   Ú__classcell__r   r   r„   r   r~   8  s    ør~   c                       s6   e Zd Zd‡ fdd„	Zdd„ Zdd„ Zd	d
„ Z‡  ZS )ÚSampleAVAFramesr   Fc                    s   t ƒ j|||d� d S )N)rJ   )r�   r   )r   rD   rE   rJ   r„   r   r   r   ¡  s   zSampleAVAFrames.__init__c                 C   sd   || j d | j  }|| j d d | j  }tt||| jƒƒ}|| }t ||d |d d ¡}|S )Nr   r   r   )rD   rE   r‰   r   rR   rz   )r   Úcenter_indexÚskip_offsetsÚ	shot_infoÚstartÚendrj   r   r   r   Ú
_get_clips¥  s   zSampleAVAFrames._get_clipsc           	      C   s°   |d }|d }|d }|d }|||  d }t jj| j d | jd d | jd�}|  |||¡}t j|t jd�|d	< | j|d
< | j|d< d|d< t jg d¢t jd�|d< |S )NÚfpsÚ	timestampÚtimestamp_startr“   r   r   rN   rP   rj   rD   rE   rF   )r   r   r   r   Úcrop_quadruple)	rR   rT   rU   rE   rD   r–   r‹   rY   Úfloat32)	r   r0   r—   r˜   r™   r“   r‘   r’   rj   r   r   r   r4   ®  s    þ

zSampleAVAFrames.__call__c                 C   ó*   | j j› d| j› d| j› d| j› d�}|S )Nrq   rr   rv   r6   )r7   r8   rD   rE   rJ   r9   r   r   r   r;   Â  ó   
ÿþýzSampleAVAFrames.__repr__)r   F)r8   r=   r>   r   r–   r4   r;   r�   r   r   r„   r   r�   ž  s
    	r�   c                       sr   e Zd ZdZ				d‡ fdd„	Zedd	„ ƒZed
d„ ƒZdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Z‡  ZS )ÚSampleProposalFramesaÓ  Sample frames from proposals in the video.

    Required keys are "total_frames" and "out_proposals", added or
    modified keys are "frame_inds", "frame_interval", "num_clips",
    'clip_len' and 'num_proposals'.

    Args:
        clip_len (int): Frames of each sampled output clip.
        body_segments (int): Number of segments in course period.
        aug_segments (list[int]): Number of segments in starting and
            ending period.
        aug_ratio (int | float | tuple[int | float]): The ratio
            of the length of augmentation to that of the proposal.
        frame_interval (int): Temporal interval of adjacent sampled frames.
            Default: 1.
        test_interval (int): Temporal interval of adjacent sampled frames
            in test mode. Default: 6.
        temporal_jitter (bool): Whether to apply temporal jittering.
            Default: False.
        mode (str): Choose 'train', 'val' or 'test' mode.
            Default: 'train'.
    r   é   FÚtrainc	           	         sz   t ƒ j|||d� || _|| _t|ƒ| _t | jtt	f¡s&t
dt|ƒ› �ƒ‚t| jƒdks/J ‚|dv s5J ‚|| _|| _d S )N)rE   rG   zAaug_ratio should be int, floator tuple of int and float, but got r   )r    ÚvalÚtest)r�   r   Úbody_segmentsÚaug_segmentsr   Ú	aug_ratioÚmmcvÚis_tuple_ofrY   rb   Ú	TypeErrorÚtyper   ÚmodeÚtest_interval)	r   rD   r£   r¤   r¥   rE   r«   rG   rª   r„   r   r   r   ã  s    	ý
þ
zSampleProposalFrames.__init__c                 C   sP   | d | }|dkrt  |¡| }|t jj||d� }|S t j|ft jd�}|S )a<  Get indices of different stages of proposals in train mode.

        It will calculate the average interval for each segment,
        and randomly shift them within offsets between [0, average_duration].
        If the total number of frames is smaller than num segments, it will
        return all zero indices.

        Args:
            valid_length (int): The length of the starting point's
                valid interval.
            num_segments (int): Total number of segments.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   rN   rP   )rR   rS   rT   rU   r-   rY   ©Úvalid_lengthZnum_segmentsr\   r]   Úoffsetsr   r   r   Ú_get_train_indicesü  s   ÿþz'SampleProposalFrames._get_train_indicesc                 C   sP   | |kr| t |ƒ }t |¡| }||d   tj¡}|S tj|ftjd�}|S )aþ  Get indices of different stages of proposals in validation mode.

        It will calculate the average interval for each segment.
        If the total number of valid length is smaller than num segments,
        it will return all zero indices.

        Args:
            valid_length (int): The length of the starting point's
                valid interval.
            num_segments (int): Total number of segments.

        Returns:
            np.ndarray: Sampled frame indices in validation mode.
        ra   rP   )rb   rR   rS   rc   rY   r-   r¬   r   r   r   Ú_get_val_indices  s   þz%SampleProposalFrames._get_val_indicesc                 C   s:  |j }|j}| j| j }|| }|dksJ ‚|| }td|t|| jd  ƒ ƒ}t|| d |d t|| jd  ƒ ƒ}	|| | }
|	| d | }| jdkri|  	|
| j
d ¡}|  	|| j¡}|  	|| j
d ¡}n| jdkr‡|  |
| j
d ¡}|  || j¡}|  || j
d ¡}||7 }||7 }||7 }t |||f¡}|S )au  Get clip offsets in train mode.

        It will calculate sampled frame indices in the proposal's three
        stages: starting, course and ending stage.

        Args:
            proposal (obj): The proposal object.
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   r    r¡   )Ústart_frameÚ	end_framerD   rE   rV   rY   r¥   Úminrª   r¯   r¤   r£   r°   rR   rd   )r   ÚproposalrZ   r±   r²   r[   Údurationr­   Zvalid_startingÚvalid_endingZvalid_starting_lengthZvalid_ending_lengthZstarting_offsetsZcourse_offsetsZending_offsetsr®   r   r   r   Ú_get_proposal_clips0  sR   ÿÿ
ÿÿÿ
ÿÿÿÿz(SampleProposalFrames._get_proposal_clipsc                 C   s4   g }|D ]}|   |d d |¡}t ||g¡}q|S )a\  Get clip offsets in train mode.

        It will calculate sampled frame indices of each proposal, and then
        assemble them.

        Args:
            num_frames (int): Total number of frame in the video.
            proposals (list): Proposals fetched.

        Returns:
            np.ndarray: Sampled frame indices in train mode.
        r   r   )r·   rR   rd   )r   rZ   Ú	proposalsr^   r´   Zproposal_clip_offsetsr   r   r   r`   d  s   ÿÿz%SampleProposalFrames._get_train_clipsc                 C   s&   | j | j }tjd|| | jtjd�S )a  Get clip offsets in test mode.

        It will calculate sampled frame indices based on test interval.

        Args:
            num_frames (int): Total number of frame in the video.

        Returns:
            np.ndarray: Sampled frame indices in test mode.
        r   rP   )rD   rE   rR   rS   r«   rY   )r   rZ   r[   r   r   r   re   z  s   ÿz$SampleProposalFrames._get_test_clipsc                 C   s4   | j dkr|  |¡}|S |dusJ ‚|  ||¡}|S )a5  Choose clip offsets for the video in a given mode.

        Args:
            num_frames (int): Total number of frame in the video.
            proposals (list | None): Proposals fetched.
                It is set to None in test mode.

        Returns:
            np.ndarray: Sampled frame indices.
        r¢   N)rª   re   r`   )r   rZ   r¸   r^   r   r   r   rf   ‰  s   

ýz"SampleProposalFrames._sample_clipsc                 C   sú   |d }|  dd¡}|  ||¡}|dd…df t | j¡ddd…f | j  }t |¡}| jr>tjj	| jt
|ƒd�}||7 }|d }t ||¡| }t |¡ tj¡|d< | j|d< | j|d< | j| jd	  | jd
  |d< | jdv r{t
|d ƒ|d< |S )rg   rh   Úout_proposalsNrN   rM   rj   rD   rE   r   r   rF   )r    r¡   Únum_proposals)Úgetrf   rR   rS   rD   rE   rd   rG   rT   rU   r   rl   r‹   rc   rY   r£   r¤   rª   )r   r0   rh   r¹   r^   rj   ro   rM   r   r   r   r4   œ  s4   ÿ
ÿÿ

ÿ

ÿ
zSampleProposalFrames.__call__c                 C   r�   )
Nrq   z, body_segments=z, aug_segments=z, aug_ratio=rr   z, test_interval=rt   z, mode=r6   )
r7   r8   rD   r£   r¤   r¥   rE   r«   rG   rª   r9   r   r   r   r;   ½  rŽ   zSampleProposalFrames.__repr__)r   rŸ   Fr    )r8   r=   r>   r?   r   Ústaticmethodr¯   r°   r·   r`   re   rf   r4   r;   r�   r   r   r„   r   rž   Ê  s"    ø

4!rž   c                   @   ó*   e Zd ZdZd
dd„Zdd„ Zdd„ Zd	S )ÚPyAVInitaM  Using pyav to initialize the video.

    PyAV: https://github.com/mikeboers/PyAV

    Required keys are "filename",
    added or modified keys are "video_reader", and "total_frames".

    Args:
        io_backend (str): io backend where frames are store.
            Default: 'disk'.
        kwargs (dict): Args for file client.
    Údiskc                 K   s   || _ || _d | _d S ©N)Ú
io_backendr   Úfile_client)r   rÁ   r   r   r   r   r   Ù  s   
zPyAVInit.__init__c                 C   s‚   zddl }W n ty   tdƒ‚w | jdu r"t| jfi | j¤Ž| _t | j |d ¡¡}| 	|¡}||d< |j
jd j|d< |S )z´Perform the PyAV initialization.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        r   NzWPlease run "conda install av -c conda-forge" or "pip install av" to install PyAV first.ÚfilenameÚvideo_readerrh   )ÚavÚImportErrorrÂ   r   rÁ   r   ÚioÚBytesIOr»   ÚopenÚstreamsÚvideoÚframes)r   r0   rÅ   Úfile_objÚ	containerr   r   r   r4   Þ  s   ÿ

zPyAVInit.__call__c                 C   s   | j j› d�}|S )Nz(io_backend=disk))r7   r8   r9   r   r   r   r;   ö  s   zPyAVInit.__repr__N©r¿   r}   r   r   r   r   r¾   Ê  s
    
r¾   c                   @   r½   )Ú
PyAVDecodeaM  Using pyav to decode the video.

    PyAV: https://github.com/mikeboers/PyAV

    Required keys are "video_reader" and "frame_inds",
    added or modified keys are "imgs", "img_shape" and "original_shape".

    Args:
        multi_thread (bool): If set to True, it will apply multi
            thread processing. Default: False.
    Fc                 C   ó
   || _ d S rÀ   )Úmulti_thread)r   rÒ   r   r   r   r   	  ó   
zPyAVDecode.__init__c                    sâ   |d }t ƒ ‰ | jrd|jjd _|d jdkr!t |d ¡|d< t|d ƒ}d}|j	dd�D ]}||d kr9 nˆ  
| ¡  ¡ ¡ |d7 }q/d|d< ~‡ fdd	„|d D ƒ|d
< ˆ d jdd… |d< ˆ d jdd… |d< |S )z®Perform the PyAV decoding.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rÄ   ÚAUTOr   rj   r   )rË   Nc                    ó   g | ]
}ˆ |t ˆ ƒ  ‘qS r   ©r   ©r%   r    ©Úimgsr   r   r(   )  ó    z'PyAVDecode.__call__.<locals>.<listcomp>rÙ   r   Úoriginal_shapeÚ	img_shape)r‰   rÒ   rÊ   rË   Úthread_typeÚndimrR   ÚsqueezerV   Údecoder   Úto_rgbÚ
to_ndarrayry   )r   r0   rÎ   Zmax_indsr    Úframer   rØ   r   r4     s&   
zPyAVDecode.__call__c                 C   s   | j j}|d| j› d�7 }|S )Nz(multi_thread=r6   )r7   r8   rÒ   r9   r   r   r   r;   0  s   zPyAVDecode.__repr__N)Fr}   r   r   r   r   rÐ   û  s
    
$rÐ   c                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )ÚPyAVDecodeMotionVectoraƒ  Using pyav to decode the motion vectors from video.

    Reference: https://github.com/PyAV-Org/PyAV/
        blob/main/tests/test_decode.py

    Required keys are "video_reader" and "frame_inds",
    added or modified keys are "motion_vectors", "frame_inds".

    Args:
        multi_thread (bool): If set to True, it will apply multi
            thread processing. Default: False.
    c                 C   sÌ   |d |d |d |d |d |d f\}}}}}}	|| }
|	| }||d  }|	|d  }|| }|| }t |||||
|ƒD ]$\}}}}}}|dkrc||k rc|dkrc||k rc||f| ||…||…f< q?| S )	zParse the returned vectors.ÚwÚhÚsrc_xÚsrc_yÚdst_xÚdst_yr   r   )r   )ÚmvÚvectorsÚheightÚwidthrå   ræ   rç   rè   ré   rê   Zval_xZval_yZstart_xZstart_yZend_xZend_yÚsxÚexÚsyZeyZvxZvyr   r   r   Ú_parse_vectorsE  s$   ÿÿ
ÿ €z%PyAVDecodeMotionVector._parse_vectorsc                    sD  |d }t ƒ ‰ | jrd|jjd _|d jdkr!t |d ¡|d< t|d ƒ}d}|jjd }|j	}ddi|_
| |¡D ]N}| ¡ D ]G}||d krL n>|d7 }|j}	|j}
tj|	|
dftjd	�}|j d
¡}|jrp|du spJ ‚|dur„t|ƒdkr„|  || ¡ |	|
¡}ˆ  |¡ qBq<d|d< ~t ‡ fdd„|d D ƒ¡|d< |S )z¼Perform the PyAV motion vector decoding.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rÄ   rÔ   r   rj   r   Úflags2z+export_mvsr   rP   ZMOTION_VECTORSNc                    rÕ   r   rÖ   r×   rØ   r   r   r(   „  rÚ   z3PyAVDecodeMotionVector.__call__.<locals>.<listcomp>Zmotion_vectors)r‰   rÒ   rÊ   rË   rÝ   rÞ   rR   rß   rV   Úcodec_contextÚoptionsÚdemuxrà   rí   rî   r-   Úint8Z	side_datar»   Z	key_framer   rò   râ   r   r‹   )r   r0   rÎ   Zmax_idxr    Ústreamrô   Úpacketrã   rí   rî   rë   rì   r   rØ   r   r4   X  sD   
ÿ€ÿzPyAVDecodeMotionVector.__call__N)r8   r=   r>   r?   r¼   rò   r4   r   r   r   r   rä   6  s
    
rä   c                   @   ó*   e Zd ZdZddd„Zdd„ Zdd	„ Zd
S )Ú
DecordInitzÂUsing decord to initialize the video_reader.

    Decord: https://github.com/dmlc/decord

    Required keys are "filename",
    added or modified keys are "video_reader" and "total_frames".
    r¿   r   c                 K   ó   || _ || _|| _d | _d S rÀ   )rÁ   Únum_threadsr   rÂ   )r   rÁ   rý   r   r   r   r   r   ’  ó   
zDecordInit.__init__c                 C   s‚   zddl }W n ty   tdƒ‚w | jdu r"t| jfi | j¤Ž| _t | j |d ¡¡}|j	|| j
d�}||d< t|ƒ|d< |S )z¶Perform the Decord initialization.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        r   Nz8Please run "pip install decord" to install Decord first.rÃ   )rý   rÄ   rh   )ÚdecordrÆ   rÂ   r   rÁ   r   rÇ   rÈ   r»   ÚVideoReaderrý   r   )r   r0   rÿ   rÍ   rÎ   r   r   r   r4   ˜  s   ÿÿ
zDecordInit.__call__c                 C   r{   )Nú(io_backend=z, num_threads=r6   )r7   r8   rÁ   rý   r9   r   r   r   r;   ®  r|   zDecordInit.__repr__N)r¿   r   r}   r   r   r   r   rû   ˆ  s
    
rû   c                   @   ó   e Zd ZdZdd„ ZdS )ÚDecordDecodezÒUsing decord to decode the video.

    Decord: https://github.com/dmlc/decord

    Required keys are "video_reader", "filename" and "frame_inds",
    added or modified keys are "imgs" and "original_shape".
    c                    sœ   |d ‰ |d j dkrt |d ¡|d< |d }‡ fdd„t |¡D ƒ‰‡fdd„|D ƒ}d|d< Š ||d	< |d
 jdd… |d< |d
 jdd… |d< |S )z°Perform the Decord decoding.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rÄ   rj   r   c                    s   i | ]	}|ˆ |   ¡ “qS r   )Zasnumpyr$   )rÎ   r   r   Ú
<dictcomp>Í  s    ÿÿz)DecordDecode.__call__.<locals>.<dictcomp>c                    s   g | ]}ˆ | ‘qS r   r   r$   )Ú
frame_dictr   r   r(   Ò  r)   z)DecordDecode.__call__.<locals>.<listcomp>NrÙ   r   r   rÛ   rÜ   )rÞ   rR   rß   Úuniquery   )r   r0   rj   rÙ   r   )rÎ   r  r   r4   ¿  s   
þzDecordDecode.__call__N©r8   r=   r>   r?   r4   r   r   r   r   r  µ  s    r  c                   @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )Ú
OpenCVInitz¢Using OpenCV to initialize the video_reader.

    Required keys are "filename", added or modified keys are "new_path",
    "video_reader" and "total_frames".
    r¿   c                 K   s\   || _ || _d | _d | _| j dkr,tƒ }tƒ }t tƒ |› d|› �¡| _t	 
| j¡ d S d S )Nr¿   Ú_)rÁ   r   rÂ   Ú
tmp_folderr   r   ÚospÚjoinr   ÚosÚmkdir)r   rÁ   r   Zrandom_stringÚ	thread_idr   r   r   r   æ  s   
ÿûzOpenCVInit.__init__c                 C   sº   | j dkr
|d }n>| jdu rt| j fi | j¤Ž| _tƒ }t | jd|› d�¡}t|dƒ�}| 	| j 
|d ¡¡ W d  ƒ n1 sCw   Y  t |¡}||d< ||d< t|ƒ|d	< |S )
z¶Perform the OpenCV initialization.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        r¿   rÃ   NZtmp_z.mp4ÚwbÚnew_pathrÄ   rh   )rÁ   rÂ   r   r   r   r  r  r
  rÉ   Úwriter»   r¦   r   r   )r   r0   r  r  ÚfrÎ   r   r   r   r4   ò  s   


ÿ
zOpenCVInit.__call__c                 C   s*   | j rt | j ¡rt | j ¡ d S d S d S rÀ   )r
  r  ÚexistsÚshutilÚrmtree)r   r   r   r   Ú__del__  s   ÿzOpenCVInit.__del__c                 C   r5   )Nr  r6   )r7   r8   rÁ   r9   r   r   r   r;     r<   zOpenCVInit.__repr__NrÏ   )r8   r=   r>   r?   r   r4   r  r;   r   r   r   r   r  Þ  s    
r  c                   @   r  )ÚOpenCVDecodez³Using OpenCV to decode the video.

    Required keys are "video_reader", "filename" and "frame_inds", added or
    modified keys are "imgs", "img_shape" and "original_shape".
    c                 C   sì   |d }t ƒ }|d jdkrt |d ¡|d< |d D ]!}|| }t|tdƒƒr7|d8 }|| }t|tdƒƒs(| |¡ qd|d< ~t |¡}|dd…dd…dd…ddd…f }t |ƒ|d< |d jdd… |d	< |d jdd… |d
< |S )z°Perform the OpenCV decoding.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rÄ   rj   r   Nr   rÙ   r   r   rÛ   rÜ   )	r‰   rÞ   rR   rß   Ú
isinstancer©   r   r‹   ry   )r   r0   rÎ   rÙ   Z	frame_indÚ	cur_framer   r   r   r4     s&   þ
"zOpenCVDecode.__call__Nr  r   r   r   r   r    ó    r  c                   @   rú   )ÚRawFrameDecodea°  Load and decode frames with given indices.

    Required keys are "frame_dir", "filename_tmpl" and "frame_inds",
    added or modified keys are "imgs", "img_shape" and "original_shape".

    Args:
        io_backend (str): IO backend where frames are stored. Default: 'disk'.
        decoding_backend (str): Backend used for image decoding.
            Default: 'cv2'.
        kwargs (dict, optional): Arguments for FileClient.
    r¿   Úcv2c                 K   rü   rÀ   ©rÁ   Údecoding_backendr   rÂ   ©r   rÁ   r  r   r   r   r   r   N  rþ   zRawFrameDecode.__init__c                 C   sò  t  | j¡ |d }|d }|d }|dkr|d }n|dkr(|d }|d }nt‚|d }| jd	u r>t| jfi | j¤Ž| _tƒ }	|d
 j	dkrQt
 |d
 ¡|d
< | dd¡}
|d
 D ]Á}||
7 }|d }|dk rkd}||krs|d }|dkr™z|| }W n   |dkrŒ||d  }n||d  }Y |	 |¡ q[|dk�rz|| }W n   |dkr³||d  }n||d  }Y z|| }W nD   |dkrÏ||d  }n5z||d  }W n,   z||d  }W n   z||d  }W n   td|ƒ td|ƒ |d }Y Y Y Y t |tj¡}t |tj¡}|	 ||g¡ q[t‚|	|d< |	d jd	d… |d< |	d jd	d… |d< d|v �rw|d \}}t
 ||||g¡}|d }||  t
j¡}||d< d|v �rw|d d	u�rw|d }||  t
j¡}||d< |S )zÐPerform the ``RawFrameDecode`` to pick frames given indices.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        Ú	frame_dirÚfilename_tmplÚmodalityÚRGBrË   ÚFlowÚvideo_yÚ	frame_numNrj   r   Úoffsetr   r   r   zdirectory: zframe_num: rÙ   rÛ   rÜ   Ú	gt_bboxesr¸   )r¦   Úuse_backendr  ÚNotImplementedErrorrÂ   r   rÁ   r   r‰   rÞ   rR   rß   r»   r   Úprintr  ÚcvtColorÚCOLOR_RGB2GRAYrŠ   ry   r‹   rc   r›   )r   r0   Ú	directoryr"  r#  rË   Zvideo_xr&  r'  rÙ   r(  Z	frame_idxZ	cur_indexr  Zcur_frame_xZcur_frame_yræ   rå   Úscale_factorr)  r¸   r   r   r   r4   T  sš   


€
€


€
zRawFrameDecode.__call__c                 C   r{   )Nr  z, decoding_backend=r6   )r7   r8   rÁ   r  r9   r   r   r   r;   Î  r|   zRawFrameDecode.__repr__N©r¿   r  r}   r   r   r   r   r  @  s
    
zr  c                   @   s"   e Zd ZdZd	dd„Zdd„ ZdS )
ÚImageDecodeax  Load and decode images.

    Required key is "filename", added or modified keys are "imgs", "img_shape"
    and "original_shape".

    Args:
        io_backend (str): IO backend where frames are stored. Default: 'disk'.
        decoding_backend (str): Backend used for image decoding.
            Default: 'cv2'.
        kwargs (dict, optional): Arguments for FileClient.
    r¿   r  c                 K   rü   rÀ   r  r   r   r   r   r   ã  rþ   zImageDecode.__init__c                 C   s–   t  | j¡ |d }| jdu rt| jfi | j¤Ž| _tƒ }| j |¡}t j	|dd�}| 
|¡ ||d< |d jdd… |d< |d jdd… |d	< |S )
zÒPerform the ``ImageDecode`` to load image given the file path.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rÃ   NÚrgb)Úchannel_orderrÙ   r   r   rÛ   rÜ   )r¦   r*  r  rÂ   r   rÁ   r   r‰   r»   Úimfrombytesr   ry   )r   r0   rÃ   rÙ   Ú	img_bytesÚimgr   r   r   r4   é  s   

zImageDecode.__call__Nr1  ©r8   r=   r>   r?   r   r4   r   r   r   r   r2  Õ  s    
r2  c                   @   sH   e Zd ZdZ			ddd„Zedd„ ƒZed	d
„ ƒZdd„ Zdd„ Z	dS )ÚAudioDecodeInitaJ  Using librosa to initialize the audio reader.

    Required keys are "audio_path", added or modified keys are "length",
    "sample_rate", "audios".

    Args:
        io_backend (str): io backend where frames are store.
            Default: 'disk'.
        sample_rate (int): Audio sampling times per second. Default: 16000.
    r¿   é€>  Úzeroc                 K   s0   || _ || _|dv r|| _nt‚|| _d | _d S )N)rT   r;  )rÁ   Úsample_rateÚ
pad_methodr+  r   rÂ   )r   rÁ   r<  r=  r   r   r   r   r     s   
zAudioDecodeInit.__init__c                 C   ó   t j| t jd�S ©NrP   ©rR   r-   r›   ©ry   r   r   r   Ú	_zero_pad  ó   zAudioDecodeInit._zero_padc                 C   s   t j | ¡ t j¡d d S )Nr   r   ©rR   rT   Úrandrc   r›   rA  r   r   r   Ú_random_pad"  s   zAudioDecodeInit._random_padc                 C   sÐ   zddl }W n ty   tdƒ‚w | jdu r"t| jfi | j¤Ž| _t |d ¡r?t 	| j 
|d ¡¡}|j|| jd�\}}nt| d| j› d�ƒ}|ttd| j ƒƒƒ}| j}|jd |d	< ||d
< ||d< |S )z·Perform the librosa initialization.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        r   NzPlease install librosa first.Ú
audio_path)Úsrr	  Ú_padg      $@Úlengthr<  Úaudios)ÚlibrosarÆ   rÂ   r   rÁ   r   r  r  rÇ   rÈ   r»   Úloadr<  Úgetattrr=  rY   Úroundry   )r   r0   rL  rÍ   ÚyrH  Úpad_funcr   r   r   r4   '  s"   ÿ
zAudioDecodeInit.__call__c                 C   rœ   )Nr  z, sample_rate=z, pad_method=r6   )r7   r8   rÁ   r<  r=  r9   r   r   r   r;   C  r�   zAudioDecodeInit.__repr__N)r¿   r:  r;  ©
r8   r=   r>   r?   r   r¼   rB  rF  r4   r;   r   r   r   r   r9    s    
ý

r9  c                   @   sB   e Zd ZdZddd„Zedd„ ƒZedd„ ƒZd	d
„ Zdd„ Z	dS )ÚLoadAudioFeaturez‚Load offline extracted audio features.

    Required keys are "audio_path", added or modified keys are "length",
    audios".
    r;  c                 C   s   |dvrt ‚|| _d S )N)r;  rT   )r+  r=  )r   r=  r   r   r   r   S  s   
zLoadAudioFeature.__init__c                 C   r>  r?  r@  rA  r   r   r   rB  X  rC  zLoadAudioFeature._zero_padc                 C   s   t j | ¡ t j¡S rÀ   rD  rA  r   r   r   rF  \  s   zLoadAudioFeature._random_padc                 C   sT   t  |d ¡rt |d ¡}nt| d| j› d�ƒ}|dƒ}|jd |d< ||d< |S )z®Perform the numpy loading.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rG  r	  rI  )i€  éP   r   rJ  rK  )r  r  rR   rM  rN  r=  ry   )r   r0   Úfeature_maprQ  r   r   r   r4   a  s   zLoadAudioFeature.__call__c                 C   r5   )Nz(pad_method=r6   )r7   r8   r=  r9   r   r   r   r;   t  r<   zLoadAudioFeature.__repr__N)r;  rR  r   r   r   r   rS  K  s    


rS  c                   @   s"   e Zd ZdZddd„Zdd„ ZdS )	ÚAudioDecodeaŒ  Sample the audio w.r.t. the frames selected.

    Args:
        fixed_length (int): As the audio clip selected by frames sampled may
            not be exactly the same, `fixed_length` will truncate or pad them
            into the same size. Default: 32000.

    Required keys are "frame_inds", "num_clips", "total_frames", "length",
    added or modified keys are "audios", "audios_shape".
    é }  c                 C   rÑ   rÀ   ©Úfixed_length©r   rY  r   r   r   r   ‡  rÓ   zAudioDecode.__init__c                 C   s  |d }|d }|d }t ƒ }| |d¡}t|ƒD ]]}|| }tdtt|d d |d  |d  ƒƒƒ}t|d tt|d d |d  |d  ƒƒƒ}	|||	… }
|
jd | jkra|
d	| j… }nt	j
|
d| j|
jd  fd
d�}| |¡ qt	 |¡|d< |d j|d< |S )z0Perform the ``AudioDecode`` to pick audio clips.rK  rj   rF   r   r   r   rh   rJ  NÚconstant©rª   Úaudios_shape©r‰   rk   r   rV   rY   rO  r³   ry   rY  rR   Úpadr   r‹   ©r   r0   Úaudiorj   rF   Zresampled_clipsÚclip_idxZclip_frame_indsr   Úend_idxZcropped_audioZtruncated_audior   r   r   r4   Š  sF   ÿÿþÿÿþýzAudioDecode.__call__N)rW  r8  r   r   r   r   rV  z  s    
rV  c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚBuildPseudoClipzîBuild pseudo clips with one single image by repeating it n times.

    Required key is "imgs", added or modified key is "imgs", "num_clips",
        "clip_len".

    Args:
        clip_len (int): Frames of the generated pseudo clips.
    c                 C   rÑ   rÀ   )rD   )r   rD   r   r   r   r   ¸  rÓ   zBuildPseudoClip.__init__c                 C   s\   t |d ƒdks
J ‚|d d }td| jƒD ]}|d  t |¡¡ q| j|d< d|d< |S )NrÙ   r   r   rD   rF   )r   r   rD   r   rR   Úcopy)r   r0   Úimr	  r   r   r   r4   »  s   
zBuildPseudoClip.__call__c                 C   r5   ©Nz(fix_length=r6   ©r7   r8   rY  r9   r   r   r   r;   Å  r<   zBuildPseudoClip.__repr__Nr}   r   r   r   r   rd  ­  s
    	
rd  c                       s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚFrameSelectorz(Deprecated class for ``RawFrameDecode``.c                    s    t  d¡ tƒ j|i |¤Ž d S )Nz?"FrameSelector" is deprecated, please switch to"RawFrameDecode")rK   rL   r�   r   )r   Úargsr   r„   r   r   r   Ï  s   
zFrameSelector.__init__)r8   r=   r>   r?   r   r�   r   r   r„   r   ri  Ë  s    ri  c                   @   r½   )ÚAudioFeatureSelectora›  Sample the audio feature w.r.t. the frames selected.

    Required keys are "audios", "frame_inds", "num_clips", "length",
    "total_frames", added or modified keys are "audios", "audios_shape".

    Args:
        fixed_length (int): As the features selected by frames sampled may
            not be extactly the same, `fixed_length` will truncate or pad them
            into the same size. Default: 128.
    é€   c                 C   rÑ   rÀ   rX  rZ  r   r   r   r   â  rÓ   zAudioFeatureSelector.__init__c                 C   s"  |d }|d }|d }t ƒ }| |d¡}t|ƒD ]g}|| }tdtt|d d |d  |d  ƒƒƒ}t|d tt|d d |d  |d  ƒƒƒ}	|||	…d	d	…f }
|
jd | jkri|
d	| j…d	d	…f }nt	j
|
d| j|
jd  fd
fdd�}| |¡ qt	 |¡|d< |d j|d< |S )zÕPerform the ``AudioFeatureSelector`` to pick audio feature clips.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rK  rj   rF   r   r   r   rh   rJ  N)r   r   r[  r\  r]  r^  r`  r   r   r   r4   å  sF   ÿÿþÿÿþýzAudioFeatureSelector.__call__c                 C   r5   rg  rh  r9   r   r   r   r;     r<   zAudioFeatureSelector.__repr__N)rl  r}   r   r   r   r   rk  Õ  s
    
'rk  c                   @   r½   )ÚLoadLocalizationFeaturezÿLoad Video features for localizer with given video_name list.

    Required keys are "video_name" and "data_prefix", added or modified keys
    are "raw_feature".

    Args:
        raw_feature_ext (str): Raw feature file extension.  Default: '.csv'.
    ú.csvc                 C   s   d}||vrt ‚|| _d S )N©rn  )r+  Úraw_feature_ext)r   rp  Zvalid_raw_feature_extr   r   r   r     s   
z LoadLocalizationFeature.__init__c                 C   sJ   |d }|d }t  ||| j ¡}tj|tjddd�}t |d¡|d< |S )zÀPerform the LoadLocalizationFeature loading.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        Ú
video_nameÚdata_prefixÚ,r   ©rQ   Ú	delimiterÚskiprows)r   r   Úraw_feature)r  r  rp  rR   Úloadtxtr›   Ú	transpose)r   r0   rq  rr  Z	data_pathrw  r   r   r   r4   #  s   
ÿz LoadLocalizationFeature.__call__c                 C   r5   )Nz(raw_feature_ext=r6   )r7   r8   rp  r9   r   r   r   r;   5  r<   z LoadLocalizationFeature.__repr__Nro  r}   r   r   r   r   rm    s
    
	rm  c                   @   r  )ÚGenerateLocalizationLabelszÅLoad video label for localizer with given video_name list.

    Required keys are "duration_frame", "duration_second", "feature_frame",
    "annotations", added or modified keys are "gt_bbox".
    c                 C   sš   |d }|d }|d }t |ƒ| | }|d }g }|D ]%}ttd|d d | ƒdƒ}	ttd|d d | ƒdƒ}
| |	|
g¡ qt |¡}||d< |S )	zÃPerform the GenerateLocalizationLabels loading.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        Zduration_frameZduration_secondÚfeature_frameÚannotationsr   Úsegmentr   Úgt_bbox)rb   rV   r³   r   rR   r‹   )r   r0   Zvideo_frameZvideo_secondr{  Zcorrected_secondr|  r~  Ú
annotationZcurrent_startZcurrent_endr   r   r   r4   C  s"   ÿÿ
z#GenerateLocalizationLabels.__call__Nr  r   r   r   r   rz  ;  r  rz  c                   @   s.   e Zd ZdZ		ddd„Zdd„ Zdd	„ Zd
S )ÚLoadProposalsa  Loading proposals with given proposal results.

    Required keys are "video_name", added or modified keys are 'bsp_feature',
    'tmin', 'tmax', 'tmin_score', 'tmax_score' and 'reference_temporal_iou'.

    Args:
        top_k (int): The top k proposals to be loaded.
        pgm_proposals_dir (str): Directory to load proposals.
        pgm_features_dir (str): Directory to load proposal features.
        proposal_ext (str): Proposal file extension. Default: '.csv'.
        feature_ext (str): Feature file extension. Default: '.npy'.
    rn  ú.npyc                 C   sB   || _ || _|| _d}||vrt‚|| _d}||vrt‚|| _d S )Nro  )r�  )Útop_kÚpgm_proposals_dirÚpgm_features_dirr+  Úproposal_extÚfeature_ext)r   r‚  rƒ  r„  r…  r†  Zvalid_proposal_extZvalid_feature_extr   r   r   r   m  s   
zLoadProposals.__init__c                 C   s  |d }t  | j|| j ¡}| jdkrtj|tjddd�}t |d| j… ¡}|dd…df }|dd…df }|dd…df }|dd…d	f }|dd…d
f }	t  | j	|| j
 ¡}
| j
dkrgt |
¡ tj¡}|d| j…dd…f }||d< ||d< ||d< ||d< ||d< |	|d< |S )z¶Perform the LoadProposals loading.

        Args:
            results (dict): The resulting dict to be modified and passed
                to the next transform in pipeline.
        rq  rn  rs  r   rt  Nr   r   r   é   r�  Úbsp_featureÚtminÚtmaxÚ
tmin_scoreÚ
tmax_scoreÚreference_temporal_iou)r  r  rƒ  r…  rR   rx  r›   r‹   r‚  r„  r†  rM  rc   )r   r0   rq  Zproposal_pathZpgm_proposalsr‰  rŠ  r‹  rŒ  r�  Zfeature_pathrˆ  r   r   r   r4     s6   ÿ

ÿÿ
zLoadProposals.__call__c                 C   s:   | j j› d| j› d| j› d| j› d| j› d| j› d�}|S )Nz(top_k=z, pgm_proposals_dir=z, pgm_features_dir=z, proposal_ext=z, feature_ext=r6   )r7   r8   r‚  rƒ  r„  r…  r†  r9   r   r   r   r;   ¤  s   
ÿþýüûzLoadProposals.__repr__N)rn  r�  r}   r   r   r   r   r€  ^  s    
û%r€  )/rÇ   r  Úos.pathÚpathr  r  rK   r¦   ÚnumpyrR   r,   Zmmcv.fileior   Ztorch.nn.modules.utilsr   Úutilsr   r   r   Úregistryr	   r  Úregister_moduler
   r@   rw   r~   r�   rž   r¾   rÐ   rä   rû   r  r  r  r  r2  r9  rS  rV  rd  ri  rk  rm  rz  r€  r   r   r   r   Ú<module>   s‚    ; 0:e+  0:Q,(7) -G.2	<("