
    ڧg                          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
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DictListOptionalTuple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         Z/var/www/html/ai-engine/env/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   openr0   r!   r    r"   )r3   r7   imgtargets       r5   __getitem__zWIDERFace.__getitem__T   s     ju-j9::>%..%%Cv--4=3G3V ,**622FF{r6   c                 *    t          | j                  S )N)lenr0   )r3   s    r5   __len__zWIDERFace.__len__j   s    4=!!!r6   c                 R    dg} d                     |          j        di | j        S )NzSplit: {split}
 )r,   format__dict__)r3   liness     r5   
extra_reprzWIDERFace.extra_reprm   s1    !"&tyy&77777r6   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 rD   )int).0xs     r5   
<listcomp>z>WIDERFace.parse_train_val_annotations_file.<locals>.<listcomp>   s    ">">">a3q66">">">r6   r                  	   )bboxblur
expressionillumination	occlusionposeinvalid)r9   r:   zError parsing annotation file )r    r*   r+   r,   r   r;   	readlinesrstripr   r   rO   appendtorchtensorr0   cloneclearr/   )r3   filenamefilepathfrG   file_name_linenum_boxes_linebox_annotation_line	num_boxesbox_counterlabelsliner9   
line_splitline_valueslabels_tensors                   r5   r1   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 )NrJ   zwider_face_test_filelist.txt
WIDER_testrL   r9   )r*   r+   r,   r   r   r   r;   r`   ra   r0   rb   )r3   rh   ri   rG   rp   r9   s         r5   r2   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copyrb   ANNOTATIONS_FILEr*   r+   splitextr,   r   exists)r3   	all_files_md5rg   fileextextracted_dirs           r5   r.   zWIDERFace._check_integrity   s    N''))	.///"+ 	 	QX((22ID#GLLD99M7>>-00 uutr6   c                 \   |                                  rt          d           d S | j        D ]Q\  }}}t          || j        ||           t
          j                            | j        |          }t          |           Rt          | j
        d         | j        | j
        d                    d S )Nz%Files already downloaded and verifiedr   r   )urldownload_rootr~   )r.   printrw   r   r   r*   r+   r,   r   r   ry   )r3   file_idr~   rg   rh   s        r5   r#   zWIDERFace.download   s      "" 	9:::F )- 	& 	&$Wc8+GTY#NNNw||DIx88HH%%%% 	%%a(	tG\]^G_	
 	
 	
 	
 	
 	
r6   )r   NNF)r$   N)__name__
__module____qualname____doc__r-   rw   ry   r   strr   r
   r   boolr)   rO   r   r   r>   rA   rH   r1   r2   r.   r#   __classcell__)r4   s   @r5   r   r      s        < K  I (,/3/ /CI/ / H%	/
 #8,/ / 
/ / / / / /4 sCx    ," " " " "8C 8 8 8 8.T .T .T .T`	= 	= 	= 	=	$ 	 	 	 	
 
 
 
 
 
 
 
r6   r   )r*   os.pathr   r   pathlibr   typingr   r   r   r	   r
   r   r   rc   PILr   utilsr   r   r   r   visionr   r   rD   r6   r5   <module>r      s    				 ' ' ' ' ' ' ' '       D D D D D D D D D D D D D D D D D D        q q q q q q q q q q q q ! ! ! ! ! !w
 w
 w
 w
 w
 w
 w
 w
 w
 w
r6   