我想创建一个具有以下格式的JSON文件:
{
fid: {
"filename": "",
"size": -1,
"regions": [
"shape_attributes": {
"name": "polyline",
"all_points_x": [],
"all_points_y": []
} ,
"shape_attributes": {
"name": "polyline",
"all_points_x": [],
"all_points_y": []
}
"file_attributes": {}
},
"region_attributes": {}
},
fid: {
"filename": "",
"size": -1,
"regions": [
"shape_attributes": {
"name": "polyline",
"all_points_x": [],
"all_points_y": []
} ,
"shape_attributes": {
"name": "polyline",
"all_points_x": [],
"all_points_y": []
}
"file_attributes": {}
},
"region_attributes": {}
}
}
对于区域而言,这很容易。由于它是shape_attributes
个对象的数组,因此我只用了append()
来添加它。但是我需要在整个{}
如何创建一个像这样的空json文件:data = {}
,然后将总计fid
附加到其中?我尝试了这种方法,但出现错误:
AttributeError: 'dict' object has no attribute 'append'
这是我正在运行的代码:
import json
from tqdm import tqdm
from os import path, mkdir, listdir
im = '/home/luiscosta/PycharmProjects/wsi_preprocessing/nn_models/Unet/dsb2018_topcoders/selim/result/pred_resnet101_full_masks/1_0.jpeg'
import numpy as np
import cv2
import matplotlib.pyplot as plt
img = cv2.imread(
'/home/luiscosta/PycharmProjects/wsi_preprocessing/nn_models/Unet/dsb2018_topcoders/selim/result/pred_resnet101_full_masks/1_0.jpeg')
original = '/home/luiscosta/PycharmProjects/wsi_preprocessing/nn_models/Unet/dsb2018_topcoders/data_test/1_0.jpeg'
# mask = np.zeros(20000, dtype=np.uint8).reshape(100, 200)
img_original = cv2.imread(
'/home/luiscosta/PycharmProjects/wsi_preprocessing/nn_models/Unet/dsb2018_topcoders/data_test/1_0.jpeg',
cv2.IMREAD_COLOR)
test_folder = '/home/luiscosta/PycharmProjects/wsi_preprocessing/nn_models/Unet/dsb2018_topcoders/selim/result/pred_resnet101_full_masks'
total_annotations = {} #I want this to work
for d in tqdm(listdir(test_folder)):
fid = d
annotation = {
fid: {
"filename": "",
"size": -1,
"regions": [],
"file_attributes": {}
},
"region_attributes": {}
}
img = cv2.imread(path.join(test_folder, '{0}'.format(fid)))
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
# define range of blue color in HSV
lower_blue = np.array([110, 50, 50])
upper_blue = np.array([130, 255, 255])
# Threshold the HSV image to get only blue colors
mask = cv2.inRange(hsv, lower_blue, upper_blue)
blue_only = cv2.bitwise_and(img, img, mask=mask)
im2, contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for i, cnt in enumerate(contours):
cv2.drawContours(img_original, cnt, -1, (0, 0, 255), 1)
cv2.putText(blue_only, str(i), (cnt[0][0][0], cnt[0][0][1]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 1)
total_x = []
total_y = []
for cn in cnt:
total_x.append(int(cn[0][0]))
total_y.append(int(cn[0][1]))
shape_attributes = {
"shape_attributes": {
"name": "polyline",
"all_points_x": [],
"all_points_y": []
}
}
# print(total_x)
# print(contours[0][0])
# print(total_x)
shape_attributes["shape_attributes"]["all_points_x"] = total_x
shape_attributes["shape_attributes"]["all_points_y"] = total_y
annotation[fid]["regions"].append(shape_attributes)
annotation[fid]["filename"] = fid
total_annotations.append(annotation)
with open('data_start.json', 'w+', encoding='utf-8') as outfile:
json.dump(total_annotations, outfile, indent=4)