"Can't broadcast (37,) -> (37, 2)" 在写入 H5 文件时

"Can't broadcast (37,) -> (37, 2)" while writing in a H5 file

我想在我的 H5 文件(称为“注释”)的每个单元格中插入坐标 (x, y)None,其中 (x, y) = (0.0, 0.0).

这是H5文件的可视化:

这是我创建和填充 H5 文件的代码:


# Create a NaN matrix
none_matrix = np.zeros((num_images, len(skeleton_names_list), 2))
for i in range(num_images):
    for j in range(len(skeleton_names_list)):
        none_matrix[i][j] = None

# Create the H5 file with all NaN entries
hf.create_dataset('annotations', shape=(num_images, len(skeleton_names_list), 2), dtype=np.float64, data=none_matrix)

joints_coordinates = []
num_ann = 0
j_file = open('dog.json')
j_data = json.load(j_file)
for xy in j_data:
    joints_coordinates.append(xy['joints'])
    image_name = xy['segmentation_path']
    image_index = idx_dict.get(image_name)
    for i in range(len(skeleton_names_list)):
        if joints_coordinates[num_ann][i][0] + joints_coordinates[num_ann][i][1] == 0:
            joints_coordinates[num_ann][i] = None
    # Update the image_index-row of the H5 file with the array "joints_coordinates[num_ann]"
    hf["annotations"][image_index] = joints_coordinates[num_ann]
    num_ann += 1

joints_coordinates 的形式为:

[[[x, y], ...], ...]

为什么会出现此错误?为什么在我的 H5 文件中有 0.0 个值而不是 NaN?

    Traceback (most recent call last):
      File "C:/Users/Alberto Ursino/Desktop/IntellIj Local Files/Write on H5/write.py", line 120, in <module>
        hf["annotations"][image_index] = joints_coordinates[num_ann]
      File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
      File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
      File "C:\Users\Alberto Ursino\anaconda3\envs\TensorFlow 1x\lib\site-packages\h5py\_hl\dataset.py", line 707, in __setitem__
        for fspace in selection.broadcast(mshape):
      File "C:\Users\Alberto Ursino\anaconda3\envs\TensorFlow 1x\lib\site-packages\h5py\_hl\selections.py", line 299, in broadcast
        raise TypeError("Can't broadcast %s -> %s" % (target_shape, self.mshape))
    TypeError: Can't broadcast (37,) -> (37, 2)

在这里写 np.nan 而不是 None:

for i in range(len(skeleton_names_list)):
        if joints_coordinates[num_ann][i][0] + joints_coordinates[num_ann][i][1] == 0:
            joints_coordinates[num_ann][i] = None

有效!