-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathlightglue.py
170 lines (137 loc) · 4.92 KB
/
lightglue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sys
import time
import io
import numpy as np
import cv2
import matplotlib.pyplot as plt
from lightglue_utils import *
import ailia
# import original modules
sys.path.append('../../util')
from arg_utils import get_base_parser, update_parser, get_savepath # noqa
from model_utils import check_and_download_models # noqa
from image_utils import normalize_image # noqa
from webcamera_utils import get_capture, get_writer # noqa
# logger
from logging import getLogger # noqa
logger = getLogger(__name__)
# ======================
# Parameters
# ======================
WEIGHT_PATH = "superpoint.onnx"
MODEL_PATH = "superpoint.onnx.prototxt"
LIGHTGLUE_WEIGHT_PATH = "superpoint_lightglue.onnx"
LIGHTGLUE_MODEL_PATH = "superpoint_lightglue.onnx.prototxt"
REMOTE_PATH = 'https://storage.googleapis.com/ailia-models/lightglue/'
IMAGE_A_PATH = 'img_A.png'
IMAGE_B_PATH = 'img_B.png'
SAVE_IMAGE_PATH = 'output.png'
# ======================
# Arguemnt Parser Config
# ======================
parser = get_base_parser(
'LightGlue', IMAGE_A_PATH, SAVE_IMAGE_PATH, fp16_support=False
)
parser.add_argument(
'-i2', '--input2', metavar='IMAGE2', default=IMAGE_B_PATH,
help='Pair image path of input image.'
)
parser.add_argument(
"--extractor_type",
type=str,
choices=["superpoint", "disk"],
default="superpoint",
help="Type of feature extractor. Supported extractors are 'superpoint' and 'disk'.",
)
parser.add_argument(
"--img_size",
nargs="+",
type=int,
default=512,
required=False,
help="Sample image size for ONNX tracing. If a single integer is given, resize the longer side of the images to this value. Otherwise, please provide two integers (height width) to resize both images to this size, or four integers (height width height width).",
)
args = update_parser(parser)
# ======================
# Main functions
# ======================
def infer(
img_paths: List[str],
extractor_type: str,
img_size=512,
runner=None
):
# Handle args
img0_path = img_paths[0]
img1_path = img_paths[1]
if isinstance(img_size, List):
if len(img_size) == 1:
size0 = size1 = img_size[0]
elif len(img_size) == 2:
size0 = size1 = img_size
elif len(img_size) == 4:
size0, size1 = img_size[:2], img_size[2:]
else:
raise ValueError("Invalid img_size. Please provide 1, 2, or 4 integers.")
else:
size0 = size1 = img_size
image0, scales0 = load(img0_path, resize=size0)
image1, scales1 = load(img1_path, resize=size1)
extractor_type = extractor_type.lower()
if extractor_type == "superpoint":
image0 = rgb_to_grayscale(image0)
image1 = rgb_to_grayscale(image1)
elif extractor_type == "disk":
pass
else:
raise NotImplementedError(
f"Unsupported feature extractor type: {extractor_type}."
)
# Run inference
m_kpts0, m_kpts1 = runner.run(image0, image1, scales0, scales1)
# Visualisation
orig_image0, _ = load(img0_path)
orig_image1, _ = load(img1_path)
plot_images(
[orig_image0[0].transpose(1, 2, 0), orig_image1[0].transpose(1, 2, 0)]
)
plot_matches(m_kpts0, m_kpts1, color="lime", lw=0.2)
def recognize_from_image(runner):
# input image loop
for image_path1 ,image_path2 in zip(args.input, [args.input2]):
# inference
logger.info('Start inference...')
if args.benchmark:
logger.info('BENCHMARK mode')
total_time_estimation = 0
for i in range(args.benchmark_count):
start = int(round(time.time() * 1000))
output = infer([image_path1,image_path2], args.extractor_type, args.img_size, runner)
end = int(round(time.time() * 1000))
estimation_time = (end - start)
# Logging
logger.info(f'\tailia processing estimation time {estimation_time} ms')
if i != 0:
total_time_estimation = total_time_estimation + estimation_time
logger.info(f'\taverage time estimation {total_time_estimation / (args.benchmark_count - 1)} ms')
else:
output = infer([image_path1,image_path2], args.extractor_type, args.img_size, runner)
# plot result
savepath = get_savepath(args.savepath, image_path1, ext='.png')
logger.info(f'saved at : {savepath}')
save_plot(savepath)
logger.info('Script finished successfully.')
def main():
# model files check and download
check_and_download_models(WEIGHT_PATH, MODEL_PATH, REMOTE_PATH)
check_and_download_models(LIGHTGLUE_WEIGHT_PATH, LIGHTGLUE_MODEL_PATH, REMOTE_PATH)
env_id = args.env_id
# Load ONNX models
runner = LightGlueRunner(
extractor_path="superpoint.onnx",
lightglue_path="superpoint_lightglue.onnx",
env_id = env_id
)
recognize_from_image(runner)
if __name__ == '__main__':
main()