
    i!!                     b    S r SSKrSSKrSSKrSSKJr  SSKJrJ	r	  S r
S	S jr " S S\5      rg)
zv
This code is refer from:
https://github.com/open-mmlab/mmocr/blob/v0.3.0/mmocr/models/textdet/postprocess/wrapper.py
    N)ifft)poly_nmsvalid_boundaryc                    U R                   u  p[        R                  " US-   US-   4[        R                  5      nU R	                  5       USUS-   2SUS-   24'   [        R                  " US-   US-   4[        R                  5      n[
        R                  " X4SS5        USUS-   2SUS-   24   R                  [        R                  5      nU) U -  $ )N         )r   r   )	shapenpzerosuint8copycv2	floodFillastypebool_)
input_maskhwcanvasmasks        k/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddleocr/ppocr/postprocess/fce_postprocess.py	fill_holer      s    DAXXq1ua!enbhh/F#-??#4F1q1u9a!a%i 88QUAENBHH-DMM&*AAIq1q5y()00:F7Z    c                    [         R                  " [        U 5      U4SS9n[        U S   5      S-
  S-  nU SS2US24   USS2SUS-   24'   U SS2SU24   USS2U* S24'   [        U5      U-  n[         R                  " [        U 5      US45      nUR                  USS2SS2S4'   UR
                  USS2SS2S4'   UR                  S5      R                  [        U 5      S45      $ )	aL  Inverse Fourier transform
Args:
    fourier_coeff (ndarray): Fourier coefficients shaped (n, 2k+1),
        with n and k being candidates number and Fourier degree
        respectively.
    num_reconstr_points (int): Number of reconstructed polygon points.
Returns:
    Polygons (ndarray): The reconstructed polygons shaped (n, n')
complexdtyper   r   r   Nint32)r   r   lenr   realimagr   reshape)fourier_coeffnum_reconstr_pointsakpoly_complexpolygons         r   fourier2polyr+   '   s     	#m$&9:)LA	]1		"q(A#AqrE*AaQUlOa!e$Aa!fI700LhhM*,?CDG#((GAq!G#((GAq!G>>'"**C,>+CDDr   c                   ^    \ rS rSrSr        SS jrS rS rS rS r	     SS jr
S	rg
)FCEPostProcess?   z
The post process for FCENet.
c
                 p    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        Xl        g )N)	scalesfourier_degreer&   decoding_type	score_thrnms_thralphabetabox_type)selfr0   r1   r&   r2   r3   r4   r5   r6   r7   kwargss              r   __init__FCEPostProcess.__init__D   s4     ,#6 *"
	 r   c                     / nUR                  5        Hh  u  pE[        U[        R                  5      (       a  UR	                  5       nUS S 2S S2S S 2S S 24   nUS S 2SS 2S S 2S S 24   nUR                  Xg/5        Mj     U R                  X25      $ )Nr	   )items
isinstancepaddleTensornumpyappendget_boundary)r8   preds
shape_list
score_mapskeyvaluecls_resreg_ress           r   __call__FCEPostProcess.__call__[   s    
++-JC%//Arr1aK(GAqr1aK(Gw01 (   88r   c           	         / n/ nU H  n[        U5      n[        US5        UR                  US   5        [        R                  " USUS-
   5      [        R
                  " USS [        US-
  S-  5      5      R                  SUS-
  5      -  R                  5       R                  5       nUR                  [        R                  " U5      R                  SS/5      5        M     [        R                  " U[        R                  S9U4$ )a  Rescale boundaries via scale_factor.

Args:
    boundaries (list[list[float]]): The boundary list. Each boundary
    with size 2k+1 with k>=4.
    scale_factor(ndarray): The scale factor of size (4,).

Returns:
    boundaries (list[list[float]]): The scaled boundaries.
Tr    Nr   r   r   )r!   r   rB   r   arraytileintr$   flattentolistfloat32)r8   
boundariesscale_factorboxesscoresbszs          r   resize_boundaryFCEPostProcess.resize_boundaryf   s     AQB1d#MM!B%  HHQxa[)ww|BQ/b1f\1BCKKArTUvVX   LL!,,b!W56  xxRZZ0&88r   c                 `   [        U5      [        U R                  5      :X  d   e/ n[        U5       H'  u  pEU R                  U   nX0R                  XV5      -   nM)     [	        X0R
                  5      nU R                  USUSSS 24   -  R                  5       S S S2   5      u  p7[        X7S9/nU$ )Nr   r   r   r    )pointsrW   )	r!   r0   	enumerate_get_boundary_singler   r4   rZ   rR   dict)	r8   rF   rE   rT   idx	score_mapscalerW   boxes_batchs	            r   rC   FCEPostProcess.get_boundary   s    :#dkk"2222
'
3NCKK$E#&?&?	&QQJ 4
 j,,7
!11Z12..6682>

 :=>r   c                 .   [        U5      S:X  d   eUS   R                  S   SU R                  -  S-   :X  d   eU R                  UU R                  U R                  UU R
                  U R                  U R                  U R                  U R                  S9	$ )Nr   r   r	   )	rD   r1   r&   rc   r5   r6   r7   r3   r4   )
r!   r
   r1   fcenet_decoder&   r5   r6   r7   r3   r4   )r8   rb   rc   s      r   r_   #FCEPostProcess._get_boundary_single   s    9~"""|!!!$D,?,?(?!(CCCC!!.. $ 8 8**]]nnLL " 

 
	
r   c
                    [        U[        5      (       d   e[        U5      S:X  d   eUS;   d   eUS   S   n
U
SS nU
SS nUS   S   R                  / SQ5      nUSS2SS2SSU-  S-   24   nUSS2SS2SU-  S-   S24   nUS   U-  US   U-  -  nUU:  n[	        U5      n[
        R                  " UR                  [        R                  5      [
        R                  [
        R                  5      u  nn[        R                  " U5      n/ nU GH  nUR                  5       R                  [        R                  5      n[
        R                  " UU/SSS5        UU-  nUS:  n[        R                   " U5      nUSS2S4   USS2S4   S-  -   nUU   UU   nnUUS-  -   nUSS2U4   U-   USS2U4'   UU-  n[#        UU5      n UU   R%                  SS5      n!['        [        R(                  " U U!45      R+                  5       U	5      n UU -   nGM     ['        UU	5      nUS	:X  a  / n"U H  n#[        R,                  " U#SS 5      R%                  SS5      R                  [        R.                  5      n$U#S   n![
        R0                  " [
        R2                  " U$5      5      n%[        R4                  " U%5      n%U"R7                  U%R%                  S5      R+                  5       U!/-   5        U"nM     U$ )
ad  Decoding predictions of FCENet to instances.

Args:
    preds (list(Tensor)): The head output tensors.
    fourier_degree (int): The maximum Fourier transform degree k.
    num_reconstr_points (int): The points number of the polygon
        reconstructed from predicted Fourier coefficients.
    scale (int): The down-sample scale of the prediction.
    alpha (float) : The parameter to calculate final scores. Score_{final}
            = (Score_{text region} ^ alpha)
            * (Score_{text center region}^ beta)
    beta (float) : The parameter to calculate final score.
    box_type (str):  Boundary encoding type 'poly' or 'quad'.
    score_thr (float) : The threshold used to filter out the final
        candidates.
    nms_thr (float) :  The threshold of nms.

Returns:
    boundaries (list[list[float]]): The instance boundary and confidence
        list.
r   )polyquadr   Nr   )r   r   r   r    y              ?rk   )r>   listr!   	transposer   r   findContoursr   r   r   	RETR_TREECHAIN_APPROX_SIMPLE
zeros_liker   int8drawContoursargwherer+   r$   r   hstackrR   rN   rS   	boxPointsminAreaRectint64rB   )&r8   rD   r1   r&   rc   r5   r6   r7   r3   r4   cls_predtr_predtcl_predreg_predx_predy_pred
score_predtr_pred_masktr_masktr_contours_r   rT   contdeal_maprb   
score_maskxy_textdxyxycpolygonsscorenew_boundariesboundaryrj   r]   s&                                         r   rg   FCEPostProcess.fcenet_decode   s   B %&&&&5zQ++++8A;1Q-AB<8A;((3!Q 8!n"4q"8 889!QN 2Q 6 889ajE)hqkT.AB
"i/L)))NN288$cmmS5L5L
Q }}W%
Dyy{))"''2HXvr1b9"X-I"QJkk*-G!Q$-'!Q$-""44C*%vj'9qAAF
A#$Q%6#7##=Aa JA#A':;Hj)11"a8E		8U*; < C C EwOH#h.J%  ( j'2
vN&xx".66r1=DDRZZP st'<=&)%%fnnR&8&?&?&AUG&KL+
 ' r   )	r5   r6   r7   r2   r1   r4   r&   r0   r3   N)   2   fcenet333333?皙?      ?r   rj   )r   g       @rj   r   r   )__name__
__module____qualname____firstlineno____doc__r:   rK   rZ   rC   r_   rg   __static_attributes__ r   r   r-   r-   ?   sQ     !.	99: 
, Wr   r-   )r   )r   r   r?   rA   r   	numpy.fftr   ppocr.utils.poly_nmsr   r   r   r+   objectr-   r   r   r   <module>r      s4   
     9
 E0{V {r   