o
    *j]+                     @   s   d dl Z d dlZd dlmZ d dlmZm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 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! ddl"m#Z#m$Z$m%Z% e! Z&dZ'dZ(dZ)g dZ*dZ+dZ,ej-ej.ej.dG dd deZ/dS )    N)AnyDict)	Pipelines)OCRDetection)
OutputKeys)InputPipeline)	PIPELINES)	LoadImage)Config)	ModelFileTasks)device_placement)
get_logger   )	cal_width
nms_pythonrboxes_to_polygons         )皙?r   r   r   r   r   g?g333333?)module_namec                       s   e Zd ZdZdef fddZ 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 )OCRDetectionPipelinea   OCR Detection Pipeline.

    Example:

    ```python
    >>> from modelscope.pipelines import pipeline

    >>> ocr_detection = pipeline('ocr-detection', model='damo/cv_resnet18_ocr-detection-line-level_damo')
    >>> result = ocr_detection('https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/ocr_detection.jpg')

        {'polygons': array([[220,  14, 780,  14, 780,  64, 220,  64],
       [196, 369, 604, 370, 604, 425, 196, 425],
       [ 21, 730, 425, 731, 425, 787,  21, 786],
       [421, 731, 782, 731, 782, 789, 421, 789],
       [  0, 121, 109,   0, 147,  35,  26, 159],
       [697, 160, 773, 160, 773, 197, 697, 198],
       [547, 205, 623, 205, 623, 244, 547, 244],
       [548, 161, 623, 161, 623, 199, 547, 199],
       [698, 206, 772, 206, 772, 244, 698, 244]])}
    ```
    note:
    model = damo/cv_resnet18_ocr-detection-line-level_damo, for general text line detection, based on SegLink++.
    model = damo/cv_resnet18_ocr-detection-word-level_damo, for general text word detection, based on SegLink++.
    model = damo/cv_resnet50_ocr-detection-vlpt, for toaltext dataset, based on VLPT_pretrained DBNet.
    model = damo/cv_resnet18_ocr-detection-db-line-level_damo, for general text line detection, based on DBNet.

    modelc           #         s  t |ts	J dt jd(d|i| td|  ttj	
|tj}t|dr8t|jdr8|jj| _nd| _| jdkrT| j| j| _| j  td d	S dd	l}|jd
kra|jj}|jj  |jjdtd |jjdtd |  t
t
| jtj d}|! | _"|j#dd}d|j$_%|j&|d| _'| j"( E t)| j*| j+# |j,|j-g ddd| _.i | _/|j0d|j1d |j2dg |3d|j4dd}|j56d|}ddl7m8}	m9}
m:} |	 }|j;| j.dd}g g g }}}t<|D ]d\}}|d |d |d }}}|=|t>}|j?@|A|d dg}|j?@|A|d d!gd	d	d	df }|j?@|A|d d!gd	d	dd	f }|jB||gdd"}|C| |C| |C| q|D| j.dd# }|||||tE|jFd$\}}}}|
|||\}} || j/d%< | | j/d&< W d	   n	1 sw   Y  | j'( }!td'|  |j5G|H }"|"I|!| W d	   n	1 sw   Y  W d	   n1 sw   Y  W d	   d	S W d	   d	S 1 sw   Y  d	S ))z
        use `model` to create a OCR detection pipeline for prediction
        Args:
            model: model id on modelscope hub.
        zmodel must be a single strr   zloading model from dir 
model_typez	SegLink++DBNetzloading model doner   Nz2.0Znode_thresholdzConfidence threshold for nodesZlink_thresholdzConfidence threshold for linkszcheckpoint-80000T)Zallow_soft_placement)config)r      r      input_images)shapename )Zreuseglobal_stepF)ZinitializerdtypeZ	trainablegCl?r   )SegLinkDetectorcombine_segments_pythondecode_segments_links_python)Zis_training      Zaxisr   )anchor_sizescombined_rboxescombined_countszloading model from  )J
isinstancestrsuper__init__loggerinfor   	from_fileospathjoinr   ZCONFIGURATIONhasattrr   r   toZdeviceocr_detectoreval
tensorflow__version__compatZv1Zdisable_eager_executionZappflagsZDEFINE_floatTF_NODE_THRESHOLDTF_LINK_THRESHOLDZreset_default_graphospZTF_CHECKPOINT_FOLDERZget_default_graph_graphZConfigProtoZgpu_optionsZallow_growthSession_session
as_defaultr   Z	frameworkZdevice_nameplaceholderfloat32r    outputZvariable_scopeZ
AUTO_REUSEZget_variableZconstant_initializerZint64trainZExponentialMovingAverage	ocr_utilsr&   r'   r(   Zbuild_model	enumeratemultiplyOFFSET_VARIANCEnnZsoftmaxZreshapeconcatappendr!   listr-   ZSaverZvariables_to_restoreZrestore)#selfr   kwargsZcfgstfZ
model_pathr   r$   Zvariable_averagesr&   r'   r(   ZdetectorZall_mapsZ	all_nodesZ	all_linksZall_regimapsZcls_mapsZlnk_mapsZreg_mapsZcls_probZlnk_prob_posZlnk_prob_mutZlnk_probZ
image_sizesegmentsZgroup_indicesZsegment_counts_r.   r/   sessZmodel_loader	__class__r0   o/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/pipelines/cv/ocr_detection_pipeline.pyr4   A   s   








3 $zOCRDetectionPipeline.__init__c                    s   t  j|fi |S )aK  
        Detect text instance in the text image.

        Args:
            input (`Image`):
                The pipeline handles three types of images:

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

                The pipeline currently supports single image input.

        Return:
            An array of contour polygons of detected N text instances in image,
            every row is [x1, y1, x2, y2, x3, y3, x4, y4, ...].
        )r3   __call__)rV   inputrW   r^   r0   r`   ra      s   zOCRDetectionPipeline.__call__rb   returnc                 C   s(  | j dkr| |}|S t|}|j\}}}tjt||t||dftjd}||d |d |d d f< d}t	
|||f}	t	|	t	j}	|	tjg dtjd }	dd l}
| j & |
||g}|
t||t||g}|| jd< || jd< W d    n1 sw   Y  d	tj|	dd
i}|S )Nr   r   )r%   r   )gQ^@gR1]@g\(Y@r   	orig_sizeresize_sizeimgr,   )r   Zpreprocessorr
   Zconvert_to_ndarrayr!   npZzerosmaxrK   cv2resizeZcvtColorZCOLOR_RGB2BGRarrayr?   rF   rI   stackrL   Zexpand_dims)rV   rb   resultrf   hwcZimg_padre   Zimg_pad_resizerX   rd   r0   r0   r`   
preprocess   s,   


"

zOCRDetectionPipeline.preprocessc              	   C   s   | j dkr| |}|S | j 7 | j ! | j|d i}| jj| j|d}|W  d    W  d    S 1 s;w   Y  W d    d S 1 sKw   Y  d S )Nr   rf   )	feed_dict)r   r=   rF   rI   rH   r    runrL   )rV   rb   Zoutputsrr   Zsess_outputsr0   r0   r`   forward   s   

"zOCRDetectionPipeline.forwardinputsc              
   C   st  | j dkrtj|d i}|S |d d }|d d }|dks%||jd k r)td|d |d d f }|d \}}|d \}}t|}	t|t| }
t|t| }tdt	|	d d d d d	f | |d
 |	d d d d d	f< tdt	|	d d d
d d	f |
 |d
 |	d d d
d d	f< t
|	tj}	dd |	 D }t|}tdd |D }tj|i}|S )Nr   Zdet_polygonsr.   r   r/   z"modelscope error: No text detectedrd   re   r)   r   c                 S   s   g | ]	}|t |g qS r0   )r   .0or0   r0   r`   
<listcomp>  s    z4OCRDetectionPipeline.postprocess.<locals>.<listcomp>c                 S   s   g | ]}|d d qS )Nr   r0   rv   r0   r0   r`   ry     s    )r   r   ZPOLYGONSr!   	Exceptionr   floatrg   maximumminimumroundZastypeZint32tolistr   rk   )rV   ru   rm   ZrboxescountZorig_hZorig_wZresize_hZresize_wZpolygonsZscale_yZscale_xZdt_n9Zdt_nmsZdt_polygonsr0   r0   r`   postprocess   s2   
&&
z OCRDetectionPipeline.postprocess)__name__
__module____qualname____doc__r2   r4   ra   r   r   r   rq   rt   r   __classcell__r0   r0   r^   r`   r   "   s    j"*r   )0mathr8   Zos.pathr9   rE   typingr   r   ri   numpyrg   ZtorchZmodelscope.metainfor   Z"modelscope.models.cv.ocr_detectionr   Zmodelscope.outputsr   Zmodelscope.pipelines.baser   r   Zmodelscope.pipelines.builderr	   Zmodelscope.preprocessorsr
   Zmodelscope.utils.configr   Zmodelscope.utils.constantr   r   Zmodelscope.utils.devicer   Zmodelscope.utils.loggerr   rN   r   r   r   r5   ZRBOX_DIMZ
OFFSET_DIMZWORD_POLYGON_DIMrQ   rC   rD   Zregister_moduleZocr_detectionr   r0   r0   r0   r`   <module>   s:   