o
    *jW
                     @   s   d dl Z d dlZd dl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mZ d dlmZ d dlmZ d dlmZmZmZ ejejejdG d	d
 d
eZdS )    N)AnyDict)Preprocessors)Preprocessor
load_image)PREPROCESSORS)Config)FieldsModeKeys	ModelFile)module_namec                       s4   e Zd Zejfdedef fddZdd Z  ZS )OCRDetectionPreprocessor	model_dirmodec                    s2   t  | ttj|tj}|j	j
j| _dS )zThe base constructor for all ocr recognition preprocessors.

        Args:
            model_dir (str): model directory to initialize some resource
            mode: The mode for the preprocessor.
        N)super__init__r   	from_fileospathjoinr   ZCONFIGURATIONmodelZinference_kwargsimage_short_side)selfr   r   Zcfgs	__class__ p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/cv/ocr_detection/preprocessor.pyr      s
   z!OCRDetectionPreprocessor.__init__c           
      C   s2  t |trtt|}nt |tjjrt|}nt |tjr#|}n	tdt	| |dddddddf }|j
\}}}||k rW| j}tt|| | d d }n| j}tt|| | d d }t|||f}|tjg dtjd }|d }t|dd	d
 d	}|||gd}	|	S )ar  process the raw input data
        Args:
            inputs:
                - A string containing an HTTP link pointing to an image
                - A string containing a local path to an image
                - An image loaded in PIL(PIL.Image.Image) or opencv(np.ndarray) directly, 3 channels RGB
        Returns:
            outputs: the preprocessed image
        z:inputs should be either str, PIL.Image, np.array, but got N    )gQ^@gR1]@g\(Y@)Zdtypeg     o@   r      )imgZ	org_shape)
isinstancestrnparrayr   PILZImageZndarray	TypeErrortypeshaper   intmathceilcv2resizeZfloat32torchZ
from_numpyZpermutefloatZ	unsqueeze)
r   Zinputsr!   heightwidth_Z
new_heightZ	new_widthZresized_imgresultr   r   r   __call__"   s8   

 

z!OCRDetectionPreprocessor.__call__)	__name__
__module____qualname__r
   Z	INFERENCEr#   r   r5   __classcell__r   r   r   r   r      s    r   )r+   r   typingr   r   r-   numpyr$   r&   r/   Zmodelscope.metainfor   Zmodelscope.preprocessorsr   r   Z modelscope.preprocessors.builderr   Zmodelscope.utils.configr   Zmodelscope.utils.constantr	   r
   r   Zregister_moduleZcvZocr_detectionr   r   r   r   r   <module>   s    