o
    *jg                     @   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   /var/www/html/Deteccion_Ine/venv/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   Z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#   torchZ
from_numpyfloatZ	unsqueezecuda)	r   r$   Z	pcd1_fileZ	pcd2_filer&   r'   Z
pcd1_torchZ
pcd2_torchdatar   r   r   
preprocess'   s(   



z0PointCloudSceneFlowEstimationPipeline.preprocessc                 C   s4   i }| j |}||d< |d |d< |d |d< |S )Noutputr(   r)   )r   Z	inference)r   r$   resultsr4   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   )Zaxis)   r   r   )dtype)r   r7   r   r   c                 S      g | ]}t |qS r   r+   .0vr   r   r   
<listcomp>N       zGPointCloudSceneFlowEstimationPipeline.save_ply_data.<locals>.<listcomp>))xf4)yrA   )zrA   c                 S   r9   r   r:   r;   r   r   r   r>   P   r?   ))redu1)greenrE   )bluerE   Zvertex)r-   ZconcatenatearrayZuint8Ztiler"   emptylenr8   descrnamesr   Zdescriber   )r   r&   r'   Zvertexscolor1color2Zvertex_colorsZ
vertex_allpropelZ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   ZOUTPUTr*   r/   ZTensorcpunumpyZPCD12rQ   ZPCD12_ALIGN)r   rR   r5   Zflowr&   r'   Zoutputsr   r   r   rS   `   s   
z1PointCloudSceneFlowEstimationPipeline.postprocess)__name__
__module____qualname__r,   r   r#   r	   r   r   r3   r6   rQ   rS   __classcell__r   r   r   r   r      s    
"*r   ) typingr   r   r   rU   r-   r/   Zplyfiler   r   Zmodelscope.metainfor   Zmodelscope.outputsr   Zmodelscope.pipelines.baser	   r
   r   Zmodelscope.pipelines.builderr   Zmodelscope.preprocessorsr   Zmodelscope.utils.constantr   Zmodelscope.utils.cv.image_utilsr   Zmodelscope.utils.loggerr   r   Zregister_moduleZpointcloud_sceneflow_estimationr   r   r   r   r   <module>   s$   