read a json file and modify some content
# cat resize_json.py
# Python program to read
# json file
#https://www.geeksforgeeks.org/read-json-file-using-python/
import json
import shutil
target_json = "./new.json"
# JSON string
#a = '{"name": "Bob", "languages": "English"}'
# deserializes into dict
# and returns dict.
#y = json.loads(a)
# JSON file
f = open ('old.json', "r")
# Reading from file
data = json.loads(f.read())
# Iterating through the json
# list
for i in data['images']:
#print(i['file_name'])
base_name = i['file_name']
for i in data['annotations']:
print("\n========\n")
i['bbox'] = [ b/2 for b in i['bbox']]
print("there are {} segmentation ".format(len(i['segmentation'])))
for num in range(len(i['segmentation'])):
print(i['segmentation'][num])
i['segmentation'][num] = [ b/2 for b in i['segmentation'][num] ]
#i['segmentation'] = [ b/2 ]
i['width'] = int(i['width'] /2)
i['height'] = int(i['height'] /2)
#data['images']['width'] = 2
for i in data['images']:
i['width'] = int(i['width'] /2)
i['height'] = int(i['height'] /2)
#print(data)
with open(target_json, "w") as j:
json.dump(data, j)
# Closing file
f.close()
https://stackoverflow.com/questions/21035762/python-read-json-file-and-modify
浙公网安备 33010602011771号