how to change float array into integers array in json file

154 views Asked by At
    human_pose ='''{
    "annotations": [
        {
            "area": "356725",
            "image_id": "1",
            "iscrowd": "0",
            "category_id": "1",
            "keypoints": [
                371.5701271305881,
                188.86769422623237,
                2,
                380.9752835872622,
                174.5554996182501,
                2,
                353.4413472938106,
                177.5542451551607,
                2,
                397.6046906555844,
                184.91480238212299,
                2,
                328.49723669132726,
                196.90978452976526,
                2,
                478,
                258,
                2,
                302,
                285,
                2,
                577,
                287,
                1,
                343,
                390,
                2,
                661,
                282,
                2,
                431,
                363,
                2,
                430,
                486,
                1,
                352,
                477,
                2,
                416,
                662,
                2,
                335,
                643,
                2,
                329,
                785,
                2,
                307,
                810,
                2
            ]

        }]}'''

import json
import math


data = json.loads(human_pose)

for person in data['annotations']:
    key = person["keypoints"]
    [math.trunc(key) for key in key]

    #del person['area']

new_string = json.dumps(data)
print(new_string)


output:

{"annotations": [{"image_id": "1", "iscrowd": "0", "category_id": "1", "keypoints": [371.5701271305881, 188.86769422623237, 2, 380.9752835872622, 174.5554996182501, 2, 353.4413472938106, 177.5542451551607, 2, 397.6046906555844, 184.91480238212299, 2, 328.49723669132726, 196.90978452976526, 2, 478, 258, 2, 302, 285, 2, 577, 287, 1, 343, 390, 2, 661, 282, 2, 431, 363, 2, 430, 486, 1, 352, 477, 2, 416, 662, 2, 335, 643, 2, 329, 785, 2, 307, 810, 2]}]}

I am trying to convert the keypoint float values into integers the json.dumps save the changes like i perform (del person['area']) but do not save the changes i applied on keypoint. in CLI of python it displays the keypoints as integer array but when i dumps it to new json file the changes are not don and save as the same original. Help appreciate

1

There are 1 answers

0
Amit Nanaware On BEST ANSWER

You have to reassign the keypoints in the given json after converting the data.

check below code:

for person in data['annotations']:
    key = person["keypoints"]
    person["keypoints"] = [math.trunc(k) for k in key]