-
Notifications
You must be signed in to change notification settings - Fork 23
/
gradio_app.py
231 lines (187 loc) · 7.6 KB
/
gradio_app.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""Gradio app for GeoCalib inference."""
from copy import deepcopy
from time import time
import gradio as gr
import numpy as np
import spaces
import torch
from geocalib import logger, viz2d
from geocalib.camera import camera_models
from geocalib.extractor import GeoCalib
from geocalib.perspective_fields import get_perspective_field
from geocalib.utils import rad2deg
# flake8: noqa
# mypy: ignore-errors
description = """
<p align="center">
<h1 align="center"><ins>GeoCalib</ins> 📸<br>Single-image Calibration with Geometric Optimization</h1>
<p align="center">
<a href="https://www.linkedin.com/in/alexander-veicht/">Alexander Veicht</a>
·
<a href="https://psarlin.com/">Paul-Edouard Sarlin</a>
·
<a href="https://www.linkedin.com/in/philipplindenberger/">Philipp Lindenberger</a>
·
<a href="https://www.microsoft.com/en-us/research/people/mapoll/">Marc Pollefeys</a>
</p>
<h2 align="center">
<p>ECCV 2024</p>
<a href="https://arxiv.org/pdf/2409.06704" align="center">Paper</a> |
<a href="https://github.com/cvg/GeoCalib" align="center">Code</a> |
<a href="https://colab.research.google.com/drive/1oMzgPGppAPAIQxe-s7SRd_q8r7dVfnqo#scrollTo=etdzQZQzoo-K" align="center">Colab</a>
</h2>
</p>
## Getting Started
GeoCalib accurately estimates the camera intrinsics and gravity direction from a single image by
combining geometric optimization with deep learning.
To get started, upload an image or select one of the examples below.
You can choose between different camera models and visualize the calibration results.
"""
example_images = [
["assets/pinhole-church.jpg"],
["assets/pinhole-garden.jpg"],
["assets/fisheye-skyline.jpg"],
["assets/fisheye-dog-pool.jpg"],
]
device = "cuda" if torch.cuda.is_available() else "cpu"
model = GeoCalib().to(device)
def format_output(results):
camera, gravity = results["camera"], results["gravity"]
vfov = rad2deg(camera.vfov)
roll, pitch = rad2deg(gravity.rp).unbind(-1)
txt = "Estimated parameters:\n"
txt += f"Roll: {roll.item():.2f}° (± {rad2deg(results['roll_uncertainty']).item():.2f})°\n"
txt += f"Pitch: {pitch.item():.2f}° (± {rad2deg(results['pitch_uncertainty']).item():.2f})°\n"
txt += f"vFoV: {vfov.item():.2f}° (± {rad2deg(results['vfov_uncertainty']).item():.2f})°\n"
txt += (
f"Focal: {camera.f[0, 1].item():.2f} px (± {results['focal_uncertainty'].item():.2f} px)\n"
)
if hasattr(camera, "k1"):
txt += f"K1: {camera.k1[0].item():.2f}\n"
return txt
@spaces.GPU(duration=10)
def inference(img, camera_model):
out = model.calibrate(img.to(device), camera_model=camera_model)
save_keys = ["camera", "gravity"] + [
f"{k}_uncertainty" for k in ["roll", "pitch", "vfov", "focal"]
]
res = {k: v.cpu() for k, v in out.items() if k in save_keys}
# not converting to numpy results in gpu abort
res["up_confidence"] = out["up_confidence"].cpu().numpy()
res["latitude_confidence"] = out["latitude_confidence"].cpu().numpy()
return res
def process_results(
image_path,
camera_model,
plot_up,
plot_up_confidence,
plot_latitude,
plot_latitude_confidence,
plot_undistort,
):
"""Process the image and return the calibration results."""
if image_path is None:
raise gr.Error("Please upload an image first.")
img = model.load_image(image_path)
start = time()
inference_result = inference(img, camera_model)
logger.info(f"Calibration took {time() - start:.2f} sec. ({camera_model})")
inference_result["image"] = img.cpu()
if inference_result is None:
return ("", np.ones((128, 256, 3)), None)
plot_img = update_plot(
inference_result,
plot_up,
plot_up_confidence,
plot_latitude,
plot_latitude_confidence,
plot_undistort,
)
return format_output(inference_result), plot_img, inference_result
def update_plot(
inference_result,
plot_up,
plot_up_confidence,
plot_latitude,
plot_latitude_confidence,
plot_undistort,
):
"""Update the plot based on the selected options."""
if inference_result is None:
gr.Error("Please calibrate an image first.")
return np.ones((128, 256, 3))
camera, gravity = inference_result["camera"], inference_result["gravity"]
img = inference_result["image"].permute(1, 2, 0).numpy()
if plot_undistort:
if not hasattr(camera, "k1"):
return img
return camera.undistort_image(inference_result["image"][None])[0].permute(1, 2, 0).numpy()
up, lat = get_perspective_field(camera, gravity)
fig = viz2d.plot_images([img], pad=0)
ax = fig.get_axes()
if plot_up:
viz2d.plot_vector_fields([up[0]], axes=[ax[0]])
if plot_latitude:
viz2d.plot_latitudes([lat[0, 0]], axes=[ax[0]])
if plot_up_confidence:
viz2d.plot_confidences([torch.tensor(inference_result["up_confidence"][0])], axes=[ax[0]])
if plot_latitude_confidence:
viz2d.plot_confidences(
[torch.tensor(inference_result["latitude_confidence"][0])], axes=[ax[0]]
)
fig.canvas.draw()
img = np.array(fig.canvas.renderer.buffer_rgba())
return img
# Create the Gradio interface
with gr.Blocks() as demo:
gr.Markdown(description)
with gr.Row():
with gr.Column():
gr.Markdown("""## Input Image""")
image_path = gr.Image(label="Upload image to calibrate", type="filepath")
choice_input = gr.Dropdown(
choices=list(camera_models.keys()), label="Choose a camera model.", value="pinhole"
)
submit_btn = gr.Button("Calibrate 📸")
gr.Examples(examples=example_images, inputs=[image_path, choice_input])
with gr.Column():
gr.Markdown("""## Results""")
image_output = gr.Image(label="Calibration Results")
gr.Markdown("### Plot Options")
plot_undistort = gr.Checkbox(
label="undistort",
value=False,
info="Undistorted image "
+ "(this is only available for models with distortion "
+ "parameters and will overwrite other options).",
)
with gr.Row():
plot_up = gr.Checkbox(label="up-vectors", value=True)
plot_up_confidence = gr.Checkbox(label="up confidence", value=False)
plot_latitude = gr.Checkbox(label="latitude", value=True)
plot_latitude_confidence = gr.Checkbox(label="latitude confidence", value=False)
gr.Markdown("### Calibration Results")
text_output = gr.Textbox(label="Estimated parameters", type="text", lines=5)
# Define the action when the button is clicked
inference_state = gr.State()
plot_inputs = [
inference_state,
plot_up,
plot_up_confidence,
plot_latitude,
plot_latitude_confidence,
plot_undistort,
]
submit_btn.click(
fn=process_results,
inputs=[image_path, choice_input] + plot_inputs[1:],
outputs=[text_output, image_output, inference_state],
)
# Define the action when the plot checkboxes are clicked
plot_up.change(fn=update_plot, inputs=plot_inputs, outputs=image_output)
plot_up_confidence.change(fn=update_plot, inputs=plot_inputs, outputs=image_output)
plot_latitude.change(fn=update_plot, inputs=plot_inputs, outputs=image_output)
plot_latitude_confidence.change(fn=update_plot, inputs=plot_inputs, outputs=image_output)
plot_undistort.change(fn=update_plot, inputs=plot_inputs, outputs=image_output)
# Launch the app
demo.launch()