B
    S^                 @   s   d dl Z d dl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 Zdd Zd	d
 Zdd Zdd ZdddZej ddddfddZdddZdS )    N)groupby)measure)Image)maskc             C   s   |   rt| S |  S )N)isdigitintlower)text r
   e/home/peike/Projects/Augmented-CE2P/mhp_extension/coco_style_annotation_creator/pycococreatortools.py<lambda>	       r   c             C   s   dd t d| D S )Nc             S   s   g | ]}t |qS r
   )convert).0cr
   r
   r   
<listcomp>
   s    z<lambda>.<locals>.<listcomp>z([0-9]+))resplit)keyr
   r
   r   r   
   r   c             C   s2   t | tjd }||}t|tjS )N   )r   	fromarrayastypenpuint8resizeZasarrayZbool_)arrayZnew_sizeimager
   r
   r   resize_binary_mask   s    
r   c             C   s*   t | d | d s&t | | d f} | S )Nr   )r   Zarray_equalZvstack)contourr
   r
   r   close_contour   s    r    c             C   sp   g t | jd}|d}xPtt| jddD ]8\}\}}|dkrV|dkrV|d |tt | q0W |S )N)countssizer!   F)orderr      )listshapeget	enumerater   ravelappendlen)binary_maskZrler!   ivalueelementsr
   r
   r   binary_mask_to_rle   s    
"
r1   c             C   s   g }t j| dddd}t|d}t |d}x`|D ]X}t|}t||}t|dk rZq4t j|dd}|	 
 }dd	 |D }|| q4W |S )
a@  Converts a binary mask to COCO polygon representation
    Args:
        binary_mask: a 2D binary numpy array where '1's represent the object
        tolerance: Maximum distance from original points of polygon to approximated
            polygonal chain. If tolerance is 0, the original coordinate array is returned.
    r%   constantr   )Z	pad_widthmodeZconstant_valuesg      ?   )Zaxisc             S   s   g | ]}|d k rd n|qS )r   r
   )r   r.   r
   r
   r   r   8   s    z*binary_mask_to_polygon.<locals>.<listcomp>)r   Zpadr   Zfind_contourssubtractr    Zapproximate_polygonr,   Zflipr*   tolistr+   )r-   	toleranceZpolygonsZpadded_binary_maskZcontoursr   segmentationr
   r
   r   binary_mask_to_polygon$   s    
r9    r%    c          	   C   s"   | ||d |d ||||d}|S )Nr   r%   )id	file_namewidthheightdate_capturedlicensecoco_url
flickr_urlr
   )image_idr=   
image_sizer@   Z
license_idrB   rC   
image_infor
   r
   r   create_image_info>   s    rG      c          
   C   s   |d k	rt ||}tt|tj}t|}|dk r@d S |d krRt|}|d rhd}	t	|}
nd}	t
||}
|
s~d S | ||d |	| | |
|jd |jd d	}|S )Nr%   is_crowdr   r<   )	r<   rD   Zcategory_idZiscrowdareaZbboxr8   r>   r?   )r   r   encoder   Zasfortranarrayr   r   rJ   ZtoBboxr1   r9   r6   r'   )Zannotation_idrD   Zcategory_infor-   rE   r7   Zbounding_boxZbinary_mask_encodedrJ   rI   r8   Zannotation_infor
   r
   r   create_annotation_infoO   s2    




rL   )r   )NrH   N)r   datetimeZnumpyr   	itertoolsr   Zskimager   PILr   Zpycocotoolsr   r   Znatrual_keyr   r    r1   r9   utcnow	isoformatrG   rL   r
   r
   r
   r   <module>   s   
