o
    0j                     @   s   d dl mZmZmZ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 d	d
lmZ d	dlmZmZmZ ddlmZ ejedG dd deZdS )    )AnyDictOptionalUnion   )pipeline_requires_extra   )ImageBatchSampler)	ReadImage)	HPIConfigPaddlePredictorOption)	benchmark   )BasePipeline)CropByBoxesFaissBuilderFaissIndexer   )ShiTuResultZcvc                       s   e Zd 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dZdd Zdd Z		dddZdd Zdd Z  ZS ) ShiTuV2PipelinezShiTuV2 Pipelinez
PP-ShiTuV2NFdeviceengineengine_config	pp_optionuse_hpip
hpi_configconfigr   r   r   r   r   r   returnc          
   	      s   t  jd||||||d| |dd|dd|dd|ddf\| _| _| _| _|d	d}	td
d| _| 	|d d | _
| 	|d d | _t | _|	rYt|	dnd| _t| j
jjd| _dS )a  Initializes the ShiTuV2 pipeline.

        Args:
            config (Dict): Configuration dictionary containing various settings.
            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   rec_topk   rec_thresholdg      ?hamming_radiusNdet_thresholdindexZBGR)formatZ
SubModulesZ	DetectionZRecognition)r$   )
batch_size )super__init__get_topk_rec_threshold_hamming_radius_det_thresholdr
   
img_readerZcreate_model	det_model	rec_modelr   crop_by_boxesr   indexerr	   batch_samplerr&   )
selfr   r   r   r   r   r   r   kwargsr$   	__class__r'   q/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/pp_shitu_v2/pipeline.pyr)   "   s0   



zShiTuV2Pipeline.__init__c              
   k   s    |d ur	t |n| j}|sJ dd | D }|d| j}|d| j}|d| j}|d| j}t| 	|D ]3\}	}
| 
|
j}t| j||d}t|
j||D ]\}}}| ||||||}| ||||V  qVq<d S )Nc                 S   s   i | ]\}}|d ur||qS Nr'   ).0kvr'   r'   r9   
<dictcomp>Y   s    z+ShiTuV2Pipeline.predict.<locals>.<dictcomp>r   r!   r"   r#   )	threshold)r   r3   itemsr*   r+   r,   r-   r.   	enumerater4   r/   Z	instanceslistr0   zipget_rec_resultget_final_result)r5   inputr$   r6   r3   topkr!   r"   r#   Zimg_idZ
batch_dataZraw_imgsZall_det_res
input_dataraw_imgdet_resrec_resr'   r'   r9   predictV   s(   zShiTuV2Pipeline.predictc              	   C   s   t |d dkr!|jd d \}}|d ddddd||gd t| ||d }	dd |	D }
t| |
}|dd |D |||d	}g g d
}|D ]}|d |d  |d |d  qM|S )Nboxesr   r   Zfull_img)Zcls_idlabelscore
coordinatec                 S      g | ]}|d  qS )imgr'   )r;   rR   r'   r'   r9   
<listcomp>w       z2ShiTuV2Pipeline.get_rec_result.<locals>.<listcomp>c                 S   rQ   )featurer'   )r;   rK   r'   r'   r9   rS   z   rT   )Zscore_thresr"   rG   )rN   rO   rN   rO   )lenshapeappendrB   r2   r1   )r5   rI   rJ   r3   r!   r"   rG   whZsubs_of_imgZimg_listZall_rec_resoutputresr'   r'   r9   rD   i   s.   

zShiTuV2Pipeline.get_rec_resultc           
      C   s   ||g d}t |d D ]2\}}|d | }|d ur|nd g}|d | }	|	d ur+|	nd g}	|d |	||d |d d qt|S )N)Z
input_pathZ	input_imgrM   rM   rO   rN   rP   )labels
rec_scoresZ	det_scorerP   )rA   rX   r   )
r5   rH   rI   rJ   rK   Zsingle_img_resiobjr^   r]   r'   r'   r9   rE      s   z ShiTuV2Pipeline.get_final_resultIPHNSW32c                 K   s   t j||| jj||dS )N)metric_type
index_type)r   buildr1   rL   )r5   gallery_imgsgallery_labelrc   rd   r6   r'   r'   r9   build_index   s   zShiTuV2Pipeline.build_indexc                 C   s   t ||S r:   )r   remove)r5   Z
remove_idsr$   r'   r'   r9   remove_index   s   zShiTuV2Pipeline.remove_indexc                 C   s   t ||| jj|S r:   )r   rX   r1   rL   )r5   rf   rg   r$   r'   r'   r9   append_index   s   zShiTuV2Pipeline.append_indexr:   )ra   rb   )__name__
__module____qualname____doc__entitiesr   r   strr   r   boolr   r   r)   rL   rD   rE   rh   rj   rk   __classcell__r'   r'   r7   r9   r      sD    	
4
r   N)typingr   r   r   r   Z
utils.depsr   Zcommon.batch_samplerr	   Zcommon.readerr
   modelsr   r   Zutils.benchmarkr   baser   
componentsr   r   r   resultr   Ztime_methodsr   r'   r'   r'   r9   <module>   s   