首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >写入H5文件时出现"Can't broadcast (37,) -> (37,2)“

写入H5文件时出现"Can't broadcast (37,) -> (37,2)“
EN

Stack Overflow用户
提问于 2020-12-08 01:39:08
回答 1查看 70关注 0票数 0

我想在H5文件的原始文件中插入一个数组,该数组包含坐标为(0.0, 0.0)(x, y) coordinatesNone

这是我的代码:

代码语言:javascript
复制
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

hf.create_dataset('annotations', shape=(num_images, len(skeleton_names_list), 2), dtype=np.float64, data=none_matrix)
# hf.create_dataset('annotated', shape=(num_images, len(skeleton_names_list)), dtype=bool)

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
    hf["annotations"][image_index] = joints_coordinates[num_ann]
    # hf["annotated"][image_index] = np.ones(len(skeleton_names_list), int)
    num_ann += 1

为什么会出现这个错误?

代码语言:javascript
复制
    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)

joints_coordinates的格式为:

代码语言:javascript
复制
[[[x, y], ...], ...]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-10 12:13:56

在此处编写np.nan而不是None

代码语言:javascript
复制
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

成功了!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65186603

复制
相关文章

相似问题

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