
    {-j
-                     n   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e                      Z ed	          dedej        dededej        f
d            Z ed	          dd            Zdej        dej        f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                   p    e Zd ZdZdeeej        f         fdZdeeef         fdZdeeef         fdZ	dS )	OCRResultz
OCR resultreturnc           
      &
   d| v r?g }g }d | d         D             }d | d         D             }t          |          D ]\  }}|}t          t          j        |d         d         |d         d         z
  dz  |d         d         |d         d         z
  dz  z                       }t          t          j        |d         d         |d         d         z
  dz  |d         d         |d         d         z
  dz  z                       }	|dk    s|	dk    r|                    |           |                    ||                    n| d	         }| d
         }| d         d         }
|
j        dd         \  }}t          j        |
t          j                  }t          j
        |          }t          j        ||dft          j                  dz  }t          j        d           t!          j        |          }t$          }| d         r| d         d         }t          t'          ||                    D ]'\  }\  }}	 t          j        dd          t          j        dd          t          j        dd          f}t          j        |          }t-          |          dk    rd |                                D             }|                    ||d|           t3          |          }t          dt5          |dddf                   t7          |dddf                   z
  z            }t          j        |dddf                   |dddf<   t          j        |dddf                   t7          d|          z   |dddf<   n5d |                                D             }|                    ||           t;          |t<                    r|d         }t?          ||f|||j                   }t          j        |t          j!                  "                    d          }t          j#        ||gd|d           t          j$        ||          }!#  Y &xY wt          j%        t          j
        |          |d          }t          j&        d|dz  |fd          }|'                    |dd||f           |'                    t          j
        |          |d|dz  |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                     g | ]	}|D ]}|
S  r   .0sublistitems      f/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddlex/inference/pipelines/ocr/result.py
<listcomp>z%OCRResult._to_img.<locals>.<listcomp>-   s9              AE           c                     g | ]	}|D ]}|
S r   r   r   s      r   r   z%OCRResult._to_img.<locals>.<listcomp>0   s%    SSS'7SS4SSSSr   	text_wordr   r         	rec_polys	rec_textsdoc_preprocessor_res
output_imgdtype   	vis_fontsr   c                     g | ]	\  }}||f
S r   r   r   xys      r   r   z%OCRResult._to_img.<locals>.<listcomp>Y   s     ;;;daAq6;;;r      )outlinewidthfillg      ?N   c                 P    g | ]#\  }}t          |          t          |          f$S r   )intr+   s      r   r   z%OCRResult._to_img.<locals>.<listcomp>`   s-    IIIDAqAA/IIIr   )r1   )r!   r    TRGBr(   r(   r(   model_settingsocr_res_imguse_doc_preprocessorr   )*	enumerater4   mathsqrtappendshapecv2cvtColorCOLOR_BGR2RGBr   	fromarraynponesuint8randomseedr   Drawr
   ziprandintarraylentolistpolygonget_minarea_rectmaxminmean
isinstancetupledraw_box_txt_finepathint32reshape	polylinesbitwise_andblendnewpasteupdateimg)selfboxestxtsr   r   idxword_regionchar_box
box_height	box_widthimagehw	image_rgbimg_left	img_right	draw_leftvis_fontboxtxtcolorptsheightbox_ptsimg_right_textimg_showr8   res_img_dicts                               r   _to_imgzOCRResult._to_img"   s&    %%ED   $();$<      TST+->SSSI$-.>$?$? , , [& I!!Q(1+a.8Q>#A;q>HQKN:q@A  
  I!!Q(1+a.8Q>#A;q>HQKN:q@A  	 ??i1nn[)))IcN++++#,& %E$D+,\:{1Q31L(9::	?9--GQ1IRX666<	AN8,,	 	,K(+H(UD)9)9:: 	 	OC#sN1c**N1c**N1c**
 hsmms88a<<;;cjjll;;;C%%c5%NNN*3//C C1ISAY(G!HIIF!#QQQT!3!3CAJ!#QQQT!3!3c"foo!ECAJJIICJJLLIIIG%%gE%:::c5)) !a&C!2Aq63X]!S!ShsBH--55jAAnseT5!DDDOI~FF		;uy998SII9UQUAJ@@x!Q1...uy11Aq!a%3CDDD./&101 	DLCC$'=">"BCCCs   :HQQc                    i }| d         |d<   | d         |d<   | d         |d<   | d         d         r| d         j         d         |d<   | d         dk    r| d	         nt          j        | d	                   |d	<   | d
         |d
<   | d         |d<   d| v rt          j        | d                   |d<   | d         |d<   | d         |d<   | d         |d<   t          j        | d                   |d<   | d         dk    r| d         nt          j        | d                   |d<   t          j        | 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_indexr8   r:   r$   res	text_typesealdt_polystext_det_paramstextline_orientation_anglestext_rec_score_threshreturn_word_boxr#   
rec_scoresr"   	rec_boxestext_word_boxesr   )strrD   rL   r   _to_strra   argskwargsdatas       r   r   zOCRResult._to_strv   s    !,/\!,/\!%&6!7 !"89 	S+/0F+G+KE+RD'( K F** $z*++ 	Z
 #''8"9 -[(D0024(233 3D./ )--D(E$%"&'8"9 -[Xd<&899\ K F** ${+,, 	[
 HT+%677[$$&*+<&=D"# $[ 1D 7777777r   c                    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 r| 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}   r8   r:   r$   r~   r   r   r   r   r   r   r#   r   r"   r   r   r   )jsonr   _to_jsonr   s       r   r   zOCRResult._to_json   s[    !,/\!,/\!%&6!7 !"89 	T+/0F+G+LU+SD'(
+Z"&'8"9 -[(D00267T2UD./(,-D(E$%"&'8"9 -[!,/\ -[ -[$$&*+<&=D"# $[ 1D!$8888888r   N)
__name__
__module____qualname____doc__r   r   r   rz   r   r   r   r   r   r   r      s        Rc5;./ R R R Rh)8$sCx. )8 )8 )8 )8V94S> 9 9 9 9 9 9r   r   img_sizerq   rr   	font_pathr   c           
         t          t          j        t          |d         d         |d         d         z
            dz  t          |d         d         |d         d         z
            dz  z                       }t          t          j        t          |d         d         |d         d         z
            dz  t          |d         d         |d         d         z
            dz  z                       }|d|z  k    r]|dk    rWt	          j        d||fd          }t          j        |          }|r(t          |||f|          }t          |d||d	d
           n\t	          j        d||fd          }t          j        |          }|r.t          |||f|          }|                    ddg|d	|           t          j        ddg|dg||gd|gg          }	t          j        |t          j                  }
t          j        |	|
          }t          j        |t          j                  }t          j        ||| t          j        t          j        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!      r6   r7   )r   r   r   r   r   )r1   line_spacing)r1   fontr&   )flags
borderModeborderValue)r4   r<   r=   floatr   r]   r   rI   r   draw_vertical_textr   textrD   float32rL   r@   getPerspectiveTransformrF   warpPerspectiveINTER_NEARESTBORDER_CONSTANT)r   rq   rr   r   rg   rh   img_text	draw_textr   pts1pts2Mrw   s                r   rV   rV      s`     	%Aq	CF1I-..!3eCF1IAq	<Q6R6RVW6WWXX J 	%Aq	CF1I-..!3eCF1IAq	<Q6R6RVW6WWXX I A	M!!j2oo9UY
$;_MMN8,,	 	'i-DiPPD6391    9UY
$;_MMN8,,	 	CsY
$;YGGDNNAq63YTNBBB:
Q)Q)Z!81j/J D 8Crz***D#D$//Ax111H(	&#  N r   r   r    c                     |\  }}|D ]K}|                      ||f|||           |                    |          }	|	d         |	d         z
  }
||
|z   z  }Ld S )N)r   r1   r   r!   )r   getbbox)drawpositionr   r   r1   r   r,   r-   charbboxchar_heights              r   r   r      sv    DAq ( (		1a&$T	555||D!!1gQ'	[<''	( (r   pointsc                    t          j        |           }t          t          t          j        |                    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          j        | |         | |         | |         | |         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                     | d         S )Nr   r   )r,   s    r   <lambda>z"get_minarea_rect.<locals>.<lambda>  s
    QqT r   )key)r   r!   r    r   r!   r   r   r    )	r@   minAreaRectsortedlist	boxPointsrD   rL   astyperX   )r   bounding_boxindex_aindex_bindex_cindex_drq   s          r   rP   rP     s     ?6**LD|4455>>JJJF)3&GWgway|fQil""ay|fQil""
(	&/6'?F7OL fRX  Jr   )r   r    )r<   rG   typingr   numpyrD   PILr   r   
utils.depsr   r   r	   utils.fontsr
   r   r   common.resultr   r   r@   r   rU   ndarrayr   rV   r   rP   r   r   r   <module>r      s                               X X X X X X X X X X K K K K K K K K K K 4 4 4 4 4 4 4 4+,, JJJ ,--a9 a9 a9 a9 a9 a9 a9 .-a9J /0044*4+.4;>4Z4 4 4 104n /00( ( ( 10( RZ  BJ            r   