
    Yh1                          d dl Z d dlmZmZ d dlmZ d dlmZmZm	Z	m
Z
 d dlZd dlmZ ddlmZmZmZmZ ddlmZ  G d	 d
e          ZdS )    N)abspath
expanduser)Path)AnyCallableOptionalUnion)Image   )download_and_extract_archivedownload_file_from_google_driveextract_archiveverify_str_arg)VisionDatasetc                        e Zd ZdZdZg dZdZ	 	 	 	 ddeee	f         d	ed
e
e         de
e         deddf fdZdedeeef         fdZdefdZdefdZddZddZdefdZddZ xZS )	WIDERFaceuu  `WIDERFace <http://shuoyang1213.me/WIDERFACE/>`_ Dataset.

    Args:
        root (str or ``pathlib.Path``): Root directory where images and annotations are downloaded to.
            Expects the following folder structure if download=False:

            .. code::

                <root>
                    └── widerface
                        ├── wider_face_split ('wider_face_split.zip' if compressed)
                        ├── WIDER_train ('WIDER_train.zip' if compressed)
                        ├── WIDER_val ('WIDER_val.zip' if compressed)
                        └── WIDER_test ('WIDER_test.zip' if compressed)
        split (string): The dataset split to use. One of {``train``, ``val``, ``test``}.
            Defaults to ``train``.
        transform (callable, optional): A function/transform that takes in a PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
        download (bool, optional): If true, downloads the dataset from the internet and
            puts it in root directory. If dataset is already downloaded, it is not
            downloaded again.

            .. warning::

                To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required.

    	widerface))!15hGDLhsx8bLgLcIRD5DhYt5iBxnjNF1M 3fedf70df600953d25982bcd13d91ba2zWIDER_train.zip)!1GUCogbp16PMGa39thoMMeWxp7Rp5oM8Q dfa7d7e790efa35df3788964cf0bbaeazWIDER_val.zip)!1HIfDbVEWKmsYKJZm4lchTBDLW5N7dY5T e5d8f4248ed24c334bbd12f49c29dd40zWIDER_test.zip)zLhttp://shuoyang1213.me/WIDERFACE/support/bbx_annotation/wider_face_split.zip 0e3767bcf0e326556d407bf5bff5d27czwider_face_split.ziptrainNFrootsplit	transformtarget_transformdownloadreturnc                    t                                          t          j                            || j                  ||           t          |dd          | _        |r|                                  | 	                                st          d          g | _        | j        dv r|                                  d S |                                  d S )N)r   r   r   r   )r   valtestzTDataset not found or corrupted. You can use download=True to download and prepare it)r   r#   )super__init__ospathjoinBASE_FOLDERr   r   r    _check_integrityRuntimeErrorimg_info parse_train_val_annotations_fileparse_test_annotations_file)selfr   r   r   r   r    	__class__s         p/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/datasets/widerface.pyr&   zWIDERFace.__init__:   s     	dD$455]m 	 	
 	
 	
 $E74LMM
 	MMOOO$$&& 	wuvvvNP:)))1133333,,.....    indexc                    t          j        | j        |         d                   }| j        |                     |          }| j        dk    rdn| j        |         d         }| j        |                     |          }||fS )z
        Args:
            index (int): Index

        Returns:
            tuple: (image, target) where target is a dict of annotations for all faces in the image.
            target=None for the test split.
        img_pathNr$   annotations)r
   openr-   r   r   r   )r0   r4   imgtargets       r2   __getitem__zWIDERFace.__getitem__T   s     ju-j9::>%..%%Cv--4=3G3V ,**622FF{r3   c                 *    t          | j                  S )N)lenr-   )r0   s    r2   __len__zWIDERFace.__len__j   s    4=!!!r3   c                 R    dg} d                     |          j        di | j        S )NzSplit: {split}
 )r)   format__dict__)r0   liness     r2   
extra_reprzWIDERFace.extra_reprm   s1    !"&tyy&77777r3   c                    | j         dk    rdnd}t          j                            | j        d|          }t          |          5 }|                                }d\  }}}d\  }}	g }
|D ]}|                                }|rPt          j                            | j        d| j         z   d|          }t          t          |                    }d	}d
}i|rt          |          }d	}d
}|r]|	dz  }	|                     d          }d |D             }|
                    |           |	|k    rd	}d
}t          j        |
          }| j                            ||d d ddf                                         |d d df                                         |d d df                                         |d d df                                         |d d df                                         |d d df                                         |d d df                                         dd           d}	|
                                 t#          d|           	 d d d            d S # 1 swxY w Y   d S )Nr   zwider_face_train_bbx_gt.txtzwider_face_val_bbx_gt.txtwider_face_split)TFF)r   r   WIDER_imagesFTr    c                 ,    g | ]}t          |          S rA   )int).0xs     r2   
<listcomp>z>WIDERFace.parse_train_val_annotations_file.<locals>.<listcomp>   s    ">">">a3q66">">">r3   r                  	   )bboxblur
expressionillumination	occlusionposeinvalid)r6   r7   zError parsing annotation file )r   r'   r(   r)   r   r8   	readlinesrstripr   r   rL   appendtorchtensorr-   cloneclearr,   )r0   filenamefilepathfrD   file_name_linenum_boxes_linebox_annotation_line	num_boxesbox_counterlabelsliner6   
line_splitline_valueslabels_tensors                   r2   r.   z*WIDERFace.parse_train_val_annotations_fileq   s   48J'4I4I00Oj7<<	+=xHH(^^ *	TqKKMMEBT?NN,?%)"I{F %T %T{{}}! #T!w||DIx$*7LhX\]]H&z(';';<<H%*N%)NN# T #D		I%*N*.''( T1$K!%CJ">">:">">">KMM+..."i//.3+)-(-V(<(<,,,4,9!!!QqS&,A,G,G,I,I,9!!!Q$,?,E,E,G,G2?12E2K2K2M2M4A!!!Q$4G4M4M4O4O1>qqq!t1D1J1J1L1L,9!!!Q$,?,E,E,G,G/<QQQT/B/H/H/J/J0" 0"    '(&'R'R'RSSSK%T*	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	T *	Ts   HI((I,/I,c                    t           j                            | j        dd          }t	          t          |                    }t          |          5 }|                                }|D ]u}|                                }t           j                            | j        dd|          }t	          t          |                    }| j	        
                    d|i           v	 d d d            d S # 1 swxY w Y   d S )NrG   zwider_face_test_filelist.txt
WIDER_testrI   r6   )r'   r(   r)   r   r   r   r8   r]   r^   r-   r_   )r0   re   rf   rD   rm   r6   s         r2   r/   z%WIDERFace.parse_test_annotations_file   s   7<<	+=?]^^:h//00(^^ 	=qKKMME = ={{}}7<<	<4PP":h#7#788$$j(%;<<<<	=	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	=s   BC--C14C1c                 P   | j                                         }|                    | j                   |D ]o\  }}}t          j                            |          \  }}t          j                            | j        |          }t          j        	                    |          s dS pdS )NFT)
	FILE_LISTcopyr_   ANNOTATIONS_FILEr'   r(   splitextr)   r   exists)r0   	all_files_md5rd   fileextextracted_dirs           r2   r+   zWIDERFace._check_integrity   s    N''))	./// ) 	 	AsH((22ID#GLLD99M7>>-00 uutr3   c                 >   |                                  rd S | j        D ]Q\  }}}t          || j        ||           t          j                            | j        |          }t          |           Rt          | j	        d         | j        | j	        d                    d S )Nr   r   )urldownload_rootr{   )
r+   rt   r   r   r'   r(   r)   r   r   rv   )r0   file_idr{   rd   re   s        r2   r    zWIDERFace.download   s      "" 	F '+n 	& 	&"GS(+GTY#NNNw||DIx88HH%%%% 	%%a(	tG\]^G_	
 	
 	
 	
 	
 	
r3   )r   NNF)r!   N)__name__
__module____qualname____doc__r*   rt   rv   r	   strr   r   r   boolr&   rL   tupler   r;   r>   rE   r.   r/   r+   r    __classcell__)r1   s   @r2   r   r      s        < K  I (,/3/ /CI/ / H%	/
 #8,/ / 
/ / / / / /4 sCx    ," " " " "8C 8 8 8 8.T .T .T .T`	= 	= 	= 	=	$ 	 	 	 	
 
 
 
 
 
 
 
r3   r   )r'   os.pathr   r   pathlibr   typingr   r   r   r	   r`   PILr
   utilsr   r   r   r   visionr   r   rA   r3   r2   <module>r      s    				 ' ' ' ' ' ' ' '       1 1 1 1 1 1 1 1 1 1 1 1        q q q q q q q q q q q q ! ! ! ! ! !v
 v
 v
 v
 v
 v
 v
 v
 v
 v
r3   