-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.py
281 lines (253 loc) · 12.1 KB
/
run.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import os
import cv2
import csv
import json
import time
import requests
import datetime
import argparse
from cutdetector import predict
class run():
def __init__(self, conf):
self.conf = conf
def check_dir(self, outdir):
#Creates output directory if not present
if not os.path.isdir(outdir):
os.mkdir(outdir)
def sort_cuts(self, hc, sc):
#Sort cuts according to timestamps
cuts = {**hc, **sc}
cuts = dict(sorted(cuts.items()))
return cuts
def shot_time(self, cuts, length, fps):
#Creates a list of start and end timestamps of shots
shots = []
if len(list(cuts.items())) >= 1:
first = 0
second = float("{:.2f}".format(list(cuts.keys())[0]))
shots.append([first, second])
for k in range(0, len(list(cuts.items()))-1):
first = float("{:.2f}".format(list(cuts.keys())[k]))
second = float("{:.2f}".format(list(cuts.keys())[k+1]))
transition = float("{:.2f}".format(50/fps))
if list(cuts.values())[k]=='hard-cut' and list(cuts.values())[k+1]=='hard-cut':
shots.append([first, second])
if list(cuts.values())[k]=='soft-cut' and list(cuts.values())[k+1]=='hard-cut':
shots.append([first+transition, second])
if list(cuts.values())[k]=='hard-cut' and list(cuts.values())[k+1]=='soft-cut':
shots.append([first, second-transition])
if list(cuts.values())[k]=='soft-cut' and list(cuts.values())[k+1]=='soft-cut':
shots.append([first+transition, second-transition])
if len(list(cuts.items())) >= 1:
if list(cuts.values())[-1]=='soft-cut':
shots.append([second+float("{:.2f}".format(50/fps)), float("{:.2f}".format(length))])
else:
shots.append([second, float("{:.2f}".format(length))])
else:
shots.append([0, float("{:.2f}".format(length))])
return shots
def shot_duration(self, cuts, length, fps):
#Creates a list of duration of shots
duration = []
transition = float("{:.2f}".format(50/fps))
if len(list(cuts.items())) > 1:
if list(cuts.values())[0] == 'hard-cut':
duration.append(float("{:.2f}".format(list(cuts.keys())[0])))
if list(cuts.values())[0] == 'soft-cut':
duration.append(float("{:.2f}".format(list(cuts.keys())[0]))-transition)
for k in range(0, len(list(cuts.items()))-1):
first = float("{:.2f}".format(list(cuts.keys())[k]))
second = float("{:.2f}".format(list(cuts.keys())[k+1]))
if list(cuts.values())[k]=='hard-cut' and list(cuts.values())[k+1]=='hard-cut':
duration.append(second-first)
elif list(cuts.values())[k]=='soft-cut' and list(cuts.values())[k+1]=='hard-cut':
duration.append(second-first-2*(transition))
else:
duration.append(second-(first+transition))
return duration
def csv_frames(self, outpath, cuts):
#Writes a CSV file with frame index of cuts
with open(outpath, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['index', 'frame', 'type'])
count = 0
for fr, typ in cuts.items():
writer.writerow([count, fr, typ])
count=count+1
def csv_time(self, outpath, shots):
#Writes a CSV file with timestamps of start and end frames
with open(outpath, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['index', 'start_frame', 'end_frame'])
count = 0
for shot in shots:
writer.writerow([count, shot[0], shot[1]])
count=count+1
def mep_json(self, outdir, vidpath, filename, shots, length, height, width):
#Writes a JSON file formatted with MEP web annotation, containing
#timestamps of start and end timestanps of shots
here = os.path.dirname(os.path.abspath(__file__))
ts = time.time()
st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
str_shots=""
for k, shot in enumerate(shots):
if k!=len(shots)-1:
str_shot = "{} {}, ".format(shot[0], shot[1])
else:
str_shot = "{} {}".format(shot[0], shot[1])
str_shots = str_shots + str_shot
with open(os.path.join(here, "sample_json", "MEP_sample.json")) as f:
sample = json.load(f)
sample['id'] = outdir
sample['label']['en'][0] = ["Annotations for \"{}\"".format(filename)]
sample['items'][0]['id'] = "{}#canvas".format(outdir)
sample['items'][0]['height'] = height
sample['items'][0]['width'] = width
sample['items'][0]['duration'] = length
sample['items'][0]['content'][0]['id'] = vidpath
sample['items'][0]['content'][0]['height'] = height
sample['items'][0]['content'][0]['width'] = width
sample['items'][0]['content'][0]['duration'] = length
sample['items'][0]['content'][0]['label']['en'] = "{}".format(filename)
items = sample['items'][0]['items'][0]['items']
count=1
for shot in shots:
item = {'id': "{}#scene{}".format(outdir, count),
'type': 'Annotation',
'generator': 'https://github.com/tre3x/FilmEditDetection',
'motivation': 'highlighting',
'creator': {'type': 'Agent', 'nickname': 'FilmEditDetector'},
'created': st,
'rights': 'http://creativecommons.org/licenses/by/4.0/',
'body': [
{
"type": "TextualBody",
"value": "{}, Scene{}".format(filename, count),
"format": "text/plain",
"language": "en",
"purpose": "describing"
}
],
'target': {
'source': vidpath,
'type': 'SpecificResource',
'selector':
{
'type': 'FragmentSelector',
'conformsTo': 'http://www.w3.org/TR/media-frags/',
'value': "t={},{}".format(shot[0], shot[1])
}
}
}
items.append(item)
count=count+1
with open(outdir, 'w', encoding='utf-8') as f:
json_string = json.dump(sample, f, ensure_ascii=False, indent=4)
def cinemetrics(self, outpath, cuttimestamp, shotduration, length, submit, cine_details):
#Writes a .cms file which supports cinemetrics (http://www.cinemetrics.lv/)
totduration = 0
iterator = 1
shotlist = []
for shot in shotduration:
totduration = totduration + shot
totduration = 10*totduration
avgshotlength = 10*(totduration/len(shotduration))
for duration, cuttime in zip(shotduration, cuttimestamp):
shotlist.append("{};{:.2f};{:.2f};0".format(iterator, 10*duration, 10*cuttime))
iterator = iterator+1
mdata = '\n'.join(shotlist)
with open(outpath, 'w', newline='') as cmsfile:
cmsfile.write("1\n")
cmsfile.write("{}\n".format(len(cuttimestamp)))
cmsfile.write("{}\n".format(avgshotlength))
cmsfile.write("{}\n".format(length))
cmsfile.write("BCU;CU;MCU;MS;MLS;FS;LS;Other\n")
cmsfile.write("0;0;0;0;0;0;0;0\n")
cmsfile.write("0;0;0;0;0;0;0;0\n")
cmsfile.write("&@")
cmsfile.write(mdata)
if submit:
CINEMETRICS_API_ENDPOINT = "http://www.cinemetrics.lv/submit.php"
data = {'yname':cine_details['yname'],
'mtitle':cine_details['mtitle'],
'myear':cine_details['myear'],
'email':cine_details['email'],
'comments':'',
'mdata':mdata,
'simple':1,
'numshots':len(shotduration),
'asl':avgshotlength,
'filmlength':totduration }
r = requests.post(url = CINEMETRICS_API_ENDPOINT, data=data)
if r.status_code==200:
print("Sumbitted to cinemetrics with status code 200")
else:
print("Cannot submit to cinemetrics server. Error Code {}".format(r.status_code))
def get_csvframes(self, vidpath, modpath, outdir):
#Driver function to write csv format with cuts frame index
filename = vidpath.split('/')[-1].split('.')[0]
hc, sc = predict(self.conf, modpath).run(vidpath, False)
cuts = self.sort_cuts(hc, sc)
outdir = os.path.join(outdir, filename+'.csv')
self.csv_frames(outdir, cuts)
return outdir
def get_csvtime(self, vidpath, modpath, outdir):
#Driver function to write CSV format with start and end timestamps
# of shots
filename = vidpath.split('/')[-1].split('.')[0]
cap = cv2.VideoCapture(vidpath)
fps = cap.get(cv2.CAP_PROP_FPS)
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
length = length/fps
hc, sc = predict(self.conf, modpath).run(vidpath, True)
cuts = self.sort_cuts(hc, sc)
outdir = os.path.join(outdir, filename+'.csv')
shots = self.shot_time(cuts, length, fps)
self.csv_time(outdir, shots)
return outdir
def get_mepjson(self, vidpath, modpath, outdir):
#Driver function to write MEP web annotation JSON format
filename = vidpath.split('/')[-1].split('.')[0]
cap = cv2.VideoCapture(vidpath)
fps = cap.get(cv2.CAP_PROP_FPS)
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
length = float("{:.2f}".format(length/fps))
hc, sc = predict(self.conf, modpath).run(vidpath, True)
cuts = self.sort_cuts(hc, sc)
outdir = os.path.join(outdir, filename+'.json')
shots = self.shot_time(cuts, length, fps)
self.mep_json(outdir, vidpath, filename, shots, length, height, width)
return outdir
def get_cinemetrics(self, vidpath, modpath, outdir, submit, cine_details):
#Driver function to write cinemetrics formatted output
filename = vidpath.split('/')[-1].split('.')[0]
cap = cv2.VideoCapture(vidpath)
fps = cap.get(cv2.CAP_PROP_FPS)
length = int(cap.get(cv2.CAP_PROP_FPS))
length = float("{:.2f}".format(length/fps)) #length in second
hc, sc = predict(self.conf, modpath).run(vidpath, True)
cuts = self.sort_cuts(hc, sc)
outpath = os.path.join(outdir, filename+'.cms')
shotsduration = self.shot_duration(cuts, length, fps)
self.cinemetrics(outpath, cuts, shotsduration, length, submit=submit, cine_details=cine_details)
return
def readonly(self, vidpath, modpath):
#Driver function to show only cut timestamps in command line
hc, sc = predict(self.conf, modpath).run(vidpath, True)
return hc, sc
def run(self, vidpath, modpath, outdir, iscsvframe=False, iscsvtime=False, ismepjson=False, readonly=False, iscinemetrics=False, submit=False, cine_details={}):
#Driver function
if not readonly:
self.check_dir(outdir)
if iscsvframe:
filepath = self.get_csvframes(vidpath, modpath, outdir)
if iscsvtime:
filepath = self.get_csvtime(vidpath, modpath, outdir)
if ismepjson:
filepath = self.get_mepjson(vidpath, modpath, outdir)
if iscinemetrics:
filepath = self.get_cinemetrics(vidpath, modpath, outdir, submit, cine_details)
else:
self.readonly(vidpath, modpath)