o
    ॵi                     @   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   devicekwargs	__class__r   f/home/ubuntu/.local/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_dir	case_nameaction_name	gltf_path	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verticestexture_mapzsave animation succeed!zsave animation failed!)r,   r-   dirnameabspathr/   r   cv2imreadr   r)   r:   existsr   r   )r   	mesh_pathr&   r'   r4   r%   tex_pathmeshtexr<   r7   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_idaction_datasetr'   r4   r2   r;   /r   )	namespacesubset_namesplit_configtestzbody.objz
load mesh:)rN   split	actions_a)r2   endswithrR   r   loadconfig_kwargsr,   r-   r0   r   r   rG   r	   OUTPUT)r   rH   rI   rJ   action_data_idr'   r4   rC   dataset_name	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)   r:   rG   r]   r^   __classcell__r   r   r   r   r      s    "

"*&r   )r,   typingr   r   r@   modelscope.metainfor   &modelscope.models.cv.human3d_animationr   r   r   modelscope.msdatasetsr   modelscope.outputsr	   modelscope.pipelines.baser
   modelscope.pipelines.builderr   modelscope.utils.constantr   modelscope.utils.loggerr   r   register_modulehuman3d_animationr   r   r   r   r   <module>   s    