
    kh&                         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y)    N)imencode   )BaseOCRAgentBaseOCRElementType   )Layout	TextBlockQuadrilateralr	   )is_gcv_availablec                 ~    t        j                  | D cg c]  }|j                  |j                  g c}      S c c}w N)nparrayxy)verticesvertexs     V/var/www/teggl/fontify/venv/lib/python3.12/site-packages/layoutparser/ocr/gcv_agent.py_cvt_GCV_vertices_to_pointsr   !   s,    88Affhh)ABBAs   :c                   D    e Zd ZdZdZdZdZdZdZe	d        Z
e	d        Zy	)
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   sN       (!!9
 ~    c           
          t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j                  t         j
                  t         j
                  d i}||    S r   r    )r&   	child_cvts     r   child_levelzGCVFeatureType.child_level;   sf     !5!5  ."5"5!4!4!6!6!!4
	 r)   N)__name__
__module____qualname____doc__r!   r"   r#   r$   r%   propertyr(   r,    r)   r   r   r   %   sH     DEDDF   r)   r   c                   n    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y)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-visionNc                     	 t        j                         | _        t         j
                  j                  |      | _        || _        y#  t        j                  d       Y FxY w)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&   	languagesr?   s      r   __init__zGCVAgent.__init__`   sP    "	"779DL
  22)2L%:"	MM\s   A A!c                 8    |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_CREDENTIALSr2   )osenviron)clscredential_pathkwargss      r   with_credentialzGCVAgent.with_credentialz   s      8G

34}V}r)   c                     t         j                  j                  |      }| j                  j	                  || j
                        }|S )N)content)imageimage_context)r7   r<   Imager9   document_text_detectionr>   )r&   img_contentresponses      r   _detectzGCVAgent._detect   sB    mm))+)><<77T]] 8 
 r)   c                    t        |t        j                        r(t        | j                  |      d   j                         }n?t        |t              r/t        j                  |d      5 }|j                         }ddd       | j                        }|r|S |r|j                  j                  S || j                  ||      S | j                  |      S # 1 sw Y   [xY w)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   r?   tostringstrioopenreadrR   full_text_annotationtextgather_full_text_annotationgather_text_annotations)r&   rL   return_responsereturn_only_textagg_output_levelrP   
image_fileress           r   detectzGCVAgent.detect   s    , eRZZ("4#=#=uEaHQQSKs#% 0(oo/0 ll;'J++000'33C9IJJ++C000 0s   )CCc                     | j                   dd }t               }t        |      D ]T  \  }}t        |j                  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)rQ   docgathered_texti	text_comppointss         r   r_   z GCVAgent.gather_text_annotations   sx     ''+%cN 	LAy01H1H1Q1QRF  f 5I<Q<QVWX	 r)   c                 \   dddt         j                  ffd	|t         j                  k(  rZ| j                  d   }t        |j                  j
                        }t        t        |      |j                        g}t        |      S | 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                 n   t        | |j                        D ]  }||k(  rg }|t        j                  k(  r|j	                  |j
                         nu|t        j                  k(  rL|t        j                  k7  r9g } 
|||||j                         |j	                  dj                  |             n 
|||||j                         ||k(  st        |j                  j                        }t        t        |      dj                  |      |j                  	      }|j	                  |       	dz  	 y )N  )rg   r]   scorerh   r   )getattrr(   r   r%   rl   r]   r$   r,   joinr   bounding_boxr   r	   r
   
confidence)iter	agg_leveltext_blockstexts	cur_levelitemcharsrr   
text_block
element_id
iter_levels            r   r   z8GCVAgent.gather_full_text_annotation.<locals>.iter_level   s     i&9&9:  $	)E  5 55LL+!4!44!^%:%::EieY=R=R LL0ieY=R=R 	)89J9J9S9STF!*+F3 XXe_"oo%	"J  &&z2!OJA $r)   r   )rg   r]   )r   r!   ri   r   rk   r   r	   r
   rm   r\   r   )rQ   r}   rn   rr   r~   r   r   s        @@r   r^   z$GCVAgent.gather_full_text_annotation   s    " $))(	$T +++++A.C01B1B1K1KLF$=+@sWXK k"" //CKJsI{3k""r)   c                     t        |d      5 }|j                         }d d d        t        j                  t        j
                  j                         d      S # 1 sw Y   =xY w)NrT)ignore_unknown_fields)rZ   r[   _json_formatParser7   r<   AnnotateImageResponse)r&   filenamefdatas       r   load_responsezGCVAgent.load_response  sU    (C  	A668D	!!'--557t
 	
	 	s   AA#c                     t        j                  |      }t        |d      5 }t        j                  |      }t        j
                  ||       d d d        y # 1 sw Y   y xY w)Nw)r   MessageToJsonrZ   jsonloadsdump)r&   rd   	file_namer   	json_files        r   save_responsezGCVAgent.save_response  sN    ((-)S! 	$Q

3IIIi#	$ 	$ 	$s   ,AA )Nz.png)FFN)r-   r.   r/   r0   DEPENDENCIESrA   classmethodrI   rR   re   staticmethodr_   r^   r   r   r2   r)   r   r4   r4   G   su    , **L;4   (1T  . D# D#L
$r)   r4   )rY   rD   r   r:   numpyr   cv2r   baser   r   elementsr   r	   r
   
file_utilsr   google.protobuf.json_formatprotobufjson_formatr   google.cloud.visioncloudvisionr7   r   r   r4   r2   r)   r   <module>r      sY    
 	     2 B B )66))C' DY$| Y$r)   