
    9i)                        S SK JrJrJrJr  S SKrS SKrS SKrS SK	r	S SK
Jr  S SKJr  S SKJrJrJr  S SKJr  S SKJr  S SKJr  S S	KJr  \" 5       r\R6                  " \R8                  \R8                  S
9 " S S\5      5       rg)    )AnyDictListUnionN)	Pipelines)
OutputKeys)InputModelPipeline)	PIPELINES)	LoadImage)Tasks)
get_logger)module_namec                      ^  \ rS rSrSrS\4U 4S jjrS rS rSS jr	S r
SS	\4S
 jjrS rS	\\\4   S\\\4   4S jrS\\\4   S\\\4   4S jrU 4S jrSrU =r$ )ImageMatchingPipeline   a  Image Matching Pipeline.

Examples:

>>> from modelscope.outputs import OutputKeys
>>> from modelscope.pipelines import pipeline
>>> from modelscope.utils.constant import Tasks


>>> task = 'image-matching'
>>> model_id = 'damo/cv_quadtree_attention_image-matching_outdoor'

>>> input_location = [
>>>                     ['data/test/images/image_matching1.jpg',
>>>                     'data/test/images/image_matching2.jpg']
>>>                 ]
>>> estimator = pipeline(Tasks.image_matching, model=self.model_id)
>>> result = estimator(input_location)
>>> kpts0, kpts1, conf = result[0][OutputKeys.MATCHES]
>>> print(f'Found {len(kpts0)} matches')
modelc                    > [         TU ]  " SSU0UD6  [        R                  R	                  5       (       d  [        S5      e[        R                  S5        g)zm
use `model` to create a image matching pipeline for prediction
Args:
    model: model id on modelscope hub.
r   z?Cuda is not available. Image matching model only supports cuda.z#image matching model, pipeline initN )super__init__torchcudais_availableRuntimeErrorloggerinfo)selfr   kwargs	__class__s      o/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/pipelines/cv/image_matching_pipeline.pyr   ImageMatchingPipeline.__init__-   sN     	/u// zz&&((Q  	9:    c                     UR                   S S u  p4Sn[        X45      U:  aL  U[        X45      -  n[        XE-  5      [        X5-  5      pv[        R                  " XU4[        R
                  S9nX4$ )N      )interpolation)shapemaxintcv2resize
INTER_AREA)r   imgmax_image_sizehwscalenew_wnew_hs           r"   resize_image"ImageMatchingPipeline.resize_image=   se    yy!}q9~%"SY.Eqy>3qy>5**S%.OCzr$   c                 J    [        [        R                  " X-  5      U-  5      $ N)r+   npceil)r   sizedivs      r"   compute_paded_size(ImageMatchingPipeline.compute_paded_sizeF   s    2774:&,--r$   c           
          UR                   S S u  pVUc  Uc  XVp2U R                  UU5      U R                  X45      p[        R                  " USXu-
  SX-
  [        R                  SS9nU$ )Nr&   r   )value)r)   r>   r,   copyMakeBorderBORDER_CONSTANT)	r   r/   r1   r2   r=   cur_hcur_wh_padw_pads	            r"   	pad_imageImageMatchingPipeline.pad_imageI   s    yy!}9q..q/24595L5L346;    MM 
r$   c                     [         R                  " U5      R                  [        R                  5      nUS-  n[        UR                  5      S:X  a%  [        R                  " U[        R                  5      nU$ )Ng     o@   )
r   convert_to_ndarrayastyper:   float32lenr)   r,   cvtColorCOLOR_RGB2GRAY)r   img_namer/   s      r"   
load_image ImageMatchingPipeline.load_imageZ   sU    **84;;BJJGDjsyy>Q,,sC$6$67C
r$   inputc                 r   [        U5      S:X  d   S5       eU R                  US   5      nU R                  X25      u  p4UR                  S S u  pVU R                  US   5      nU R                  Xr5      u  pxUR                  S S u  p[	        XY5      [	        Xj5      pU R                  X;U5      nU R                  X{U5      n[        R                  " U5      S    S    R                  5       R                  5       n[        R                  " U5      S    S    R                  5       R                  5       nUUXHXVX/S.$ )Nr&   z$input should be a list of two imagesr   r'   )image0image1preprocess_info)
rO   rS   r6   r)   r*   rH   r   
from_numpyr   float)r   rU   r0   img1scale1	scaled_h1	scaled_w1img2scale2	scaled_h2	scaled_w2h_maxw_maxs                r"   
preprocess ImageMatchingPipeline.preprocessb   s)   5zQF FFuQx(((>#zz"1~	uQx(((>#zz"1~	90#i2Ku~~d51~~d51%d+D1668>>@%d+D1668>>@ Y9H
 	
r$   c
                     US S 2S4   U:  US S 2S4   U:  -  US S 2S4   U	:  -  US S 2S4   U:  -  n
X   X*   p!X-  nX%-  nX:   nXU4$ )Nr   r'   r   )r   kpt1kpt2confr]   ra   r^   r_   rb   rc   valid_matchs              r"   postprocess_match'ImageMatchingPipeline.postprocess_match|   s     AqDzI-$q!t*y2HIAJ"$QT
Y&( &(9d}} 4r$   returnc                 <    U R                   R                  U5      nU$ r9   )r   	inference)r   rU   resultss      r"   forwardImageMatchingPipeline.forward   s    **&&u-r$   inputsc                    U R                   R                  U5      nU[        R                     nUS   R	                  5       R                  5       nUS   R	                  5       R                  5       nUS   R	                  5       R                  5       nUS    Vs/ s H   owR	                  5       R                  5       PM"     nnU R                  " XEU/UQ76 u  pEn[        R                  XEU/0n	U	$ s  snf )Nkpts0kpts1rk   rY   )r   postprocessr   MATCHEScpunumpyrm   )
r   ru   rr   matchesrw   rx   rk   vrY   outputss
             r"   ry   !ImageMatchingPipeline.postprocess   s    **((0*,,- $$&,,. $$&,,.v""$**,4:;L4MN4Mq557==?4MN!33E$ F5DFd t 4
  Os   'C/c                 &   > [         TU ]  " U40 UD6$ )a^  
Match two images and return the matched keypoints and confidence.

Args:
    input (`List[List[str]]`): A list of two image paths.

Return:
    A list of result.
    The list contain the following values:

    - kpts0 -- Matched keypoints in the first image
    - kpts1 -- Matched keypoints in the second image
    - conf -- Confidence of the match
)r   __call__)r   rU   r    r!   s      r"   r   ImageMatchingPipeline.__call__   s     w000r$   r   )NN    )i   )__name__
__module____qualname____firstlineno____doc__strr   r6   r>   rH   rS   r	   rf   rm   r   r   rs   ry   r   __static_attributes____classcell__)r!   s   @r"   r   r      s    ,;c ; ."
 
4 T#s(^ S#X $sCx. T#s(^ "1 1r$   r   )typingr   r   r   r   r,   r|   r:   PILr   modelscope.metainfor   modelscope.outputsr   modelscope.pipelines.baser	   r
   r   modelscope.pipelines.builderr   modelscope.preprocessorsr   modelscope.utils.constantr   modelscope.utils.loggerr   r   register_moduleimage_matchingr   r   r$   r"   <module>r      sr    ) ) 
  
  ) ) < < 2 . + .	 	i&>&>@W1H W1@W1r$   