o
    ॵig                     @   s   d dl mZmZmZ d dlZd dl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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 )    )AnyDictUnionN)PlyData
PlyElement)	Pipelines)
OutputKeys)InputModelPipeline)	PIPELINES)	LoadImage)Tasks)depth_to_color)
get_logger)module_namec                       s   e Zd Zdef fddZ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d Zdeeef deeef fddZ  ZS )%PointCloudSceneFlowEstimationPipelinemodelc                    s$   t  jdd|i| td dS )z
        use `model` to create a image depth estimation pipeline for prediction
        Args:
            model: model id on modelscope hub.
        r   z3pointcloud scenflow estimation model, pipeline initN )super__init__loggerinfo)selfr   kwargs	__class__r   t/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/pointcloud_sceneflow_estimation_pipeline.pyr      s   z.PointCloudSceneFlowEstimationPipeline.__init__c                 C   s,   |j dks	J d|jd dksJ dd S )N   zpcd ndim must equal to 2      zpcd.shape[1] must equal to 3)ndimshape)r   pcdr   r   r   check_input_pcd#   s   z5PointCloudSceneFlowEstimationPipeline.check_input_pcdinputreturnc           	      C   s   t |ts	J dt |d trt |d tsJ d|\}}td| d|  t|}t|}| | | | t	|
 d }t	|
 d }||||d}|S )Nzonly support tuple inputr   r   z&only support tuple input with str typezinput pcd file:z,  
  )pcd1pcd2pcd1_oripcd2_ori)
isinstancetuplestrr   r   nploadr$   torch
from_numpyfloat	unsqueezecuda)	r   r%   	pcd1_file	pcd2_filer'   r(   
pcd1_torch
pcd2_torchdatar   r   r   
preprocess'   s(   



z0PointCloudSceneFlowEstimationPipeline.preprocessc                 C   s4   i }| j |}||d< |d |d< |d |d< |S )Noutputr)   r*   )r   	inference)r   r%   resultsr;   r   r   r   forward>   s   z-PointCloudSceneFlowEstimationPipeline.forwardc                 C   s  t j||gdd}t jg dgt jd}t jg dgt jd}t ||jd df}t ||jd df}t j||gdd}t jdd |D g d	d}t jd
d |D g dd}t t||jj	|jj	 }|jj
D ]}|| ||< ql|jj
D ]}|| ||< qyt|d}	t|	g}
|
S )Nr   )axis)   r   r   )dtype)r   r@   r   r   c                 S      g | ]}t |qS r   r,   .0vr   r   r   
<listcomp>N       zGPointCloudSceneFlowEstimationPipeline.save_ply_data.<locals>.<listcomp>))xf4)yrJ   )zrJ   c                 S   rB   r   rC   rD   r   r   r   rG   P   rH   ))redu1)greenrN   )bluerN   vertex)r.   concatenatearrayuint8tiler"   emptylenrA   descrnamesr   describer   )r   r'   r(   vertexscolor1color2vertex_colors
vertex_allpropelply_datar   r   r   save_ply_dataF   s,   
z3PointCloudSceneFlowEstimationPipeline.save_ply_datainputsc              	   C   s   | j |}|tj }|d }|d }t|tjr|  }t|tjr+|  }t|tjr7|  }tj|tj	| 
||tj| 
|| |i}|S )Nr)   r*   )r   postprocessr   OUTPUTr+   r0   TensorcpunumpyPCD12rc   PCD12_ALIGN)r   rd   r=   flowr'   r(   outputsr   r   r   re   `   s   
z1PointCloudSceneFlowEstimationPipeline.postprocess)__name__
__module____qualname__r-   r   r$   r	   r   r   r:   r>   rc   re   __classcell__r   r   r   r   r      s    
"*r   ) typingr   r   r   ri   r.   r0   plyfiler   r   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser	   r
   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.cv.image_utilsr   modelscope.utils.loggerr   r   register_modulepointcloud_sceneflow_estimationr   r   r   r   r   <module>   s$   