keypoint_matching.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Copyright 2025 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from collections.abc import Sequence
  15. from typing import Any, TypeAlias, TypedDict, Union
  16. from typing_extensions import overload
  17. from ..image_utils import is_pil_image
  18. from ..utils import is_vision_available, requires_backends
  19. from .base import Pipeline
  20. if is_vision_available():
  21. from PIL import Image
  22. from ..image_utils import load_image
  23. ImagePair: TypeAlias = Sequence[Union["Image.Image", str]]
  24. class Keypoint(TypedDict):
  25. x: float
  26. y: float
  27. class Match(TypedDict):
  28. keypoint_image_0: Keypoint
  29. keypoint_image_1: Keypoint
  30. score: float
  31. def validate_image_pairs(images: Any) -> Sequence[Sequence[ImagePair]]:
  32. error_message = (
  33. "Input images must be a one of the following :",
  34. " - A pair of images.",
  35. " - A list of pairs of images.",
  36. )
  37. def _is_valid_image(image):
  38. """images is a PIL Image or a string."""
  39. return is_pil_image(image) or isinstance(image, str)
  40. if isinstance(images, Sequence):
  41. if len(images) == 2 and all((_is_valid_image(image)) for image in images):
  42. return [images]
  43. if all(
  44. isinstance(image_pair, Sequence)
  45. and len(image_pair) == 2
  46. and all(_is_valid_image(image) for image in image_pair)
  47. for image_pair in images
  48. ):
  49. return images
  50. raise ValueError(error_message)
  51. class KeypointMatchingPipeline(Pipeline):
  52. """
  53. Keypoint matching pipeline using any `AutoModelForKeypointMatching`. This pipeline matches keypoints between two images.
  54. """
  55. _load_processor = False
  56. _load_image_processor = True
  57. _load_feature_extractor = False
  58. _load_tokenizer = False
  59. def __init__(self, *args, **kwargs):
  60. super().__init__(*args, **kwargs)
  61. requires_backends(self, "vision")
  62. def _sanitize_parameters(self, threshold=None, timeout=None):
  63. preprocess_params = {}
  64. if timeout is not None:
  65. preprocess_params["timeout"] = timeout
  66. postprocess_params = {}
  67. if threshold is not None:
  68. postprocess_params["threshold"] = threshold
  69. return preprocess_params, {}, postprocess_params
  70. @overload
  71. def __call__(self, inputs: ImagePair, threshold: float = 0.0, **kwargs: Any) -> list[Match]: ...
  72. @overload
  73. def __call__(self, inputs: list[ImagePair], threshold: float = 0.0, **kwargs: Any) -> list[list[Match]]: ...
  74. def __call__(
  75. self,
  76. inputs: list[ImagePair] | ImagePair,
  77. threshold: float = 0.0,
  78. **kwargs: Any,
  79. ) -> list[Match] | list[list[Match]]:
  80. """
  81. Find matches between keypoints in two images.
  82. Args:
  83. inputs (`str`, `list[str]`, `PIL.Image` or `list[PIL.Image]`):
  84. The pipeline handles three types of images:
  85. - A string containing a http link pointing to an image
  86. - A string containing a local path to an image
  87. - An image loaded in PIL directly
  88. The pipeline accepts either a single pair of images or a batch of image pairs, which must then be passed as a string.
  89. Images in a batch must all be in the same format: all as http links, all as local paths, or all as PIL
  90. images.
  91. threshold (`float`, *optional*, defaults to 0.0):
  92. The threshold to use for keypoint matching. Keypoints matched with a lower matching score will be filtered out.
  93. A value of 0 means that all matched keypoints will be returned.
  94. kwargs:
  95. `timeout (`float`, *optional*, defaults to None)`
  96. The maximum time in seconds to wait for fetching images from the web. If None, no timeout is set and
  97. the call may block forever.
  98. Return:
  99. Union[list[Match], list[list[Match]]]:
  100. A list of matches or a list if a single image pair is provided, or of lists of matches if a batch
  101. of image pairs is provided. Each match is a dictionary containing the following keys:
  102. - **keypoint_image_0** (`Keypoint`): The keypoint in the first image (x, y coordinates).
  103. - **keypoint_image_1** (`Keypoint`): The keypoint in the second image (x, y coordinates).
  104. - **score** (`float`): The matching score between the two keypoints.
  105. """
  106. if inputs is None:
  107. raise ValueError("Cannot call the keypoint-matching pipeline without an inputs argument!")
  108. formatted_inputs = validate_image_pairs(inputs)
  109. outputs = super().__call__(formatted_inputs, threshold=threshold, **kwargs)
  110. if len(formatted_inputs) == 1:
  111. return outputs[0]
  112. return outputs
  113. def preprocess(self, images, timeout=None):
  114. images = [load_image(image, timeout=timeout) for image in images]
  115. model_inputs = self.image_processor(images=images, return_tensors="pt")
  116. model_inputs = model_inputs.to(self.dtype)
  117. target_sizes = [image.size for image in images]
  118. preprocess_outputs = {"model_inputs": model_inputs, "target_sizes": target_sizes}
  119. return preprocess_outputs
  120. def _forward(self, preprocess_outputs):
  121. model_inputs = preprocess_outputs["model_inputs"]
  122. model_outputs = self.model(**model_inputs)
  123. forward_outputs = {"model_outputs": model_outputs, "target_sizes": [preprocess_outputs["target_sizes"]]}
  124. return forward_outputs
  125. def postprocess(self, forward_outputs, threshold=0.0) -> list[Match]:
  126. model_outputs = forward_outputs["model_outputs"]
  127. target_sizes = forward_outputs["target_sizes"]
  128. postprocess_outputs = self.image_processor.post_process_keypoint_matching(
  129. model_outputs, target_sizes=target_sizes, threshold=threshold
  130. )
  131. postprocess_outputs = postprocess_outputs[0]
  132. pair_result = []
  133. for kp_0, kp_1, score in zip(
  134. postprocess_outputs["keypoints0"],
  135. postprocess_outputs["keypoints1"],
  136. postprocess_outputs["matching_scores"],
  137. ):
  138. kp_0 = Keypoint(x=kp_0[0].item(), y=kp_0[1].item())
  139. kp_1 = Keypoint(x=kp_1[0].item(), y=kp_1[1].item())
  140. pair_result.append(Match(keypoint_image_0=kp_0, keypoint_image_1=kp_1, score=score.item()))
  141. pair_result = sorted(pair_result, key=lambda x: x["score"], reverse=True)
  142. return pair_result