
    j                     L    d dl mZ d dlZd dlmZ d dlmZ  G d d          ZdS )    )AnyN)requires_package)download_from_urlc            	           e Zd ZdZddededz  dedz  ddfdZddedz  dedz  dedefd	Zd
e	j
        de	j
        fdZdee	j
                 deee	j
                          defdZdee	j
                 defdZdS )_BasePredictora  
    Base class for all predictors

    Args:
        batch_size: the batch size to use
        url: the url to use to download a model if needed
        model_path: the path to the model to use
        **kwargs: additional arguments to be passed to `download_from_url`
    N
batch_sizeurl
model_pathreturnc                 X    || _          | j        ||fi || _        g | _        g | _        d S )N)r   _init_modelsession_inputs_results)selfr   r	   r
   kwargss        U/var/www/html/Carbon-Document/venv/lib/python3.11/site-packages/doctr/contrib/base.py__init__z_BasePredictor.__init__   s9    $'t'ZBB6BB)+#%    r   c                     t          dd           ddl}|s|st          d          |r|nt          t	          |fddi|          }|                    |dd	g
          S )a-  
        Download the model from the given url if needed

        Args:
            url: the url to use
            model_path: the path to the model to use
            **kwargs: additional arguments to be passed to `download_from_url`

        Returns:
            Any: the ONNX loaded model
        onnxruntimez9`.contrib` module requires `onnxruntime` to be installed.r   Nz-You must provide either a url or a model_pathcache_subdirmodelsCUDAExecutionProviderCPUExecutionProvider)	providers)r   r   
ValueErrorstrr   InferenceSession)r   r	   r
   r   ortonnx_model_paths         r   r   z_BasePredictor._init_model    s     	(cddd!!!! 	N: 	NLMMM(2t**<Mc<s<s`h<slr<s<s8t8t##O@WYo?p#qqqr   imgc                     t           )z
        Preprocess the input image

        Args:
            img: the input image to preprocess

        Returns:
            np.ndarray: the preprocessed image
        NotImplementedError)r   r"   s     r   
preprocessz_BasePredictor.preprocess4   s
     "!r   outputinput_imagesc                     t           )z
        Postprocess the model output

        Args:
            output: the model output to postprocess
            input_images: the input images used to generate the output

        Returns:
            Any: the postprocessed output
        r$   )r   r'   r(   s      r   postprocessz_BasePredictor.postprocess@   s
     "!r   inputsc                      _          j                                         fdt          dt	                     j                  D             } fd|D             } fd|D             }                     ||          S )z
        Call the model on the given inputs

        Args:
            inputs: the inputs to use

        Returns:
            Any: the postprocessed output
        c                 4    g | ]}||j         z            S  )r   ).0ir+   r   s     r   
<listcomp>z+_BasePredictor.__call__.<locals>.<listcomp>Z   s*    jjja&Q%8!89jjjr   r   c                 h    g | ].}t          j        fd |D             t           j                  /S )c                 :    g | ]}                     |          S r.   )r&   )r/   r"   r   s     r   r1   z6_BasePredictor.__call__.<locals>.<listcomp>.<listcomp>\   s%    <<<sdooc**<<<r   )dtype)nparrayfloat32)r/   batchr   s     r   r1   z+_BasePredictor.__call__.<locals>.<listcomp>[   sL     
 
 
TYBH<<<<e<<<BJOOO
 
 
r   c                 `    g | ]*}j                             d d         j        |i          +S )Nr   )r   runname)r/   r8   model_inputsr   s     r   r1   z+_BasePredictor.__call__.<locals>.<listcomp>_   s7    hhhU4<##D<?+?*GHHhhhr   )r   r   
get_inputsrangelenr   r*   )r   r+   batched_inputsprocessed_batchesoutputsr<   s   ``   @r   __call__z_BasePredictor.__call__M   s     |..00jjjjj5CPVKKY]YhCiCijjj
 
 
 
]k
 
 
 ihhhhVghhh888r   )NN)__name__
__module____qualname____doc__intr   r   r   r   r5   ndarrayr&   listr*   rC   r.   r   r   r   r      s1        & &3 &S4Z &CRVJ &lp & & & &r rsTz rcDj r[^ rcf r r r r(
"bj 
"RZ 
" 
" 
" 
""$rz"2 "$tBJGWBX "]` " " " "9tBJ/ 9C 9 9 9 9 9 9r   r   )	typingr   numpyr5   doctr.file_utilsr   doctr.utils.datar   r   r.   r   r   <module>rO      s              - - - - - - . . . . . .R9 R9 R9 R9 R9 R9 R9 R9 R9 R9r   