data = [
]
for x in range(0, len(directory_files)):
# print(directory_files[x])
# print(os.stat('Data\\' directory_files[x]).st_size)
img = ("Data\\" directory_files[x])
img_with_no_tag = directory_files[x]
size_of_img = str(os.stat('Data\\' directory_files[x]).st_size)
img_capsulated = cv2.imread(img, cv2.IMREAD_GRAYSCALE)
img_capsulated_rgb = cv2.imread(img, cv2.IMREAD_COLOR)
parent_tag = img_with_no_tag size_of_img
contourIndexNo = 0
listTest = {
img_with_no_tag size_of_img: {
'filename': img_with_no_tag,
'size': size_of_img,
'regions': [
],
'file_attributes': {
'caption': '',
'public_domain': 'no',
'image_url': ''
}
}
}
# print(*listTest, sep="{}")
data.append(listTest)
This is how I execute the code. The output is shown like this:
[
{
"138_mask.png4776": {
"filename": "138_mask.png",
"size": "4776",
"regions": [
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
299,
294,
255,
245,
227,
229,
207,
200,
219,
209,
248,
251,
330,
341,
394,
407,
441,
447,
475,
485,
424,
438
],
"all_points_y": [
517,
540,
534,
610,
610,
772,
778,
838,
843,
922,
928,
949,
955,
937,
943,
825,
827,
795,
799,
714,
673,
556
]
},
"region_attributes": {
"name": "rooftop",
"type": "rooftop",
"image_quality": {
"good": true
}
}
}
],
"file_attributes": {
"caption": "",
"public_domain": "no",
"image_url": ""
}
}
},
{----------------------------------------> cannot remove it
"332_mask.png8391": {
"filename": "332_mask.png",
"size": "8391",
"regions": [
{
....
However, I would like to get the desired output like this:
"138_mask.png4776": {
"filename": "138_mask.png",
"size": 4776,
"regions": [
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
214,
209,
218,
200,
209,
223,
252,
300,
433,
455,
476,
407,
398,
328,
250
],
"all_points_y": [
921,
921,
843,
835,
782,
613,
535,
518,
552,
680,
794,
861,
948,
962,
953
]
},
"region_attributes": {
"name": "not_defined",
"type": "unknown",
"image_quality": {
"good": true,
"frontal": true,
"good_illumination": true
}
}
}
],
"file_attributes": {
"caption": "",
"public_domain": "no",
"image_url": ""
}
},
"332_mask.png8391": { ------------------------------> here what I want
"filename": "332_mask.png",
"size": 8391,
"regions": [
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
137,
171,
395,
361
],
"all_points_y": [
515,
389,
437,
567
]
},
"region_attributes": {
"name": "not_defined",
"type": "unknown",
"image_quality": {
"good": true,
"frontal": true,
"good_illumination": true
}
}
},
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
567,
586,
814,
794
],
"all_points_y": [
605,
464,
492,
636
]
},
"region_attributes": {
"name": "not_defined",
"type": "unknown",
"image_quality": {
"good": true,
"frontal": true,
"good_illumination": true
}
}
},
{
"shape_attributes": {
"name": "polygon",
"all_points_x": [
451,
464,
739,
729
],
"all_points_y": [
833,
677,
694,
845
]
},
"region_attributes": {
"name": "not_defined",
"type": "unknown",
"image_quality": {
"good": true,
"frontal": true,
"good_illumination": true
}
}
}
],
"file_attributes": {
"caption": "",
"public_domain": "no",
"image_url": ""
}
}
}
This means, while appending the dictionary values into the list, I want to put them as they are not separate objects, or is there an alternative way while writing them into a json file?
I tried feature manipulation on json file write prompt like .replace or remove but that didn't work.
CodePudding user response:
What you show as desired output is a dictionary, so just create a dictionary instead of a list. The keys of this dictionary are then your image names, such as "138_mask.png4776"
data = {}
And then later on in your code add the entry to the dictionary:
data[img_with_no_tag size_of_img] = {
'filename': img_with_no_tag,
'size': size_of_img,
'regions': [],
'file_attributes': {
'caption': '',
'public_domain': 'no',
'image_url': ''
}
}
Finally, if you want to put this dictionary in a list, you can always do this:
data = [data]