o
    0j                     @   s   d dl mZmZmZmZmZmZ d dlZddl	m
Z
 ddlmZmZ ddlmZ ddlmZ d	d
lmZ d	dlmZ eeef ZejG dd deZe
dG dd deZdS )    )AnyDictListOptionalTupleUnionN   )pipeline_requires_extra   )	HPIConfigPaddlePredictorOption)	KptResult)	benchmark   )(AutoParallelImageSimpleInferencePipeline)BasePipelinec                       s   e Zd ZdZddddddddedee dee deeeef  d	ee d
e	dee
eeef ef  ddf fddZ	dde
eejf dedeejejf fddZ	dde
eee ejeej f dee defddZ  ZS )_KeypointDetectionPipelinezKeypoint Detection pipelineNFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr   r   r   r   r   r   returnc             	      s   t  jd
||||||d| |d d }	i }
d| _d|	v r*|	d |
d< |	d | _d|	v r4|	d |
d< | j|	fi |
| _|d d }	i }
d|	v rP|	d |
d< d	|	v rZ|	d	 |
d	< | j|	fi |
| _| jj| _dS )a  Initializes the keypoint detection pipeline.

        Args:
            config (Dict): Configuration dictionary containing model and other parameters.
            device (Optional[str], optional): The device to use for prediction. Defaults to `None`.
            engine (Optional[str], optional): Inference engine. Defaults to `None`.
            engine_config (Optional[Dict[str, Any]], optional): Engine-specific config. Defaults to `None`.
            pp_option (Optional[PaddlePredictorOption], optional): Paddle predictor options.
                Defaults to `None`.
            use_hpip (bool, optional): Whether to use HPIP. Defaults to `False`.
            hpi_config (Optional[Union[Dict[str, Any], HPIConfig]], optional):
                HPIP configuration. Defaults to `None`.
        r   
SubModulesObjectDetectionN	thresholdZimgszZKeypointDetectionflipZuse_udp )super__init__det_thresholdZcreate_model	det_model	kpt_modelZ
input_sizekpt_input_size)selfr   r   r   r   r   r   r   kwargsZ	model_cfgZmodel_kwargs	__class__r    x/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/keypoint_detection/pipeline.pyr"   !   s6   
z#_KeypointDetectionPipeline.__init__      ?bboxpaddingc                 C   s   |dd \}}}}t || || gd }| jd | jd  }|| || }	}
|	||
 kr5|	| }
n
|	||
 k r?|
| }	t |	|
g| }||fS )a}  
        Convert bounding box from (x1, y1, x2, y2) to center and scale.

        Args:
            bbox (Union[Number, np.ndarray]): The bounding box coordinates (x1, y1, x2, y2).
            padding (float): The padding factor to adjust the scale of the bounding box.

        Returns:
            Tuple[np.ndarray, np.ndarray]: The center and scale of the bounding box.
        Nr   g      ?r      )nparrayr&   )r'   r-   r.   x1y1Zx2y2centerZaspect_ratiowhscaler    r    r+   _box_xyxy2csZ   s   
z'_KeypointDetectionPipeline._box_xyxy2csinputr#   c              	   k   s    |du r| j n|}| j||dD ]O}|d |d }}||g d}|d D ]4}| |d \}	}
t| ||	|
dd	 }|d |d |d
 |d d	 d |d d	 d d q&t|V  qdS )a  Predicts image classification results for the given input.

        Args:
            input (str | list[str] | np.ndarray | list[np.ndarray]): The input image(s) or path(s) to the images.
            det_threshold (float): The detection threshold. Defaults to `None`.
            **kwargs: Additional keyword arguments that can be passed to the function.

        Returns:
            KptResult: The predicted KeyPoint Detection results.
        N)r   	input_img
input_path)r<   r;   boxesr=   
coordinate)Zimgr5   r8   r   ZscoreZkpts	keypoints	kpt_score)r>   Z	det_scorer?   r@   )r#   r$   r9   listr%   appendr   )r'   r:   r#   r(   Zdet_resZori_imgZimg_pathZsingle_img_resboxr5   r8   Zkpt_resr    r    r+   predictv   s4   	z"_KeypointDetectionPipeline.predict)r,   N)__name__
__module____qualname____doc__r   r   strr   r   boolr   r   r"   Numberr0   Zndarrayfloatr   r9   r   r   rD   __classcell__r    r    r)   r+   r      sR    	:
r   Zcvc                   @   s$   e Zd ZdZedd Zdd ZdS )KeypointDetectionPipelineZhuman_keypoint_detectionc                 C   s   t S rE   )r   )r'   r    r    r+   _pipeline_cls   s   z'KeypointDetectionPipeline._pipeline_clsc                 C   s   |d d  ddS )Nr   r   Z
batch_sizer/   )get)r'   r   r    r    r+   _get_batch_size   s   z)KeypointDetectionPipeline._get_batch_sizeN)rF   rG   rH   entitiespropertyrP   rR   r    r    r    r+   rO      s
    
rO   )typingr   r   r   r   r   r   numpyr0   Z
utils.depsr	   modelsr   r   Z models.keypoint_detection.resultr   Zutils.benchmarkr   Z	_parallelr   baser   intrM   rL   Ztime_methodsr   rO   r    r    r    r+   <module>   s     