-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_jsons.py
152 lines (102 loc) · 4.58 KB
/
prepare_jsons.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import os
import json
from colorama import Fore, Style
from utils import openJsonSafely, extractCategory, makeJSONname, putNested
from config import Path, Extensions, Constants as const
def getKeysOffset(keys):
keys = list(keys)
offset = min([getFrameNumber(l) for l in keys]) # key == "frame_{idx}"
return offset
def getFrameNumber(frameKey):
return int(frameKey.split("_")[-1])
def getVideoMarks(videoPath, marksPath):
try:
marks = json.load(open(marksPath, "r"))
except:
print(f"{Fore.RED}\nThere is no json file {marksPath} for {videoPath} {Style.RESET_ALL}")
return {}
marksSeparated = {}
for frame, frameMarks in marks.items():
subcategory = frameMarks[const.subcategory]
coords = frameMarks[const.coords]
putNested(dictionary=marksSeparated, keys=[subcategory, frame], value=coords)
return marksSeparated
def summarizeInfo(rawPath=Path.raw, summarizedPath=Path.summarizedRaw, allowedCategories=None, allowedSubCtgList=None,
overwrite=True):
summarized = openJsonSafely(summarizedPath) if not overwrite else {}
rawVideosPath = os.path.join(rawPath, const.videos)
rawJsonsPath = os.path.join(rawPath, const.json)
rawVideos = sorted([j for j in os.listdir(rawVideosPath) if j.endswith(Extensions.videos())])
maxIdx = summarized.get(const.maxIdx, 0)
for i, video in enumerate(rawVideos):
print(f"\rProcessing {video} ({i + 1} out of {len(rawVideos)})", end="")
category, name = extractCategory(video)
if category not in allowedCategories:
continue
categoryInfo = summarized.get(category, {})
videoJson = os.path.join(rawJsonsPath, makeJSONname(name))
videoMarks = getVideoMarks(os.path.join(rawVideosPath, video), videoJson)
for subctg, subctgMarks in videoMarks.items():
if allowedSubCtgList is not None and subctg not in allowedSubCtgList:
continue
if subctg not in categoryInfo:
subctgIdx = maxIdx
maxIdx += 1
curSubctgMarks = {
const.overall: 0,
const.ctgIdx: subctgIdx,
const.videos: {},
const.parent: category
}
else:
curSubctgMarks = categoryInfo[subctg]
if video not in curSubctgMarks[const.videos]:
curSubctgMarks[const.videos][video] = subctgMarks
curSubctgMarks[const.overall] += len(subctgMarks)
categoryInfo[subctg] = curSubctgMarks
if categoryInfo:
summarized[category] = categoryInfo
summarized[const.maxIdx] = maxIdx
json.dump(summarized, open(summarizedPath, "w"), indent=3)
print(f"\n{Fore.GREEN}Summarized info file {summarizedPath} has been updated{Style.RESET_ALL}")
def fixFrameNumbers(jsonPath):
jsons = [j for j in os.listdir(jsonPath) if j.endswith(Extensions.json)]
for js in jsons:
path = os.path.join(jsonPath, js)
marks = openJsonSafely(path)
if not marks:
continue
offset = getKeysOffset(marks.keys())
if offset == 0:
continue
fixedMarks = {}
for frame, info in marks.items():
newFrame = "frame_{}".format(getFrameNumber(frame) - offset)
fixedMarks[newFrame] = info
json.dump(fixedMarks, open(path, "w"), indent=3)
print(f"{Fore.BLUE}JSON file {path} has been fixed{Style.RESET_ALL}")
def updateCategoriesIndices(datasetPath, categories):
from utils import walk, makeJSONname
from verifier import getFullCategory
marks = walk(datasetPath, targetFiles=makeJSONname(const.marks)).get("files")
for mrk in marks:
try:
marksPath = os.path.join(datasetPath, *mrk)
category, subcategory = mrk[-3:-1]
fullCategory = getFullCategory(category, subcategory)
if fullCategory not in categories:
continue
marks = openJsonSafely(marksPath)
for f, value in marks.items():
fullCategory = value[const.fullCategory]
value[const.ctgIdx] = categories.index(fullCategory)
json.dump(marks, open(marksPath, "w"), indent=3)
print(f"{Fore.BLUE}JSON file {marksPath} has been fixed{Style.RESET_ALL}")
except Exception as e:
print(e)
def main():
from utils import readLines
categories = readLines(Path.categories)
updateCategoriesIndices(Path.dataset, categories)
if __name__ == "__main__":
main()