Views
No views yet


1from transformers import AutoImageProcessor, AutoModel
2import torch
3from PIL import Image
4import requests
5url = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_98169888_3347710852.jpg"
6im1 = Image.open(requests.get(url, stream=True).raw)
7url = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_26757027_6717084061.jpg"
8im2 = Image.open(requests.get(url, stream=True).raw)
9images = [im1, im2]
10processor = AutoImageProcessor.from_pretrained("stevenbucaille/superglue_outdoor")
11model = AutoModel.from_pretrained("stevenbucaille/superglue_outdoor")
12inputs = processor(images, return_tensors="pt")
13outputs = model(**inputs)1from transformers import AutoImageProcessor, AutoModel
2import torch
3from PIL import Image
4import requests
5url_image_1 = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_98169888_3347710852.jpg"
6image_1 = Image.open(requests.get(url_image_1, stream=True).raw)
7url_image_2 = "https://raw.githubusercontent.com/magicleap/SuperGluePretrainedNetwork/refs/heads/master/assets/phototourism_sample_images/united_states_capitol_26757027_6717084061.jpg"
8image_2 = Image.open(requests.get(url_image_2, stream=True).raw)
9images = [image_1, image_2]
10processor = AutoImageProcessor.from_pretrained("stevenbucaille/superglue_indoor")
11model = AutoModel.from_pretrained("stevenbucaille/superglue_indoor")
12inputs = processor(images, return_tensors="pt")
13with torch.no_grad():
14 outputs = model(**inputs)
15# Get the respective image masks
16image0_mask, image1_mask = outputs_mask[0]
17image0_indices = torch.nonzero(image0_mask).squeeze()
18image1_indices = torch.nonzero(image1_mask).squeeze()
19image0_matches = outputs.matches[0, 0][image0_indices]
20image1_matches = outputs.matches[0, 1][image1_indices]
21image0_matching_scores = outputs.matching_scores[0, 0][image0_indices]
22image1_matching_scores = outputs.matching_scores[0, 1][image1_indices]post_process_keypoint_matching method from the SuperGlueImageProcessor to get the keypoints and matches in a more readable format:1image_sizes = [(image.height, image.width) for image in images]
2outputs = processor.post_process_keypoint_matching(outputs, image_sizes, threshold=0.2)
3for i, output in enumerate(outputs):
4 print("For the image pair", i)
5 for keypoint0, keypoint1, matching_score in zip(output["keypoints0"], output["keypoints1"],
6 output["matching_scores"]):
7 print(
8 f"Keypoint at coordinate {keypoint0.numpy()} in the first image matches with keypoint at coordinate {keypoint1.numpy()} in the second image with a score of {matching_score}."
9 )1import matplotlib.pyplot as plt
2import numpy as np
3# Create side by side image
4merged_image = np.zeros((max(image1.height, image2.height), image1.width + image2.width, 3))
5merged_image[: image1.height, : image1.width] = np.array(image1) / 255.0
6merged_image[: image2.height, image1.width :] = np.array(image2) / 255.0
7plt.imshow(merged_image)
8plt.axis("off")
9# Retrieve the keypoints and matches
10output = outputs[0]
11keypoints0 = output["keypoints0"]
12keypoints1 = output["keypoints1"]
13matching_scores = output["matching_scores"]
14keypoints0_x, keypoints0_y = keypoints0[:, 0].numpy(), keypoints0[:, 1].numpy()
15keypoints1_x, keypoints1_y = keypoints1[:, 0].numpy(), keypoints1[:, 1].numpy()
16# Plot the matches
17for keypoint0_x, keypoint0_y, keypoint1_x, keypoint1_y, matching_score in zip(
18 keypoints0_x, keypoints0_y, keypoints1_x, keypoints1_y, matching_scores
19):
20 plt.plot(
21 [keypoint0_x, keypoint1_x + image1.width],
22 [keypoint0_y, keypoint1_y],
23 color=plt.get_cmap("RdYlGn")(matching_score.item()),
24 alpha=0.9,
25 linewidth=0.5,
26 )
27 plt.scatter(keypoint0_x, keypoint0_y, c="black", s=2)
28 plt.scatter(keypoint1_x + image1.width, keypoint1_y, c="black", s=2)
29# Save the plot
30plt.savefig("matched_image.png", dpi=300, bbox_inches='tight')
31plt.close()
1@inproceedings{sarlin2020superglue,
2 title={Superglue: Learning feature matching with graph neural networks},
3 author={Sarlin, Paul-Edouard and DeTone, Daniel and Malisiewicz, Tomasz and Rabinovich, Andrew},
4 booktitle={Proceedings of the IEEE/CVF conference on computer vision and pattern recognition},
5 pages={4938--4947},
6 year={2020}
7}