【发布时间】:2020-02-24 18:02:55
【问题描述】:
我正在提取图像的人脸嵌入并将其附加到现有的 pickle 文件中。但看起来它不起作用,因为当我解开文件时,它不包含添加的新数据。下面是代码:
file = client_dir + '\embeddings.pickle'
data = {"embeddings": known_embeddings, "names": known_names}
with open(file, 'ab+') as fp:
pickle.dump(data, fp)
fp.close()
log("[INFO] Data appended to embeddings.pickle ")
当前的pickle文件包含以下数据:
{'embeddings': [array([-0.03656099, 0.11354745, -0.00438912, 0.0367547 , 0.06391761,
0.18440282, 0.06150107, -0.17380905, 0.03094344, -0.00182147,
0.00969766, 0.06890091, 0.04974053, -0.0502388 , -0.03414046,
-0.13550822, -0.02251128, 0.14556041, -0.04045469, 0.06500552,
0.0726142 , -0.04139924, -0.04662199, 0.08869533, -0.00061307,
-0.11912274, 0.13141112, -0.00648551, 0.00296356, 0.03682912,
-0.15076959, 0.03989822, 0.02799555, 0.03429572, 0.09865954,
0.14113557, -0.08355764, 0.09193961, -0.00819231, -0.01184336,
-0.12519744, 0.00668721, 0.0816237 , 0.00464355, -0.00339399,
0.07501812, 0.11679655, -0.09211859, 0.06211261, -0.00543289,
0.10347278, 0.06651585, -0.01512023, 0.09477805, 0.09886038,
-0.03837246, 0.02265131, -0.14867221, 0.00781244, 0.04845129,
-0.0363168 , -0.00186919, -0.16163988, 0.09539618, 0.14983718,
0.09159472, -0.05315595, -0.05073383, 0.01501674, -0.03789762,
0.07116041, 0.07650694, -0.02975985], dtype=float32)], 'names': ['rock']}
我要附加的新数据如下:
{'embeddings': [array([-0.03656099, 0.11354745, -0.00438912, 0.0367547 , 0.06391761,
0.18440282, 0.06150107, -0.17380905, 0.03094344, -0.00182147,
0.00969766, 0.06890091, 0.04974053, -0.0502388 , -0.03414046,
0.07501812, 0.11679655, -0.09211859, 0.06211261, -0.00543289,
-0.13550822, -0.02251128, 0.14556041, -0.04045469, 0.06500552,
0.0726142 , -0.04139924, -0.04662199, 0.08869533, -0.00061307,
-0.11912274, 0.13141112, -0.00648551, 0.00296356, 0.03682912,
-0.15076959, 0.03989822, 0.02799555, 0.03429572, 0.09865954,
0.14113557, -0.08355764, 0.09193961, -0.00819231, -0.01184336,
-0.12519744, 0.00668721, 0.0816237 , 0.00464355, -0.00339399,
0.10347278, 0.06651585, -0.01512023, 0.09477805, 0.09886038,
-0.03837246, 0.02265131, -0.14867221, 0.00781244, 0.04845129,
-0.0363168 , -0.00186919, -0.16163988, 0.09539618, 0.14983718,
0.09159472, -0.05315595, -0.05073383, 0.01501674, -0.03789762,
0.07116041, 0.07650694, -0.02975985], dtype=float32)], 'names': ['john']}
但是当我解开文件时,它只有rock 的数据,而不是john。谁能帮助我做错了什么。下面是我用来解压并观察添加了哪些数据的代码。可能是我取消文件的方式是错误的,因为当我附加数据时,我可以看到文件大小增加。
import pickle
file = open('G:\\output\\embeddings.pickle', 'rb')
data = pickle.load(file)
file.close()
print(data)
请帮忙。谢谢
更新代码:
file_path = client_dir + '\embeddings.pickle'
file = open(file_path, 'rb')
old_data = pickle.load(file)
new_embeddings = old_data['embeddings']
new_names = old_data['names']
new_embeddings.append(known_embeddings[0])
new_names.append(known_names[0])
data1 = {"embeddings": new_embeddings, "names": new_names}
with open(file_path, 'ab+') as fp:
pickle.dump(data1, fp)
fp.close()
log.error("[INFO] Data appended to embeddings.pickle ")
在上面的代码中,我首先将pickle文件中的数据加载到列表中,然后将新数据附加到列表中,然后将所有数据(旧+新)添加到pickle文件中。谁能告诉我这是否是正确的做法。
在此之后,当我解开文件时,我并没有获得所有数据。谢谢
【问题讨论】:
-
如果你在同一个文件上多次调用
dump,我认为pickle 协议不能保证工作。不要腌制一个字典然后另一个,尝试腌制一个包含这两个字典的列表。 -
@Kevin 您的意思是首先将所有泡菜数据加载到列表/字典中,然后将新数据附加到该列表/字典中,然后将其保存在泡菜文件中。?我认为这可以工作。我会试试的
-
@Kevin 我已经更新了代码,你可以检查一次。