
    9i-                     :   S SK r S SKrS SKJr  S SKrS SKJrJr  SSK	J
r
JrJr  SSKJrJrJr  SSKJrJr  \" S	5      (       a  S SKr\
" S	5       " S
 S\5      5       r\" S	5      S\S\R.                  S\S\S\R.                  4
S j5       r\" S	5      SS j5       rg)    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                       \ rS rSrSrS\R                  S\R                  4S jrS\\	\
R                  4   4S jrS\\	\	4   4S jrS\\	\	4   4S jrS	rg
)	OCRResult   z
OCR resultpointsreturnc                 v   [         R                  " U5      n[        [        [         R                  " U5      5      S S9nSu  p4pVUS   S   US   S   :  a  SnSnOSnSnUS   S   US   S   :  a  SnSnOSnSn[
        R                  " X   X   X   X   /5      R                  [
        R                  5      nU$ )aE  
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                     U S   $ )Nr    )xs    f/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/paddlex/inference/pipelines/ocr/result.py<lambda>,OCRResult.get_minarea_rect.<locals>.<lambda>.   s    1    )key)r         r   r   r   r   r   )	cv2minAreaRectsortedlist	boxPointsnparrayastypeint32)selfr   bounding_boxindex_aindex_bindex_cindex_dboxs           r   get_minarea_rectOCRResult.get_minarea_rect"   s     v.S]]<89~N-7*'!9Q<&)A,&GGGG!9Q<&)A,&GGGGhh_fovP

&
 	 
r   c           
      
   SU ;   Ga  / n/ nU S    VVs/ s H  o3  H  oDPM     M     nnnU S    VVs/ s H  o3  H  oDPM     M     nnn[        U5       H  u  pxUn	[        [        R                  " U	S   S   U	S   S   -
  S-  U	S   S   U	S   S   -
  S-  -   5      5      n
[        [        R                  " U	S   S   U	S   S   -
  S-  U	S   S   U	S   S   -
  S-  -   5      5      nU
S:X  d  US:X  a  M  UR	                  U5        UR	                  Xg   5        M     O
U S   nU S   nU S	   S
   nUR
                  SS u  p[        R                  " U[        R                  5      n[        R                  " U5      n[        R                  " XS4[        R                  S9S-  n[        R                  " S5        [         R"                  " U5      n[$        nU S   (       a  U S   S   n[        ['        X5      5       GH'  u  nu  nn [        R(                  " SS5      [        R(                  " SS5      [        R(                  " SS5      4n[        R*                  " U5      n[-        U5      S:  a  UR/                  5        VVs/ s H
  u  nnUU4PM     nnnUR1                  UUSUS9  U R3                  U5      n[        S[5        USS2S4   5      [7        USS2S4   5      -
  -  5      n[        R8                  " USS2S4   5      USS2S4'   [        R8                  " USS2S4   5      [7        SU5      -   USS2S4'   OIUR/                  5        VVs/ s H  u  nn[        U5      [        U5      4PM     nnnUR1                  UUS9  [;        U[<        5      (       a  US   n[?        X4UUUR@                  5      n[        R*                  " U[        RB                  5      RE                  S5      n[        RF                  " UU/SUS5        [        RH                  " UU5      nGM*     [        RJ                  " [        R                  " U5      US5      n[        RL                  " SUS-  U4S5      nURO                  USSX45        URO                  [        R                  " U5      USUS-  U45        U S   nSU0nUS   (       a  URP                  " S0 U S	   RR                  D6  U$ s  snnf s  snnf s  snnf s  snnf !    GM	  = f)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_region	text_wordr   r   r   r   	rec_polys	rec_textsdoc_preprocessor_res
output_imgdtype   	vis_fontsr      )outlinewidthfillg      ?N   )r@   )r   r   TRGBr;   r;   r;   model_settingsocr_res_imguse_doc_preprocessorr   )*	enumerateintmathsqrtappendshaper    cvtColorCOLOR_BGR2RGBr   	fromarrayr%   onesuint8randomseedr   Drawr
   ziprandintr&   lentolistpolygonr0   maxminmean
isinstancetupledraw_box_txt_finepathr(   reshape	polylinesbitwise_andblendnewpasteupdateimg) r)   boxestxtssublistitemr3   r4   idxword_regionchar_box
box_height	box_widthimagehw	image_rgbimg_left	img_right	draw_leftvis_fontr/   txtcolorr   yptsheightbox_ptsimg_right_textimg_showrE   res_img_dicts                                    r   _to_imgOCRResult._to_imgD   s    %ED$();$< $<$<    .2+->S->'747->IS$-.>$? & II!!Q(1+a.8Q>#A;q>HQKN:q@A
  II!!Q(1+a.8Q>#A;q>HQKN:q@A	 ?i1n[)IN+# %@& %E$D+,\:{{1QLL(9(9:	??9-GGQ1IRXX6<	ANN8,	K(+H(U)9:OC#sNN1c*NN1c*NN1c*
 hhsms8a<.1jjl;ldaAq6lC;%%c5%N//4C C1ISAY(G!HIF!#QT!3CAJ!#QT!3c"fo!ECAJ<?JJLILDAqAA/LGI%%gE%:c5))a&C!2A63X]]!ShhsBHH-55jAnseT5!DOOI~F	/  ;6 ;;uy98SI99UQUAJ@x!Q.uy1Aq!a%3CD./%101C$'=">"B"BCO  TR < Js7   S(S.A<T S4C T #S:8B,T 4T  Tc                    0 nU S   US'   U S   US'   U S   US'   U S   S   (       a  U S   R                   S   US'   U S   S:X  a  U S	   O[        R                  " U S	   5      US	'   U S
   US
'   U S   US'   SU ;   a  [        R                  " U S   5      US'   U S   US'   U S   US'   U S   US'   [        R                  " U S   5      US'   U S   S:X  a  U S   O[        R                  " U S   5      US'   [        R                  " U S   5      US'   SU ;   a  U S   US'   U S   US'   [        R                  " U/UQ70 UD6$ )aH  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_indexrE   rG   r7   res	text_typesealdt_polystext_det_paramstextline_orientation_anglestext_rec_score_threshreturn_word_boxr6   
rec_scoresr5   	rec_boxestext_word_boxesr4   )strr%   r&   r   _to_strr)   argskwargsdatas       r   r   OCRResult._to_str   s    !,/\!,/\!%&6!7 !"89+/0F+G+K+KE+RD'( K F* $z*+ 	Z
 #''8"9 -[(D024((233D./ )--D(E$%"&'8"9 -[XXd<&89\ K F* ${+, 	[
 HHT+%67[$&*+<&=D"# $[ 1D  7777r   c                    0 nU S   US'   U S   US'   U S   US'   U S   S   (       a  U S   R                   S   US'   U S   US'   U S   US'   U S	   US	'   S
U ;   a  U S
   US
'   U S   US'   U S   US'   U S   US'   U S   US'   U S   US'   U S   US'   SU ;   a  U S   US'   U S   US'   [        R                  " U/UQ70 UD6$ )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   rE   rG   r7   r   r   r   r   r   r   r   r6   r   r5   r   r   r4   )jsonr   _to_jsonr   s       r   r   OCRResult._to_json   sL    !,/\!,/\!%&6!7 !"89+/0F+G+L+LU+SD'(
+Z"&'8"9 -[(D0267T2UD./(,-D(E$%"&'8"9 -[!,/\ -[ -[$&*+<&=D"# $[ 1D!!$8888r   r   N)__name__
__module____qualname____firstlineno____doc__r%   ndarrayr0   r   r   r   r   r   r   __static_attributes__r   r   r   r   r      sg     rzz  bjj  DRc5;;./ Rh)8$sCx. )8V94S> 9r   r   img_sizer/   r{   	font_pathr   c           
      <   [        [        R                  " [        US   S   US   S   -
  5      S-  [        US   S   US   S   -
  5      S-  -   5      5      n[        [        R                  " [        US   S   US   S   -
  5      S-  [        US   S   US   S   -
  5      S-  -   5      5      nUSU-  :  aX  US:  aR  [        R
                  " SXT4S5      n[        R                  " U5      nU(       a  [        X%U4U5      n[        USX(S	SS
9  OX[        R
                  " SXT4S5      n[        R                  " U5      nU(       a"  [        X%U4U5      nUR                  SS/US	US9  [        R                  " SS/US/XT/SU//5      n	[        R                  " U[        R                  S9n
[        R                   " X5      n[        R                  " U[        R"                  S9n[        R$                  " UUU [        R&                  [        R(                  SS9nU$ )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   rC   rD   )r   r   r   r   r   )r@   line_spacing)r@   fontr9   )flags
borderModeborderValue)rI   rJ   rK   floatr   rf   r   rU   r   draw_vertical_textr   textr%   float32r&   r    getPerspectiveTransformrR   warpPerspectiveINTER_NEARESTBORDER_CONSTANT)r   r/   r{   r   rq   rr   img_text	draw_textr   pts1pts2Mr   s                r   r`   r`      s     		%Aq	CF1I-.!3eCF1IAq	<Q6RVW6WWXJ 		%Aq	CF1I-.!3eCF1IAq	<Q6RVW6WWXI A	M!j2o99UY$;_MNN8,	'-DiPD6391 99UY$;_MNN8,	s
$;YGDNNAq63YTNB::
Q)Q)!81j/JD 88Crzz*D##D/Axx1H((	&&#N r   c                     Uu  pgU H8  nU R                  Xg4XUS9  UR                  U5      n	U	S   U	S   -
  n
XzU-   -  nM:     g )N)r   r@   r   r   )r   getbbox)drawpositionr   r   r@   r   r   r}   charbboxchar_heights              r   r   r     sT    DA		1&$	5||D!1gQ'	<''	 r   )r   r   )rJ   rS   typingr   numpyr%   PILr   r   
utils.depsr   r   r	   utils.fontsr
   r   r   common.resultr   r   r    r   r_   r   r   r`   r   r   r   r   <module>r      s          X X K K 4+,, ,-C9 C9 .C9N /044**4+.4;>4ZZ4 14n /0( 1(r   