o
    ߥih                     @   s   d dl m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mZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ejejejdejejejdG dd de	ZdS )    N)Models)
TorchModel)MODELS)Config)	ModelFileTasks   )ViT)RPNNHead)FPNF)FCNMaskNHeadShared4Conv1FCBBoxNHead)module_namec                       s:   e Zd Zdef fddZdd Zdd Zdd	 Z  ZS )
DetectionModel	model_dirc                    s   t  j|g|R i | ddlm} ddlm} ddlm} ddlm	} t
|tj}t
|d}	t|	}
d|
j_||
j| _|| j|dd	}|d
 d | _d|
jd _|||
j| _|
| j_| j  |
j| _dS )zstr -- model file root.r   )load_checkpoint)replace_ImageToTensor)Compose)build_detectorzmmcv_config.pyNcpu)map_locationmetaCLASSESLoadImageFromWebcam)super__init__mmcv.runnerr   mmdet.datasetsr   mmdet.datasets.pipelinesr   mmdet.modelsr   ospjoinr   TORCH_MODEL_FILEr   	from_filemodel
pretrainedclass_namestest_pipelinetypetransform_inputcfgeval	score_thr)selfr   argskwargsr   r   r   r   
model_pathconfig_pathconfig
checkpoint	__class__ e/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/object_detection/mmdet_model.pyr      s*   

zDetectionModel.__init__c                 C   sD   t   | jdddd|}W d   |S 1 sw   Y  |S )z9data is dict,contain img and img_metas,follow with mmdet.FT)return_lossrescaleNr6   )torchno_gradr$   )r-   dataresultsr6   r6   r7   	inference0   s   

zDetectionModel.inferencec                 C   s   ddl m}m} t|d}| |}||gdd}dd |d D |d< d	d |d
 D |d
< t| j jrE||t| j j	gd }|S )zJimage is numpy return is dict contain img and img_metas,follow with mmdet.r   )collatescatter)imgr   )samples_per_gpuc                 S      g | ]}|j d  qS r   r<   ).0	img_metasr6   r6   r7   
<listcomp>>   s    
z-DetectionModel.preprocess.<locals>.<listcomp>rG   c                 S   rC   rD   rE   )rF   rA   r6   r6   r7   rH   A       rA   )
mmcv.parallelr?   r@   dictr)   nextr$   
parametersis_cudadevice)r-   imager?   r@   r<   r6   r6   r7   
preprocess7   s   


zDetectionModel.preprocessc                    s   t |d tr|d \}}n|d d }}dd t|D }t|}t|}|d d df }| jk}tt|	ddkrDdS ||d d f }|| }t
|d d df d}|d d ddf 	t} fd	d|D }|||fS )
Nr   c                 S   s(   g | ]\}}t j|jd  |t jdqS )r   )dtype)npfullshapeint32)rF   ibboxr6   r6   r7   rH   N   s    z.DetectionModel.postprocess.<locals>.<listcomp>int)NNN      c                    s   g | ]} j | qS r6   )r&   )rF   i_labelr-   r6   r7   rH   ]   rI   )
isinstancetuple	enumeraterS   concatenatevstackr,   sumarrayastypearoundrZ   )r-   inputsbbox_result_labelsscoresindsbboxesr6   r^   r7   postprocessH   s$   



zDetectionModel.postprocess)	__name__
__module____qualname__strr   r>   rQ   ro   __classcell__r6   r6   r4   r7   r      s
    r   )os.pathpathr    numpyrS   r:   modelscope.metainfor   'modelscope.models.base.base_torch_modelr   modelscope.models.builderr   modelscope.utils.configr   modelscope.utils.constantr   r   mmdet_ms.backbonesr	   mmdet_ms.dense_headsr
   mmdet_ms.necksr   mmdet_ms.roi_headsr   r   register_modulehuman_detection	detectionimage_object_detectionr   r6   r6   r6   r7   <module>   s"   