-
Notifications
You must be signed in to change notification settings - Fork 23
/
rgb_image_hand_fitting.py
192 lines (167 loc) · 5.55 KB
/
rgb_image_hand_fitting.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
"""Example with fitting a colored hand mesh model to an image."""
import datetime
import glob
import json
import os
import time
from typing import List
import cv2
from imageio.v3 import imread, imwrite
import matplotlib.pyplot as plt
import numpy as np
from typing_extensions import Literal
import deodr
from deodr import ColoredTriMesh, read_obj
from deodr.mesh_fitter import MeshRGBFitterWithPose
from deodr.meshlab_io import export_meshlab
from deodr.pytorch import MeshRGBFitterWithPose as PyTorchMeshRGBFitterWithPose
# from deodr.tensorflow import (
# MeshRGBFitterWithPose as TensorflowTorchMeshRGBFitterWithPose,
# )
DlLibraryType = Literal["pytorch", "tensorflow", "none"]
def run(
dl_library: DlLibraryType = "pytorch",
plot_curves: bool = True,
save_images: bool = True,
display: bool = True,
max_iter: int = 100,
n_subdivision: int = 0,
) -> List[float]:
mesh_fitter_selector = {
"none": MeshRGBFitterWithPose,
"pytorch": PyTorchMeshRGBFitterWithPose,
# "tensorflow": TensorflowTorchMeshRGBFitterWithPose,
}
hand_image = imread(os.path.join(deodr.data_path, "hand.png")).astype(np.double) / 255
obj_file = os.path.join(deodr.data_path, "hand.obj")
faces, vertices = read_obj(obj_file)
mesh = ColoredTriMesh(faces.copy(), vertices=vertices, nb_colors=3).subdivise(n_subdivision)
default_color = np.array([0.4, 0.3, 0.25])
default_light_directional = -np.array([0.1, 0.5, 0.4])
default_light_ambient = 0.6
euler_init = np.array([0, 0, 0])
translation_init = np.mean(mesh.vertices, axis=0)
# centering vertices
mesh.set_vertices(mesh.vertices - translation_init[None, :])
hand_fitter: MeshRGBFitterWithPose = mesh_fitter_selector[dl_library]( # type: ignore
mesh.vertices,
mesh.faces,
default_color=default_color,
default_light_directional=default_light_directional,
default_light_ambient=default_light_ambient,
update_lights=True,
update_color=True,
euler_init=euler_init,
translation_init=translation_init,
cregu=1000,
)
hand_fitter.reset()
background_color = np.median(
np.vstack(
(
hand_image[:10, :10, :].reshape(-1, 3),
hand_image[-10:, :10, :].reshape(-1, 3),
hand_image[-10:, -10:, :].reshape(-1, 3),
hand_image[:10, -10:, :].reshape(-1, 3),
)
),
axis=0,
)
background_color = np.array([0.5, 0.6, 0.7])
distortion = np.array([-1, 0, 0, 0, 0])
hand_fitter.set_image(hand_image, distortion=distortion)
hand_fitter.set_background_color(background_color)
energies = []
durations = []
start = time.time()
iterfolder = "./iterations/rgb"
if not os.path.exists(iterfolder):
os.makedirs(iterfolder)
for niter in range(max_iter):
energy, image, diff_image = hand_fitter.step()
energies.append(energy)
durations.append(time.time() - start)
if display or save_images:
combined_image = np.column_stack((hand_image, image, np.tile(diff_image[:, :, None], (1, 1, 3))))
if display:
cv2.imshow(
"animation",
cv2.resize(combined_image[:, :, ::-1], None, fx=2, fy=2),
)
if save_images:
imwrite(os.path.join(iterfolder, f"hand_iter_{niter}.png"), combined_image)
cv2.waitKey(1)
export_meshlab(
"iterations/rgb_fitted_meshlab.mlp",
hand_fitter.mesh,
[hand_fitter.camera],
[hand_image],
)
# save convergence curve
with open(
os.path.join(
iterfolder,
f'rgb_image_fitting_result_{str(datetime.datetime.now()).replace(":", "_")}.json',
),
"w",
) as f:
json.dump(
{
"label": f"{dl_library} {datetime.datetime.now()}",
"durations": durations,
"energies": energies,
},
f,
indent=4,
)
# compare with previous runs
if plot_curves:
plt.figure()
for file in glob.glob(os.path.join(iterfolder, "rgb_image_fitting_result_*.json")):
with open(file, "r") as fp:
json_data = json.load(fp)
plt.plot(
json_data["durations"],
json_data["energies"],
label=json_data["label"],
)
plt.xlabel("duration in seconds")
plt.ylabel("energies")
plt.legend()
plt.figure()
for file in glob.glob(os.path.join(iterfolder, "rgb_image_fitting_result_*.json")):
with open(file, "r") as fp:
json_data = json.load(fp)
plt.plot(json_data["energies"], label=json_data["label"])
plt.xlabel("interations")
plt.ylabel("energies")
plt.legend()
plt.show()
return energies
def main() -> None:
display = True
save_images = False
n_subdivision = 1
run(
dl_library="pytorch",
plot_curves=False,
display=display,
save_images=save_images,
n_subdivision=n_subdivision,
)
run(
dl_library="none",
plot_curves=False,
display=display,
save_images=save_images,
n_subdivision=n_subdivision,
)
run(
dl_library="tensorflow",
plot_curves=True,
display=display,
save_images=save_images,
n_subdivision=n_subdivision,
)
if __name__ == "__main__":
main()