a
    )yc                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dl	m
Z
 d dlmZ d dl	mZ d dl	mZ d dl	mZ dej_ejdd ZG d	d
 d
eZG dd deZdS )    N)__version__)logger)PY2)QT4)utilsc                 c   s8   |dv sJ t r|d7 }d }nd}tj| ||dV  d S )N)rwbutf-8)encoding)r   ioopen)namemoder    r   _/nfs/NAS7/SABIOD/METHODE/ermites/ermites_venv/lib/python3.9/site-packages/labelme/label_file.pyr      s    r   c                   @   s   e Zd ZdS )LabelFileErrorN)__name__
__module____qualname__r   r   r   r   r      s   r   c                   @   sP   e Zd ZdZdddZedd Zdd Zed	d
 ZdddZ	edd Z
dS )	LabelFilez.jsonNc                 C   s.   g | _ d | _d | _|d ur$| | || _d S )N)shapes	imagePath	imageDataloadfilename)selfr   r   r   r   __init__'   s    
zLabelFile.__init__c                 C   s   zt j| }W n$ ty4   td|  Y d S 0 t|}t	
 b}t| d  }trjtrjd}n|dv rxd}nd}|j||d |d | W  d    S 1 s0    Y  d S )NzFailed opening image file: {}   ZPNG)z.jpgz.jpegZJPEG)formatr   )PILImager   IOErrorr   errorr   r   Zapply_exif_orientationr   BytesIOospsplitextlowerr   r   saveseekread)r   Z	image_pilfextr   r   r   r   load_image_file/   s     


zLabelFile.load_image_filec              
      s  g d}g d z,t |d}t|}W d    n1 s>0    Y  |d}|d u rltd| n0|dd tdd krtd||t |d	 d urt	
|d	 }trtrt|}n tt||d
 }| |}|dpi }|d
 }| t	|d|d|d  fdd|d D }	W n. tyl }
 zt|
W Y d }
~
n
d }
~
0 0 i }| D ]\}}||vrz|||< qz|| _|	| _|| _|| _|| _|| _d S )N)versionr   r   r   flagsimageHeight
imageWidth)labelpointsgroup_id
shape_typer/   r   r.   z)Loading JSON file ({}) of unknown version.r   zfThis JSON file ({}) may be incompatible with current labelme. version in file: {}, current version: {}r   r   r/   r
   r0   r1   c                    sP   g | ]H}t |d  |d |dd|di |d fdd| D dqS )	r2   r3   r5   Zpolygonr/   r4   c                    s   i | ]\}}| vr||qS r   r   ).0kvZ
shape_keysr   r   
<dictcomp>   s   z-LabelFile.load.<locals>.<listcomp>.<dictcomp>)r2   r3   r5   r/   r4   Z
other_data)dictgetitems)r7   sr:   r   r   
<listcomp>y   s   


z"LabelFile.load.<locals>.<listcomp>r   )r   jsonr   r=   r   warningr   splitr   base64	b64decoder   r   r   Zimg_data_to_png_datar%   joindirnamer-   _check_image_height_and_width	b64encodedecode	Exceptionr   r>   r/   r   r   r   r   	otherData)r   r   keysr+   datar.   r   r   r/   r   erL   keyvaluer   r:   r   r   F   s\    	(




zLabelFile.loadc                 C   sf   t | }|d ur4|jd |kr4td |jd }|d ur^|jd |kr^td |jd }||fS )Nr   zaimageHeight does not match with imageData or imagePath, so getting imageHeight from actual image.r   z_imageWidth does not match with imageData or imagePath, so getting imageWidth from actual image.)r   Zimg_b64_to_arrshaper   r#   )r   r0   r1   Zimg_arrr   r   r   rH      s    


z'LabelFile._check_image_height_and_widthc	              
   C   s   |d ur*t |d}| |||\}}|d u r6i }|d u rBi }tt||||||d}	| D ]\}
}|
|	vstJ ||	|
< q`zFt|d"}tj	|	|ddd W d    n1 s0    Y  || _
W n, ty } zt|W Y d }~n
d }~0 0 d S )Nr
   )r.   r/   r   r   r   r0   r1   r   F   )ensure_asciiindent)rD   rI   rJ   rH   r<   r   r>   r   rA   dumpr   rK   r   )r   r   r   r   r0   r1   r   rL   r/   rN   rP   rQ   r+   rO   r   r   r   r(      s6    	
0
zLabelFile.savec                 C   s   t | d  tjkS )Nr   )r%   r&   r'   r   suffix)r   r   r   r   is_label_file   s    zLabelFile.is_label_file)N)NNN)r   r   r   rW   r   staticmethodr-   r   rH   r(   rX   r   r   r   r   r   #   s   

P
   
'r   )rD   
contextlibr   rA   os.pathpathr%   Z	PIL.Imager    labelmer   Zlabelme.loggerr   r   r   r   r!   ZMAX_IMAGE_PIXELScontextmanagerr   rK   r   objectr   r   r   r   r   <module>   s   
