
     `i a                        d dl Z d dlmZmZ d dlZd dlZddlmZm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZm Z  ddl!m"Z" d	d
l#m$Z$  e            rd dl%m&Z&m'Z'  e            rd dl%Z% ej(        e)          Z*	 ddej+        deee,ef                  fdZ-	 ddedeee,ef                  defdZ.defdZ/ e"d           G d de                      Z0dgZ1dS )    N)OptionalUnion   )BaseImageProcessorBatchFeatureget_size_dict)resizeto_channel_dimension_format)ChannelDimension
ImageInput	ImageTypePILImageResamplingget_image_typeinfer_channel_dimension_formatis_pil_imageis_scaled_imageis_valid_imageis_vision_availableto_numpy_arrayvalid_imagesvalidate_preprocess_arguments)
TensorTypeis_matplotlib_availableloggingrequires_backends)requires   )LightGlueKeypointMatchingOutput)Image	ImageDrawimageinput_data_formatc                    |t           j        k    r[| j        d         dk    rdS t          j        | d         | d         k              o#t          j        | d         | d         k              S |t           j        k    r[| j        d         dk    rdS t          j        | d         | d	         k              o#t          j        | d	         | d
         k              S d S )Nr   r   Tr   .r   .   ..r   .r   .r'   )r   FIRSTshapenpallLAST)r!   r"   s     /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/lightglue/image_processing_lightglue.pyis_grayscaler2   9   s     ,222;q>Q4veFmuV}455`"&vRWX^R_A_:`:``	.3	3	3;r?a4veFmuV}455`"&vRWX^R_A_:`:`` 
4	3    returnc                 2   t          t          dg           t          | t          j                  rt          | |          r| S |t          j        k    r>| d         dz  | d         dz  z   | d         dz  z   }t          j        |gd	z  d
          }nM|t          j	        k    r=| d         dz  | d         dz  z   | d         dz  z   }t          j        |gd	z  d          }|S t          | t          j        j                  s| S |                     d          } | S )ao  
    Converts an image to grayscale format using the NTSC formula. Only support numpy and PIL Image. TODO support torch
    and tensorflow grayscale conversion

    This function is supposed to return a 1-channel image, but it returns a 3-channel image with the same value in each
    channel, because of an issue that is discussed in :
    https://github.com/huggingface/transformers/pull/25786#issuecomment-1730176446

    Args:
        image (Image):
            The image to convert.
        input_data_format (`ChannelDimension` or `str`, *optional*):
            The channel dimension format for the input image.
    visionr"   r$   gŏ1w-!?r%   gbX9?r&   gv/?r   r   )axisr)   r*   r+   r(   L)r   convert_to_grayscale
isinstancer.   ndarrayr2   r   r,   stackr0   PILr   convert)r!   r"   
gray_images      r1   r:   r:   G   s+   $ *XJ777%$$ 	1BCCC 	L 0 666v/%-&2HH5QW=[aKaaJ:,"2;;;JJ"2"777v/%-&2HH5QW=[aKaaJ:,"2<<<JeSY_-- MM#ELr3   imagesc                    d}d t          | t                    rWt          |           dk    rt          fd| D                       r| S t          fd| D                       rd | D             S t	          |          )N)z-Input images must be a one of the following :z - A pair of PIL images.z - A pair of 3D arrays.z! - A list of pairs of PIL images.z  - A list of pairs of 3D arrays.c                     t          |           pCt          |           o4t          |           t          j        k    ot          | j                  dk    S )z$images is a PIL Image or a 3D array.r   )r   r   r   r   r>   lenr-   )r!   s    r1   _is_valid_imagez8validate_and_format_image_pairs.<locals>._is_valid_imagev   sN    E"" 
5!!fnU&;&;y}&LfQTUZU`QaQaefQf	
r3   r'   c              3   .   K   | ]} |          V  d S N .0r!   rE   s     r1   	<genexpr>z2validate_and_format_image_pairs.<locals>.<genexpr>}   s-      #Q#Q__U%;%;#Q#Q#Q#Q#Q#Qr3   c              3      K   | ]G}t          |t                    o-t          |          d k    ot          fd|D                       V  HdS )r'   c              3   .   K   | ]} |          V  d S rG   rH   rI   s     r1   rK   z<validate_and_format_image_pairs.<locals>.<genexpr>.<genexpr>   s-      CCuOOE**CCCCCCr3   N)r;   listrD   r/   )rJ   
image_pairrE   s     r1   rK   z2validate_and_format_image_pairs.<locals>.<genexpr>   s}       
 
  z4(( DJ1$DCCCC
CCCCC
 
 
 
 
 
r3   c                     g | ]	}|D ]}|
S rH   rH   )rJ   rO   r!   s      r1   
<listcomp>z3validate_and_format_image_pairs.<locals>.<listcomp>   s%    KKKj
KKuEKKKKr3   )r;   rN   rD   r/   
ValueError)rA   error_messagerE   s     @r1   validate_and_format_image_pairsrT   m   s    M
 
 
 &$ 	Lv;;!#Q#Q#Q#Q&#Q#Q#Q Q QM 
 
 
 
 %	
 
 
 
 
 	L LKFKKKK
]
#
##r3   )torch)backendsc                       e Zd ZdZdgZddej        dddfdedee	e
ef                  ded	ed
ededdf fdZ	 	 ddej        de	e
ef         deee
ef                  deee
ef                  fdZdddddddej        df	dee         dee	e
ef                  dee         d	ee         d
ee         dee         deee
ef                  dedeee
ef                  defdZ	 d dedeeee         f         dedee	e
ej        f                  fdZdedee	e
ej        f                  ded         fdZd ZdedefdZ  xZ!S )!LightGlueImageProcessorap  
    Constructs a LightGlue image processor.

    Args:
        do_resize (`bool`, *optional*, defaults to `True`):
            Controls whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden
            by `do_resize` in the `preprocess` method.
        size (`dict[str, int]` *optional*, defaults to `{"height": 480, "width": 640}`):
            Resolution of the output image after `resize` is applied. Only has an effect if `do_resize` is set to
            `True`. Can be overridden by `size` in the `preprocess` method.
        resample (`PILImageResampling`, *optional*, defaults to `Resampling.BILINEAR`):
            Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method.
        do_rescale (`bool`, *optional*, defaults to `True`):
            Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in
            the `preprocess` method.
        rescale_factor (`int` or `float`, *optional*, defaults to `1/255`):
            Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess`
            method.
        do_grayscale (`bool`, *optional*, defaults to `True`):
            Whether to convert the image to grayscale. Can be overridden by `do_grayscale` in the `preprocess` method.
    pixel_valuesTNgp?	do_resizesizeresample
do_rescalerescale_factordo_grayscaler4   c                      t                      j        di | ||nddd}t          |d          }|| _        || _        || _        || _        || _        || _        d S )Ni  i  )heightwidthFdefault_to_squarerH   )	super__init__r   rZ   r[   r\   r]   r^   r_   )	selfrZ   r[   r\   r]   r^   r_   kwargs	__class__s	           r1   rf   z LightGlueImageProcessor.__init__   s|     	""6"""'ttc-J-JTU;;;"	 $,(r3   r!   data_formatr"   c                 b    t          |d          }t          |f|d         |d         f||d|S )aL  
        Resize an image.

        Args:
            image (`np.ndarray`):
                Image to resize.
            size (`dict[str, int]`):
                Dictionary of the form `{"height": int, "width": int}`, specifying the size of the output image.
            data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format of the output image. If not provided, it will be inferred from the input
                image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        Frc   ra   rb   )r[   rj   r"   )r   r	   )rg   r!   r[   rj   r"   rh   s         r1   r	   zLightGlueImageProcessor.resize   sU    : TU;;;
x.$w-0#/	
 

 
 
 	
r3   return_tensorsc                 >   ||n| j         }||n| j        }||n| j        }||n| j        }||n| j        }||n| j        }t          |d          }t          |          }t          |          st          d          t          |||||           d |D             }t          |d                   r|rt                              d           |
t          |d                   }
g |D ]q}|r|                     ||||
	          }|r|                     |||

          }|rt#          ||
          }t%          ||	|
          }                    |           rfdt)          dt+                    d          D             }d|i}t-          ||          S )a   
        Preprocess an image or batch of images.

        Args:
            images (`ImageInput`):
                Image pairs to preprocess. Expects either a list of 2 images or a list of list of 2 images list with
                pixel values ranging from 0 to 255. If passing in images with pixel values between 0 and 1, set
                `do_rescale=False`.
            do_resize (`bool`, *optional*, defaults to `self.do_resize`):
                Whether to resize the image.
            size (`dict[str, int]`, *optional*, defaults to `self.size`):
                Size of the output image after `resize` has been applied. If `size["shortest_edge"]` >= 384, the image
                is resized to `(size["shortest_edge"], size["shortest_edge"])`. Otherwise, the smaller edge of the
                image will be matched to `int(size["shortest_edge"]/ crop_pct)`, after which the image is cropped to
                `(size["shortest_edge"], size["shortest_edge"])`. Only has an effect if `do_resize` is set to `True`.
            resample (`PILImageResampling`, *optional*, defaults to `self.resample`):
                Resampling filter to use if resizing the image. This can be one of `PILImageResampling`, filters. Only
                has an effect if `do_resize` is set to `True`.
            do_rescale (`bool`, *optional*, defaults to `self.do_rescale`):
                Whether to rescale the image values between [0 - 1].
            rescale_factor (`float`, *optional*, defaults to `self.rescale_factor`):
                Rescale factor to rescale the image by if `do_rescale` is set to `True`.
            do_grayscale (`bool`, *optional*, defaults to `self.do_grayscale`):
                Whether to convert the image to grayscale.
            return_tensors (`str` or `TensorType`, *optional*):
                The type of tensors to return. Can be one of:
                    - Unset: Return a list of `np.ndarray`.
                    - `TensorType.TENSORFLOW` or `'tf'`: Return a batch of type `tf.Tensor`.
                    - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`.
                    - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`.
                    - `TensorType.JAX` or `'jax'`: Return a batch of type `jax.numpy.ndarray`.
            data_format (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`):
                The channel dimension format for the output image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - Unset: Use the channel dimension format of the input image.
            input_data_format (`ChannelDimension` or `str`, *optional*):
                The channel dimension format for the input image. If unset, the channel dimension format is inferred
                from the input image. Can be one of:
                - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format.
                - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format.
                - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.
        NFrc   zkInvalid image type. Must be of type PIL.Image.Image, numpy.ndarray, torch.Tensor, tf.Tensor or jax.ndarray.)rZ   r[   r\   r]   r^   c                 ,    g | ]}t          |          S rH   r   rJ   r!   s     r1   rQ   z6LightGlueImageProcessor.preprocess.<locals>.<listcomp>4       <<<E.''<<<r3   r   zIt looks like you are trying to rescale already rescaled images. If the input images have pixel values between 0 and 1, set `do_rescale=False` to avoid rescaling them again.)r!   r[   r\   r"   )r!   scaler"   r7   )input_channel_dimc                 *    g | ]}||d z            S r'   rH   )rJ   i
all_imagess     r1   rQ   z6LightGlueImageProcessor.preprocess.<locals>.<listcomp>O  s&    SSSz!a!e),SSSr3   r'   rY   )datatensor_type)rZ   r\   r]   r^   r_   r[   r   rT   r   rR   r   r   loggerwarning_oncer   r	   rescaler:   r
   appendrangerD   r   )rg   rA   rZ   r[   r\   r]   r^   r_   rl   rj   r"   rh   r!   image_pairsrx   rw   s                  @r1   
preprocessz"LightGlueImageProcessor.preprocess   s,   t "+!6IIDN	'388#-#9ZZt
+9+E4K^'3'?||TEV'ttTYTU;;; 188F## 	:  
 	&!)	
 	
 	
 	
 =<V<<<6!9%% 	* 	s  
 $ >vay I I
 	% 	%E t%dXarss m5Zkll Y,UFWXXX/{VghhhEe$$$$ TSSSeAs:PQ6R6RSSS,>BBBBr3           outputstarget_sizes	thresholdc                    |j         j        d         t          |          k    rt          d          t	          d |D                       st          d          t          |t                    r!t          j        ||j         j	                  }n3|j        d         dk    s|j        d         dk    rt          d          |}|j
                                        }||                    d                              dddd          z  }|                    t          j                  }g }t!          |j         ||j        d	d	df         |j        d	d	df                   D ]\  }}}	}
|d         dk    }|d         dk    }|d         |         }|d         |         }|	|         }|
|         }t          j        ||k    |dk              }||         }|||                  }||         }|                    |||d
           |S )a  
        Converts the raw output of [`KeypointMatchingOutput`] into lists of keypoints, scores and descriptors
        with coordinates absolute to the original image sizes.
        Args:
            outputs ([`KeypointMatchingOutput`]):
                Raw outputs of the model.
            target_sizes (`torch.Tensor` or `list[tuple[tuple[int, int]]]`, *optional*):
                Tensor of shape `(batch_size, 2, 2)` or list of tuples of tuples (`tuple[int, int]`) containing the
                target size `(height, width)` of each image in the batch. This must be the original image size (before
                any processing).
            threshold (`float`, *optional*, defaults to 0.0):
                Threshold to filter out the matches with low scores.
        Returns:
            `list[Dict]`: A list of dictionaries, each dictionary containing the keypoints in the first and second image
            of the pair, the matching scores and the matching indices.
        r   zRMake sure that you pass in as many target sizes as the batch dimension of the maskc              3   <   K   | ]}t          |          d k    V  dS )r'   N)rD   )rJ   target_sizes     r1   rK   zILightGlueImageProcessor.post_process_keypoint_matching.<locals>.<genexpr>m  s/      II[3{##q(IIIIIIr3   zTEach element of target_sizes must contain the size (h, w) of each image of the batch)devicer   r'   r(   N)
keypoints0
keypoints1matching_scores)maskr-   rD   rR   r/   r;   rN   rU   tensorr   	keypointscloneflipreshapetoint32zipmatchesr   logical_andr}   )rg   r   r   r   image_pair_sizesr   results	mask_pairkeypoints_pairr   scoresmask0mask1r   r   matches0scores0valid_matchesmatched_keypoints0matched_keypoints1r   s                        r1   post_process_keypoint_matchingz6LightGlueImageProcessor.post_process_keypoint_matchingU  s?   , <a C$5$555qrrrIILIIIII 	ustttlD)) 	,$|LATUUU!!$))\-?-Ba-G-G j    ,%++--	 0 5 5b 9 9 A A"aA N NN	LL--	:=L)W_QQQT%:G<STUTUTUWXTX<Y;
 ;
 	 	6I~w aL1$EaL1$E'*51J'*51Ju~HUmG "-g	.A8b=QQM!+M!:!+H],C!D%m4ONN"4"4'6     r3   rA   keypoint_matching_outputzImage.Imagec           	         t                    d D             fdt          dt                    d          D             }g }t          ||          D ]\  }}|d         j        dd         \  }}|d         j        dd         \  }	}
t          j        t          ||	          ||
z   dft
          j                  }|d         |d|d|f<   |d         |d|	|df<   t          j
        |          }t          j        |          }|d	                             d          \  }}|d
                             d          \  }}t          |||||d                   D ]\  }}}}}|                     |          }|                    ||||z   |f|d           |                    |dz
  |dz
  |dz   |dz   fd           |                    ||z   dz
  |dz
  ||z   dz   |dz   fd           |                    |           |S )a  
        Plots the image pairs side by side with the detected keypoints as well as the matching between them.

        Args:
            images (`ImageInput`):
                Image pairs to plot. Same as `LightGlueImageProcessor.preprocess`. Expects either a list of 2
                images or a list of list of 2 images list with pixel values ranging from 0 to 255.
            keypoint_matching_output (List[Dict[str, torch.Tensor]]]):
                A post processed keypoint matching output

        Returns:
            `List[PIL.Image.Image]`: A list of PIL images, each containing the image pairs side by side with the detected
            keypoints as well as the matching between them.
        c                 ,    g | ]}t          |          S rH   ro   rp   s     r1   rQ   zGLightGlueImageProcessor.visualize_keypoint_matching.<locals>.<listcomp>  rq   r3   c                 *    g | ]}||d z            S ru   rH   rJ   rv   rA   s     r1   rQ   zGLightGlueImageProcessor.visualize_keypoint_matching.<locals>.<listcomp>  &    KKKQva!a%i(KKKr3   r   r'   Nr   r   )dtyper   r   r   )fillrb   black)r   )rT   r~   rD   r   r-   r.   zerosmaxuint8r   	fromarrayr    Drawunbind
_get_colorlineellipser}   )rg   rA   r   r   r   rO   pair_outputheight0width0height1width1
plot_imageplot_image_pildrawkeypoints0_xkeypoints0_ykeypoints1_xkeypoints1_ykeypoint0_xkeypoint0_ykeypoint1_xkeypoint1_ymatching_scorecolors    `                      r1   visualize_keypoint_matchingz3LightGlueImageProcessor.visualize_keypoint_matching  s}   & 188<<V<<<KKKK%3v;;2J2JKKK'*;8P'Q'Q 	+ 	+#J(m1"1"5OGV(m1"1"5OGV3w#8#8&6/1"MUWU]^^^J,6qMJxx&(),6qMJxx()"_Z88N>.11D)4\)B)I)I!)L)L&L,)4\)B)I)I!)L)L&L,VYlL,TeHfW W  R[+{N 77		 +{V/C[Q    
 kAo{QaQ\_`Q`ahoppp 6)A-{Qf@TWX@XZehiZij      
 NN>****r3   c                 ^    t          dd|z
  z            }t          d|z            }d}|||fS )zMaps a score to a color.   r   r   )int)rg   scorergbs        r1   r   z"LightGlueImageProcessor._get_color  s:    q5y!""e1ayr3   c           
      ~   t          j        dt                     t                      rddlm} nt          d          t                    d D             fdt          dt                    d          D             }t          ||          D ]\  }}|d         j        dd         \  }}|d         j        dd         \  }	}
t          j        t          ||	          ||
z   d	f          }|d         d
z  |d|d|f<   |d         d
z  |d|	|df<   |                    |           |                    d           |d                             d          \  }}|d                             d          \  }}t          |||||d                   D ]\  }}}}}|                    |||z   g||g |                    d          |                                          dd           |                    ||dd           |                    ||z   |dd           |                                 dS )a  
        Plots the image pairs side by side with the detected keypoints as well as the matching between them. Requires
        matplotlib to be installed.

        .. deprecated::
            `plot_keypoint_matching` is deprecated and will be removed in a future version. Use `visualize_keypoint_matching` instead.

        Args:
            images (`ImageInput`):
                Image pairs to plot. Same as `LightGlueImageProcessor.preprocess`. Expects either a list of 2 images or
                a list of list of 2 images list with pixel values ranging from 0 to 255.
            keypoint_matching_output ([`LightGlueKeypointMatchingOutput`]):
                Raw outputs of the model.
        zx`plot_keypoint_matching` is deprecated and will be removed in transformers v. Use `visualize_keypoint_matching` instead.r   Nz@Please install matplotlib to use `plot_keypoint_matching` methodc                 ,    g | ]}t          |          S rH   ro   rp   s     r1   rQ   zBLightGlueImageProcessor.plot_keypoint_matching.<locals>.<listcomp>  rq   r3   c                 *    g | ]}||d z            S ru   rH   r   s     r1   rQ   zBLightGlueImageProcessor.plot_keypoint_matching.<locals>.<listcomp>  r   r3   r'   r   r   g     o@offr   r   r   RdYlGng?g      ?)r   alpha	linewidthr   )cs)warningswarnFutureWarningr   matplotlib.pyplotpyplotImportErrorrT   r~   rD   r   r-   r.   r   r   imshowr8   r   plotget_cmapitemscattershow)rg   rA   r   pltr   rO   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s    `                   r1   plot_keypoint_matchingz.LightGlueImageProcessor.plot_keypoint_matching  s    	9	
 	
 	
 #$$ 	b+++++++`aaa088<<V<<<KKKK%3v;;2J2JKKK'*;8P'Q'Q 	 	#J(m1"1"5OGV(m1"1"5OGV3w#8#8&6/1"MNNJ,6qME,AJxx&(),6qME,AJxx()JJz"""HHUOOO)4\)B)I)I!)L)L&L,)4\)B)I)I!)L)L&L,VYlL,TeHfW W O OR[+{N  +"67 +.0#,,x001D1D1F1FGG!     K1EEEK&0+ANNNNHHJJJJ/	 	r3   )NN)r   )"__name__
__module____qualname____doc__model_input_namesr   BILINEARboolr   dictstrr   floatrf   r.   r<   r   r   r	   r,   r   r   r   r   rN   tuplerU   Tensorr   r   r   r   r   __classcell__)ri   s   @r1   rX   rX      s        , (( )-'9'B '!) )) tCH~&) %	)
 ) ) ) 
) ) ) ) ) )2 ?CDH%
 %
z%
 38n%
 eC)9$9:;	%

 $E#/?*?$@A%
 %
 %
 %
T %))-15%)*.'+;?(8(>DHtC tC D>tC tCH~&	tC
 -.tC TNtC !tC tntC !sJ!78tC &tC $E#/?*?$@AtC 
tC tC tC tCt 	B B0B JU34B 	B
 
d3$%	&B B B BH44 #'tC,='>"?4 
m		4 4 4 4l  5Z 5Sr 5 5 5 5 5 5 5 5r3   rX   rG   )2r   typingr   r   numpyr.   rU   image_processing_utilsr   r   r   image_transformsr	   r
   image_utilsr   r   r   r   r   r   r   r   r   r   r   r   r   utilsr   r   r   r   utils.import_utilsr   modeling_lightgluer   r>   r   r    
get_loggerr   rz   r<   r   r2   r:   rT   rX   __all__rH   r3   r1   <module>r      s  (  " " " " " " " "      U U U U U U U U U U C C C C C C C C                              U T T T T T T T T T T T * * * * * * ? ? ? ? ? ?  %$$$$$$$$ JJJ		H	%	%
 AEa a:ac+;&; <=a a a a  AE# ##c+;&; <=# # # # #L$J $ $ $ $8 
:A A A A A0 A A AH %
%r3   