
    Ng&                         d dl Z d dlZd dlZd dlZd dlZd dlmZ ddlm	Z	m
Z
 ddlmZmZmZmZ ddlmZ  e            rd dlmc mZ d dlmc mZ d Z G d	 d
e
          Z G d de	          ZdS )    N)imencode   )BaseOCRAgentBaseOCRElementType   )Layout	TextBlockQuadrilateralr	   )is_gcv_availablec                 >    t          j        d | D                       S )Nc                 *    g | ]}|j         |j        gS  )xy).0vertexs     V/var/www/html/ai-engine/env/lib/python3.11/site-packages/layoutparser/ocr/gcv_agent.py
<listcomp>z/_cvt_GCV_vertices_to_points.<locals>.<listcomp>"   s!    AAAffh)AAA    )nparray)verticess    r   _cvt_GCV_vertices_to_pointsr   !   s!    8AAAAABBBr   c                   R    e Zd ZdZdZdZdZdZdZe	d             Z
e	d             Zd	S )
GCVFeatureTypez8
    The element types from Google Cloud Vision API
    r   r   r         c           
          t           j        dt           j        dt           j        dt           j        dt           j        di}||          S )Npagesblocks
paragraphswordssymbolsr   PAGEBLOCKPARAWORDSYMBOL)selfname_cvts     r   	attr_namezGCVFeatureType.attr_name0   s?      (!9
 ~r   c           
          t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        t           j        d i}||          S )Nr$   )r*   	child_cvts     r   child_levelzGCVFeatureType.child_level;   sK     !5 ."5!4!6!4
	 r   N)__name__
__module____qualname____doc__r%   r&   r'   r(   r)   propertyr,   r/   r   r   r   r   r   %   sm          DEDDF  X   X  r   r   c                       e Zd ZdZdgZddZed             Zd Z	 	 	 dd	Z	e
d
             Ze
d             Zd Zd ZdS )GCVAgentaJ  A wrapper for `Google Cloud Vision (GCV) <https://cloud.google.com/vision>`_ Text
    Detection APIs.

    Note:
        Google Cloud Vision API returns the output text in two types:

        * `text_annotations`:

            In this format, GCV automatically find the best aggregation
            level for the text, and return the results in a list. We use
            :obj:`~gather_text_annotations` to reterive this type of
            information.

        * `full_text_annotation`:

            To support better user control, GCV also provides the
            `full_text_annotation` output, where it returns the hierarchical
            structure of the output text. To process this output, we provide
            the :obj:`~gather_full_text_annotation` function to aggregate the
            texts of the given aggregation level.
    zgoogle-cloud-visionN.pngc                     	 t          j                    | _        n#  t          j        d           Y nxY wt           j                            |          | _        || _        dS )a  Create a Google Cloud Vision OCR Agent.

        Args:
            languages (:obj:`list`, optional):
                You can specify the language code of the documents to detect to improve
                accuracy. The supported language and their code can be found on `this page
                <https://cloud.google.com/vision/docs/languages>`_.
                Defaults to None.

            ocr_image_decode_type (:obj:`str`, optional):
                The format to convert the input image to before sending for GCV OCR.
                Defaults to `".png"`.

                    * `".png"` is suggested as it does not compress the image.
                    * But `".jpg"` could also be a good choice if the input image is very large.
        zJThe GCV credential has not been set. You could not run the detect command.)language_hintsN)	_visionImageAnnotatorClient_clientwarningswarntypesImageContext_contextocr_image_decode_type)r*   	languagesrB   s      r   __init__zGCVAgent.__init__`   sm    "	"799DLL	M\      22)2LL%:"""s    3c                 0    |t           j        d<    | di |S )zSpecifiy the credential to use for the GCV OCR API.

        Args:
            credential_path (:obj:`str`): The path to the credential file
        GOOGLE_APPLICATION_CREDENTIALSr   )osenviron)clscredential_pathkwargss      r   with_credentialzGCVAgent.with_credentialz   s$     8G
34s}}V}}r   c                     t           j                            |          }| j                            || j                  }|S )N)content)imageimage_context)r:   r?   Imager<   document_text_detectionrA   )r*   img_contentresponses      r   _detectzGCVAgent._detect   sD    m))+)>><77T] 8 
 
 r   Fc                    t          |t          j                  r.t          | j        |          d                                         }nVt          |t                    rAt          j        |d          5 }|	                                }ddd           n# 1 swxY w Y   | 
                    |          }|r|S |r|j        j        S ||                     ||          S |                     |          S )a  Send the input image for OCR.

        Args:
            image (:obj:`np.ndarray` or :obj:`str`):
                The input image array or the name of the image file
            return_response (:obj:`bool`, optional):
                Whether directly return the google cloud response.
                Defaults to `False`.
            return_only_text (:obj:`bool`, optional):
                Whether return only the texts in the OCR results.
                Defaults to `False`.
            agg_output_level (:obj:`~GCVFeatureType`, optional):
                When set, aggregate the GCV output with respect to the
                specified aggregation level. Defaults to `None`.
        r   rbN)
isinstancer   ndarrayr   rB   tostringstrioopenreadrU   full_text_annotationtextgather_full_text_annotationgather_text_annotations)r*   rO   return_responsereturn_only_textagg_output_levelrS   
image_fileress           r   detectzGCVAgent.detect   s'   , eRZ(( 	0"4#=uEEaHQQSSKKs## 	0%% 0(oo//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ll;'' 	J 	1+00'33C9IJJJ++C000s   3BBBc                 
   | j         dd         }t                      }t          |          D ]U\  }}t          |j        j                  }|                    t          t          |          |j	        |                     V|S )a9  Convert the text_annotations from GCV output to an :obj:`Layout` object.

        Args:
            response (:obj:`AnnotateImageResponse`):
                The returned Google Cloud Vision AnnotateImageResponse object.

        Returns:
            :obj:`Layout`: The reterived layout from the response.
        r   N)blockr`   id)
text_annotationsr   	enumerater   bounding_polyr   appendr	   r
   description)rT   docgathered_texti	text_comppointss         r   rb   z GCVAgent.gather_text_annotations   s     '+%cNN 	 	LAy01H1QRRF  f 5 5I<QVWXXX    r   c                 4   dddt           j        ffd	|t           j        k    rK| j        d         }t          |j        j                  }t          t          |          |j                  g}n| j	        }g }d |||           t          |          S )a  Convert the full_text_annotation from GCV output to an :obj:`Layout` object.

        Args:
            response (:obj:`AnnotateImageResponse`):
                The returned Google Cloud Vision AnnotateImageResponse object.

            agg_level (:obj:`~GCVFeatureType`):
                The layout level to aggregate the text in full_text_annotation.

        Returns:
            :obj:`Layout`: The reterived layout from the response.
        Nc                 j   t          | |j                  D ]}||k    rg }|t          j        k    r|                    |j                   ns|t          j        k    rO|t          j        k    r?g } 
|||||j                   |                    d                    |                     n 
|||||j                   ||k    rkt          |j
        j                  }t          t          |          d                    |          |j        	          }|                    |           	dz  	d S )N  )rj   r`   scorerk   r   )getattrr,   r   r)   ro   r`   r(   r/   joinr   bounding_boxr   r	   r
   
confidence)iter	agg_leveltext_blockstexts	cur_levelitemcharsru   
text_block
element_id
iter_levels            r   r   z8GCVAgent.gather_full_text_annotation.<locals>.iter_level   s]     i&9::  $  $	))E  555LL++++!444!^%:::EJieY=R   LL0000JieY=R   	))89J9STTF!*+F33 XXe__"o%	" " "J  &&z222!OJA $  $r   r   )rj   r`   )r   r%   rl   r   rn   r   r	   r
   rp   r_   r   )rT   r   rq   ru   r   r   r   s        @@r   ra   z$GCVAgent.gather_full_text_annotation   s    " $)(	$ (	$ (	$ (	$ (	$ (	$ (	$T ++++A.C01B1KLLF$=+@+@sWWWXKK /CKJJsI{333k"""r   c                     t          |d          5 }|                                }d d d            n# 1 swxY w Y   t          j        |t          j                                        d          S )NrT)ignore_unknown_fields)r]   r^   _json_formatParser:   r?   AnnotateImageResponse)r*   filenamefdatas       r   load_responsezGCVAgent.load_response  s    (C   	A6688D	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!'-5577t
 
 
 	
s   266c                     t          j        |          }t          |d          5 }t          j        |          }t          j        ||           d d d            d S # 1 swxY w Y   d S )Nw)r   MessageToJsonr]   jsonloadsdump)r*   rg   	file_namer   	json_files        r   save_responsezGCVAgent.save_response  s    (--)S!! 	$Q
3IIi###	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$ 	$s   *AA #A )Nr7   )FFN)r0   r1   r2   r3   DEPENDENCIESrD   classmethodrL   rU   rh   staticmethodrb   ra   r   r   r   r   r   r6   r6   G   s         , **L; ; ; ;4   [   (1 (1 (1 (1T   \. D# D# \D#L
 
 
$ $ $ $ $r   r6   )r\   rG   r   r=   numpyr   cv2r   baser   r   elementsr   r	   r
   
file_utilsr   google.protobuf.json_formatprotobufjson_formatr   google.cloud.visioncloudvisionr:   r   r   r6   r   r   r   <module>r      sZ   
			 				             2 2 2 2 2 2 2 2 B B B B B B B B B B B B ) ) ) ) ) ) *666666666)))))))))C C C    '   DY$ Y$ Y$ Y$ Y$| Y$ Y$ Y$ Y$ Y$r   