o
    ߥi                     @   s   d dl Z d dlmZmZ d dlZd dlZd dlm  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mZ d dlmZ d	d
lmZmZmZ d	dlmZmZ e Zej ej!ej!dG dd deZ"dS )    N)AnyDict)Models)
TorchModel)MODELS)Config)	ModelFileTasks)
get_logger   )DBModel
DBNasModel	VLPTModel)boxes_from_bitmappolygons_from_bitmap)module_namec                       sf   e Zd Zd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 )OCRDetection	model_dirc                    s   t  j|fi | tj|tj}ttj|tj	}|j
jj| _|j
jj| _|j
j| _d| _d| _| jdkr>t | _n| jdkrHt | _n| jdkrRt | _n	td|j
j |dkrn| jjtj|dd	dd
 dS dS )zinitialize the ocr recognition model from the `model_dir` path.

        Args:
            model_dir (str): the model path.
        NFresnet50resnet18proxylessnasz?detector backbone should be either resnet18, resnet50, but got  cpu)map_location)strict)super__init__ospathjoinr   TORCH_MODEL_FILEr   	from_fileCONFIGURATIONmodelinference_kwargsthreshreturn_polygonbackbonedetectoronnx_exportr   r   r   	TypeErrorload_state_dicttorchload)selfr   kwargs
model_pathcfgs	__class__ \/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/models/cv/ocr_detection/model.pyr      s0   







zOCRDetection.__init__inputreturnc                 C   sF   t |tu r| |d }n|ddgd}| |d }||d dS )aS  
        Args:
            img (`torch.Tensor`): image tensor,
                shape of each tensor is [3, H, W].

        Return:
            results (`torch.Tensor`): bitmap tensor,
                shape of each tensor is [1, H, W].
            org_shape (`List`): image original shape,
                value is [height, width].
        imgi   )r8   	org_shaper9   )resultsr9   )typedictr(   )r.   r6   predr4   r4   r5   forward6   s
   zOCRDetection.forwardinputsc           	      C   sj   |d d }| j r|S |d \}}|| jk}| jr#t||||\}}n	t||||\}}dt|i}|S )Nr:   r   r9   det_polygons)r)   r%   r&   r   r   nparray)	r.   r?   r=   heightwidthsegmentationboxesscoresresultr4   r4   r5   postprocessJ   s   

zOCRDetection.postprocess)
__name__
__module____qualname__strr   r   r   r>   rI   __classcell__r4   r4   r2   r5   r      s    "*r   )#r   typingr   r   numpyrA   r,   torch.nn.functionalnn
functionalFmodelscope.metainfor   'modelscope.models.base.base_torch_modelr   modelscope.models.builderr   modelscope.utils.configr   modelscope.utils.constantr   r	   modelscope.utils.loggerr
   modules.dbnetr   r   r   utilsr   r   LOGGERregister_moduleocr_detectionr   r4   r4   r4   r5   <module>   s    