o
    0j
-                     @   s   d dl Z d dlZd dlmZ d dlZd dlmZmZ ddl	m
Z
mZmZ ddlmZmZmZ ddlmZmZ ed	r>d dlZe
d	G d
d deZed	dedejdededejf
ddZed	dddZdejdejfddZdS )    N)Dict)Image	ImageDraw   )class_requires_depsfunction_requires_depsis_dep_available)SIMFANG_FONTcreate_fontcreate_font_vertical   )BaseCVResult	JsonMixinzopencv-contrib-pythonc                   @   sT   e Zd ZdZdeeejf fddZdeeef fddZdeeef fddZ	d	S )
	OCRResultz
OCR resultreturnc                 C   s  d| v r}g }g }dd | d D }dd | d D }t |D ]]\}}|}tt|d d |d d  d |d d	 |d d	  d  }tt|d d |d	 d  d |d d	 |d	 d	  d  }	|dksn|	dkroq|| |||  qn| d
 }| d }| d d }
|
jdd \}}t|
tj}t	
|}tj||dftjdd }td t|}t}| d r| d d }t t||D ]\}\}}ztddtddtddf}t|}t|dkrDdd | D }|j||d|d t|}tdt|ddd	f t|ddd	f   }t|ddd	f |ddd	f< t|ddd	f td| |ddd	f< ndd | D }|j||d t|tr^|d }t||f|||j }t|tj!"d}t#||gd|d	 t$||}W q   Y qt	%t	
||d}t	&d|d |fd}|'|dd||f |'t	
||d|d |f | d }d|i}|d  r|j(d!i | d j) |S )"z
        Converts the internal data to a PIL Image with detection and recognition results.

        Returns:
            Dict[Image.Image]: A dictionary containing two images: 'doc_preprocessor_res' and 'ocr_res_img'.
        text_word_regionc                 S      g | ]	}|D ]}|qqS  r   .0Zsublistitemr   r   g/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddlex/inference/pipelines/ocr/result.py
<listcomp>-   s
    z%OCRResult._to_img.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r   0   s    	text_wordr   r         	rec_polys	rec_textsdoc_preprocessor_resZ
output_imgZdtype   Z	vis_fontsr   c                 S   s   g | ]\}}||fqS r   r   r   xyr   r   r   r   Y   s       )outlinewidthfillg      ?N   c                 S   s    g | ]\}}t |t |fqS r   )intr!   r   r   r   r   `   s     )r'   )r   r   TRGBr    r    r    model_settingsZocr_res_imguse_doc_preprocessorr   )*	enumerater)   mathsqrtappendshapecv2ZcvtColorZCOLOR_BGR2RGBr   Z	fromarraynpZonesuint8randomseedr   Drawr	   ziprandintarraylentolistZpolygonget_minarea_rectmaxminmean
isinstancetupledraw_box_txt_finepathint32ZreshapeZ	polylinesZbitwise_andblendnewZpasteupdateZimg)selfZboxesZtxtsr   r   idxZword_regionZchar_box
box_height	box_widthimagehwZ	image_rgbZimg_leftZ	img_rightZ	draw_leftZvis_fontboxtxtcolorZptsheightZbox_ptsimg_right_textZimg_showr-   Zres_img_dictr   r   r   _to_img"   s   







0".
zOCRResult._to_imgc                 O   sD  i }| d |d< | d |d< | d |d< | d d r#| d j d |d< | d dkr-| d	 nt| d	 |d	< | d
 |d
< | d |d< d| v rOt| d |d< | d |d< | d |d< | d |d< t| d |d< | d dkrt| d nt| d |d< t| d |d< d| v r| d |d< | d |d< tj|g|R i |S )ax  Converts the instance's attributes to a dictionary and then to a string.

        Args:
            *args: Additional positional arguments passed to the base class method.
            **kwargs: Additional keyword arguments passed to the base class method.

        Returns:
            Dict[str, str]: A dictionary with the instance's attributes converted to strings.
        
input_path
page_indexr-   r.   r   res	text_typeZsealdt_polystext_det_paramstextline_orientation_anglestext_rec_score_threshreturn_word_boxr   
rec_scoresr   	rec_boxestext_word_boxesr   )strr5   r<   r   _to_strrK   argskwargsdatar   r   r   re   v   s:   
zOCRResult._to_strc                 O   s   i }| d |d< | d |d< | d |d< | d d r#| d j d |d< | d |d< | d |d< | d	 |d	< d
| v r?| d
 |d
< | d |d< | d |d< | d |d< | d |d< | d |d< | d |d< d| v rs| d |d< | d |d< tj|g|R i |S )a[  
        Converts the object's data to a JSON dictionary.

        Args:
            *args: Positional arguments passed to the JsonMixin._to_json method.
            **kwargs: Keyword arguments passed to the JsonMixin._to_json method.

        Returns:
            Dict[str, str]: A dictionary containing the object's data in JSON format.
        rX   rY   r-   r.   r   rZ   r\   r]   r[   r^   r_   r`   r   ra   r   rb   rc   r   )jsonr   _to_jsonrf   r   r   r   rk      s*   zOCRResult._to_jsonN)
__name__
__module____qualname____doc__r   rd   r   rW   re   rk   r   r   r   r   r      s
    T+r   img_sizerR   rS   	font_pathr   c                 C   s  t tt|d d |d d  d t|d d |d d  d  }t tt|d d |d d  d t|d d |d d  d  }|d| krw|dkrwtd||fd}t|}|rvt|||f|}t	|d||d	dd
 n#td||fd}t|}|rt
|||f|}|jddg|d	|d tddg|dg||gd|gg}	tj|tjd}
t|	|
}tj|tjd}tj||| tjtjdd}|S )a  
    Draws text in a box on an image with fine control over size and orientation.

    Args:
        img_size (tuple): The size of the output image (width, height).
        box (np.ndarray): A 4x2 numpy array defining the corners of the box in (x, y) order.
        txt (str): The text to draw inside the box.
        font_path (str): The path to the font file to use for drawing the text.

    Returns:
        np.ndarray: An image with the text drawn in the specified box.
    r   r   r   r      r+   r,   )r   r   r   r   r   )r'   line_spacing)r'   fontr   )flagsZ
borderModeZborderValue)r)   r0   r1   floatr   rI   r   r9   r   draw_vertical_textr
   textr5   Zfloat32r<   r4   ZgetPerspectiveTransformr6   ZwarpPerspectiveZINTER_NEARESTZBORDER_CONSTANT)rp   rR   rS   rq   rM   rN   Zimg_textZ	draw_textru   Zpts1Zpts2MrV   r   r   r   rE      sF   DD

rE   rs   r   c                 C   sR   |\}}|D ] }| j ||f|||d ||}	|	d |	d  }
||
| 7 }qd S )N)ru   r'   r   r   )ry   Zgetbbox)Zdrawpositionry   ru   r'   rt   r"   r#   charZbboxchar_heightr   r   r   rx      s   
rx   pointsc                 C   s   t | }ttt |dd d} d\}}}}| d d | d d kr)d}d}nd}d}| d d | d d kr>d}d}nd}d}t| | | | | | | | gtj}|S )	aa  
    Get the minimum area rectangle for the given points using OpenCV.

    Args:
        points (np.ndarray): An array of 2D points.

    Returns:
        np.ndarray: An array of 2D points representing the corners of the minimum area rectangle
                 in a specific order (clockwise or counterclockwise starting from the top-left corner).
    c                 S   s   | d S )Nr   r   )r"   r   r   r   <lambda>  s    z"get_minarea_rect.<locals>.<lambda>)key)r   r   r   r   r   r   r   r   )	r4   ZminAreaRectsortedlistZ	boxPointsr5   r<   ZastyperG   )r~   Zbounding_boxZindex_aZindex_bZindex_cZindex_drR   r   r   r   r?     s&   
r?   )rs   r   )r0   r7   typingr   numpyr5   ZPILr   r   Z
utils.depsr   r   r   Zutils.fontsr	   r
   r   Zcommon.resultr   r   r4   r   rD   Zndarrayrd   rE   rx   r?   r   r   r   r   <module>   s8    &7	