当前位置:   article > 正文

2.GraspNet-API之examples_graspnetapi怎么调用

graspnetapi怎么调用

2.1 检查 Dataset Files

您可以通过以下代码检查数据集中是否有任何丢失的文件。

from graspnetAPI import GraspNet
# GraspNetAPI example for checking the data completeness.
# change the graspnet_root path

### ROOT PATH FOR GRASPNET ###
graspnet_root = 'E:\datasets\GraspNet'

g = GraspNet(graspnet_root, 'kinect', 'all')
if g.checkDataCompleteness():
    print('\n')
    print('Check for kinect passed')


g = GraspNet(graspnet_root, 'realsense', 'all')
if g.checkDataCompleteness():
    print('Check for realsense passed')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

2.2 生成 Rectangle Grasp Labels

您可以自行生成矩形抓取标签。

# 1.导入必要的库:
# GraspNetAPI example for generating rectangle grasp from 6d grasp.
# change the graspnet_root path and NUM_PROCESS

from graspnetAPI import GraspNet
from graspnetAPI.graspnet import TOTAL_SCENE_NUM
import os
import numpy as np
from tqdm import tqdm

# 2.设置在生成标签时使用的进程数。
######################################################################
NUM_PROCESS = 24 # change NUM_PROCESS to the number of cores to use. #
######################################################################

# 3. 生成标签的函数。
def generate_scene_rectangle_grasp(sceneId, dump_folder, camera):
    g = GraspNet(graspnet_root, camera=camera, split='all')
    objIds = g.getObjIds(sceneIds = sceneId)
    grasp_labels = g.loadGraspLabels(objIds)
    collision_labels = g.loadCollisionLabels(sceneIds = sceneId)
    scene_dir = os.path.join(dump_folder,'scene_%04d' % sceneId)
    if not os.path.exists(scene_dir):
        os.mkdir(scene_dir)
    camera_dir = os.path.join(scene_dir, camera)
    if not os.path.exists(camera_dir):
        os.mkdir(camera_dir)
    for annId in tqdm(range(256), 'Scene:{}, Camera:{}'.format(sceneId, camera)):
        _6d_grasp = g.loadGrasp(sceneId = sceneId, annId = annId, format = '6d', camera = camera, grasp_labels = grasp_labels, collision_labels = collision_labels, fric_coef_thresh = 1.0)
        rect_grasp_group = _6d_grasp.to_rect_grasp_group(camera)
        rect_grasp_group.save_npy(os.path.join(camera_dir, '%04d.npy' % annId))
# 4.为每个场景和相机运行该函数。
if __name__ == '__main__':
    ####################################################################
    graspnet_root = 'E:\datasets\GraspNet'   # ROOT PATH FOR GRASPNET ##
    ####################################################################

    dump_folder = 'rect_labels'
    if not os.path.exists(dump_folder):
        os.mkdir(dump_folder)

    if NUM_PROCESS > 1:
        from multiprocessing import Pool
        pool = Pool(24)
        for camera in ['realsense', 'kinect']:
            for sceneId in range(120):
                pool.apply_async(func = generate_scene_rectangle_grasp, args = (sceneId, dump_folder, camera))
        pool.close()
        pool.join()
    
    else:
        generate_scene_rectangle_grasp(sceneId, dump_folder, camera)
  • 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

2.3 加载 Grasp Labels

6D 和 rect 格式标签都可以被加载。

# 1.导入相关库。
from graspnetAPI import GraspNet
import open3d as o3d
import cv2

# GraspNetAPI example for loading grasp for a scene.
# change the graspnet_root path


# 2.设置参数 and 创建 GraspNet 实例
####################################################################
graspnet_root = 'E:\datasets\GraspNet'      # ROOT PATH FOR GRASPNET
####################################################################

sceneId = 1
annId = 3

# initialize a GraspNet instance  
g = GraspNet(graspnet_root, camera='kinect', split='train')

# 3.以6d格式加载 Grasp Label,并可视化结果。
# load grasps of scene 1 with annotation id = 3, camera = kinect and fric_coef_thresh = 0.2
_6d_grasp = g.loadGrasp(sceneId = sceneId, annId = annId, format = '6d', camera = 'kinect', fric_coef_thresh = 0.2)
print('6d grasp:\n{}'.format(_6d_grasp))


# visualize the grasps using open3d
geometries = []
geometries.append(g.loadScenePointCloud(sceneId = sceneId, annId = annId, camera = 'kinect'))
geometries += _6d_grasp.random_sample(numGrasp = 20).to_open3d_geometry_list()
o3d.visualization.draw_geometries(geometries)


# 4.以矩形格式加载 Grasp Label ,并可视化结果。
# load rectangle grasps of scene 1 with annotation id = 3, camera = realsense and fric_coef_thresh = 0.2
rect_grasp = g.loadGrasp(sceneId = sceneId, annId = annId, format = 'rect', camera = 'realsense', fric_coef_thresh = 0.2)
print('rectangle grasp:\n{}'.format(rect_grasp))

# visualize the rectanglegrasps using opencv
bgr = g.loadBGR(sceneId = sceneId, annId = annId, camera = 'realsense')
img = rect_grasp.to_opencv_image(bgr, numGrasp = 20)
cv2.imshow('rectangle grasps', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 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

在这里插入图片描述
在这里插入图片描述

2.4 可视化 Dataset

Visualization of Dataset

# GraspNetAPI example for visualization.
# change the graspnet_root path
####################################################################
graspnet_root = 'E:\datasets\GraspNet'      # ROOT PATH FOR GRASPNET
####################################################################
from graspnetAPI import GraspNet

# 1. initialize a GraspNet instance  
g = GraspNet(graspnet_root, camera='kinect', split='train')
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在物体上显示grasp labels。

# 2.1 show object grasps
g.showObjGrasp(objIds = 0, show=True)
  • 1
  • 2

在这里插入图片描述

显示场景中物体的6d poses。

# 2.2 show 6d poses
g.show6DPose(sceneIds = 0, show = True)
  • 1
  • 2

在这里插入图片描述

在场景中显示矩形grasp labels。

# 2.3 show scene rectangle grasps
g.showSceneGrasp(sceneId = 0, camera = 'realsense', annId = 0, format = 'rect', numGrasp = 20)
  • 1
  • 2

在这里插入图片描述

在场景中显示 6D grasp labels。

# 2.4 show scene 6d grasps(You may need to wait several minutes)
g.showSceneGrasp(sceneId = 4, camera = 'kinect', annId = 2, format = '6d')
  • 1
  • 2

在这里插入图片描述

2.5 使用 NMS on Grasps

每个物体的初始标签均为一个np.arry列表。

2.6 转换 Labels between rectangle format and 6d format

2.7 评价

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/115936
推荐阅读
相关标签
  

闽ICP备14008679号