o
    *j%                     @   s  d dl mZ d dlmZmZmZmZ d dlZd dl	Z	d dl
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mZ d d	lmZ 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%m&Z& d dl'm(Z( e( Z)e!j*e&j+ej+dG dd deZ,G dd dZ-dS )    N)AnyDictListUnion)Image)
transforms)	Pipelines)PoseHighResolutionNetV2)cfg_128x128_15)
OutputKeys)pipeline)InputModelPipelineTensor)	PIPELINES)	LoadImage)	ModelFileTasks)
get_logger)module_namec                       s   e Zd Zdef fddZdedeeeee	j
f f fddZdedeeeee	j
f f fdd	Zdeeeee	j
f f defd
dZ  ZS )Body2DKeypointsPipelinemodelc                    s\   t  jdd|i| ttj rdd nd}t||| _d| _t	t
j| jd| _d S )Nr   zcuda:r   cpuz damo/cv_resnet18_human-detection)r    )super__init__torchdevicecudaZis_availableKeypointsDetectionkeypoint_modelZhuman_detect_model_idr   r   Zhuman_detectionhuman_detector)selfr   kwargsr   	__class__r   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/body_2d_keypoints_pipeline.pyr       s   z Body2DKeypointsPipeline.__init__inputreturnc                 C   s8   |  |}t|}|d d d d g df }||dS )N)      r   )imageoutput)r"   r   Zconvert_to_ndarray)r#   r(   r-   r,   r   r   r'   
preprocess*   s   


z"Body2DKeypointsPipeline.preprocessc                 C   s   |d }|d }g }t j|tj  t jd}t j|tj  t jd}t|D ]#\}}|d |d |d |d  |d |d  || dg}	||	 q(t	|dkr[t
d d d gS | j||g\}
}| j|
}||gS )	Nr,   r-   Zdtyper   r+   r*      z cannot detect human in the image)nparrayr   SCORESr   float32BOXES	enumerateappendlenloggererrorr!   r.   forward)r#   r(   Zinput_imager-   bboxesscoresboxesidboxZbox_tmpZhuman_imagesZmetasoutputsr   r   r'   r;   2   s"   0
zBody2DKeypointsPipeline.forwardc                 K   s   |d d u s|d d u rt jg t jg t jg iS | j|\}}}g }|D ]}||d d |d d |d d |d d g q$t j|t j|t j|iS )Nr   r+   )r   r5   Z	KEYPOINTSr3   r!   postprocessr7   )r#   r(   r$   Zposesr=   r>   Zresult_boxesr@   r   r   r'   rB   G   s   4z#Body2DKeypointsPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r   r1   ndarrayr.   r;   rB   __classcell__r   r   r%   r'   r      s    $
$r   c                   @   s   e Zd ZdedefddZdd Zdd Zd	ed
efddZdd Z	dd Z
deeeeejf f fddZddgfddZd	ed
eeef fddZdd Zd	eeef d
eeef fddZdS )r    r   r   c                 K   s   || _ || _t}t|| _tjt| j t	j
|dd}| jj|dd | j|| _| j  |d d | _|d d | _|d d	 | _|d d
 | _d| _d S )NT)Zmap_locationZweights_onlyF)strictZMODELZ
IMAGE_SIZEZDATASETZ
PARENT_IDSZLEFT_IDSZ	RIGHT_IDSg?)r   r   r
   r	   key_points_modelr   loadospjoinr   ZTORCH_MODEL_FILEZload_state_dicttoeval
input_sizeZlst_parent_idsZlst_left_idsZlst_right_idsbox_enlarge_ratio)r#   r   r   r$   cfgZpretrained_state_dictr   r   r'   r   ]   s&   


zKeypointsDetection.__init__c                 C   
   | j  S N)rJ   trainr#   r   r   r'   rU   q      
zKeypointsDetection.trainc                 C   rS   rT   )rJ   rO   rV   r   r   r'   rO   t   rW   zKeypointsDetection.evalr(   r)   c                 C   s@   t   | j|| jW  d    S 1 sw   Y  d S rT   )r   Zno_gradrJ   r;   rN   r   )r#   r(   r   r   r'   r;   w   s   
$zKeypointsDetection.forwardc                 C   sL  |j \}}}g }g }t|D ]}||d d d d f }t|t|k}	|t| |	d d }
|	d d }|j \}}|
dkr|
|d kr|dkr||d kr|||
d f |||
d f  }||d |
f ||d |
f  }d}d}|dk r|d}|dkrd}|dk rd}|dkrd}|
|d  }
||d  }||
|g q||fS )Nr+   r   r*   g      ?)shaperanger1   wheremaxr7   )r#   ZheatmapsZpts_numheightwidthptsr=   iZheatmapptxyhwZx_diffZy_diffZx_signZy_signr   r   r'   get_pts{   s6   
(  zKeypointsDetection.get_ptsc                 C   s   g }|d }|d }t |}t|D ]1}	d}
t||	 d |
 |d  |d  }t||	 d |
 |d  |d  }||g}|| q|S )Nso   r   r+   )r8   rZ   intr7   )r#   metar_   Zlt_xZlt_yZpts_newrg   rh   sizer`   ratiorb   rc   ra   r   r   r'   pts_transform   s   $$z KeypointsDetection.pts_transforminputsc           
      K   s   g }g }g }t |d jd D ]N}| |d |    \}}| |d | |dd}t|d | d dd 	dd}	t||	d  }|
|  |
| |
|	  q|||fS )Nr   r+   	human_boxri   r*   )rZ   rY   rf   detachr   numpyrn   r1   r2   reshaper7   tolist)
r#   ro   r$   Zoutput_posesZoutput_scoresZoutput_boxesr`   rA   r=   r@   r   r   r'   rB      s   &

zKeypointsDetection.postprocessr   c              	   C   s@  t j| jd | jd dft jd}|j\}}}| jd |d d  }| jd |d d  }t|| }	t|| }
t|	|
}t|| }t|| }t	|||ftj
}| jd d }| jd d }||||d  ||d  | ||d  ||d  | d d f< |t ||gt ||gt ||d  ||d  gfS )Nr+   r   r0   r/   r*   )r1   zerosrP   Zuint8rY   floatminrj   cv2resizeZINTER_LINEARr2   )r#   r(   marginZpad_imgrd   re   chh_neww_news0s1rg   Zimg_newZcxcyr   r   r'   image_crop_resize   s*   
 z$KeypointsDetection.image_crop_resizec                 C   s\  t |trt|dd d d d ddf }n<t |tjr<t|jdkr,t|tj	}n|}|d d d d ddf }nt |t
jrS|  d d d d ddf }|j\}}}| jd }| jd }t|tj}| |\}}	}
}t|d }g d}g d}|| | }tjdd||gtjd	}|d
}||dd d f< |	|
|d}t
||gS )NrX   r   r0   r*   r+   g     o@)g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?r/   )r*   r   r+   )crg   rh   )
isinstancerF   rx   Zimreadr1   rG   r8   rY   ZcvtColorZCOLOR_GRAY2BGRr   r   r   rr   rP   ZCOLOR_BGR2RGBr   r4   ru   Z	transposeZ
from_numpy)r#   r(   r,   re   rd   _r}   r|   Z
img_resizer   rg   rh   meanZstdZ
input_datark   r   r   r'   image_transform   s.   
$"


z"KeypointsDetection.image_transformc           	      C   s  |j \}}}|d |d  \}}|dd d f  || j || j f8  < |dd d f  || j || j f7  < tt|d d||d< tt|d d||d< tt|d d||d< tt|d d||d< |t|d d t|d d t|d d t|d d f }|S )Nr+   r   )r   r   g        )r   r+   )r+   r   )r+   r+   )rY   rQ   rw   r\   rj   )	r#   r,   r@   r]   r^   r   re   rd   Zcropped_imager   r   r'   
crop_image   s   (( zKeypointsDetection.crop_imagec           
      C   s   |d }|d }g }g }t t|D ]8}t|| dd dd}|d  |d 7  < | | |}| |\}}	|| ||	d< ||	 qt	j
|dd|gS )Nr   r+   ri   r*   rp   )dim)rZ   r8   r1   r2   rs   r   cloner   r7   r   cat)
r#   r(   r<   r,   Zlst_human_imagesZlst_metar`   r@   Zhuman_imagerk   r   r   r'   r.      s   
zKeypointsDetection.preprocessN)rC   rD   rE   rF   r   rU   rO   r   r;   rf   rn   r   r1   rG   rB   r   r   r   r   r   r.   r   r   r   r'   r    [   s     &r    ).Zos.pathpathrL   typingr   r   r   r   rx   jsonrr   r1   r   ZPILr   Ztorchvisionr   Zmodelscope.metainfor   Z/modelscope.models.cv.body_2d_keypoints.hrnet_v2r	   Z*modelscope.models.cv.body_2d_keypoints.w48r
   Zmodelscope.outputsr   Zmodelscope.pipelinesr   Zmodelscope.pipelines.baser   r   r   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.loggerr   r9   Zregister_moduleZbody_2d_keypointsr   r    r   r   r   r'   <module>   s0   =