o
    ॵi                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZ d dl	Z	d dl
Zd dlZd dl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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)TemporaryDirectory)AnyDict)	Pipelines)
DepeDetect)plot_result)	MsDataset)
OutputKeys)InputPipeline)	PIPELINES)Config)Tasks)
get_logger)module_namec                       s   e Zd Zdef fddZ 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eee	f d	eee	f fddZ  ZS )ObjectDetection3DPipelinemodelc                    s   t  jd
d|i| t|d}t|| _tj	 r#t
d| _
nt
d| _
t|| j
| _tddkr>d| _d	S d| _d	S )a`  
        use `model` to create a 3d object detection pipeline for prediction
        Args:
            model: model id on modelscope hub.

        Example:
            >>> import cv2
            >>> from modelscope.pipelines import pipeline
            >>> from modelscope.msdatasets import MsDataset
            >>> ms_ds_nuscenes = MsDataset.load('nuScenes_mini', namespace='shaoxuan')
            >>> data_path = ms_ds_nuscenes.config_kwargs['split_config']
            >>> val_dir = data_path['validation']
            >>> val_root = val_dir + '/' + os.listdir(val_dir)[0] + '/'
            >>> depe = pipeline('object-detection-3d', model='damo/cv_object-detection-3d_depe')
            >>> input_dict = {'data_root': val_root, 'sample_idx': 0}
            >>> result = depe(input_dict)
            >>> cv2.imwrite('result.jpg', result['output_img'])
        r   zmmcv_depe.pycudacpuMODELSCOPE_ENVIRONMENTeasr      N )super__init__ospjoinr   	from_filecfgtorchr   is_availabledevicer   todetectorosgetenvnum_workers)selfr   kwargsconfig_path	__class__r   h/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/object_detection_3d_pipeline.pyr      s   


z"ObjectDetection3DPipeline.__init__c                    s   t  j|fi |S )a.  
        Detect 3D objects in images from multi-cameras that passed as inputs

        Args:
            input (`Dict[str, Any]`):
                A dictionary of input consist 2 keys:
                - `data_root` is the path of input data in nuScenes format,
                you can create your own data according steps from model-card,
                if `data_root` is False, a default input data from
                nuScenes-mini validation set will be used, which includes 81
                samples from 2 scenes.
                - `sample_idx` is the index of sample to be inferenced, the
                value should in range of sample number in input data.

        Return:
            A dictionary of result consist 1 keys:
            - `output_img` plots all detection results in one image.

        )r   __call__)r'   inputr(   r*   r   r,   r-   ?   s   z"ObjectDetection3DPipeline.__call__c                 C   s>   t jddd}|jd }|d }|d t|d  d }|S )NnuScenes_minishaoxuan)	namespacesplit_config
validation/r   )r   loadconfig_kwargsr$   listdir)r'   ms_ds_nuscenes	data_pathval_dirval_rootr   r   r,   get_default_dataU   s
   
z*ObjectDetection3DPipeline.get_default_datar.   returnc           
   
   C   s\  d|v sJ |d }t |d trt|d |d< |dd}|du r/|  }td|  ztj	ds;t
d t
d| d W n tyY } ztd	| d
d }~ww ddlm}m} ||d f| jjj_d| jjj_|| jjj| _|| jd| jddd}tt|}d|v rddlm}	 t| j jr|	|t| j jjgd }|S )N
sample_idx	data_rootFz%Note: forward using default data in: z/data/Datasetzmkdir -p /data/Datasetzln -snf z /data/Dataset/nuScenesz
exception:zH, please make sure to have permission create and write in: /data/Datasetr   )build_dataloaderbuild_dataset   T)samples_per_gpuworkers_per_gpudistshuffle	img_metas)scatter) 
isinstancestrintgetr<   loggerinfor$   pathexistssystem	ExceptionRuntimeErrormmdet3d.datasetsr@   rA   r   datatest	idx_range	test_modedatasetr&   nextitermmcv.parallelrH   r#   
parametersis_cudar!   index)
r'   r.   idxr?   er@   rA   data_loaderresultrH   r   r   r,   
preprocess\   sN   

z$ObjectDetection3DPipeline.preprocessc                 C   s>   t   | jdi |}W d    |S 1 sw   Y  |S )Nr   )r   no_gradr#   )r'   r.   rc   r   r   r,   forward   s   

z!ObjectDetection3DPipeline.forwardinputsc                 K   s   | dd }|d u rt j}ntj|st| t|dd}d|i}| j	j
|fi | td|  t|ddd }tj|tjiS )	N	save_pathpts_bboxzresults_nusc.jsonjsonfile_prefixzDone, results saved into: g333333?)	vis_thredr   )rL   r   namer$   rO   rP   makedirsr   r   rY   format_resultsrM   rN   r   r	   
OUTPUT_IMGastypenpuint8)r'   rg   r(   rh   	file_pathkwargs_format
result_imgr   r   r,   postprocess   s   

z%ObjectDetection3DPipeline.postprocess)__name__
__module____qualname__rJ   r   r-   r<   r
   r   r   rd   rf   rv   __classcell__r   r   r*   r,   r      s     "%*r   )(r$   os.pathrO   r   tempfiler   typingr   r   cv2numpyrq   PILr   modelscope.metainfor   -modelscope.models.cv.object_detection_3d.deper   8modelscope.models.cv.object_detection_3d.depe.result_visr   modelscope.msdatasetsr   modelscope.outputsr	   modelscope.pipelines.baser
   r   modelscope.pipelines.builderr   modelscope.utils.configr   modelscope.utils.constantr   modelscope.utils.loggerr   rM   register_moduleobject_detection_3dobject_detection_3d_deper   r   r   r   r,   <module>   s.   