o
    e                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZ d dlZd dl	Z
d dlmZmZ d dlmZ d dlmZ d dlmZmZ d	ee d
ee fddZd	ee dee d
eeef fddZdee d
ee fddZdee d
eeee f fddZdee
j deeef d
e
jfddZdee deeef ded
efddZ			 d4d!ed"ed#ed$e d%e d&e d
eee ef fd'd(Z!	)d5d*ed+ed,ed
eee eee
jf eeef f fd-d.Z"			 d4d/ed0eee
jf d1eeef dee d$e d%e d&e d
dfd2d3Z#dS )6    N)datetime)Path)DictListTuple)approximate_mask_with_polygonsmap_detections_class_id)
Detectionspolygon_to_mask)read_json_filesave_json_filecoco_categoriesreturnc                 C   s   dd t | dd dD S )Nc                 S      g | ]}|d  qS )name .0categoryr   r   O/var/www/myenv/lib/python3.10/site-packages/supervision/dataset/formats/coco.py
<listcomp>   s    z.coco_categories_to_classes.<locals>.<listcomp>c                 S   s   | d S )Nidr   )r   r   r   r   <lambda>   s    z,coco_categories_to_classes.<locals>.<lambda>)key)sortedr   r   r   r   coco_categories_to_classes   s   r   target_classesc                    s$   dd | D   fddt |D S )Nc                 S   s   i | ]	}|d  |d qS )r   r   r   r   r   r   r   
<dictcomp>   s    z2build_coco_class_index_mapping.<locals>.<dictcomp>c                    s   i | ]	\}} | |qS r   r   )r   target_class_indextarget_class_namesource_class_to_indexr   r   r      s    	enumerater   r   r   r"   r   build_coco_class_index_mapping   s   
r'   classesc                 C   s   dd t | D S )Nc                 S   s   g | ]
\}}||d dqS )zcommon-objects)r   r   supercategoryr   )r   class_id
class_namer   r   r   r   &   s    z.classes_to_coco_categories.<locals>.<listcomp>r$   r(   r   r   r   classes_to_coco_categories%   s   r-   coco_annotationsc                 C   s8   i }| D ]}|d }||vrg ||< ||  | q|S )Nimage_id)append)r.   annotations
annotationr/   r   r   r   "group_coco_annotations_by_image_id0   s   r3   polygonsresolution_whc                    s   t j fdd| D tdS )Nc                    s   g | ]}t | d qS ))polygonr5   r
   )r   r6   r5   r   r   r   @   s    
z&_polygons_to_masks.<locals>.<listcomp>dtype)nparrayboolr4   r5   r   r7   r   _polygons_to_masks<   s   
r>   image_annotations
with_masksc                 C   s   | st  S dd | D }dd | D }t|}|d d ddf  |d d ddf 7  < |rJdd | D }t||d}t tj|td	||d
S t |tj|td	dS )Nc                 S   r   )category_idr   r   image_annotationr   r   r   r   N   s    z2coco_annotations_to_detections.<locals>.<listcomp>c                 S   r   )bboxr   rB   r   r   r   r   Q   s          r   c                 S   s(   g | ]}t t j|d  t jddqS )segmentationr8   )rE   )r:   reshapeasarrayint32rB   r   r   r   r   V   s    r=   r8   )r*   xyxymask)rL   r*   )r	   emptyr:   rJ   r>   int)r?   r5   r@   	class_idsrL   r4   rM   r   r   r   coco_annotations_to_detectionsH   s"   
,rQ                 ?      ?
detectionsr/   annotation_idmin_image_area_percentagemax_image_area_percentageapproximation_percentagec              	   C   s   g }| D ]P\}}}	}
}	|d |d  |d |d  }}g }|d ur0t t||||dd  }||t|
|d |d ||g|| |rF|gng dd}|| |d7 }q||fS )NrE   r         )rM   rW   rX   rY   )r   r/   rA   rD   arearG   iscrowd)listr   flattenrO   r0   )rU   r/   rV   rW   rX   rY   r.   rL   rM   _r*   	box_width
box_heightr6   coco_annotationr   r   r   detections_to_coco_annotationsd   s6   "	
	
rd   Fimages_directory_pathannotations_pathforce_masksc                 C   s   t |d}t|d d}t|d |d}|d }t|d d}i }i }	|D ];}
|
d |
d	 |
d
 }}}||
d g }tj| |}t	|}t
|||f|d}t||d}|||< ||	|< q%|||	fS )N	file_path
categoriesr   r&   imagesr1   )r.   	file_namewidthheightr   )r?   r5   r@   )source_to_target_mappingrU   )r   r   r'   r3   getospathjoincv2imreadrQ   r   )re   rf   rg   	coco_datar(   class_index_mappingcoco_imagescoco_annotations_groupsrk   r1   
coco_image
image_nameimage_widthimage_heightr?   
image_pathimager2   r   r   r   load_coco_annotations   s>   




r   annotation_pathrk   r1   c              	   C   s   t | jjddd i }ddddg}g }	g }
t|d}d\}}| D ]B\}}|j\}}}t |j t |j }|d|||t	 
d	d
}|
| || }t||||||d\}}|	| |d7 }q$||||
|	d}t|| d d S )NT)parentsexist_okr[   z,https://creativecommons.org/licenses/by/4.0/z	CC BY 4.0)r   urlr   r,   )r[   r[   z%m/%d/%Y,%H:%M:%S)r   licenserl   rn   rm   date_captured)rU   r/   rV   rW   rX   rY   )infolicensesrj   rk   r1   rh   )r   parentmkdirr-   itemsshapestemsuffixr   nowstrftimer0   rd   extendr   )r   rk   r1   r(   rW   rX   rY   r   r   r.   rx   r   r/   rV   r~   r   r}   r|   r`   r{   rz   rU   rc   annotation_dictr   r   r   save_coco_annotations   sP   	

	

	
r   )rR   rS   rT   )F)$rq   r   pathlibr   typingr   r   r   rt   numpyr:   supervision.dataset.utilsr   r   supervision.detection.corer	   supervision.detection.utilsr   supervision.utils.filer   r   dictstrr   rO   r'   r-   r3   ndarrayr>   r<   rQ   floatrd   r   r   r   r   r   r   <module>   s    






 
&"
1
