o
    yqi-                     @   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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                   @   sj   e Zd ZdZdejdejfd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pointsreturnc                 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 )	a}  
        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    )xr   r   m/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddlex/inference/pipelines/ocr/result.py<lambda>.   s    z,OCRResult.get_minarea_rect.<locals>.<lambda>)key)r         r   r   r   r   r   )	cv2ZminAreaRectsortedlistZ	boxPointsnparrayZastypeint32)selfr   Zbounding_boxZindex_aZindex_bZindex_cZindex_dboxr   r   r   get_minarea_rect"   s&   
zOCRResult.get_minarea_rectc                 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rEdd | D }|j||d|d | |}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   r   .0Zsublistitemr   r   r   
<listcomp>O   s
    z%OCRResult._to_img.<locals>.<listcomp>c                 S   r#   r   r   r$   r   r   r   r'   R   s    	text_wordr   r   r   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%   r   yr   r   r   r'   {   s       )outlinewidthfillg      ?N   c                 S   s    g | ]\}}t |t |fqS r   )intr.   r   r   r   r'      s     )r3   )r   r   TRGBr-   r-   r-   model_settingsZocr_res_imguse_doc_preprocessorr   )*	enumerater5   mathsqrtappendshaper   ZcvtColorZCOLOR_BGR2RGBr   Z	fromarrayr   Zonesuint8randomseedr   Drawr	   ziprandintr   lentolistZpolygonr!   maxminmean
isinstancetupledraw_box_txt_finepathr   ZreshapeZ	polylinesZbitwise_andblendnewZpasteupdateimg)r   ZboxesZtxtsr"   r(   idxZword_regionZchar_box
box_height	box_widthimagehwZ	image_rgbZimg_leftZ	img_rightZ	draw_leftZvis_fontr    txtcolorZptsheightZbox_ptsimg_right_textZimg_showr9   Zres_img_dictr   r   r   _to_imgD   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_indexr9   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(   )strr   r   r   _to_strr   argskwargsdatar   r   r   rk      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.
        r^   r_   r9   r:   r+   r`   rb   rc   ra   rd   re   rf   r*   rg   r)   rh   ri   r(   )jsonr   _to_jsonrl   r   r   r   rq      s*   zOCRResult._to_jsonN)__name__
__module____qualname____doc__r   ndarrayr!   r   rj   r   r]   rk   rq   r   r   r   r   r      s    "T+r   img_sizer    rY   	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      r7   r8   )r   r   r   r   r   )r3   line_spacing)r3   fontr,   )flagsZ
borderModeZborderValue)r5   r<   r=   floatr   rP   r   rC   r   draw_vertical_textr
   textr   Zfloat32r   r   ZgetPerspectiveTransformr@   ZwarpPerspectiveZINTER_NEARESTZBORDER_CONSTANT)rw   r    rY   rx   rT   rU   Zimg_textZ	draw_textr|   Zpts1Zpts2Mr\   r   r   r   rM      sF   DD

rM   rz   r   c                 C   sR   |\}}|D ] }| j ||f|||d ||}	|	d |	d  }
||
| 7 }qd S )N)r|   r3   r   r   )r   Zgetbbox)Zdrawpositionr   r|   r3   r{   r   r/   charZbboxchar_heightr   r   r   r     s   
r   )rz   r   )r<   rA   typingr   numpyr   ZPILr   r   Z
utils.depsr   r   r   Zutils.fontsr	   r
   r   Zcommon.resultr   r   r   r   rL   rv   rj   rM   r   r   r   r   r   <module>   s6    H7