o
    0jM                     @   s   d dl mZmZ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mZ d
dlmZ G dd de
ZG dd deZdS )    )AnyListOptionalSequenceN   )logging   )ImageBatchSampler   ToBatch)DetRunnerPredictor   )KptPostProcessTopDownAffine	KptResultc                   @   s   e Zd Zg Zdd ZdS )KptBatchSamplerc                 c   s    t |ts	|g}g }|D ]R}t |tjtfr)|| t|| jkr(|V  g }qt |trV|	dr8| 
|n|}| |}|D ]}|| t|| jkrT|V  g }qAqtd| d qt|dkrk|V  d S d S )NhttpzbNot supported input data type! Only `numpy.ndarray` and `str` are supported! So has been ignored: .r   )
isinstancelistnpZndarraydictappendlenZ
batch_sizestr
startswithZ_download_from_urlZ_get_files_listr   warning)selfZinputsbatchinput	file_path	file_list r$   v/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/models/keypoint_detection/predictor.pysample   s<   






zKptBatchSampler.sampleN)__name__
__module____qualname__Z
PDF_SUFFIXr&   r$   r$   r$   r%   r      s    r   c                	       s   e Zd ZdZddgddgddgdd	gd
dgddgddgddggZddddedee f fddZdd Zdd Z	de
e dee fdd Zd!d" Zd#ee fd$d%Zed&d-d'd(Zd)d* Zd+d, Z  ZS ).KptRunnerPredictorzKeypoint detection predictor.r   r
   r   r               	   
                     FN)flipuse_udpr7   r8   c                   sj   | _ | _t j|i |  jD ]}t|tr|j _ nqt fdddD r0d _	dS d _	dS )zKeypoint Predictor

        Args:
            flip (bool): Whether to do flipping test. Default value is ``False``.
            use_udp (Optional[bool]): Whether to use unbiased data processing. Default value is ``None``.

        c                    s   g | ]}| j v qS r$   )Z
model_name).0namer   r$   r%   
<listcomp>a   s    z/KptRunnerPredictor.__init__.<locals>.<listcomp>)zPP-TinyPoseTFN)
r7   r8   super__init__pre_opsr   r   
input_sizeanyshift_heatmap)r   r7   r8   argskwargsop	__class__r;   r%   r>   L   s   



zKptRunnerPredictor.__init__c                 C      t  S N)r   r;   r$   r$   r%   _build_batch_samplerf      z'KptRunnerPredictor._build_batch_samplerc                 C   s   t S rI   r   r;   r$   r$   r%   _get_result_classi   s   z$KptRunnerPredictor._get_result_classpredreturnc                 C   s   dd t | D S )z;Transform batch outputs into a list of single image output.c                 S   s   g | ]}|d  |d dqS )r   r   )ZheatmapZmasksr$   )r9   resr$   r$   r%   r<   o   s    z5KptRunnerPredictor._format_output.<locals>.<listcomp>)zip)r   rM   r$   r$   r%   _format_outputl   s   z!KptRunnerPredictor._format_outputc                 C   s   |j dks	J d|d d d d d d d d df }|D ]B}|d d |d d d d d f  }|d d |d d d d d f |d d |d d d d d f< ||d d |d d d d d f< q|S )Nr   z@output_flipped should be [batch_size, num_joints, height, width]r   r   )ndimcopy)r   output_flippedZmatched_partspairtmpr$   r$   r%   	flip_backw   s   "$<"zKptRunnerPredictor.flip_back
batch_datac           
      C   s
  |}| j dd D ]}||}q	| j d dd |D }| |}| jrjtj|d dd|d< | |}| |d | j}| jr`| ddddddddf |ddddddddf< |d | d	 |d< | |}| 	||}	d
d |D dd |D |	dS )a  
        Process a batch of data through the preprocessing, inference, and postprocessing.

        Args:
            batch_data (List[Union[str, np.ndarray], ...]): A batch of input data (e.g., image file paths).

        Returns:
            dict: A dictionary containing the input path, raw image, class IDs, scores, and label names
                for every instance of the batch. Keys include 'input_path', 'input_img', 'class_ids', 'scores', and 'label_names'.
        NrR   c                 S      g | ]}|d  qS )Zimgr$   r9   datar$   r$   r%   r<          z.KptRunnerPredictor.process.<locals>.<listcomp>r   r   )Zaxisr   g      ?c                 S   s   g | ]}| d dqS )Zimg_pathN)getr[   r$   r$   r%   r<      s    c                 S   rZ   )Zori_imgr$   r[   r$   r$   r%   r<      r]   )Z
input_pathZ	input_imgZkpts)
r?   runnerr7   r   rX   	flip_permrB   rT   rQ   Zpost_op)
r   rY   ZdatasZpre_opZbatch_inputsZbatch_predsZpreds_flippedrU   Z
preds_listZ	keypointsr$   r$   r%   process   s$   


@
zKptRunnerPredictor.processZTopDownEvalAffinec                 C   s    t || jd u r|dS | jdS )N)r@   r8   )r   r8   )r   Z	trainsizer8   r$   r$   r%   build_topdown_affine   s   z'KptRunnerPredictor.build_topdown_affinec                 C   rH   rI   r   r;   r$   r$   r%   build_to_batch   rK   z!KptRunnerPredictor.build_to_batchc                 C   s
   t ddS )NT)Zuse_dark)r   r;   r$   r$   r%   build_postprocess   s   
z$KptRunnerPredictor.build_postprocess)F)r'   r(   r)   __doc__r`   boolr   r>   rJ   rL   r   r   r   r   rQ   rX   ra   r   registerrb   rc   rd   __classcell__r$   r$   rF   r%   r*   >   s6    ,r*   )typingr   r   r   r   numpyr   utilsr   Zcommon.batch_samplerr	   commonr   Zobject_detectionr   Z
processorsr   r   resultr   r   r*   r$   r$   r$   r%   <module>   s   #