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 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ListUnion)	Pipelines)Model)Pipeline)	PIPELINES)Tasks)Image)
get_logger)module_namec                       s   e Zd ZdZdef fddZdeeee f f f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 )CardDetectionPipelinea	   Card Detection Pipeline.

    Examples:

    >>> from modelscope.pipelines import pipeline

    >>> detector = pipeline('card-detection', 'damo/cv_resnet_carddetection_scrfd34gkps')
    >>> detector("http://www.modelscope.cn/api/v1/models/damo/cv_resnet_carddetection_scrfd34gkps/repo?Revision=master"
    >>>             "&FilePath=description/card_detection1.jpg")
    >>>   {
    >>>    "boxes": [
    >>>        [
    >>>        446.9007568359375,
    >>>        36.374977111816406,
    >>>        907.0919189453125,
    >>>        337.439208984375
    >>>        ],
    >>>        [
    >>>        454.3310241699219,
    >>>        336.08477783203125,
    >>>        921.26904296875,
    >>>        641.7871704101562
    >>>        ]
    >>>    ],
    >>>    "keypoints": [
    >>>        [
    >>>        457.34710693359375,
    >>>        339.02044677734375,
    >>>        446.72271728515625,
    >>>        52.899078369140625,
    >>>         902.8200073242188,
    >>>        35.063236236572266,
    >>>        908.5877685546875,
    >>>        325.62030029296875
    >>>         ],
    >>>         [
    >>>        465.2864074707031,
    >>>        642.8411254882812,
    >>>       454.38568115234375,
    >>>        357.4076232910156,
    >>>        902.5343017578125,
    >>>        334.18377685546875,
    >>>        922.0982055664062,
    >>>         621.0704345703125
    >>>        ]
    >>>    ],
    >>>    "scores": [
    >>>        0.9296008944511414,
    >>>        0.9260380268096924
    >>>    ]
    >>>   }
    >>>
    modelc                    sB   t  jdd|i| t| jtsJ d| j| j}|| _dS )z
        use `model` to create a face detection pipeline for prediction
        Args:
            model: model id on modelscope hub or `ScrfdDetect` Model.
            preprocessor: `SCRFDPreprocessor`.
        r   z model object is not initialized.N )super__init__
isinstancer   r   todevicedetector)selfr   kwargsr   	__class__r   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/card_detection_pipeline.pyr   H   s   
zCardDetectionPipeline.__init__inputc                    s   t  j|fi |S )a  
        Detect objects (bounding boxes or keypoints) in the image(s) passed as inputs.

        Args:
            input (`Image` or `List[Image]`):
                The pipeline handles three types of images:

                - A string containing an HTTP(S) link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL or opencv directly

                The pipeline accepts either a single image or a batch of images. Images in a batch must all be in the
                same format.


        Return:
            A dictionary of result or a list of dictionary of result. If the input is an image, a dictionary
            is returned. If input is a list of image, a list of dictionary is returned.

            The dictionary contain the following keys:

            - **scores** (`List[float]`) -- The detection score for each card in the image.
            - **boxes** (`List[float]) -- The bounding boxe [x1, y1, x2, y2] of detected objects in in image's
                original size.
            - **keypoints** (`List[Dict[str, int]]`, optional) -- The corner kepoint [x1, y1, x2, y2, x3, y3, x4, y4]
                of detected object in image's original size.
        )r   __call__)r   r   r   r   r   r   r   U   s   zCardDetectionPipeline.__call__returnc                 C   s`   |  |}d|v r.ddlm}m} ||gdd}t| j jr.||t| j jgd }|S )N	img_metasr   )collatescatter   )samples_per_gpu)	preprocessormmcv.parallelr    r!   nextr   
parametersis_cudar   )r   r   resultr    r!   r   r   r   
preprocesss   s   
z CardDetectionPipeline.preprocessc                 C   s   | j di |S )Nr   )r   )r   r   r   r   r   forward   s   zCardDetectionPipeline.forwardinputsc                 C   s   |S )Nr   )r   r,   r   r   r   postprocess   s   z!CardDetectionPipeline.postprocess)__name__
__module____qualname____doc__strr   r   r   r   r   r   r   r*   r+   r-   __classcell__r   r   r   r   r      s    6"*r   N)typingr   r   r   r   modelscope.metainfor   !modelscope.models.base.base_modelr   modelscope.pipelines.baser   modelscope.pipelines.builderr	   modelscope.utils.constantr
   $modelscope.utils.input_output_typingr   modelscope.utils.loggerr   loggerregister_modulecard_detectionr   r   r   r   r   <module>   s   