o
    ॵi                     @   s   d dl mZ d dlmZmZ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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! 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)AnyDictListUnion)	Pipelines)Model)ScrfdDetectSCRFDPreprocessor)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Config)	ModelFileTasks)Image)
get_logger)module_namec                       s   e 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 )FaceDetectionPipelinemodelc                    s   t  jdd|i| t|tj}t|}t|dd}|du r+t	dd|i|}nt
| jts5J d| j| j}| jdu rEt | _|| _dS )a  
        use `model` to create a face detection pipeline for prediction
        Args:
            model (`str` or `Model`): model_id or `ScrfdDetect` or `TinyMogDetect` model.
            preprocessor(`Preprocessor`, *optional*,  defaults to None): `SCRFDPreprocessor`.
        r   N	model_dirz model object is not initialized. )super__init__ospjoinr   CONFIGURATIONr   	from_filegetattrr   
isinstancer   r   todevicepreprocessorr	   detector)selfr   kwargsconfig_pathcfg	cfg_modelr$   	__class__r   c/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/pipelines/cv/face_detection_pipeline.pyr      s   


zFaceDetectionPipeline.__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 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.   6   s   zFaceDetectionPipeline.__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)	r#   mmcv.parallelr1   r2   nextr   
parametersis_cudar"   )r%   r-   resultr1   r2   r   r   r,   
preprocessS   s   
z FaceDetectionPipeline.preprocessc                 C   s   | j di |S )Nr   )r$   )r%   r-   r   r   r,   forward`   s   zFaceDetectionPipeline.forwardinputsc                 C   s   |S )Nr   )r%   r<   r   r   r,   postprocessc   s   z!FaceDetectionPipeline.postprocess)__name__
__module____qualname__strr   r   r   r   r.   r   r   r:   r;   r=   __classcell__r   r   r*   r,   r      s    "*r   )*os.pathpathr   typingr   r   r   r   cv2numpynpPILtorchmodelscope.metainfor   !modelscope.models.base.base_modelr   #modelscope.models.cv.face_detectionr   r	   modelscope.outputsr
   modelscope.pipelines.baser   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.configr   modelscope.utils.constantr   r   $modelscope.utils.input_output_typingr   modelscope.utils.loggerr   loggerregister_moduleface_detectionr   r   r   r   r,   <module>   s,   