AttributeError: 'list' object has no attribute 'tolist'
AttributeError: 'list' object has no attribute 'tolist'
这是一个两部分的问题,
import face_recognition
import os
import json
loadarr=[]
encodearr=[]
for i in range(0, 4):
loadarr.append(face_recognition.load_image_file( "brad"+str(i+1)+".jpg"))
encodearr.append(face_recognition.face_encodings(loadarr[i])[0])
encodearr = encodearr.tolist()
# print(encodearr)
encodedDic = {"des": encodearr}
with open("sample.json", "w") as outfile:
json.dump(encodedDic,outfile)
当我尝试将列表 encodearr
转换为键 "des"
的值时(没有 .tolist())它显示
TypeError: Object of type ndarray is not JSON serializable
。然后我添加了 .tolist() 来对 arr 进行编码,如图所示。它显示AttributeError: 'list' object has no attribute 'tolist'
,brad1到brad5是目录中的jpg文件。
我使用 numpy 做了一个解决方法。
import face_recognition
import os
import json
import numpy as np
encodearr=[]
for i in range(0, 4):
load=face_recognition.load_image_file( "brad"+str(i+1)+".jpg")
encodearr.append(face_recognition.face_encodings(loadarr)[0])
reshapped_array = np.reshape(encodearr,(total_images,128) //each image is an array consisting 128 images
encodedDic = {"des": reshapped_array }
with open("sample.json", "w") as outfile:
json.dump(encodedDic,outfile)
这是一个两部分的问题,
import face_recognition
import os
import json
loadarr=[]
encodearr=[]
for i in range(0, 4):
loadarr.append(face_recognition.load_image_file( "brad"+str(i+1)+".jpg"))
encodearr.append(face_recognition.face_encodings(loadarr[i])[0])
encodearr = encodearr.tolist()
# print(encodearr)
encodedDic = {"des": encodearr}
with open("sample.json", "w") as outfile:
json.dump(encodedDic,outfile)
当我尝试将列表 encodearr
转换为键 "des"
的值时(没有 .tolist())它显示
TypeError: Object of type ndarray is not JSON serializable
。然后我添加了 .tolist() 来对 arr 进行编码,如图所示。它显示AttributeError: 'list' object has no attribute 'tolist'
,brad1到brad5是目录中的jpg文件。
我使用 numpy 做了一个解决方法。
import face_recognition
import os
import json
import numpy as np
encodearr=[]
for i in range(0, 4):
load=face_recognition.load_image_file( "brad"+str(i+1)+".jpg")
encodearr.append(face_recognition.face_encodings(loadarr)[0])
reshapped_array = np.reshape(encodearr,(total_images,128) //each image is an array consisting 128 images
encodedDic = {"des": reshapped_array }
with open("sample.json", "w") as outfile:
json.dump(encodedDic,outfile)