
    9iD"                     x    S r SSKrSSKrSSKrSSKJs  Jr  SSKJ	r	         SS jr
S	S jr       S
S jrg)zs
Created by Jaided AI
Released Date: 18/08/2022
Description:
A wrapper for DBNet text detection module for EasyOCR
    N   )DBNetc	                    [        U [        R                  5      (       a  [        U R                  5      S:X  a  U n	OU /n	[        U	 V
s/ s H  oR                  X5      PM     sn
6 u  pU Vs/ s H)  n[        R                  " UR                  U5      S5      PM+     nn[        R                  " [        R                  " U5      5      R                  U5      n[        R                  " 5          UR                  UR                  U5      5      nUR                  UUUUUUUSS9u  nnU(       a  UR                  UUUUUUUSS9u  nnOUnSSS5        UU4$ s  sn
f s  snf ! , (       d  f       WW4$ = f)a  
A wrapper for DBNet inference routine.

Parameters
----------
image : np.ndarray or list of np.ndarray
    OpenCV BGR image array or list of it.
detector : obj
    DBNet text detection object.
threshold : float, optional
    Minimum probability for each pixel of heatmap tensor to be considered
    as a valid text pixel. The default is 0.2.
bbox_min_score : float, optional
    Minimum score for each detected bounding box to be considered as a
    valid text bounding box. The default is 0.2.
bbox_min_size : int, optional
    Minimum size for each detected bounding box to be considered as a
    valid text bounding box. The default is 3.
max_candidates : int, optional
    Maximum number of detected bounding boxes to be considered as 
    candidates for valid text bounding boxes. Setting to 0 implies
    no maximum. The default is 0.
canvas_size : int, optional
    Target detection size. Input image will be resized such that it's 
    shorter side is equal to the closest multiple of 32 to the provided 
    canvas_size. If detection_size is not provided, it will be resized to 
    the closest multiple of 32 each side. If the original size exceeds the 
    min-/max-detection sizes (specified in DBNet_inference.yaml), it will be 
    resized to be within the min-/max-sizes. The default is None.
poly : boolean, optional
    If true, return the bounding boxes as find polygons, otherwise, return
    as rectagular. The default is False.
device : str, optional
    Device to use. Options are "cpu" and "cuda". The default is 'cpu'.

Returns
-------
bboxes : list of lists
    List of text bounding boxes in format [left, right, top, bottom].
polys : list of lists
    List of polygon text bounding boxes. If argument poly is set to false,
    this output will also hold the value of output bboxes
   )   r   r   F)text_thresholdbbox_min_scorebbox_min_sizemax_candidates
as_polygonTN)
isinstancenpndarraylenshapezipresize_image	transposenormalize_imagetorch
from_numpyarraytono_grad
image2hmap	hmap2bbox)imagedetector	thresholdr	   r
   r   canvas_sizepolydevice
image_arrsimgimagesoriginal_shapesn_imgimage_tensorhmapbboxes_polyss                      T/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/easyocr/detection_db.pytest_netr.      sk   j %$$U[[)9Q)>
W
 "Wa#bWaPS$9$9#$KWa#bcFTZ[TZ5bll833E:IFTZF[##BHHV$4588@L	""<??6#:;&&(+ -6-;,9-;', ' .	 )) , / $1:1?0=1?+/ * 1HE1 E- 
0 5=; $c[ 
0 5=s   E,0E,AE
E0c                 H   [        S[        R                  R                  SS5      USS9nUS;  a  [	        S5      eUR                  UR                  U   S   U 5        [        R                  " U5      R                  S	:X  a7  U(       a/   [        R                  R                  U[        R                  S
S9  OIOH[        R                  R                  UR                  5      R!                  U5      Ul        U["        l        UR                  R'                  5         U$ !    N = f)a9  
A wrapper to initialize DBNet text detection model

Parameters
----------
trained_model : str
    Path to trained weight to use.
backbone : str
    Backbone to use. Options are 'resnet18' or 'resnet50'. The default is 'resnet18'.
device : str, optional
    Device to use. Options are "cpu" and "cuda". The default is 'cpu'.
quantize : boolean, optional
    If use, apply model quantization method to the model. The default is True.
cudnn_benchmark : boolen, optional
    DESCRIPTION. The default is False.

Returns
-------
dbnet : obj
    DBNet text detection object.
Feasyocrr   r   )initialize_modeldynamic_import_relative_pathr"   verbose)resnet18resnet50z7Invalid backbone. Options are 'resnet18' or 'resnet50'.modelcpuT)dtypeinplace)r   ospathjoin
ValueErrorr1   configsr   r"   typequantizationquantize_dynamicqint8nnDataParallelr6   r   cudnn	benchmarkeval)trained_modelbackboner"   quantizecudnn_benchmarkdbnets         r-   get_detectorrM   i   s    , U13i1Q!E //RSS	5==27;(*||F  E)""33EVZ3[  hh++EKK8;;FC)	KKLs   
-D D!c	                    [         R                  " U5      R                  U R                  :w  a6  [        SR	                  / SQ5      R                  U R                  U5      5      e[        UU UUUUUUUS9	u  pU VVs/ s HU  o Vs/ s HE  n[        R                  " U5      R                  [        R                  5      R                  S5      PMG     snPMW     nnnU$ s  snf s  snnf )a+  
A compatibility wrapper to allow supporting calling this method while 
providing argument for other detector classes and reformat output accordingly.

Parameters
----------
detector : obj
    DBNet text detection object.
image : np.ndarray or list of np.ndarray
    OpenCV BGR image array or list of it.
canvas_size : int, optional
    Target detection size. Please see docstring under method resize_image()
    for explanation. The default is None.
poly : boolean, optional
    If true, return the bounding boxes as find polygons, otherwise, return
    as rectagular. The default is False.
threshold : float, optional
    Minimum probability for each pixel of heatmap tensor to be considered
    as a valid text pixel. The default is 0.2.
bbox_min_score : float, optional
    Minimum score for each detected bounding box to be considered as a
    valid text bounding box. The default is 0.2.
bbox_min_size : int, optional
    Minimum size for each detected bounding box to be considered as a
    valid text bounding box. The default is 3.
max_candidates : int, optional
    Maximum number of detected bounding boxes to be considered as 
    candidates for valid text bounding box. Setting it to 0 implies
    no maximum. The default is 0.
device : str, optional
    Device to use. Options are "cpu" and "cuda". The default is 'cpu'.
**kwargs : keyword arguments
    Unused. Added to support calling this method while providing argument 
    for other detector class.

Returns
-------
result : list of lists
    List of text bounding boxes in format [left, right, top, bottom].
 )zCDBNet detector is initialized with {} device, but detection routinezis called with device = {}.z.To use this detector both have to be the same.)r   r	   r
   r   r    r!   r"   )r   r"   r?   RuntimeErrorr<   formatr.   r   r   astypeint32reshape)r   r   r    r!   r   r	   r
   r   r"   kwargsr+   
polys_listr,   boxresults                  r-   get_textboxrZ      s    f ||F  HOO3388 %  vhoov.	0 	0 U%)2.<-:-;+6$(&,	MA \ff[eRWuMurxx}##BHH-55r;uM[eFfM Nfs   =	C#ACC#C#)皙?r[      r   NFr7   )r4   r7   TF)NFr[   r[   r\   r   r7   )__doc__r:   numpyr   r   torch.backends.cudnnbackendsrE   DBNet.DBNetr   r.   rM   rZ        r-   <module>rd      s_    
   $ $  !Xt*\ #!$ !!"Grc   