Follow A-SDF 的Data Generation
部分:
We follow
(1) ANSCH to create URDF for shape2motion dataset
(1-2) URDF2OBJ(本人認(rèn)為是1-2之間需要進(jìn)行的重要的過渡部分)
(2) Manifold to create watertight meshes
(3) and modified mesh_to_sdf for generating sampled points and sdf values.
1. ANSCH to create URDF for shape2motion dataset
follow這個(gè)github: ANSCH
(1)克隆數(shù)據(jù)集,編輯自己的路徑信息
git clone https://github.com/dragonlong/articulated-pose.git # 克隆倉(cāng)庫(kù)
# global_info主要放路徑信息,通過下行命令編輯路徑信息
vim global_info.py
在global_info.py
中,主要修改192行的self.base_path
路徑,改成克隆下來文件夾的位置,我這里改成/mnt/d/sdc/liutong/codes/articulated-pose
。
(2)Shape2Motion數(shù)據(jù)集的解壓和使用
因?yàn)檫@里需要使用到Shape2Motion
數(shù)據(jù)集,所以下載一下,Shape2Motion
項(xiàng)目主頁(yè):Shape2Motion,Shape2Motion
下載鏈接:Shape2Motion下載鏈接
下載好Shape2Motion
數(shù)據(jù)集后,在zip文件路徑做如下操作
unzip 'Motion Dataset v0.zip' # 解壓數(shù)據(jù)集
mv 'Motion Dataset v0' shape2motion # 重命名數(shù)據(jù)集
mv shape2motion /mnt/d/sdc/liutong/codes/articulated-pose/dataset/ # 將數(shù)據(jù)集移動(dòng)到目標(biāo)目錄下
(3)json2urdf
follow github上的操作:
cd tools # 進(jìn)入工具文件夾
python json2urdf.py # 執(zhí)行python2urdf
可能會(huì)出現(xiàn)以下報(bào)錯(cuò),根據(jù)報(bào)錯(cuò)進(jìn)行修改后重復(fù)運(yùn)行python json2urdf.py
這時(shí)候報(bào)錯(cuò)1:
Traceback (most recent call last):
File "/mnt/d/sdc/liutong/codes/articulated-pose/tools/json2urdf.py", line 62, in <module>
all_objs = os.listdir( base_path + dataset + '/objects' )
FileNotFoundError: [Errno 2] No such file or directory: '/mnt/d/sdc/liutong/codes/articulated-pose/dataset/shape2motion/objects'
(gapartnet) root@szu-SYS-7048GR-TR:/mnt/d/sdc/liutong/codes/articulated-pose/tools# ls -l ../dadtaset/shape2motion
經(jīng)檢查,是Shape2Motion
下沒有objects
文件夾,所以我將報(bào)錯(cuò)的62行進(jìn)行更改如下,確保這部分可以正常運(yùn)行:
# 從
all_objs = os.listdir( base_path + dataset + '/objects' )
# 更改至
all_objs = os.listdir( base_path + dataset )
同理,73行中的路徑代碼因?yàn)橐灿?code>objects,所以肯定也會(huì)出問題,同樣進(jìn)行修改:
# 從
instances_per_obj = sorted(glob.glob(base_path + dataset + '/objects/' + obj_name + '/*'))
# 更改至
instances_per_obj = sorted(glob.glob(base_path + dataset + '/' + obj_name + '/*'))
接著會(huì) 報(bào)錯(cuò)2:
Traceback (most recent call last):
File "/mnt/d/sdc/liutong/codes/articulated-pose/tools/json2urdf.py", line 87, in <module>
with open(json_name[0]) as json_file:
IndexError: list index out of range
這是因?yàn)榍懊孢\(yùn)行的時(shí)候在dataset/shape2motion/
下生成了一個(gè)urdf文件夾,這里面是生成的結(jié)果,所以會(huì)導(dǎo)致讀取不到.json
文件,所以只需要執(zhí)行:
rm -rf dataset/shape2motion/urdf
即可?;蛘咭粍谟酪莸馗牡?code>urdf的保存路徑(前面的更改我沒有把舊代碼刪掉,而注釋舊代碼,在下面添加新代碼,所以我這里保存路徑是在85行)。這里有個(gè)小小的擔(dān)心是會(huì)不會(huì)后續(xù)條件下也要對(duì)urdf
的路徑進(jìn)行修改:
# 從
save_dir = base_path + '/' + dataset + '/urdf/{}/{}'.format(obj_name, instance_name) # todo
# 更改至
save_dir = base_path + 'urdf/{}/{}'.format(obj_name, instance_name)
接著會(huì) 報(bào)錯(cuò)3:
Traceback (most recent call last):
File "/mnt/d/sdc/liutong/codes/articulated-pose/tools/json2urdf.py", line 224, in <module>
f.write('{}/{}\t'.format(obj_j['revolute'], obj_j['prismatic']))
KeyError: 'revolute'
發(fā)現(xiàn)是因?yàn)橛械?code>obj_j為空造成的,故更改如下:
# 從
with open(base_path + '/' + dataset + '/statistics.txt', "a+") as f:
for obj_j in object_joints:
f.write('{}/{}\t'.format(obj_j['revolute'], obj_j['prismatic']))
f.write('\n')
# 更改至
with open(base_path + '/' + dataset + '/statistics.txt', "a+") as f:
for obj_j in object_joints:
if not len(obj_j) == 0:
f.write('{}/{}\t'.format(obj_j['revolute'], obj_j['prismatic']))
else:
f.write('NAN/NAN\t')
f.write('\n')
雖然ANSCH后續(xù)還有一些渲染數(shù)據(jù)的操作,但是這里我們只需要拿到URDF就足夠了
1-2. URDF2OBJ
在這一步,我follow了一個(gè)已經(jīng)寫好的github代碼:urdf_to_obj
由于我們得到的文件形式是這樣的:
我們要取的是這下邊的syn.urdf來進(jìn)行最終結(jié)果的合成,我根據(jù)這個(gè)文件格式改了一下我上面提及的github的代碼,更改后的代碼urdf_to_obj如下,參數(shù)釋義:--urdf_file_dir
:urdf文件夾的路徑--output_dir
:輸出文件夾的路徑--angle
:用于給輸出obj文件命名時(shí)候用的,你生成的是多少度就寫多少度就好了(并不是在這里指定角度就能夠?qū)崿F(xiàn)生成)
from urdfpy import URDF
import numpy as np
import trimesh
import argparse
import meshes_extracted
import os
def merge_meshes(mesh_list):
"""Merge a list of meshes into a single mesh
Parameters:
- mesh_list (list): list of trimesh.Trimesh objects
Returns:
- mesh (trimesh.Trimesh): merged mesh"""
# Get vertices and faces
verts_list = [mesh.vertices for mesh in mesh_list]
faces_list = [mesh.faces for mesh in mesh_list]
# Num of faces per mesh
faces_offset = np.cumsum([v.shape[0] for v in verts_list], dtype=np.float32)
# Compute offset for faces, otherwise they all start from 0
faces_offset = np.insert(faces_offset, 0, 0)[:-1]
verts = np.vstack(verts_list)
faces = np.vstack([face + offset for face, offset in zip(faces_list, faces_offset)])
# Create single mesh
mesh = trimesh.Trimesh(verts, faces)
return mesh
def main(args):
"""Extract meshes from a URDF file and save them as .obj files"""
# Load urdf file dir
urdf_file_dir = args.urdf_file_dir
output_dir = args.output_dir
# Load other parameters
object_angle = args.angle
# Get the sub_dir under urdf_file_dir, and then 0001/0002/0003/0004/0005
urdf_root = sorted(os.listdir(urdf_file_dir))
for urdf_dir in urdf_root:
# urdf_file_path be like 0001/syn.urdf, 0002/syn.urdf, 0003/syn.urdf, etc
urdf_file_path = os.path.join(urdf_file_dir, urdf_dir, 'syn.urdf')
output_path = os.path.join(output_dir, f'{urdf_dir}art{object_angle}.obj')
# Load urdf file
robot = URDF.load(urdf_file_path)
meshes = robot.visual_trimesh_fk()
mesh_list = []
for idx, mesh in enumerate(meshes):
pose = meshes[mesh] # 4 x 4 : rotation + translation
translation = pose[:3, 3][:, None]
# Add a column of zeros to the vertices
verts = np.array(mesh.vertices)
zeros = np.zeros((verts.shape[0], 1))
new_verts = np.hstack((verts, zeros))
# Apply pose to the vertices
verts_pose = pose @ new_verts.transpose(1, 0)
verts_pose = verts_pose[:3, :] + translation
verts_pose = verts_pose.transpose(1, 0)
mesh_extracted = trimesh.Trimesh(verts_pose, mesh.faces)
mesh_list.append(mesh_extracted)
# Merge meshes
mesh_merged = merge_meshes(mesh_list)
# Save merged meshes
print('save ' + str(output_path))
trimesh.exchange.export.export_mesh(mesh_merged, output_path, file_type='obj')
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--urdf_file_dir", default='', type=str, help="Path to the .urdf file", required=True
)
parser.add_argument(
"--output_dir", default='', type=str, help="Path to the output dir", required=True
)
parser.add_argument(
"--angle", default='', type=str, help="Angle of objects, be used to name the file", required=True
)
args = parser.parse_args()
main(args)
運(yùn)行示例:
python urdf_to_obj_group.py --urdf_file_dir /mnt/d/sdc/liutong/codes/articulated-pose/dataset/shape2motion/urdf/door --output_dir obj_tmp --angle 90
報(bào)錯(cuò)如下:
Traceback (most recent call last):
File "urdf_to_obj_group.py", line 104, in <module>
main(args)
File "urdf_to_obj_group.py", line 56, in main
robot = URDF.load(urdf_file_path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 3729, in load
return URDF._from_xml(node, path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 3926, in _from_xml
kwargs = cls._parse(node, path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 161, in _parse
kwargs.update(cls._parse_simple_elements(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 137, in _parse_simple_elements
v = [t._from_xml(n, path) for n in vs]
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 137, in <listcomp>
v = [t._from_xml(n, path) for n in vs]
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 181, in _from_xml
return cls(**cls._parse(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 161, in _parse
kwargs.update(cls._parse_simple_elements(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 137, in _parse_simple_elements
v = [t._from_xml(n, path) for n in vs]
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 137, in <listcomp>
v = [t._from_xml(n, path) for n in vs]
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 1146, in _from_xml
kwargs = cls._parse(node, path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 161, in _parse
kwargs.update(cls._parse_simple_elements(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 127, in _parse_simple_elements
v = t._from_xml(v, path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 181, in _from_xml
return cls(**cls._parse(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 161, in _parse
kwargs.update(cls._parse_simple_elements(node, path))
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 127, in _parse_simple_elements
v = t._from_xml(v, path)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/urdf.py", line 581, in _from_xml
meshes = load_meshes(fn)
File "/home/liutong/anaconda3/envs/gapartnet/lib/python3.8/site-packages/urdfpy/utils.py", line 235, in load_meshes
raise ValueError('At least one mesh must be pmeshesent in file')
ValueError: At least one mesh must be pmeshesent in file
其實(shí)使用原始github連接中的python urdf_to_obj.py --urdf_path relative/path/to/urdf
來進(jìn)行試驗(yàn)的話,會(huì)發(fā)現(xiàn)0001的syn.urdf文件會(huì)報(bào)這樣的錯(cuò)誤,而0002的syn.urdf文件就不會(huì)報(bào)這樣的錯(cuò)誤。
經(jīng)過仔細(xì)地檢查發(fā)現(xiàn),實(shí)際上是因?yàn)?001的syn.urdf中,mesh
- none_motion.obj
的vertices數(shù)量為空所導(dǎo)致的,但是因?yàn)檫@個(gè)錯(cuò)誤是包里的錯(cuò)誤,所以我也不知道應(yīng)該怎么進(jìn)行修改比較合適。
最后我是直接刪掉會(huì)產(chǎn)生這個(gè)異常的文件夾,最后不會(huì)產(chǎn)生異常的文件夾應(yīng)該只有51個(gè)。
2. Manifold to create watertight meshes
這一步實(shí)際上就是縮減網(wǎng)格數(shù)量,因?yàn)樵镜木W(wǎng)格太密集了
follow這個(gè)github: Manifold
follow里面的Install
進(jìn)行安裝就可以了
然后我根據(jù)它的命令,用python寫了一個(gè)自動(dòng)執(zhí)行減少網(wǎng)格數(shù)量的程序,其中要用到的參數(shù)如下:--obj_file_dir
:放置了URDF2OBJ的那些obj文件的文件夾--target_dir
:輸出文件夾路徑
import os
import argparse
# res = os.popen('ls').read()
# print(res)
def main(args):
# Load obj file dir
obj_file_dir = args.obj_file_dir
target_dir = args.target_dir
obj_files = sorted(os.listdir(obj_file_dir))
for obj in obj_files:
source_obj = os.path.join(obj_file_dir, obj)
obj = "manifold_" + obj
target_obj = os.path.join(target_dir, obj)
res = os.popen('./manifold ' + source_obj + ' ' + target_obj).read()
print(res)
if __name__=='__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--obj_file_dir", default='', type=str, help="Path to the .obj file dir", required=True
)
parser.add_argument(
"--target_dir", default='', type=str, help="Path to the target dir", required=True
)
args = parser.parse_args()
main(args)
這是得到的結(jié)果:
3. modified mesh_to_sdf for generating sampled points and sdf values
follow 這個(gè)github: mesh_to_sdf
按照里面的步驟安裝好
這一步實(shí)際上就是做sdf采樣了,像A-SDF里的話采樣好像是保存成一些.npy
還是.npz
文件的,但是我這里根據(jù)我自己的需要,需要采樣并保存到.mat
文件中,下面提供一個(gè)保存到.mat文件中的代碼:--manifold_obj_dir
: 上一步manifold減少網(wǎng)格數(shù)量后的結(jié)果--output_dir
: .mat文件的保存路徑文章來源:http://www.zghlxwxcb.cn/news/detail-472621.html
import os
import mesh_to_sdf
# from mesh_to_sdf import sample_sdf_near_surface
import trimesh
import inspect
import pyrender
import numpy as np
import scipy
from scipy.io import savemat
from plyfile import PlyData
import argparse
os.environ['PYOPENGL_PLATFORM'] = 'egl'
def calculate_ply_center(mesh):
# 獲取頂點(diǎn)數(shù)據(jù)
vertices = mesh.vertices
# 提取頂點(diǎn)坐標(biāo)
x_coords = vertices[:, 0]
y_coords = vertices[:, 1]
z_coords = vertices[:, 2]
# 計(jì)算頂點(diǎn)坐標(biāo)的平均值
center_x = np.mean(x_coords)
center_y = np.mean(y_coords)
center_z = np.mean(z_coords)
return center_x, center_y, center_z
def scale_to_specific_sphere(mesh):
vertices = mesh.vertices - calculate_ply_center(mesh)
distances = np.linalg.norm(vertices, axis=1)
vertices /= np.max(distances) / 1.03
return trimesh.Trimesh(vertices=vertices, faces=mesh.faces)
if __name__ == "__main__":
print(inspect.getfile(mesh_to_sdf))
parser = argparse.ArgumentParser()
parser.add_argument(
"--manifold_obj_dir", default='./Manifold_result', type=str, help="Path to .obj file dir"
)
parser.add_argument(
"--output_dir", default='./All_result', type=str, help="Path to the .mat dir"
)
args = parser.parse_args()
manifold_obj_dir = args.manifold_obj_dir
save_dir = args.output_dir
if not os.path.exists(os.path.join(save_dir, 'surface_pts_n_normal')):
os.mkdir(os.path.join(save_dir, 'surface_pts_n_normal'))
if not os.path.exists(os.path.join(save_dir, 'free_space_pts')):
os.mkdir(os.path.join(save_dir, 'free_space_pts'))
save_surface_dir = os.path.join(save_dir, 'surface_pts_n_normal')
save_free_dir = os.path.join(save_dir, 'free_space_pts')
manifold_dirs = os.listdir(manifold_obj_dir)
for dirs in manifold_dirs:
files = os.listdir(os.path.join(manifold_obj_dir, dirs))
for file_ in files:
print("Process " + str(file_) + " ......")
mesh = trimesh.load(os.path.join(manifold_obj_dir, dirs, file_))
mesh = scale_to_specific_sphere(mesh)
surface = mesh_to_sdf.get_surface_point_cloud(mesh, surface_point_method='scan', sample_point_count=1000)
surface_points = surface.points
surface_normals = surface.normals
surface_data = np.concatenate((surface_points, surface_normals), axis=1)
surface_data = {'p': surface_data}
free_p, free_sdf = mesh_to_sdf.sample_sdf_in_cube(mesh, surface_point_method='scan', number_of_points=1000,
sample_point_count=1000, sign_method='depth')
free_sdf = free_sdf.reshape(-1, 1)
free_data = np.concatenate((free_p, free_sdf), axis=1)
free_data = {'p_sdf': free_data}
print("Saving " + str(str(file_.split("_")[-1]).split('.')[0])+'.mat' + " to " + str(os.path.join(save_surface_dir, str(str(file_.split("_")[-1]).split('.')[0])+'.mat')))
savemat(os.path.join(save_surface_dir, str(str(file_.split("_")[-1]).split('.')[0])+'.mat'), surface_data)
print("Saving " + str(str(file_.split("_")[-1]).split('.')[0])+'.mat' + " to " + str(os.path.join(save_free_dir, str(str(file_.split("_")[-1]).split('.')[0])+'.mat')))
savemat(os.path.join(save_free_dir, str(str(file_.split("_")[-1]).split('.')[0])+'.mat'), free_data)
# # 加載DIF原本的free_space_pts,查看可視化效果
# result = scipy.io.loadmat('free_space_cbc47018135fc1b1462977c6d3c24550.mat')['p_sdf']
# free_p = result[:, :3]
# free_sdf = result[:, 3:]
# free_sdf = free_sdf.reshape(-1)
# print(free_p.shape)
# print(free_sdf.shape)
# # 可視化free_space_pts的點(diǎn)
# colors = np.zeros(free_p.shape)
# colors[free_sdf < 0, 2] = 1
# colors[free_sdf > 0, 0] = 1
# cloud = pyrender.Mesh.from_points(free_p, colors=colors)
# scene = pyrender.Scene()
# scene.add(cloud)
# viewer = pyrender.Viewer(scene, use_raymond_lighting=True, point_size=2)
得到的結(jié)果如下:
最終得到的這些SDF對(duì)文件,只需要放到目標(biāo)路徑文件夾下進(jìn)行使用就好了文章來源地址http://www.zghlxwxcb.cn/news/detail-472621.html
到了這里,關(guān)于【計(jì)算機(jī)圖形學(xué)】【代碼復(fù)現(xiàn)】A-SDF中的數(shù)據(jù)集制作與數(shù)據(jù)生成的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!