0

I've a face detection code. What I want is to store metadata in json files in specific order. Basically I want to write how many faces exists in an image and the location of images. My code is the following:

data =[] 
data.append({'number_of_faces':len(faces)}) 
nested_data = []
for (x,y,w,h) in faces:
    nested_data.append({'face_x': x, 'face_y': y, 'face_h': h, 'face_w': w})
data.append(nested_data)
with open(json_path+folder+'/'+file_name, "w") as outfile:
        json.dump(data, outfile)    

The output is, for example:

[
    {
        "number_of_faces": 1
    },
    [
        {
            "face_h": 38,
            "face_w": 38,
            "face_x": 74,
            "face_y": 45
        }
    ]
]

However I want to create a nested json. Thus I want after number_of_faces object to have nested all face_location inside number_of_faces {}. How is it possible to do so?

2
  • 1
    This isn't really a question about JSON, but about dicts and lists. The json library will just serialize the structure you pass to it. Commented Sep 26, 2014 at 8:21
  • Yeah is how to efficiently pass a list to json. Commented Sep 26, 2014 at 8:23

2 Answers 2

1
data = {}
data['number_of_faces'] = len(faces)
data['faces'] = []
for (x,y,w,h) in faces:
    data['faces'].append({'face_x': x, 'face_y': y, 'face_h': h, 'face_w': w})
with open(json_path+folder+'/'+file_name, "w") as outfile:
    json.dump(data, outfile)  

This will have the output:

{
    'number_of_faces': 4
    'faces':[{
        'face_x': 'x',
        'face_y': 'y',
        'face_h': 'h',
        'face_z': 'z'
    }]
}
Sign up to request clarification or add additional context in comments.

4 Comments

THis implementation stores only the last face of image in json!
Faces is a list contains several faces coordinations. With the proposed implementation only the last face coordinates stored in json file.
Ah I see give me a minute
Python doesnt like data[faces] = [], data[faces] = [] TypeError: unhashable type: 'numpy.ndarray'
1

First of all, as I said in the comment, let's ignore the JSON part. This is a question about lists and dicts only. So, we just need to build up the data in the way you want.

To start with, rather than just appending to a list, we should start with a dict for each shape. Then, we can add the face data for each shape inside that dict.

data = []
for shape in shapes:
    shape = {'number_of_faces':len(faces)}
    nested_data = []
    for (x,y,w,h) in faces:
        nested_data.append({'face_x': x, 'face_y': y, 'face_h': h, 'face_w': w})
    shape['face_location'] = nested_data
    data.append(shape)

2 Comments

What exactly is the shapes in your implementation?
I presume you had a list of shapes to iterate through, because your original implementation was a list of dict(s). If you don't, then there's no need for that list, and no need for either data or the for loop in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.