o
    *j                     @   s   d dl Z d dlmZm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 d dl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)gen_skeleton_bvhread_obj	write_obj)	MsDataset)
OutputKeys)Pipeline)	PIPELINES)Tasks)
get_logger)module_namec                       s   e Zd ZdZd fdd	Zdeeef deeef fddZd	d
 Z	dddZ
d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 )Human3DAnimationPipelinead   Human3D library render pipeline
    Example:

    ```python
    >>> from modelscope.pipelines import pipeline
    >>> human3d = pipeline(Tasks.human3d_animation,
                'damo/cv_3d-human-animation')
    >>> human3d({
            'dataset_id': 'damo/3DHuman_synthetic_dataset', # dataset id (str)
            'case_id': '3f2a7538253e42a8', # case id (str)
            'action_dataset': 'damo/3DHuman_action_dataset', # action data id
            'action': 'ArmsHipHopDance' # action name or action file path (str)
            'save_dir': 'output' # save directory (str)
        })
    >>> #
    ```
    gpuc                    s.   t  jdd|i| || _td| j dS )z
        use model to create a image sky change pipeline for image editing
        Args:
            model (str or Model): model_id on modelscope hub
            device (str): only support gpu
        modelz
model_dir:N )super__init__	model_dirloggerinfo)selfr   Zdevicekwargs	__class__r   s/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/human3d_animation_pipeline.pyr   )   s   z!Human3DAnimationPipeline.__init__inputsreturnc                 C      |S Nr   r   r   r   r   r   
preprocess4      z#Human3DAnimationPipeline.preprocessc                 C   s,   || _ || _|| _t| j| j| j | j}|S r    )case_dir
action_diractionr   r   )r   r$   r%   r&   statusr   r   r   gen_skeleton7   s   
z%Human3DAnimationPipeline.gen_skeletonNc              	   C   s   t j| j}t j| jdd}|d u r#t j| jd||f }nt j|dd t j|d||f }t j| jd}| j	 d| d| j d	| d
| j 	}t 
| |S )Nz.npy z	%s-%s.glbT)exist_okzskinning.pyz -b -P z  -- --input z --gltf_path z
 --action )ospathbasenamer$   r&   replacejoinmakedirsr   blendersystem)r   save_dirZ	case_nameaction_name	gltf_pathZ	exec_pathcmdr   r   r   gen_weights?   s$   

z$Human3DAnimationPipeline.gen_weightsc                 C   s   t jt j|}|dd}t|}t|}|d }	|	|d< ||d< t|| | 	||| | 
|}
t j|
rCtd |
S td |
S )N.objz.pngverticesZtexture_mapzsave animation succeed!zsave animation failed!)r+   r,   dirnameabspathr.   r   cv2Zimreadr   r(   r7   existsr   r   )r   	mesh_pathr%   r&   r3   r$   Ztex_pathZmeshtexr9   r5   r   r   r   animateP   s   




z Human3DAnimationPipeline.animateinputc                 C   s  |d }|d }|d }|d }d|v r|d }nd }d|v r%|d | _ nd| _ |dr0|}n)|dd	 }|dd
 }	tj||	|djd d }
tj|
|}tj|d}t	
d| |dd	 }|dd
 }	tj||	ddjd d }tj|d}| ||||}tj|iS )N
dataset_idcase_idZaction_datasetr&   r3   r1   r8   /r   )	namespaceZsubset_nameZsplit_configtestzbody.objz
load mesh:)rF   splitZ	actions_a)r1   endswithrH   r   loadZconfig_kwargsr+   r,   r/   r   r   r@   r	   ZOUTPUT)r   rA   rB   rC   Zaction_data_idr&   r3   r>   Zdataset_nameZ	user_namedata_dirr$   r%   outputr   r   r   forwardb   sL   


z Human3DAnimationPipeline.forwardc                 C   r   r    r   r!   r   r   r   postprocess   r#   z$Human3DAnimationPipeline.postprocess)r   r    )__name__
__module____qualname____doc__r   r   strr   r"   r(   r7   r@   rM   rN   __classcell__r   r   r   r   r      s    "

"*&r   )r+   typingr   r   r<   Zmodelscope.metainfor   Z&modelscope.models.cv.human3d_animationr   r   r   Zmodelscope.msdatasetsr   Zmodelscope.outputsr	   Zmodelscope.pipelines.baser
   Zmodelscope.pipelines.builderr   Zmodelscope.utils.constantr   Zmodelscope.utils.loggerr   r   Zregister_moduleZhuman3d_animationr   r   r   r   r   <module>   s    