L i,UddlmZddlmZmZmZddlmZmZddl m Z ddl m Z m Z ddlmZe r dd lmZdd l mZeed efZeed <Gd deZGddeZdedeeefdZGddeZy))Sequence)Any TypedDictUnion) TypeAliasoverload) is_pil_image)is_vision_availablerequires_backends)Pipeline)Image) load_imagez Image.Image ImagePairc"eZdZUeed<eed<y)KeypointxyN)__name__ __module__ __qualname__float__annotations__n/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/pipelines/keypoint_matching.pyrr"s  H Hrrc,eZdZUeed<eed<eed<y)Matchkeypoint_image_0keypoint_image_1scoreN)rrrrrrrrrrr's Lrrimagesreturncd}dt|tr;t|dk(rtfd|Dr|gStfd|Dr|St |)N)z-Input images must be a one of the following :z - A pair of images.z - A list of pairs of images.c<t|xst|tS)z"images is a PIL Image or a string.)r isinstancestr)images r_is_valid_imagez-validate_image_pairs.._is_valid_image4sE"z'validate_image_pairs..9s#Q_U%;#Qc3K|]:}t|txr$t|dk(xrtfd|D<yw)r c3.K|] }|ywr,rr-s rr/z1validate_image_pairs...>sCuOE*Cr0N)r'rlenall)r. image_pairr*s rr/z'validate_image_pairs..;sN  z8 , DJ1$ DC CC D sAA)r'rr3r4 ValueError)r# error_messager*s @rvalidate_image_pairsr8-sdM =&(# v;! #Q&#Q Q8O  %   M ] ##rc eZdZdZdZdZdZdZfdZddZ e dde de de d eefd Ze ddee de de d eeefd Z ddeee e fde de d eeeeeefffd Zdd ZdZdd eefdZxZS)KeypointMatchingPipelinez Keypoint matching pipeline using any `AutoModelForKeypointMatching`. This pipeline matches keypoints between two images. FTcpt||i|t|d|jdk7r t dy)NvisionptzBKeypoint matching pipeline only supports PyTorch (framework='pt').)super__init__r frameworkr6)selfargskwargs __class__s rr?z!KeypointMatchingPipeline.__init__Os< $)&)$) >>T !ab b "rc0i}|||d<i}|||d<|i|fS)Ntimeout thresholdr)rArGrFpreprocess_paramspostprocess_paramss r_sanitize_parametersz-KeypointMatchingPipeline._sanitize_parametersUs>  +2 i (  .7 { + "&888rinputsrGrCr$c yr,rrArKrGrCs r__call__z!KeypointMatchingPipeline.__call__^sadrc yr,rrMs rrNz!KeypointMatchingPipeline.__call__asmprc | tdt|}t| |fd|i|}t |dk(r|dS|S)a Find matches between keypoints in two images. Args: inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`): The pipeline handles three types of images: - A string containing a http link pointing to an image - A string containing a local path to an image - An image loaded in PIL directly The pipeline accepts either a single pair of images or a batch of image pairs, which must then be passed as a string. Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL images. threshold (`float`, *optional*, defaults to 0.0): The threshold to use for keypoint matching. Keypoints matched with a lower matching score will be filtered out. A value of 0 means that all matched keypoints will be returned. kwargs: `timeout (`float`, *optional*, defaults to None)` The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and the call may block forever. Return: Union[list[Match], list[list[Match]]]: A list of matches or a list if a single image pair is provided, or of lists of matches if a batch of image pairs is provided. Each match is a dictionary containing the following keys: - **keypoint_image_0** (`Keypoint`): The keypoint in the first image (x, y coordinates). - **keypoint_image_1** (`Keypoint`): The keypoint in the second image (x, y coordinates). - **score** (`float`): The matching score between the two keypoints. zFCannot call the keypoint-matching pipeline without an inputs argument!rGr r)r6r8r>rNr3)rArKrGrCformatted_inputsoutputsrDs rrNz!KeypointMatchingPipeline.__call__dsYN >ef f/7'"#3SySFS  A %1: rc|Dcgc]}t||}}|j||j}|j|j}|Dcgc]}|j }}||d}|Scc}wcc}w)N)rF)r#return_tensors) model_inputs target_sizes)rimage_processorr@todtypesize)rAr#rFr)rUrVpreprocess_outputss r preprocessz#KeypointMatchingPipeline.preprocessszBHI*UG4II++6$..+Y #tzz2 067u 7 7.:LY!! J8s A3A8cF|d}|jdi|}||dgd}|S)NrUrV) model_outputsrVr)model)rAr[rUr^forward_outputss r_forwardz!KeypointMatchingPipeline._forwards9).9 " 2\2 ,9L^_mLnKoprc |d}|d}|jj|||}|d}g}t|d|d|dD]\}}} t|dj |dj }t|dj |dj }|j t ||| j t|d d }|S)Nr^rV)rVrGr keypoints0 keypoints1matching_scoresr )rr)r r!r"c |dS)Nr"r)rs rz6KeypointMatchingPipeline.postprocess..s ' rT)keyreverse)rWpost_process_keypoint_matchingzipritemappendrsorted) rAr`rGr^rVpostprocess_outputs pair_resultkp_0kp_1r"s r postprocessz$KeypointMatchingPipeline.postprocesss'8 &~6 "22QQ  R 2!4 !$  -  -  1 2"  h D$ d1gllnQ ?Dd1gllnQ ?D   udTY^YcYcYef g h[.BDQ r)NN)gr,)rrr__doc___load_processor_load_image_processor_load_feature_extractor_load_tokenizerr?rJrrrrlistrrNrr\rars __classcell__)rDs@rr:r:EsO #Oc 9dydUdCdTXY^T_dd ptIp5pRUpZ^_cdi_jZkpp -d9oy01-- - tE{De-- . -^" T%[rr:N)collections.abcrtypingrrrtyping_extensionsrr image_utilsr utilsr r baserPILrrr(rrrrr8r:rrrrs%((1&:( mS&8 9: 9: y I $$(92E)F$0mxmr