首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将.pts或.npy文件转换为.ply或.h5文件?

如何将.pts或.npy文件转换为.ply或.h5文件?
EN

Stack Overflow用户
提问于 2018-09-14 07:23:43
回答 2查看 5.8K关注 0票数 1

我有三维点云数据作为.npy文件和.pts数据。

为了将这些数据用于三维分类神经网络,我必须将这些数据转换为.h5文件。

因此,首先,我尝试使用python将.npy或.pts文件转换为.ply文件。

您能给我参考示例代码或帮助我转换文件格式吗?

另外,我非常感谢将.ply格式转换为.h5格式的方法。

对不起,我的英语水平很差。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-14 23:26:14

我希望这段代码能让你开始,它展示了如何从npy (或随机点)创建一个h5文件。警告组和数据集的名称是任意的(这是一个例子)。

代码语言:javascript
复制
import os
import h5py
import numpy as np

# reading or creating an array of points numpy style
def create_or_load_random_points_npy(file_radix, size, min, max):
    if os.path.exists(file_radix+'.npy'):
        arr = np.load(file_radix+'.npy')
    else:
        arr = np.random.uniform(min, max, (size,3))
        np.save(file_radix, arr)
    return arr


# converting a numpy array (size,3) to a h5 file with two groups representng two way
# of serializing points
def convert_array_2_shades_of_grey(file_radix, arr):
    file = h5py.File(file_radix + '.h5', 'w')
    #only one dataset in a group
    group = file.create_group("single_dataset")
    group.attrs["desc"]=np.string_("random points in a single dataset")
    dset=group.create_dataset("points", (len(arr), len(arr[0])), h5py.h5t.NATIVE_DOUBLE)
    dset[...]=arr
    #create a dataset for each coordinate
    group = file.create_group("several_datasets")
    group.attrs["desc"] = np.string_("random points in a several coordinates (one for each coord)")
    dset = group.create_dataset("x", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 0]
    dset = group.create_dataset("y", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 1]
    dset = group.create_dataset("z", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 2]

# loads the h5 file, choose which way of storing you would like to deserialize
def load_random_points_h5(file_radix, single=True):
    file = h5py.File(file_radix + '.h5', 'r')
    if single:
        group = file["single_dataset"]
        print 'reading -> ', group.attrs["desc"]
        dset=group["points"]
        arr = dset[...]
    else:
        group = file["several_datasets"]
        print 'reading -> ', group.attrs["desc"]
        dset = group["x"]
        arr = np.zeros((dset.size, 3))
        arr[:, 0] = dset[...]
        dset = group["y"]
        arr[:, 1] = dset[...]
        dset = group["z"]
        arr[:, 2] = dset[...]
    return arr

# And now we test !!!
file_radix = 'test'
# create or load the npy file
arr =  create_or_load_random_points_npy(file_radix, 10000, -100.0, 100.0)
# Well, well, what is in the box ?
print arr

# converting numpy array to h5
convert_array_2_shades_of_grey(file_radix, arr)

# loading single dataset style.
arr = load_random_points_h5(file_radix, True)
# Well, well, what is in the box ?
print arr
# loading several dataset style.
arr = load_random_points_h5(file_radix, False)
# Well, well, what is in the box ?
print arr

要查看h5文件的内容,请下载HDFview

也不要犹豫,看看h5py文档

最后但并非最不重要的一点是,您可以向HDF5社区的HDFgroup论坛询问问题(他们提供像这样的闪亮徽章,哇!)

票数 2
EN

Stack Overflow用户

发布于 2020-07-16 11:59:21

https://stackoverflow.com/a/52340306/13093413的修正/改进

如果您有多个要转换为.npy的.h5文件,那么将它们所在的目录的路径写入变量NPY_DIRECTORY

代码语言:javascript
复制
from os import listdir
from os.path import isfile, join
import os
import h5py
import numpy as np
NPY_FILES_DIRECTORY = ""
filenames = [f for f in listdir(NPY_FILES_DIRECTORY) if isfile(join(NPY_FILES_DIRECTORY, f))]



# reading or creating an array of points numpy style
def create_or_load_random_points_npy(filename, size, min, max):
    if os.path.exists(filename):
        arr = np.load(filename)
    else:
        arr = np.random.uniform(min, max, (size,3))
        np.save(filename, arr)
    return arr


# converting a numpy array (size,3) to a h5 file with two groups representng two way
# of serializing points
def convert_array_2_shades_of_grey(filename, arr):
    file = h5py.File(filename + '.h5', 'w')
    #only one dataset in a group
    group = file.create_group("single_dataset")
    group.attrs["desc"]=np.string_("random points in a single dataset")
    dset=group.create_dataset("points", (len(arr), len(arr[0])), h5py.h5t.NATIVE_DOUBLE)
    dset[...]=arr
    #create a dataset for each coordinate
    group = file.create_group("several_datasets")
    group.attrs["desc"] = np.string_("random points in a several coordinates (one for each coord)")
    dset = group.create_dataset("x", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 0]
    dset = group.create_dataset("y", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 1]
    dset = group.create_dataset("z", (len(arr),), h5py.h5t.NATIVE_DOUBLE)
    dset[...] = arr[:, 2]

# loads the h5 file, choose which way of storing you would like to deserialize
def load_random_points_h5(filename, single=True):
    file = h5py.File(filename + '.h5', 'r')
    if single:
        group = file["single_dataset"]
        print('reading -> ', group.attrs["desc"])
        dset=group["points"]
        arr = dset[...]
    else:
        group = file["several_datasets"]
        print('reading -> ', group.attrs["desc"])
        dset = group["x"]
        arr = np.zeros((dset.size, 3))
        arr[:, 0] = dset[...]
        dset = group["y"]
        arr[:, 1] = dset[...]
        dset = group["z"]
        arr[:, 2] = dset[...]
    return arr

# And now we test !!!
for filename in filenames:
    # create or load the npy file
    arr = create_or_load_random_points_npy(filename, 10000, -100.0, 100.0)
    # Well, well, what is in the box ?
    print(arr)

    # converting numpy array to h5
    convert_array_2_shades_of_grey(filename, arr)

    # loading single dataset style.
    arr = load_random_points_h5(filename, True)
    # Well, well, what is in the box ?
    print(arr)
    # loading several dataset style.
    arr = load_random_points_h5(filename, False)
    # Well, well, what is in the box ?
    print(arr)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52326888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档