【问题标题】:how to convert .pts or .npy file into .ply or .h5 file?如何将 .pts 或 .npy 文件转换为 .ply 或 .h5 文件?
【发布时间】:2019-02-18 23:12:17
【问题描述】:

我有 3d 点云数据作为 .npy 文件和 .pts 数据。

要将这些数据用于 3d 分类神经网络,我必须将这些数据更改为 .h5 文件。

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

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

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

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

【问题讨论】:

  • 我在想,你能把权重加载到 keras 模型中吗?如果可以,您可以从那里保存权重
  • @DanielR。谢谢回复,但我想做的只是将 .npy 或 .pts 格式转换为 .ply 或 .h5 格式..
  • 我问是因为this 用keras做起来很简单,输出h5/hdf5格式
  • @DanielR。我在想您是在谈论将模型保存为 h5/hdf5 格式,因为您提到了“权重”,对吗?但我有 .npy 和 .pts 格式的 3d 点云数据 (x,y,z)。我想改变这些..
  • 你是对的,我的错,我只是看了神经网络和.h5这两个词并得出了错误的解释。

标签: python numpy hdf5 ply-file-format


【解决方案1】:

更正/改进top answer

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


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)

【讨论】:

    【解决方案2】:

    我希望这段代码可以帮助您入门,它展示了如何从 npy(或随机点)创建 h5 文件。警告组和数据集的名称是任意的(这是一个示例)。

    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 doc

    最后但并非最不重要的一点是,您可以随时通过HDFgroup forum 向 HDF5 社区提问(他们提供闪亮的徽章,例如 SO,哇哦!!!)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      • 2017-05-21
      • 2019-04-14
      • 2020-12-15
      • 2022-11-10
      • 2013-08-31
      相关资源
      最近更新 更多