a
    ^DfNP                     @   s   d dl Z d dlm  mZ d dlmZ d dlmZm	Z	m
Z
 d dlmZ ddlmZ ddlmZmZ ddlmZmZ dd	lmZ G d
d dZdS )    N)Path)MappingOptionalUnion)warn   )open_atomic)BoundingBox	BoxFormat)FileParsingErrorParsingError)PathLikec                   @   s  e Zd ZdZdZdeed ed ddddZeed	d
dZ	edd	ddZ
e
jddddZ
eee d	ddZeee d	ddZedddZeeef d dddZdd	ddZeddejddddd eee eeeed ee ed d!	d"d#Zedddd$edee eed d%d&d'Zedddd$edee eed d%d(d)Zeddd*edee ed d+d,d-Zeddd*edee ed d+d.d/Zeddd*edee ed d+d0d1Zeed d2d3d4Zeed d2d5d6Z eed d2d7d8Z!eed d2d9d:Z"ee#d d;d<d=Z$ee%j&d d;d>d?Z'ed@dAddBe#eedd dCdDdEZ(dejdddFddGeeee)eef f  eed eeedHdIdJZ*ddddKeeee)eef f  ed eedLdMdNZ+ddddKeeee)eef f  ed eedLdOdPZ,dddQeeee)eef f  ed edRdSdTZ-dddQeeee)eef f  ed edRdUdVZ.dddQeeee)eef f  ed edRdWdXZ/dejdddFddGeeeee)eef f  eeed eedYdZd[Z0ddddKeeeee)eef f  ed ed\d]d^Z1ddddKeeeee)eef f  ed ed\d_d`Z2dddQeeeee)eef f  ed dadbdcZ3dddQeeeee)eef f  ed dadddeZ4dddQeeeee)eef f  ed dadfdgZ5dded e#dhdidjZ6ddeed dkdldmZ7dded e%j&dhdndoZ8dded e%j&dhdpdqZ9dded e%j&dhdrdsZ:ddeed dkdtduZ;ddeed dkdvdwZ<ddeed dkdxdyZ=dded e%j&dhdzd{Z>d@dAd|eeee#d}d~dZ?ed	ddZ@dS )
AnnotationzI
    The bounding boxes associated with a uniquely identified image.
    )	_image_id_image_sizeboxesNztuple[int, int]zlist[BoundingBox])image_id
image_sizer   returnc                 C   s~   |durd|\}}|dkr |dks6J d| d| dt ||krNt ||ksdJ d| d| d|| _|| _|pvg | _dS )a  
        Create an `Annotation` for an image identified with a unique `str` tag and with
        the provided list of bounding boxes.

        The image size in pixels ((width, height) tuple) can be optionally specified and is required
        for some export formats. This value can be queried from an image file with the
        `get_image_size()` function if it cannot be retreived from the annotation file.
        Nr   Image size '(, )' should be positive.!)' components should be integers.)intr   r   r   )selfr   r   r   img_wimg_h r   ^/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/globox/annotation.py__init__   s"    

zAnnotation.__init__)r   c                 C   s   | j S )z7The unique identifier of the image for this annotation.)r   r   r   r   r   r   /   s    zAnnotation.image_idzOptional[tuple[int, int]]c                 C   s   | j S )z<The image size in pixels ((width, height) tuple) if present.)r   r    r   r   r   r   4   s    zAnnotation.image_sizer   c                 C   sn   |d urd|\}}|dkr |dks6J d| d| dt ||krNt ||ksdJ d| d| d|| _d S )Nr   r   r   r   r   )r   r   )r   r   r   r   r   r   r   r   9   s    

c                 C   s   | j dur| j d S dS )zThe image width in pixels.Nr   r!   r    r   r   r   image_widthE   s    zAnnotation.image_widthc                 C   s   | j dur| j d S dS )zThe image height in pixels.Nr   r!   r    r   r   r   image_heightJ   s    zAnnotation.image_height)boxc                 C   s   | j | dS )z+Add a bounding box to the image annotation.N)r   append)r   r$   r   r   r   addO   s    zAnnotation.add)mappingr   c                 C   s*   | j D ]}|j| v r||j |_q| S )z
        Update all the bounding box labels according to the provided dictionary which maps former
        names to new names. If a label name is not present in the dictionary keys, then it won't
        be updated.
        )r   labelkeys)r   r'   r$   r   r   r   
map_labelsS   s    
zAnnotation.map_labelszset[str]c                 C   s   dd | j D S )z?The set of the different label names present in the annotation.c                 S   s   h | ]
}|j qS r   )r(   .0br   r   r   	<setcomp>`       z%Annotation._labels.<locals>.<setcomp>)r   r    r   r   r   _labels^   s    zAnnotation._labelsz.jpgFr   image_extension
box_formatrelativer   	separator	conf_last)		file_pathr   r2   r3   r4   r   r5   r6   r   c             
      s   t |   }|d u r>|ds2J d| d||j}z|  }	W n tyl   t	|ddY n0 z fdd|	D }
W n0 t
y } zt	||jW Y d }~n
d }~0 0 t||
S )N.zImage extension 'z' should start with a dot.zcannot read file)reasonc              
      s"   g | ]}t j| d qS ))r3   r4   r   r5   r6   )r	   from_txt)r,   lr3   r6   r   r4   r5   r   r   
<listcomp>|   s   	z'Annotation.from_txt.<locals>.<listcomp>)r   
expanduserresolve
startswithwith_suffixname	read_text
splitlinesOSErrorr   r   r9   r   )r7   r   r2   r3   r4   r   r5   r6   pathlinesr   er   r<   r   r:   b   s&    
	
"zAnnotation.from_txt)r   r2   r6   )r7   r   r   r2   r6   r   c             
   C   s   t j| ||tjd|d |dS )NTr1   )r   r:   r
   ZXYWHr7   r   r   r2   r6   r   r   r   
_from_yolo   s    	zAnnotation._from_yoloc                C   s"   t dtdd tj| ||||dS )NzK'from_yolo' is deprecated. Please use `from_yolo_darknet` or `from_yolo_v5`   category
stacklevelr   r   r2   r6   )r   DeprecationWarningr   rJ   rI   r   r   r   	from_yolo   s    	zAnnotation.from_yolo)r   r2   )r7   r   r   r2   r   c                C   s   t j| |||ddS )NFrO   r   rJ   r7   r   r   r2   r   r   r   from_yolo_darknet   s    zAnnotation.from_yolo_darknetc                C   s   t j| |||ddS )NTrO   rR   rS   r   r   r   from_yolo_v5   s    zAnnotation.from_yolo_v5c                C   s   t j| |||dS )N)r   r   r2   )r   rU   rS   r   r   r   from_yolo_v7   s    zAnnotation.from_yolo_v7)r7   r   c           
   	   C   s  t |   }z:| }t| }W d    n1 s>0    Y  W n  ttjfyj   t	dY n0 |
d}|d}|d u s|d u rt	d|
d}|
d}|d u s|d u rt	dzt|t|f}W n ty   t	dY n0 dd |dD }	t|||	S )	Nz)Syntax error in imagenet annotation file.filenamesizewidthheightc                 S   s   g | ]}t |qS r   )r	   from_xmlr,   nr   r   r   r=     r/   z'Annotation.from_xml.<locals>.<listcomp>object)r   r>   r?   openetparsegetrootrE   
ParseErrorr   findtextfindr   
ValueErroriterr   )
r7   rF   frootr   	size_noderY   rZ   r   r   r   r   r   r[      s(    
0



zAnnotation.from_xmlc                 C   s
   t | S Nr   r[   r7   r   r   r   from_pascal_voc  s    zAnnotation.from_pascal_vocc                 C   s
   t | S rk   rl   rm   r   r   r   from_imagenet	  s    zAnnotation.from_imagenetc              	   C   s   t |   }zD| (}t|}d|v r4|d= W d    n1 sH0    Y  W n  ttjfyt   tdY n0 z:t	|d }t
|d }t
|d }dd |d D }W n ttfy   tdY n0 t|||f|d	S )
N	imageDataz(Syntax error in labelme annotation file.	imagePath
imageWidthimageHeightc                 S   s"   g | ]}|d  dkrt |qS )Z
shape_typeZ	rectangle)r	   from_labelmer\   r   r   r   r=     s   z+Annotation.from_labelme.<locals>.<listcomp>shapesr   r   )r   r>   r?   r_   jsonloadrE   JSONDecodeErrorr   strr   KeyErrorrf   r   )r7   rF   rh   contentr   rY   rZ   r   r   r   r   rt     s$    

(
zAnnotation.from_labelme)noder   c              	   C   sR   z(t | d }t| d t| d f}W n ttfyF   tdY n0 t||S )N	file_namerY   rZ   z%Syntax error in COCO annotation file.)rz   r   rf   r{   r   r   )r}   r   r   r   r   r   _from_coco_partial&  s    zAnnotation._from_coco_partialc                 C   s   |  d}|  d|  d }}|d u s8|d u s8|d u r@tdzt|t|f}W n tyn   tdY n0 dd | dD }t|||dS )	NrB   rY   rZ   z%Syntax error in CVAT annotation file.c                 S   s   g | ]}t |qS r   )r	   	from_cvatr\   r   r   r   r=   =  r/   z(Annotation.from_cvat.<locals>.<listcomp>r$   rv   )getr   r   rf   rg   r   )r}   r   rY   rZ   Zimg_sizer   r   r   r   r   0  s    
zAnnotation.from_cvatZlabel_id
confidence)	label_keyconfidence_keyr   )
annotationr   r   r   r   c                   sR   z(| d }| d } fdd|D }W n t yB   tdY n0 t|||dS )NrW   regionsc                    s,   g | ]$}|d  d dkrt j| dqS )Zshape_attributesrB   Zrectr   r   )r	   from_via_json)r,   Zregionr   r   r   r   r=   M  s
   z,Annotation.from_via_json.<locals>.<listcomp>z)Syntax error in VIA JSON annotation file.r   r   r   )r{   r   r   )r   r   r   r   rW   r   Zbboxesr   r   r   r   A  s    
zAnnotation.from_via_json label_to_idr3   r4   r   r5   r6   )r   r3   r   r5   r6   r   c                   s.   p| j d fdd| jD S )N
c              	   3   s$   | ]}|j  d V  qdS )r   N)to_txtr,   r$   r3   r6   r   r   r4   r5   r   r   	<genexpr>i  s   	z$Annotation.to_txt.<locals>.<genexpr>)r   joinr   )r   r   r3   r4   r   r5   r6   r   r   r   r   ]  s    

	zAnnotation.to_txtr   r   r6   )r   r   r6   r   c                   s8   p| j d u rtdd fdd| jD S )NzsEither `image_size` shoud be provided as argument or stored in the Annotation object for conversion to YOLO format.r   c                 3   s   | ]}|j  d V  qdS ))r   r   r6   N)to_yolor   r6   r   r   r   r   r     s   z&Annotation._to_yolo.<locals>.<genexpr>)r   rf   r   r   r   r   r   r6   r   r   r   _to_yolou  s    
zAnnotation._to_yoloc                C   s   t dtdd | j|||dS )NzE'to_yolo' is deprecated. Please use `to_yolo_darknet` or `to_yolo_v5`rK   rL   r   )r   rP   r   r   r   r   r   r     s    zAnnotation.to_yolor   r   )r   r   r   c                C   s   | j ||ddS NFr   r   r   r   r   r   r   r   to_yolo_darknet  s    zAnnotation.to_yolo_darknetc                C   s   | j ||ddS NTr   r   r   r   r   r   
to_yolo_v5  s    zAnnotation.to_yolo_v5c                C   s   | j ||dS Nr   )r   r   r   r   r   
to_yolo_v7  s    zAnnotation.to_yolo_v7)rF   r   r3   r4   r   r5   r6   c          
      C   sN   | j ||||||d}t|d}	|	| W d    n1 s@0    Y  d S )Nr   w)r   r   write)
r   rF   r   r3   r4   r   r5   r6   r|   rh   r   r   r   save_txt  s    	zAnnotation.save_txt)rF   r   r   r6   c                C   sH   | j |||d}t|d}|| W d    n1 s:0    Y  d S )Nr   r   )r   r   r   )r   rF   r   r   r6   r|   rh   r   r   r   
_save_yolo  s
    zAnnotation._save_yoloc                C   s$   t dtdd | j||||d d S )NzK'save_yolo' is deprecated. Please use `save_yolo_darknet` or `save_yolo_v5`rK   rL   r   )r   rP   r   )r   rF   r   r   r6   r   r   r   	save_yolo  s    zAnnotation.save_yolo)rF   r   r   c                C   s   | j |||dd d S r   r   r   rF   r   r   r   r   r   save_yolo_darknet  s    zAnnotation.save_yolo_darknetc                C   s   | j |||dd d S r   r   r   r   r   r   save_yolo_v5  s    zAnnotation.save_yolo_v5c                C   s   | j |||d d S r   )r   r   r   r   r   save_yolo_v7  s    zAnnotation.save_yolo_v7)r   r   c                C   s@   |p| j }|d usJ d| j|d |d d dd | jD dS )NLAn image size should be provided either by argument or by `self.image_size`.r   r   c                 S   s   g | ]}|  qS r   )
to_labelmer+   r   r   r   r=     r/   z)Annotation.to_labelme.<locals>.<listcomp>)rq   rr   rs   rp   ru   )r   r   r   r   r   r   r   r   r     s    
zAnnotation.to_labelme)rF   r   c                C   sJ   | j |d}t|d }tj||dd W d    n1 s<0    Y  d S )Nr!   r   F)fp	allow_nan)r   r   rw   dumpr   rF   r   r|   rh   r   r   r   save_labelme  s    zAnnotation.save_labelmec                C   s   |p| j }|d usJ dtd}| jt|d_t|d}|d  t|d_|d  t|d_| jD ]}||  qn|S )	Nr   r   rW   rX   r   rY   r   rZ   )	r   r`   Elementr   
SubElementtextr   r%   to_xml)r   r   Zann_noderj   r$   r   r   r   r   $  s    


zAnnotation.to_xmlc                C   s   | j |dS Nr!   r   r   r   r   r   to_pascal_voc6  s    zAnnotation.to_pascal_vocc                C   s   | j |dS r   r   r   r   r   r   to_imagenet;  s    zAnnotation.to_imagenetc                C   sR   | j |d}tj|dd}t|d}|| W d    n1 sD0    Y  d S )Nr!   unicode)encodingr   )r   r`   tostringr   r   r   r   r   r   save_xml@  s    zAnnotation.save_xmlc                C   s   | j ||d d S r   r   r   rF   r   r   r   r   save_pascal_vocI  s    zAnnotation.save_pascal_vocc                C   s   | j ||d d S r   r   r   r   r   r   save_imagenetN  s    zAnnotation.save_imagenetc                C   sX   |p| j }|d usJ dtjd| j|d  |d  dd}|dd | jD  |S )	Nr   imager   r   )rB   rY   rZ   )attribc                 s   s   | ]}|  V  qd S rk   )to_cvatr   r   r   r   r   b  r/   z%Annotation.to_cvat.<locals>.<genexpr>)r   r`   r   r   extendr   )r   r   Zimg_noder   r   r   r   S  s    
	zAnnotation.to_cvatr   )image_folderr   r   r   c          	         sb   t |  }| s(J d| d| j}|| }| j} fdd| jD }|||dS )Nz
Filepath 'z$' is not a folder or does not exist.c                    s   g | ]}|j  d qS )r   )to_via_jsonr   r   r   r   r=   u  s   z*Annotation.to_via_json.<locals>.<listcomp>)rW   rX   r   )r   r>   r?   is_dirr   statst_sizer   )	r   r   r   r   rF   r   Z
image_path	file_sizer   r   r   r   r   f  s    
zAnnotation.to_via_jsonc                 C   s   d| j  d| j d| j dS )NzAnnotation(image_id: z, image_size: z	, boxes: )r   r    r   r   r   __repr__|  s    zAnnotation.__repr__)NN)A__name__
__module____qualname____doc__	__slots__rz   r   r   propertyr   r   setterr   r"   r#   r	   r&   r   r*   r0   staticmethodr
   ZLTRBr   boolr:   rJ   rQ   rT   rU   rV   r[   rn   ro   rt   dictr   r`   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r      s    )		
r   )rw   xml.etree.ElementTreeetreeElementTreer`   pathlibr   typingr   r   r   warningsr   Zatomicr   Zboundingboxr	   r
   errorsr   r   Z
file_utilsr   r   r   r   r   r   <module>   s   