質問編集履歴
1
修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
一つのkeyで複数のvalueを持つjsonファイルの作り方
|
body
CHANGED
@@ -1,5 +1,20 @@
|
|
1
|
-
下のような
|
1
|
+
下のような中身のファイルがあります。
|
2
|
+
**ファイルの中身(test_0989.jpg)**
|
2
3
|
```json
|
4
|
+
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216}, 'category': 'A'},
|
5
|
+
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216}, 'category': 'C'},
|
6
|
+
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
7
|
+
'category': 'P'},
|
8
|
+
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
9
|
+
'category': 'Si'},
|
10
|
+
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
11
|
+
'category': 'S'}]}
|
12
|
+
```
|
13
|
+
|
14
|
+
|
15
|
+
一つのkeyに対して複数のvalueを持つjsonファイルを作成したいです。
|
16
|
+
**複数のvalueを持つjsonファイル**
|
17
|
+
```json
|
3
18
|
{
|
4
19
|
'test_0989.jpg': {'A': [[0, 0, 1936, 1216]],
|
5
20
|
'C': [[0, 0, 1936, 1216]],
|
@@ -26,49 +41,21 @@
|
|
26
41
|
|
27
42
|
|
28
43
|
```python
|
29
|
-
# jsonファイルとファイル名を読み込む
|
30
|
-
def natsort_json(json_path):
|
31
|
-
load_path = [os.path.join(json_path, path) for path in os.listdir(json_path)]
|
32
|
-
filename = [path for path in os.listdir(json_path)]
|
33
|
-
sorted_name =[path for path in natsorted(filename)]
|
34
|
-
names, jsons = [], []
|
35
|
-
for i, (name, jso) in enumerate(zip(sorted_name, load_path)):
|
36
|
-
a = open(jso)
|
37
|
-
b = json.load(a)
|
38
|
-
name = name.strip(".json")
|
39
|
-
name = name + ".jpg"
|
40
|
-
print(i, name)
|
41
|
-
names.append(name)
|
42
|
-
jsons.append(b)
|
43
|
-
return names, jsons
|
44
|
-
|
45
44
|
json_path = "****.json"
|
46
|
-
|
45
|
+
jsons_name, Jsons = natsort_img(json_path))
|
47
46
|
ys = {}
|
48
|
-
for idx, name in zip(range(len(Jsons)),
|
47
|
+
for idx, name in zip(range(len(Jsons)), jsons_name):
|
49
48
|
for i in Jsons[idx]['labels']:
|
50
49
|
ys[name] = {name:[[i['box2d']['x1'], i['box2d']['y1'], i['box2d']['x2'], i['box2d']['y2']]]}
|
51
|
-
```
|
52
50
|
|
53
|
-
|
51
|
+
ys
|
52
|
+
>>>>>>>
|
54
|
-
{'test_00000.jpg': {'B': [[
|
53
|
+
{'test_00000.jpg': {'B': [[0, 0, 1936, 1216]]},
|
55
|
-
'test_00001.jpg': {'P': [[
|
54
|
+
'test_00001.jpg': {'P': [[0, 0, 1936, 1216]]},
|
56
|
-
'test_00002.jpg': {'P': [[
|
55
|
+
'test_00002.jpg': {'P': [[0, 0, 1936, 1216]]},
|
57
|
-
'test_00003.jpg': {'P': [[
|
56
|
+
'test_00003.jpg': {'P': [[0, 0, 1936, 1216]]},
|
58
|
-
'test_00004.jpg': {'Si': [[
|
57
|
+
'test_00004.jpg': {'Si': [[0, 0, 1936, 1216]]},
|
59
|
-
'test_00005.jpg': {'T': [[
|
58
|
+
'test_00005.jpg': {'T': [[0, 0, 1936, 1216]]},
|
60
59
|
ーーーー
|
61
60
|
|
62
|
-
```
|
63
|
-
|
64
|
-
#ファイルの中身(test_0989.jpg)
|
65
|
-
```json
|
66
|
-
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216}, 'category': 'A'},
|
67
|
-
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216}, 'category': 'C'},
|
68
|
-
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
69
|
-
'category': 'P'},
|
70
|
-
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
71
|
-
'category': 'Si'},
|
72
|
-
{'box2d': {'x1': 0, 'x2': 0, 'y1': 1936, 'y2': 1216},
|
73
|
-
'category': 'S'}]}
|
74
61
|
```
|