python - 写入 H5 文件时“无法广播 (37,) -> (37, 2)”
问题描述
我想在我的 H5 文件(称为“注释”)的每个单元格中插入一个坐标(x, y)
和None
where (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
作品!
推荐阅读
- scala - 火花:十进制类型未找到
- python - 在后台运行 jupyter notebook,无需登录终端
- javascript - 我有一个 excel 按钮,我想在数据表中添加 pdf 按钮
- python - 如何清除QTextEdit的文本并立即插入新文本
- excel-formula - 区分大小写的第一个出现突出显示
- xero-api - 如何获取 OAuth2 中的连接名称?
- python - 我可以读取所有 PLC 输出和输入....但我无法通过 pyads 更改 PLC 输出
- python - 我无法通过 Visual Studio 在 python 中运行我的 sigmoid 函数代码
- django - 基于客户端ID进程应该分配
- angular - 重新分组甘特图 Y 轴进行第二次休息