
    khH                          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y)    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.zipNrootsplit	transformtarget_transformdownloadreturnc                 d   t         |   t        j                  j	                  || j
                        ||       t        |dd      | _        |r| j                          | j                         st        d      g | _        | j                  dv r| j                          y | j                          y )N)r   r    r!   r   )train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/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/datasets/widerface.pyr)   zWIDERFace.__init__:   s     	dD$4$45]m 	 	
 $E74LM
MMO$$&uvvNP::))113,,.    indexc                    t        j                  | j                  |   d         }| j                  | j                  |      }| j                  dk(  rdn| j                  |   d   }| j
                  | 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~     jju-j9:>>%..%Cv-4==3G3V  ,**62FF{r6   c                 ,    t        | j                        S )N)lenr0   )r3   s    r5   __len__zWIDERFace.__len__j   s    4==!!r6   c                 ^    dg} dj                  |      j                  di | j                  S )NzSplit: {split}
 )r,   format__dict__)r3   liness     r5   
extra_reprzWIDERFace.extra_reprm   s-    !"&tyy&&777r6   c                 |   | j                   dk(  rdnd}t        j                  j                  | j                  d|      }t        |      5 }|j                         }d\  }}}d\  }}	g }
|D ]  }|j                         }|rRt        j                  j                  | j                  d| j                   z   d|      }t        t        |            }d	}d
}h|rt        |      }d	}d
}z|r-|	dz  }	|j                  d      }|D cg c]  }t        |       }}|
j                  |       |	|k\  sd	}d
}t        j                  |
      }| j                  j                  |d d ddf   j                         |d d df   j                         |d d df   j                         |d d df   j                         |d d df   j                         |d d df   j                         |d d df   j                         dd       d}	|
j!                          t#        d|        	 d d d        y c c}w # 1 sw Y   y xY w)Nr%   zwider_face_train_bbx_gt.txtzwider_face_val_bbx_gt.txtwider_face_split)TFF)r   r   WIDER_imagesFTr    r                  	   )bboxblur
expressionillumination	occlusionposeinvalid)r9   r:   zError parsing annotation file )r   r*   r+   r,   r   r;   	readlinesrstripr   r   int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xline_valueslabels_tensors                    r5   r1   z*WIDERFace.parse_train_val_annotations_fileq   s2   48JJ'4I0Oj77<<		+=xH(^ *	TqKKMEBT?NN,?%)"I{F %T{{}!!ww||DIIx$**7LhX\]H&z(';<H%*N%)N# #D	I%*N*.'(1$K!%CJ3=">a3q6">K">MM+."i/.3+)-(-V(<,,,4,9!QqS&,A,G,G,I,9!Q$,?,E,E,G2?12E2K2K2M4A!Q$4G4M4M4O1>q!t1D1J1J1L,9!Q$,?,E,E,G/<QT/B/H/H/J0" '(&)Gz'RSSK%T*	T *	T& #?'*	T *	Ts&   
B7H2H-H2,C7H2-H22H;c                    t         j                  j                  | j                  dd      }t	        t        |            }t        |      5 }|j                         }|D ]o  }|j                         }t         j                  j                  | j                  dd|      }t	        t        |            }| j                  j                  d|i       q 	 d d d        y # 1 sw Y   y xY w)NrJ   zwider_face_test_filelist.txt
WIDER_testrL   r9   )r*   r+   r,   r   r   r   r;   r[   r\   r0   r^   )r3   rd   re   rG   rl   r9   s         r5   r2   z%WIDERFace.parse_test_annotations_file   s    77<<		+=?]^:h/0(^ 	=qKKME ={{}77<<		<4P":h#78$$j(%;<	=	= 	= 	=s   BCC#c                 ^   | j                   j                         }|j                  | j                         |D ]s  \  }}}t        j
                  j                  |      \  }}t        j
                  j                  | j                  |      }t        j
                  j                  |      rs y y)NFT)
	FILE_LISTcopyr^   ANNOTATIONS_FILEr*   r+   splitextr,   r   exists)r3   	all_files_md5rc   fileextextracted_dirs           r5   r.   zWIDERFace._check_integrity   s    NN'')	../"+ 	QX((2ID#GGLLD9M77>>-0		
 r6   c                 N   | j                         ry | j                  D ]S  \  }}}t        || j                  ||       t        j
                  j                  | j                  |      }t        |       U t        | j                  d   | j                  | j                  d          y )Nr   r   )urldownload_rootr{   )
r.   rt   r   r   r*   r+   r,   r   r   rv   )r3   file_idr{   rc   rd   s        r5   r"   zWIDERFace.download   s      " )- 	&$Wc8+GTYY#Nww||DIIx8HH%	& 	%%%a(		tG\G\]^G_	
r6   )r%   NNF)r#   N)__name__
__module____qualname____doc__r-   rt   rv   r   strr   r
   r   boolr)   r]   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.T`	=	$ 	
r6   r   )r*   os.pathr   r   pathlibr   typingr   r   r   r	   r
   r   r   r_   PILr   utilsr   r   r   r   visionr   r   rD   r6   r5   <module>r      s4    	 '  D D D   q q !v
 v
r6   