# Khớp điểm chính

Keypoint matching (khớp điểm then chốt) khớp các điểm quan tâm khác nhau thuộc cùng một đối tượng xuất hiện trong hai hình ảnh khác nhau. Hầu hết các bộ khớp điểm then chốt hiện đại nhận hình ảnh làm đầu vào và xuất ra các kết quả sau:

- **Tọa độ điểm chính (x,y):** ánh xạ một-một của tọa độ pixel giữa ảnh thứ nhất và ảnh thứ hai bằng hai danh sách. Mỗi điểm chính tại một chỉ số nhất định trong danh sách thứ nhất được ghép với điểm chính tại cùng chỉ số đó trong danh sách thứ hai.
- **Điểm khớp:** Các điểm được gán cho các khớp điểm then chốt.

Trong hướng dẫn này, bạn sẽ trích xuất các khớp điểm then chốt bằng mô hình `EfficientLoFTR` được huấn luyện với [khung MatchAnything](https://huggingface.co/zju-community/matchanything_eloftr), sau đó tinh chỉnh các khớp đó. Mô hình này chỉ có 16M tham số và có thể chạy trên CPU. Bạn sẽ sử dụng lớp [AutoModelForKeypointMatching](/docs/transformers/main/vi/model_doc/auto#transformers.AutoModelForKeypointMatching).

```python
from transformers import AutoImageProcessor, AutoModelForKeypointMatching
import torch

processor = AutoImageProcessor.from_pretrained("zju-community/matchanything_eloftr")
model = AutoModelForKeypointMatching.from_pretrained("zju-community/matchanything_eloftr")
```

Tải hai hình ảnh có cùng đối tượng quan tâm. Ảnh thứ hai được chụp cách nhau một giây, màu sắc đã được chỉnh sửa, và được cắt xén thêm cũng như xoay đi.

    <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
         alt="Bee"
         style="height: 200px; object-fit: contain; margin-right: 10px;">
    <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee_edited.jpg"
         alt="Bee edited"
         style="height: 200px; object-fit: contain;">

```python
from transformers.image_utils import load_image
image1 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg")
image2 = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee_edited.jpg")

images = [image1, image2]
```

Chúng ta có thể truyền các hình ảnh vào bộ xử lý và suy luận.

```python
inputs = processor(images, return_tensors="pt")
with torch.no_grad():
    outputs = model(**inputs)
```

Chúng ta có thể xử lý hậu kỳ các đầu ra. Tham số threshold được dùng để tinh chỉnh nhiễu (ngưỡng độ tin cậy thấp) trong các kết quả khớp.

```python
image_sizes = [[(image.height, image.width) for image in images]]

outputs = processor.post_process_keypoint_matching(outputs, image_sizes, threshold=0.2)
print(outputs)
```

Dưới đây là các kết quả đầu ra.

```text
[{'keypoints0': tensor([[4514,  550],
          [4813,  683],
          [1972, 1547],
          ...
          [3916, 3408]], dtype=torch.int32),
  'keypoints1': tensor([[2280,  463],
          [2378,  613],
          [2231,  887],
          ...
          [1521, 2560]], dtype=torch.int32),
  'matching_scores': tensor([0.2189, 0.2073, 0.2414, ...
    ])}]
```

Chúng tôi đã cắt bớt đầu ra nhưng có 401 kết quả khớp!

```python
len(outputs[0]["keypoints0"])
# 401
```

Chúng ta có thể trực quan hóa chúng bằng cách sử dụng phương thức [visualize_keypoint_matching()](/docs/transformers/main/vi/model_doc/efficientloftr#transformers.EfficientLoFTRImageProcessor.visualize_keypoint_matching) của bộ xử lý.

```python
plot_images = processor.visualize_keypoint_matching(images, outputs)
plot_images
```

![Matched Image](https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/matched_bees.png)

Tùy chọn, bạn có thể sử dụng API [Pipeline](/docs/transformers/main/vi/main_classes/pipelines#transformers.Pipeline) và đặt task thành `keypoint-matching`.

```python
from transformers import pipeline 

image_1 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg"
image_2 = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee_edited.jpg"

pipe = pipeline("keypoint-matching", model="zju-community/matchanything_eloftr")
pipe([image_1, image_2])
```

Đầu ra trông như sau.

```bash
[{'keypoint_image_0': {'x': 2444, 'y': 2869},
  'keypoint_image_1': {'x': 837, 'y': 1500},
  'score': 0.9756593704223633},
 {'keypoint_image_0': {'x': 1248, 'y': 2819},
  'keypoint_image_1': {'x': 862, 'y': 866},
  'score': 0.9735618829727173},
 {'keypoint_image_0': {'x': 1547, 'y': 3317},
  'keypoint_image_1': {'x': 1436, 'y': 1500},
  ...
 }
]
```

