o
    ॵi                     @   s   d dl mZ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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 e ZejejejdejejejdG dd deZdS )    )AnyDictOptionalUnion)	Pipelines)
OutputKeys)DetectionOutput)InputPipeline)	PIPELINES)	LoadImagePreprocessor)Tasks'show_image_object_detection_auto_result)
get_logger)module_namec                       s   e Zd Z	ddedee f fddZdedeee	f f fdd	Z
deee	f deeee	f ef fd
dZdeeee	f ef deee	f fddZdddZ  ZS )TinynasDetectionPipelineNmodelpreprocessorc                    s   t  jd||d| dS )a  Object detection pipeline, currently only for the tinynas-detection model.

        Args:
            model: A str format model id or model local dir to build the model instance from.
            preprocessor: A preprocessor instance to preprocess the data, if None,
            the pipeline will try to build the preprocessor according to the configuration.json file.
            kwargs: The args needed by the `Pipeline` class.
        )r   r   N )super__init__)selfr   r   kwargs	__class__r   f/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/tinynas_detection_pipeline.pyr      s   z!TinynasDetectionPipeline.__init__inputreturnc                    s   t |}t |S N)r   convert_to_ndarrayr   
preprocess)r   r   imgr   r   r   r"   (   s   
z#TinynasDetectionPipeline.preprocessc                 C   s   |  |d S )a[  The forward method of this pipeline.

        Args:
            input: The input data output from the `preprocess` procedure.

        Returns:
            A model output, either in a dict format, or in a standard `DetectionOutput` dataclass.
            If outputs a dict, these keys are needed:
                class_ids (`Tensor`, *optional*): class id for each object.
                boxes (`Tensor`, *optional*): Bounding box for each detected object
                    in [left, top, right, bottom] format.
                scores (`Tensor`, *optional*): Detection score for each object.
        r#   )r   )r   r   r   r   r   forward,   s   z TinynasDetectionPipeline.forwardinputsc                 C   sX   |d |d |d }}}|d u rt jg t jg t jg i}|S t j|t j|t j|i}|S )Nboxesscores	class_ids)r   SCORESLABELSBOXES)r   r%   bboxesr'   labelsoutputsr   r   r   postprocess>   s   z$TinynasDetectionPipeline.postprocessc                 C   s   t ||| d S r    r   )r   img_pathresult	save_pathr   r   r   show_resultQ   s   z$TinynasDetectionPipeline.show_resultr    )__name__
__module____qualname__strr   r   r   r	   r   r   r"   r   r   r$   r/   r3   __classcell__r   r   r   r   r      s.    	


r   N)typingr   r   r   r   modelscope.metainfor   modelscope.outputsr   modelscope.outputs.cv_outputsr   modelscope.pipelines.baser	   r
   modelscope.pipelines.builderr   modelscope.preprocessorsr   r   modelscope.utils.constantr   modelscope.utils.cv.image_utilsr   modelscope.utils.loggerr   loggerregister_module domain_specific_object_detectiontinynas_detectionimage_object_detectionr   r   r   r   r   <module>   s&   