It's a two part question,
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)
When I tried to convert the list encodearr
as value of the key "des"
(without .tolist()) it shows
TypeError: Object of type ndarray is not JSON serializable
.Then I added .tolist() to encode arr as show. it shows AttributeError: 'list' object has no attribute 'tolist'
, brad1 to brad5 are the jpg files in the directory.
I did a workaround using 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)