o
    ॵi5&                     @   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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*dS )    N)AnyDictListUnion)Image)
transforms)	Pipelines)PedestrainAttribute)
OutputKeys)pipeline)InputModelPipelineTensor)	PIPELINES)	LoadImage)	ModelFileTasks)
get_logger)module_namec                       s  e Zd ZdZdef fddZdddZdd	 Zd
ee	eee
jf f 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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 )&PedestrainAttributeRecognitionPipelinea   Pedestrian attribute recognition Pipeline.

    Example:

    ```python
    >>> from modelscope.pipelines import pipeline
    >>> from modelscope.utils.constant import Tasks

    >>> model_id = 'damo/cv_resnet50_pedestrian-attribute-recognition_image'
    >>> handle = pipeline(Tasks.pedestrian_attribute_recognition, model=model_id)
    >>> output = handle('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/keypoints_detect/000000442836.jpg')
    ```
    modelc                    s   t  jd
d|i| 	 tdd| _tjt|tj	| j
d}| j| | j| j
| _| j  ddg| _d| _d| _ttj| jd	| _d S )Nr   '   )num_classes)map_location   i  r   z(damo/cv_tinynas_human-detection_damoyolo)r    )super__init__r	   attribute_modeltorchloadospjoinr   TORCH_MODEL_FILEdeviceload_state_dicttoeval
input_sizebox_enlarge_ratiohuman_detect_model_idr   r    domain_specific_object_detectionhuman_detector)selfr   kwargsstate	__class__r   u/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/pedestrian_attribute_recognition_pipeline.pyr   -   s"   

z/PedestrainAttributeRecognitionPipeline.__init__      ?c                 C   s|  |d dd }|d dd }|d dd }|d dd }|d dd }|d dd }|d dd }	|d dd	 }
|d d	d
 }|d d
d }|d dd }|d dd }||krfdnd}t |}t |}||krxdnd}||krdnd}||krdnd}|	|krdnd}|
|krdnd}t |}t |}t |}t |}||||||||||||g}|S )Nr               	   
                  r   )npargmax)r.   outputsthresgenderageorienthatglasshand_bagshoulder_bag	back_pack
upper_wear
lower_wearupper_colorlower_color	lb_genderlb_age	lb_orientlb_hatlb_glasslb_hand_baglb_shoulder_baglb_back_packlb_upper_wearlb_lower_wearlb_upper_colorlb_lower_colorlabelsr   r   r3   
get_labelsD   s:   





z1PedestrainAttributeRecognitionPipeline.get_labelsc                 C   s   ddgg dg dddgddgddgddgddgddgg d	g d
g d
g}ddgg dg dddgddgddgddgddgddgg dg dg dg}g }g }t |D ]\}}||| |  ||| |  qT||fS )NFemaleMale)	AgeOver60zAge18-60	AgeLess18)FrontSideBackYesNoShortSleeve
LongSleeve)TrousersShortszSkirt&Dress)blackgreybluegreenwhitepurpleredbrownyellowpinkorangeu   女u   男)u   大于60岁u   18-60岁之间u   小于18岁)u   正向u   侧向u   背面u	   戴帽子u   不戴帽子u	   戴眼镜u   不戴眼镜u   有手提包u   无手提包u   有肩挎包u   无肩挎包u	   有背包u	   无背包u   短袖u   长袖)u   长裤u   短裤u   裙子)u   黑u   灰u   蓝u   绿u   白u   紫u   红u   棕u   黄u   粉u   橙)	enumerateappend)r.   r\   notes_ennotes_cnnotes_labels_ennotes_labels_cnidxlbr   r   r3   labels_transformf   s@   z7PedestrainAttributeRecognitionPipeline.labels_transforminputsc           
      K   s   g }g }t t|d D ]7}| |d |    }| |\}}t|d | d dd 	dd}	|
| |
|	  q||fS )Nr   r5   	human_boxr6      )rangelenr]   detachcpunumpyr~   r@   arrayreshaperw   tolist)
r.   r   r/   output_labelsoutput_boxesirB   label_enlabel_cnboxr   r   r3   get_results   s   &
z2PedestrainAttributeRecognitionPipeline.get_resultsinputreturnc           	      C   sB  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d }| jd }t|tj}t|||ftj}t|d }g d}g d}|| | }tjdd||gtjd	}|d
}||dd d f< t
|S )Nr      r   r5   g     o@)g
ףp=
?gv/?gCl?)gZd;O?gy&1?g?dtype)r   r   r5   )
isinstancestrcv2imreadr@   ndarrayr   shapecvtColorCOLOR_GRAY2BGRr    r   r   r   r)   COLOR_BGR2RGBresizeINTER_LINEARfloat32zeros	transpose
from_numpy)	r.   r   imagew_newh_new
img_resizemeanstd
input_datar   r   r3   image_transform   s*   
$"



z6PedestrainAttributeRecognitionPipeline.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 )Nr5   r   )r   r   g        )r   r5   )r5   r   )r5   r5   )r   r*   minmaxint)	r.   r   r   heightwidth_whcropped_imager   r   r3   
crop_image   s   (( z1PedestrainAttributeRecognitionPipeline.crop_imagec           
      C   s   |d }|d }g }g }t t|D ]8}t|| dd dd}|d  |d 7  < | | |}i }	| |}|| ||	d< ||	 q||gS )Nr   r5   r6   r   r   )	r   r   r@   r   r   r   cloner   rw   )
r.   r   bboxesr   lst_human_imageslst_metar   r   human_imagemetar   r   r3   process_image   s   

z4PedestrainAttributeRecognitionPipeline.process_imagec                 C   s8   |  |}t|}|d d d d g df }||dS )N)r   r5   r   )r   output)r-   r   convert_to_ndarray)r.   r   r   r   r   r   r3   
preprocess   s   


z1PedestrainAttributeRecognitionPipeline.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 | ||g\}
}g }|
D ]}| j|| j}t|}|| qh||gS )	Nr   r   r   r   r5   r   r   z cannot detect human in the image)r@   r   r
   SCORESr   r   BOXESrv   rw   r   loggererrorr   r   forwardr'   r%   r    sigmoid)r.   r   input_imager   r   scoresboxesidr   box_tmphuman_imagesmetasrB   r   r   r   r3   r      s&   0

z.PedestrainAttributeRecognitionPipeline.forwardc                 K   s   |d d u s|d d u rt jg t jg iS | |\}}g }|D ]}||d d |d d |d d |d d g qt j|t j|iS )Nr   r5   )r
   r   LABELSr   rw   )r.   r   r/   r   r\   result_boxesr   r   r   r3   postprocess  s   4z2PedestrainAttributeRecognitionPipeline.postprocess)r4   )__name__
__module____qualname____doc__r   r   r]   r~   r   r   r@   r   r   r   r   r   r   r   r   r   r   r   __classcell__r   r   r1   r3   r      s    
" -"$$r   )+os.pathpathr"   typingr   r   r   r   r   jsonr   r@   r    PILr   torchvisionr   modelscope.metainfor   ;modelscope.models.cv.pedestrian_attribute_recognition.modelr	   modelscope.outputsr
   modelscope.pipelinesr   modelscope.pipelines.baser   r   r   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   register_module pedestrian_attribute_recognitionr   r   r   r   r3   <module>   s.   