-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsht_to_json.py
115 lines (105 loc) · 3.42 KB
/
sht_to_json.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
import sys
import json
def parse_shot_params(line,shot):
# assignments and list of frames
for i in range(4, len(line)):
if '=' in line[i]:
sub_line = line[i].split('=')
shot[sub_line[0]] = sub_line[1]
shot['Frames'] = []
def parse_map_of_values(line,frame):
# map of values
tag = line[3]
dict = {}
if len(line[5]) > 0:
sub_line = line[5].replace('(', '').replace(')', '').split(', ')
for i in range(0, len(sub_line), 2):
dict[sub_line[i]] = float(sub_line[i + 1])
frame[tag] = dict
def parse_finetuned_shot_class(line,frame):
# map of values
parse_map_of_values(line,frame)
def parse_svm_shot_class(line,frame):
# single string
tag = line[3]
frame[tag] = line[4]
def parse_obj_class(line,frame):
# map of values
parse_map_of_values(line,frame)
def parse_scene_location(line,frame):
# map of values
parse_map_of_values(line,frame)
def parse_scene_attributes(line,frame):
# list of values
tag = line[3]
frame[tag] = []
sub_line = line[4].split(', ')
for i in range(0, len(sub_line)):
frame[tag].append(sub_line[i])
def parse_yolo_persons(line,frame):
if len(line) <= 5:
return
persons = {}
cur = line[5]
while '[' in cur:
cur = cur[cur.index('[')+1:]
cur = cur[cur.index(', ')+2:]
persons['val'] = float(cur[:cur.index(')')])
cur = cur[cur.index('[')+1:]
vals = cur[:cur.index(']')].split(', ')
dict = { 'x' : int(vals[0]), 'y' : int(vals[1]),
'w' : int(vals[2]), 'h' : int(vals[3]) }
persons['pos'] = dict
frame['YOLO/PERSONS'] = persons
pass
def sht_to_json(sht_file_name):
frame = {}
shot = {}
shots = []
time = ""
last_time = ""
started = False
filename = sht_file_name
for line in open(filename, 'r'):
line = line.replace('\n', '').split('|')
if len(line) > 3:
last_time = time
time = line[0]
if "SHOT_DETECTED" in line:
# shot is filled
if 'Frames' in shot and len(shot['Frames']) > 0 or frame:
shot['Frames'].append(frame)
shots.append(shot)
shot = {}
parse_shot_params(line,shot)
started = True
elif started and len(line) > 3:
# new frame
if time != last_time:
if frame:
shot['Frames'].append(frame)
frame = {}
tag = line[3]
if tag == 'FINETUNED_CLASS':
parse_finetuned_shot_class(line,frame)
elif tag == 'SVM_CLASS':
parse_svm_shot_class(line,frame)
elif tag == 'OBJ_CLASS':
parse_obj_class(line,frame)
elif tag == 'SCENE_LOCATION':
parse_scene_location(line,frame)
elif tag == 'SCENE_ATTRIBUTES':
parse_scene_attributes(line,frame)
elif tag == 'YOLO/PERSONS':
parse_yolo_persons(line,frame)
else:
print("Unknown tag:" + tag)
exit(1)
with open(filename.split('.sht')[0] + '.json', 'w') as outfile:
outfile.write("[\n")
for i in range(0, len(shots)):
json.dump(shots[i], outfile)
if i != len(shots) - 1:
outfile.write(",")
outfile.write("\n")
outfile.write("]")