o
    *j                     @   s   d dl mZ d dlZd dlmZmZ d dlZd dl	Z	d dl
mZ d dlmZmZ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 d d
lmZ d dl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)	Pipelines)ClassifierFreeSampleModelcreate_modelload_model_wo_clip)
OutputKeys)InputPipeline)	PIPELINES)Config)	ModelFileTasks)recover_from_ric)plot_3d_motion)
get_logger)module_namec                       s   e Zd Zdef fddZdedeeef fddZdeeef deeef fdd	Z	d
eeef deeef fddZ
  ZS )MDMMotionGenerationmodelc                    s&  t  jdd|i| t| jtj}td|  t| jtj	}td|  t
t| jd| _t
t| jd| _t|| _| jdt| jdi | j| d| _d	| _d
| _t| j\| _| _tj|ddd}t| j| t| j| _| j| j | j  td dS )z
        use `model` to create motion generation pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   zloading model from zloading config from zMean.npyzStd.npyZsmpl_data_pathsmpl      x   cpuT)Zmap_locationZweights_onlyzload model doneN )super__init__ospjoinr   r   ZTORCH_MODEL_FILEloggerinfoZCONFIGURATIONnploadmeanstdr   	from_filecfgupdaten_jointsfpsn_framesr   mdm	diffusiontorchr   r   todeviceeval)selfr   kwargsZ
model_pathZconfig_pathZ
state_dict	__class__r   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/motion_generation_pipeline.pyr      s,   
zMDMMotionGeneration.__init__inputreturnc                 C   s.   t |tr|}n	tdt| d|i}|S )Nz input should be a str,  but got 
input_text)
isinstancestr	TypeErrortype)r1   r6   r8   resultr   r   r5   
preprocess:   s   
zMDMMotionGeneration.preprocessc                 C   s0  |d g}dt ddd| jdkt | jgd |t jd| jdd di}| jj}|| jd| jj| jj	| jfd|dd d	d d dd

}|
 dddd| j | j  }t|| j}|jdg|jdd  R  dddd}| jj|d dd	d	dd	d dd dd}|
  }|d ddd}tj|d|d i}|S )Nr8   y   r   )r/   g      @)masklengthstokenstextscaleFT)Zclip_denoisedmodel_kwargsZskip_timestepsZ
init_imageprogressZ
dump_stepsnoiseZconst_noise      Zxyzr   )xrA   Zpose_repglobtranslationZ
jointstypeZ
vertstransZbetasbetaZglob_rotZget_rotations_backrD   )r-   Zonesr*   Ztensorr/   r,   Zp_sample_loopr+   ZnjointsZnfeatsr   Zpermuter$   r#   floatr   r(   viewshapeZrot2xyznumpyZ	transposer   	KEYPOINTS)r1   r6   ZtextsrF   Z	sample_fnsampleZmotionoutr   r   r5   forwardC   s\   
	(zMDMMotionGeneration.forwardinputsc                 K   st   | dtjddj}g dg dg dg dg dg}|d ur0t|||tj |d	d
dd |tj	|i |S )NZoutput_videoz.mp4)suffix)r   rI            )r   r@         
   )r   rJ      	         )ra               )ra            r   rD   Zhumanmlr   )Zdatasetr)   )
gettempfileNamedTemporaryFilenamer   r   rT   popr'   ZOUTPUT_VIDEO)r1   rX   r2   Zoutput_video_pathZkinematic_chainr   r   r5   postprocessq   s&   zMDMMotionGeneration.postprocess)__name__
__module____qualname__r:   r   r	   r   r   r>   rW   rp   __classcell__r   r   r3   r5   r      s
    "	*.r   )'Zos.pathpathr   rl   typingr   r   rS   r!   r-   Zmodelscope.metainfor   Z&modelscope.models.cv.motion_generationr   r   r   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   Zmodelscope.pipelines.builderr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Z/modelscope.utils.cv.motion_utils.motion_processr   Z,modelscope.utils.cv.motion_utils.plot_scriptr   Zmodelscope.utils.loggerr   r   Zregister_moduleZmotion_generationZmotion_generattionr   r   r   r   r5   <module>   s(   