o
    mi}                     @   s   d dl mZ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 dlZd dlmZmZ d	e
d
efddZdeded
efddZ	dd	e
ded
edeej fddZ	ddeded
edejdedee dejfddZdS )    )AnyOptionalN)Image)tqdm)Results)PosePredictor)	Annotator)	get_boxes!get_ground_truth_bbox_annotationsresultvisualize_skeletonc              	   C   s`   t t| jd d d d d d df }| jj }t|jd D ]}|j	|| |d q!|j
S )Nr   kpt_line)r   npascontiguousarrayorig_img	keypointsdatanumpyrangeshapekptsim)r   r   	annotator
key_pointsidx r   f/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/wandb/integration/ultralytics/pose_utils.pyannotate_keypoint_results   s
   (r   
image_pathr   c                 C   sX   t | }t|}t|}|j| |d |jW  d    S 1 s%w   Y  d S )Nr   )r   openr   r   r   r   r   r   )r    r   r   original_imager   r   r   r   annotate_keypoint_batch   s   
$r#   
model_nametablec           	      C   sd   |  d} t| \}}t| |}tj||d}||t|d d || jg}|d ur0|j|  |S |S )Ncpuboxespredictionsbox_data)tor	   r   wandbr   lenspeedadd_data)	r   r$   r   r%   r(   mean_confidence_mapannotated_imageprediction_image	table_rowr   r   r   plot_pose_predictions!   s   


r4   	predictormax_validation_batchesepochreturnc              
   C   s&  d}t | j| j }	t||	}t| D ]}\}
}||d }tt|t |d|
d  d| d}|D ]T\}}|d}t|||}tj	t
|d | |d | |d	t||d | |||d
id}||
|g|dd   }|d urv|g| n|}|g| }|j|  |d7 }q2|
d |kr |S q|S )Nr   im_filez$Generating Visualizations for batch-   /)totaldescr&   r   zground-truth)r*   class_labelsr'   )r-   dataset
batch_sizemin	enumerater   r+   r4   r,   r   r#   r
   r/   )
dataloaderclass_label_mapr$   r5   r   r%   r6   r7   data_idxnum_dataloader_batches	batch_idxbatchprediction_resultsprogress_bar_result_iterableimg_idxprediction_resultr3   ground_truth_imager   r   r   plot_pose_validation_results8   sN   







rN   )N)typingr   r   r   r   PILr   	tqdm.autor   ultralytics.engine.resultsr   ultralytics.models.yolo.poser   ultralytics.utils.plottingr   r,   (wandb.integration.ultralytics.bbox_utilsr	   r
   boolr   strr#   Tabler4   intrN   r   r   r   r   <module>   sJ    
	