o
    ,hD"                     @   sx   d Z ddlZddlZddlZddlm  mZ ddlm	Z	 							dd	d
Z
dddZ							dddZdS )zs
Created by Jaided AI
Released Date: 18/08/2022
Description:
A wrapper for DBNet text detection module for EasyOCR
    N   )DBNet皙?   Fcpuc	                    s
  t | tjrt| jdkr| }	n| g}	t fdd|	D  \}
}fdd|
D }
tt|
	|}t
 @ |	|}j|||||||dd\}}|rcj|||||||dd\}}n|}W d   ||fS W d   ||fS 1 s|w   Y  ||fS )	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
       c                    s   g | ]} | qS  )resize_image).0imgcanvas_sizedetectorr   O/var/www/html/scripts/venv/lib/python3.10/site-packages/easyocr/detection_db.py
<listcomp>J   s    ztest_net.<locals>.<listcomp>c                    s   g | ]}t  |d qS ))   r   r   )np	transposenormalize_image)r
   n_img)r   r   r   r   L   s    F)text_thresholdbbox_min_scorebbox_min_sizemax_candidates
as_polygonTN)
isinstancer   ndarraylenshapeziptorch
from_numpyarraytono_grad
image2hmap	hmap2bbox)imager   	thresholdr   r   r   r   polydevice
image_arrsimagesoriginal_shapesimage_tensorhmapbboxes_polysr   r   r   test_net   sJ   5

	


r3   resnet18Tc                 C   s   t dtjdd|dd}|dvrtd||j| d |  t|j	d	kr=|r<ztj
j|tjd
d W n   Y nntj|j||_|t_|j  |S )a  
    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)r4   resnet50z7Invalid backbone. Options are 'resnet18' or 'resnet50'.modelr   T)dtypeinplace)r   ospathjoin
ValueErrorr6   configsr    r*   typequantizationquantize_dynamicqint8nnDataParallelr:   r#   cudnn	benchmarkeval)trained_modelbackboner*   quantizecudnn_benchmarkdbnetr   r   r   get_detectori   s*   
rP   c	                 K   s^   t |j| jkrtdg d| j|t|| |||||||d	\}
}dd |D }|S )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*   c                 S   s   g | ]	}d d |D qS )c                 S   s$   g | ]}t |t jd qS ))r   r"   astypeint32reshape)r
   boxr   r   r   r      s   $ z*get_textbox.<locals>.<listcomp>.<listcomp>r   )r
   r2   r   r   r   r      s    zget_textbox.<locals>.<listcomp>)r    r*   rB   RuntimeErrorr?   formatr3   )r   r'   r   r)   r(   r   r   r   r*   kwargsr1   
polys_listresultr   r   r   get_textbox   s    3

r\   )r   r   r   r   NFr   )r4   r   TF)NFr   r   r   r   r   )__doc__r=   numpyr   r    torch.backends.cudnnbackendsrH   DBNet.DBNetr   r3   rP   r\   r   r   r   r   <module>   s.    

Z.