forked from mkt1412/GraspGPT_public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualize.py
596 lines (487 loc) · 18.2 KB
/
visualize.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import argparse
import os
import sys
import glob
import time
import numpy as np
import open3d as o3d
import trimesh
import trimesh.transformations as tra
import matplotlib.pyplot as plt
from collections import defaultdict
from copy import deepcopy as copy
from PIL import Image
from tqdm import tqdm
from geometry_utils import farthest_grasps
def mkdir(dir):
"""
Creates folder if it doesn't exist
"""
if not os.path.isdir(dir):
os.makedirs(dir)
def get_gripper_control_points():
return np.array([
[-0.10, 0, 0, 1],
[-0.03, 0, 0, 1],
[-0.03, 0.07, 0, 1],
[0.03, 0.07, 0, 1],
[-0.03, 0.07, 0, 1],
[-0.03, -0.07, 0, 1],
[0.03, -0.07, 0, 1]])
def get_gripper_control_points_o3d(
grasp,
show_sweep_volume=False,
color=(
0.2,
0.8,
0)):
"""
Open3D Visualization of parallel-jaw grasp
grasp: [4, 4] np array
"""
meshes = []
align = tra.euler_matrix(np.pi / 2, 0, 0)
# Cylinder 3,5,6
cylinder_1 = o3d.geometry.TriangleMesh.create_cylinder(
radius=0.005, height=0.139)
transform = np.eye(4)
transform[0, 3] = -0.03
transform = np.matmul(align, transform)
transform = np.matmul(grasp, transform)
cylinder_1.paint_uniform_color(color)
cylinder_1.transform(transform)
# Cylinder 1 and 2
cylinder_2 = o3d.geometry.TriangleMesh.create_cylinder(
radius=0.005, height=0.07)
transform = tra.euler_matrix(0, np.pi / 2, 0)
transform[0, 3] = -0.065
transform = np.matmul(align, transform)
transform = np.matmul(grasp, transform)
cylinder_2.paint_uniform_color(color)
cylinder_2.transform(transform)
# Cylinder 5,4
cylinder_3 = o3d.geometry.TriangleMesh.create_cylinder(
radius=0.005, height=0.06)
transform = tra.euler_matrix(0, np.pi / 2, 0)
transform[2, 3] = 0.065
transform = np.matmul(align, transform)
transform = np.matmul(grasp, transform)
cylinder_3.paint_uniform_color(color)
cylinder_3.transform(transform)
# Cylinder 6, 7
cylinder_4 = o3d.geometry.TriangleMesh.create_cylinder(
radius=0.005, height=0.06)
transform = tra.euler_matrix(0, np.pi / 2, 0)
transform[2, 3] = -0.065
transform = np.matmul(align, transform)
transform = np.matmul(grasp, transform)
cylinder_4.paint_uniform_color(color)
cylinder_4.transform(transform)
cylinder_1.compute_vertex_normals()
cylinder_2.compute_vertex_normals()
cylinder_3.compute_vertex_normals()
cylinder_4.compute_vertex_normals()
meshes.append(cylinder_1)
meshes.append(cylinder_2)
meshes.append(cylinder_3)
meshes.append(cylinder_4)
# Just for visualizing - sweep volume
if show_sweep_volume:
finger_sweep_volume = o3d.geometry.TriangleMesh.create_box(
width=0.06, height=0.02, depth=0.14)
transform = np.eye(4)
transform[0, 3] = -0.06 / 2
transform[1, 3] = -0.02 / 2
transform[2, 3] = -0.14 / 2
transform = np.matmul(align, transform)
transform = np.matmul(grasp, transform)
finger_sweep_volume.paint_uniform_color([0, 0.2, 0.8])
finger_sweep_volume.transform(transform)
finger_sweep_volume.compute_vertex_normals()
meshes.append(finger_sweep_volume)
return meshes
def downsample_pc(pc, nsample):
if pc.shape[0] < nsample:
print(
'Less points in pc {}, than target dimensionality {}'.format(
pc.shape[0],
nsample))
chosen_one = np.random.choice(
pc.shape[0], nsample, replace=pc.shape[1] > nsample)
return pc[chosen_one, :], chosen_one
def draw_scene(
pc=None,
grasps=None,
subtract_pc_mean=False,
meshes=None,
debug_mode=False,
max_pc_points=15000,
max_grasps=10,
use_pc_color=True,
view_matrix=None,
save_dir=None,
grasp_colors=None,
window_name=""):
"""
Uses Open3D to plot grasps and point cloud data
Args:
save_dir: provide absolute path to save figure instead of visualizing on the GUI
"""
if view_matrix is None:
view_matrix = np.eye(4)
if grasps is not None:
if isinstance(grasps, np.ndarray):
grasps = list(grasps)
assert isinstance(grasps, list)
if pc is None and meshes is None:
raise InvalidArgumentError(
"Pass in at least a mesh or the point cloud")
if grasp_colors is not None:
assert isinstance(grasp_colors, list)
if grasps is not None:
assert len(grasps) == len(grasp_colors)
vis = o3d.visualization.Visualizer()
vis.create_window(window_name=window_name)
if pc is not None:
if pc.shape[1] == 6:
color = pc[:, 3:] / 255.0
if pc.shape[0] > max_pc_points:
pc, selection = downsample_pc(pc, max_pc_points)
n_points = pc.shape[0]
color = None
if pc.shape[1] == 6:
color = np.zeros((n_points, 4))
color[:, :3] = pc[:, 3:]
color[:, 3] = np.ones(n_points)
if subtract_pc_mean:
pc_mean = np.mean(pc[:, :3], 0)
pc[:, :3] -= pc_mean
pc_o3d = o3d.geometry.PointCloud()
pc_o3d.points = o3d.utility.Vector3dVector(pc[:, :3])
if color is not None and use_pc_color:
pc_o3d.colors = o3d.utility.Vector3dVector(pc[:, 3:] / 255)
else:
pc_o3d.paint_uniform_color([1, 0.706, 0])
pc_o3d.transform(view_matrix)
vis.add_geometry(pc_o3d)
if grasps is not None:
if len(grasps) > max_grasps:
assert isinstance(grasps, list)
selection = np.random.randint(
low=0, high=len(grasps), size=max_grasps)
grasps = list(np.array(grasps)[selection])
if grasp_colors is not None:
if not isinstance(grasp_colors, np.ndarray):
grasp_colors = np.array(grasp_colors)
grasp_colors = grasp_colors[selection]
for gi, grasp in enumerate(grasps):
grasp_tmp = copy(grasp)
if pc is not None and subtract_pc_mean:
grasp_tmp[:3, 3] -= pc_mean
color = (0.2, 0.8, 0)
if grasp_colors is not None:
color = grasp_colors[gi]
for item in get_gripper_control_points_o3d(
grasp_tmp, show_sweep_volume=debug_mode, color=color):
item.transform(view_matrix)
vis.add_geometry(item)
grasp_pc = get_gripper_control_points()
grasp_pc = np.matmul(grasp_tmp, grasp_pc.T).T
grasp_pc = grasp_pc[:4, :]
grasp_pc_o3d = o3d.geometry.PointCloud()
grasp_pc_o3d.points = o3d.utility.Vector3dVector(grasp_pc[:, :3])
grasp_pc_o3d.paint_uniform_color([0, 0, 1])
grasp_pc_o3d.transform(view_matrix)
vis.add_geometry(grasp_pc_o3d)
if meshes is not None:
assert isinstance(meshes, list)
if isinstance(
meshes[0],
tuple) and isinstance(
meshes[0][0],
trimesh.primitives.Box):
# Convert from trimesh to open3d
meshes_o3d = []
for elem in meshes:
(voxel, extents, transform) = elem
voxel_o3d = o3d.geometry.TriangleMesh.create_box(
width=extents[0], height=extents[1], depth=extents[2])
voxel_o3d.compute_vertex_normals()
voxel_o3d.paint_uniform_color([0.8, 0.2, 0])
voxel_o3d.transform(transform)
meshes_o3d.append(voxel_o3d)
meshes = meshes_o3d
for mesh in meshes:
mesh.transform(view_matrix)
vis.add_geometry(mesh)
if save_dir is None:
vis.run()
if save_dir is not None:
mkdir(os.path.dirname(save_dir))
time.sleep(0.25)
image = vis.capture_screen_float_buffer(True)
time.sleep(0.25)
image = np.asarray(image)
DELTA_Y = 20
DELTA_X = 200
image = crop(image, DELTA_X, DELTA_Y)
plt.imsave(save_dir, np.asarray(image))
time.sleep(0.25)
if save_dir is None:
vis.destroy_window()
def crop(image, delta_x, delta_y):
h, w, _ = image.shape
return image[delta_y:h - delta_y, delta_x:w - delta_x, :]
def data_and_grasps(pc_file, grasps_file, fps=1):
"""
Plots the point cloud data and grasps.
Args:
pc_file: Absolute path to npy file with point cloud data
grasps_file: Absolute path to npy file with grasp data
fps: (default=1) Only plots a set of grasps filtered using farthest point sampling
"""
pc = np.load(pc_file)
grasps = np.load(grasps_file)
# Ensure that grasp and pc is mean centered
pc_mean = pc[:, :3].mean(axis=0)
pc[:, :3] -= pc_mean
grasps[:, :3, 3] -= pc_mean
draw_scene(pc)
if fps:
grasps = farthest_grasps(grasps, num_clusters=32, num_grasps=50)
draw_scene(pc, grasps, max_grasps=grasps.shape[0])
def visualize_labels(
obj_path,
rgb_image_path,
vis_dir,
label_filename,
visualize_labels_blacklist_object,
visualize_labels_blacklist_task):
"""
This function is used to visualize labeled grasps
:param obj_path: path to the /scans folder
:param vis_dir: where visualizations should be saved
:param label_filename: file storing labeled grasps. In this file, each grasp can be labeled either by majority vote
or accumulated score. Need to set visualize_majority correspondingly.
:param visualize_labels_blacklist_object:
:param visualize_labels_blacklist_task:
:return:
"""
# visualization from different angles
all_yaws = [np.pi / 3, -np.pi / 3, 0, 0]
all_pitchs = [0, 0, np.pi / 3, -np.pi / 3]
# read results
# save labels in a dictionary. E.g., {("001_squeezer", "flatten"): [(1,
# "True"), (2, "Weak True"), (3, "False")]}
all_labels = defaultdict(list)
with open(label_filename, "r") as fh:
for line in fh:
line = line.strip()
if line:
grasp_id, score = line.split(":")
score = int(score)
obj, grasp_num, task = grasp_id.split("-")
grasp_num = int(grasp_num)
all_labels[(obj, task)].append((grasp_num, score))
for obj, task in tqdm(all_labels):
if obj.find(visualize_labels_blacklist_object) >= 0 and task.find(
visualize_labels_blacklist_task) >= 0:
pc_file = os.path.join(obj_path, obj, "fused_pc.npy")
if not os.path.exists(pc_file):
raise ValueError(
'Unable to find processed point cloud file {}'.format(pc_file))
rgb_file = os.path.join(rgb_image_path, "{}.png".format(obj))
if not os.path.exists(rgb_file):
raise ValueError(
'Unable to find rgb image file {}'.format(rgb_file))
pc = np.load(pc_file)
pc_mean = pc[:, :3].mean(axis=0)
pc[:, :3] -= pc_mean
grasp_colors = []
grasps = []
labels = all_labels[(obj, task)]
labels = sorted(labels, key=lambda x: x[0])
for grasp_id, label in labels:
grasp_file = os.path.join(
args.obj_path, obj, "grasps", str(grasp_id), "grasp.npy")
if not os.path.exists(grasp_file):
raise ValueError(
'Grasp {} not found, have you rendered the grasp?'.format(grasp_file))
grasp = np.load(grasp_file)
grasps.append(grasp)
if label == 1:
color = (0, 0.9, 0)
elif label == 0:
color = (0.9, 0.9, 0)
elif label == -1:
color = (0.9, 0, 0)
elif label == -2:
color = (0.9, 0.5, 0)
else:
raise Exception
grasp_colors.append(color)
print("Object {}, Task {}".format(obj, task))
draw_scene(
pc,
grasps,
grasp_colors=grasp_colors,
window_name=task,
max_grasps=len(grasps))
def combine_images(
visualization_files,
rgb_image_file,
combined_file,
single_img_dimesion=[
1920,
1080],
final_img_width=None):
"""
This function is used to combine point cloud visualizations from different angles and the rgb image
:param visualization_files: paths to visualizations
:param rgb_image_file: path to the rgb image
:param single_img_dimesion: size of each image
:param final_img_width:
:return:
"""
imgs = []
for img_file in visualization_files:
img = Image.open(img_file)
# resize images to fixed size. Images rendered from open3d do not have
# fixed size
img = img.resize(single_img_dimesion, Image.ANTIALIAS)
img = np.asarray(img)[..., :3]
imgs.append(img)
rgb_image = Image.open(rgb_image_file)
width, height = rgb_image.size
resize_height = imgs[0].shape[0]
resize_width = int(resize_height * 1.0 / height * width)
rgb_image = np.asarray(
rgb_image.resize(
(resize_width,
resize_height),
Image.ANTIALIAS))
imgs.insert(0, rgb_image)
imgs_comb = np.hstack([img for img in imgs])
imgs_comb = Image.fromarray(imgs_comb)
if final_img_width is not None:
width, height = imgs_comb.size
resize_width = final_img_width
resize_height = int(resize_width * 1.0 / width * height)
imgs_comb = imgs_comb.resize(
(resize_width, resize_height), Image.ANTIALIAS)
imgs_comb.save(combined_file)
def main(args):
session_dir = os.path.join(args.obj_path, args.obj_name)
object_files = sorted(glob.glob(os.path.join(session_dir, "*.pkl")))
obj_dir = os.path.dirname(object_files[0])
if args.data_and_grasps:
pc_file = os.path.join(obj_dir, 'fused_pc_clean.npy')
grasps_file = os.path.join(obj_dir, 'fused_grasps_clean.npy')
if not os.path.exists(pc_file):
raise ValueError(
'Unable to find processed point cloud file {}'.format(pc_file))
data_and_grasps(pc_file, grasps_file, fps=args.fps)
elif args.visualize_grasp:
if args.grasp_id == -1:
raise ValueError('Please pass in grasp_id in the args')
pc_file = os.path.join(obj_dir, 'fused_pc_clean.npy')
grasp_file = os.path.join(
obj_dir, str("grasps"), str(
args.grasp_id), "grasp.npy")
if not os.path.exists(pc_file):
raise ValueError(
'Unable to find processed point cloud file {}'.format(pc_file))
if not os.path.exists(grasp_file):
raise ValueError(
'Grasp {} not found, have you rendered the grasp?'.format(
args.grasp_id))
pc = np.load(pc_file)
# Sampled grasps are already pc mean centered
grasp = np.load(grasp_file)
pc_mean = pc[:, :3].mean(axis=0)
pc[:, :3] -= pc_mean
draw_scene(pc, [grasp, ])
elif args.visualize_labels:
if not os.path.exists(args.label_path):
raise ValueError('Please pass in grasp label file in the args')
vis_dir = os.path.join(args.obj_path, "../labeled_grasps")
if not os.path.exists(vis_dir):
os.mkdir(vis_dir)
print("Saving visualization of labeled grasps to {} ...".format(vis_dir))
if not os.path.exists(args.rgb_image_path):
raise ValueError('Please pass in path to rgb images in the args')
visualize_labels(
args.obj_path,
args.rgb_image_path,
vis_dir,
args.label_path,
args.visualize_labels_blacklist_object,
args.visualize_labels_blacklist_task)
else:
print("Nothing to do :) Please provide the right args ")
def set_seed(seed):
np.random.seed(seed)
def process_args():
parser = argparse.ArgumentParser(description="visualize data and stuff")
parser.add_argument('--obj_name', help='', default='002_strainer')
parser.add_argument('--obj_path', help='', default='')
parser.add_argument('--seed', help='', type=int, default=0)
parser.add_argument('--grasp_id', help='', type=int, default=-1)
parser.add_argument(
'--visualize_grasp',
help='',
action='store_true',
default=False)
parser.add_argument(
'--visualize_labels',
help='',
action='store_true',
default=False)
parser.add_argument(
'--visualize_labels_debug',
help='Step through grasps one at a time',
action='store_true',
default=False)
parser.add_argument(
'--visualize_labels_blacklist_task',
help='',
type=str,
default='')
parser.add_argument(
'--visualize_labels_blacklist_object',
help='',
type=str,
default='')
parser.add_argument(
'--visualize_data_and_grasps',
help='',
action='store_true',
default=False)
parser.add_argument(
'--data_and_grasps',
help='',
action='store_true',
default=False)
parser.add_argument(
'--fps',
help='Use farthest point sampling on grasps',
type=int,
default=1)
parser.add_argument('--label_path', help='', default='')
parser.add_argument('--rgb_image_path', help='', default='')
args = parser.parse_args()
if args.obj_path == '':
args.obj_path = os.path.join(os.getcwd(), 'data/taskgrasp')
assert os.path.exists(args.obj_path)
assert os.path.isabs(args.obj_path)
if args.label_path == '':
args.label_path = os.path.join(args.obj_path, "task2_results.txt")
if args.rgb_image_path == '':
args.rgb_image_path = os.path.join(args.obj_path, "rgb_images")
args.obj_path = os.path.join(args.obj_path, 'scans')
assert os.path.exists(args.obj_path)
set_seed(args.seed)
return args
if __name__ == '__main__':
args = process_args()
main(args)