
    ^j                     d    d dl mZmZmZ d dlmZ ddlmZ ee	e
f   Z G d de      Zdede	fd	Zy
)    )AnyDictUnion)CanExecuteCommands   )MobileCommandc                       e Zd Zdedededeeef   fdZdedededeeee	ef   f   fdZ
dedededeeee	ef   f   fd	ZddZy
)ImagesComparisonbase64_image1base64_image2optsreturnc                 z    dt        |      t        |      |d}| j                  t        j                  |      d   S )a
  Performs images matching by features.

        Read
        https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_matcher/py_matcher.html
        for more details on this topic.
        The method supports all image formats, which are supported by OpenCV itself.

        Args:
            base64_image1: base64-encoded content of the first image
            base64_image2: base64-encoded content of the second image

        Keyword Args:
            visualize (bool): Set it to True in order to return the visualization of the matching operation.
                matching visualization. False by default
            detectorName (str): One of possible feature detector names:
                'AKAZE', 'AGAST', 'BRISK', 'FAST', 'GFTT', 'KAZE', 'MSER', 'SIFT', 'ORB'
                Some of these detectors are not enabled in the default OpenCV deployment.
                'ORB' By default.
            matchFunc (str): One of supported matching functions names:
                'FlannBased', 'BruteForce', 'BruteForceL1', 'BruteForceHamming',
                'BruteForceHammingLut', 'BruteForceSL2'
                'BruteForce' by default
            goodMatchesFactor (int): The maximum count of "good" matches (e. g. with minimal distances).
                This count is unlimited by default.

        Returns:
            The dictionary containing the following entries:

            visualization (bytes): base64-encoded content of PNG visualization of the current comparison
                operation. This entry is only present if `visualize` option is enabled
            count (int): The count of matched edges on both images.
                The more matching edges there are no both images the more similar they are.
            totalCount (int): The total count of matched edges on both images.
                It is equal to `count` if `goodMatchesFactor` does not limit the matches,
                otherwise it contains the total count of matches before `goodMatchesFactor` is
                applied.
            points1 (dict): The array of matching points on the first image. Each point is a dictionary
                with 'x' and 'y' keys
            rect1 (dict): The bounding rect for the `points1` array or a zero rect if not enough matching points
                were found. The rect is represented by a dictionary with 'x', 'y', 'width' and 'height' keys
            points2 (dict): The array of matching points on the second image. Each point is a dictionary
                with 'x' and 'y' keys
            rect2 (dict): The bounding rect for the `points2` array or a zero rect if not enough matching points
                were found. The rect is represented by a dictionary with 'x', 'y', 'width' and 'height' keys
        matchFeaturesmode
firstImagesecondImageoptionsvalue_adjust_image_payloadexecuteCommandCOMPARE_IMAGESselfr   r   r   r   s        a/home/ubuntu/.local/lib/python3.12/site-packages/appium/webdriver/extensions/images_comparison.pymatch_images_featuresz&ImagesComparison.match_images_features   s@    ^ $/>0?	
 ||G22G<WEE    base64_full_imagebase64_partial_imagec                 z    dt        |      t        |      |d}| j                  t        j                  |      d   S )a  Performs images matching by template to find possible occurrence of the partial image
        in the full image.

        Read
        https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
        for more details on this topic.
        The method supports all image formats, which are supported by OpenCV itself.

        Args:
            base64_full_image: base64-encoded content of the full image
            base64_partial_image: base64-encoded content of the partial image

        Keyword Args:
            visualize (bool): Set it to True in order to return the visualization of the matching operation.
                False by default

        Returns:
            The dictionary containing the following entries:
                visualization (bytes): base64-encoded content of PNG visualization of the current comparison
                    operation. This entry is only present if `visualize` option is enabled
                rect (dict): The region of the partial image occurrence on the full image.
                    The rect is represented by a dictionary with 'x', 'y', 'width' and 'height' keys
        matchTemplater   r   r   )r   r!   r"   r   r   s        r   find_image_occurrencez&ImagesComparison.find_image_occurrenceO   sA    6 $/0AB01EF	
 ||G22G<WEEr    c                 z    dt        |      t        |      |d}| j                  t        j                  |      d   S )a?  Performs images matching to calculate the similarity score between them.

        The flow there is similar to the one used in
        `find_image_occurrence`, but it is mandatory that both images are of equal resolution.
        The method supports all image formats, which are supported by OpenCV itself.

        Args:
            base64_image1: base64-encoded content of the first image
            base64_image2: base64-encoded content of the second image

        Keyword Args:
            visualize (boo): Set it to True in order to return the visualization of the matching operation.
                False by default

        Returns:
            The dictionary containing the following entries:
                visualization (bytes): base64-encoded content of PNG visualization of the current comparison
                    operation. This entry is only present if `visualize` option is enabled
                score (float): The similarity score as a float number in range [0.0, 1.0].
                    1.0 is the highest score (means both images are totally equal).
        getSimilarityr   r   r   r   s        r   get_images_similarityz&ImagesComparison.get_images_similarityq   s?    2 $/>0?	
 ||G22G<WEEr    Nc                 Z    | j                   j                  t        j                  dd       y )NPOSTz)/session/$sessionId/appium/compare_images)command_executoradd_commandr   r   )r   s    r   _add_commandszImagesComparison._add_commands   s!    ))'*@*@&Juvr    )r   N)__name__
__module____qualname__Base64Payloadr   r   strr   r   bytesr%   r(   r-    r    r   r
   r
      s    4F= 4FQ^ 4Fhk 4Fptuxz}u}p~ 4Fl F!. FFS F]` F	c5%%	& FDF*F;HFRUF	c5%%	&F@wr    r
   payloadr   c                     	 t        | t              r| S | j                  d      S # t        $ r}t	        d      |d }~ww xY w)Nzutf-8zWThe image payload cannot be serialized to a string. Make sure to base64-encode it first)
isinstancer2   decodeUnicodeDecodeError
ValueError)r5   es     r   r   r      sF    {$Wc2wOw8OO {rsyzz{s   % % 	?:?N)typingr   r   r   /appium.protocols.webdriver.can_execute_commandsr   mobilecommandr   r   r2   r3   r1   r
   r   r4   r    r   <module>r?      sI    $ # N 4c5j!zw) zwz{= {S {r    