-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSBDGUI.py
358 lines (322 loc) · 16.6 KB
/
SBDGUI.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import tkinter.filedialog as filedialog
from tkinter import *
from tkinter import ttk
from PIL import Image
from PIL import ImageTk
import _thread
import time
import csv
from ShotBoundaryDetection import ShotBoundaryDetection
class SBDGUI:
def __init__(self):
self.__sbd = ShotBoundaryDetection()
self.__playing_backward = False
self.__playing_forward = False
# Making root window
self.root = Tk()
self.root.title("Shot Boundary Detection")
# Controlling size of root window
# self.root.resizable(0, 0)
width = 932
height = 607
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
self.root.geometry("{width}x{height}+{x}+{y}".format(width=width,
height=height,
x=int((screen_width - width) / 2),
y=int((screen_height - height - 50) / 2)))
# Making menu
menu_main = Menu(self.root)
menu_file = Menu(menu_main, tearoff=0)
menu_file.add_command(label="Open", command=lambda: self.open_file())
menu_file.add_separator()
menu_file.add_command(label="Close", command=exit)
menu_main.add_cascade(label="File", menu=menu_file)
self.root.config(menu=menu_main)
# Making video frame
# Some of values of other frames are set here.
frame_shot_boundaries_width = 280
frame_control_height = 100
frame_video = LabelFrame(self.root, text="Video")
frame_video.place(width=-frame_shot_boundaries_width,
height=-frame_control_height,
x=0,
y=0,
relheight=1,
relwidth=1,
relx=0,
rely=0)
self.label_video = Label(frame_video)
self.label_video.place(relwidth=1,
relheight=1,
relx=0,
rely=0)
frame_shot_boundaries = LabelFrame(self.root, text="Shot Boundaries")
frame_shot_boundaries.place(width=frame_shot_boundaries_width,
height=0,
x=-frame_shot_boundaries_width,
y=0,
relwidth=0,
relheight=1,
relx=1,
rely=0)
scrollbar_shot_boundaries = Scrollbar(frame_shot_boundaries)
scrollbar_shot_boundaries.place(relwidth=1,
relheight=1,
relx=0,
rely=0)
self.listbox_shot_boundaries = Listbox(frame_shot_boundaries,
yscrollcommand=scrollbar_shot_boundaries.set)
self.listbox_shot_boundaries.place(width=-18,
relwidth=1,
relheight=1)
self.listbox_shot_boundaries.bind('<<ListboxSelect>>', lambda event: self.__shot_selected(event))
scrollbar_shot_boundaries.config(command=self.listbox_shot_boundaries.yview)
frame_control = LabelFrame(self.root, text="Toolbox")
frame_control.place(width=-frame_shot_boundaries_width,
height=frame_control_height,
x=0,
y=-frame_control_height,
relheight=0,
relwidth=1,
relx=0,
rely=1)
self.__button_previous_frame = Button(frame_control,
text="Previous Frame",
state=DISABLED,
command=lambda: (self.__pause(), self.__previous_frame()))
self.__button_previous_frame.place(x=-200,
relx=0.5)
self.__button_play_backward = Button(frame_control,
text="Play Backward",
state=DISABLED,
command=lambda: self.__play_backward())
self.__button_play_backward.place(x=-105,
relx=0.5)
self.__button_play_forward = Button(frame_control,
text="Play",
state=DISABLED,
command=lambda: self.__play_forward())
self.__button_play_forward.place(x=-15,
relx=0.5)
self.__button_next_frame = Button(frame_control,
text="Next Frame",
state=DISABLED,
command=lambda: (self.__pause(), self.__next_frame()))
self.__button_next_frame.place(x=20,
relx=0.5)
self.__button_process_all = Button(frame_control,
text="Detect Boundaries",
state=DISABLED,
command=lambda: self.__detect())
self.__button_process_all.place(x=100,
relx=0.5)
self.__button_export = Button(frame_control,
text="Export",
state=DISABLED,
command=lambda: self.__export())
self.__button_export.place(x=210,
relx=0.5)
progress_bar_height = 31
self.progress_bar = ttk.Progressbar(frame_control,
orient=HORIZONTAL,
length=700)
self.progress_bar.place(width=-6,
x=3,
y=-progress_bar_height - 8,
height=progress_bar_height,
relwidth=1,
relx=0,
rely=1)
self.label_status = Label(frame_control, text="Video is not available!")
self.label_status.place(x=0,
y=-progress_bar_height - 3,
relx=0.2,
rely=1)
# self.open_test()
self.root.mainloop()
def __control_buttons(self,
previous_frame=None,
play_backward=None,
play_forward=None,
next_frame=None,
process_all=None,
export=None):
if previous_frame is not None:
self.__button_previous_frame.config(state=previous_frame)
if play_backward is not None:
self.__button_play_backward.config(state=play_backward)
if play_forward is not None:
self.__button_play_forward.config(state=play_forward)
if next_frame is not None:
self.__button_next_frame.config(state=next_frame)
if next_frame is not None:
self.__button_process_all.config(state=process_all)
if export is not None:
self.__button_export.config(state=export)
def open_file(self):
input_name = filedialog.askopenfile()
self.__control_buttons(previous_frame=NORMAL,
play_backward=NORMAL,
play_forward=NORMAL,
next_frame=NORMAL,
process_all=NORMAL)
if input_name is not None:
self.__sbd.open_video(input_name.name)
self.__set_progress(0, "Video is not processed!")
def __previous_frame(self):
if self.__sbd.video_is_available():
frame = self.__sbd.previous_frame(rearrange=True)
if frame is None:
return False
image = ImageTk.PhotoImage(Image.fromarray(frame))
self.label_video.configure(image=image)
self.label_video.image = image
return True
else:
print("Video is not available!")
return False
def __play_backward(self):
if self.__playing_forward:
return
self.__control_buttons(play_forward=DISABLED,
process_all=DISABLED)
self.__playing_backward = not self.__playing_backward
available_frame = True
while self.__playing_backward and available_frame:
available_frame = self.__previous_frame()
self.root.update()
self.__playing_backward = False
self.__control_buttons(play_forward=NORMAL,
process_all=NORMAL)
def __pause(self):
self.__playing_backward = False
self.__playing_forward = False
self.__control_buttons(play_forward=NORMAL,
play_backward=NORMAL)
def __play_forward(self):
if self.__playing_backward:
return
self.__control_buttons(play_backward=DISABLED,
process_all=DISABLED)
self.__playing_forward = not self.__playing_forward
available_frame = True
while self.__playing_forward and available_frame:
available_frame = self.__next_frame()
self.root.update()
time.sleep(0.01)
self.__playing_forward = False
self.__control_buttons(play_backward=NORMAL,
process_all=NORMAL)
def __next_frame(self):
if self.__sbd.video_is_available():
frame = self.__sbd.next_frame(rearrange=True)
if frame is None:
return False
image = ImageTk.PhotoImage(Image.fromarray(frame))
self.label_video.configure(image=image)
self.label_video.image = image
return True
else:
print("Video is not available!")
return False
def __detect(self):
if self.__sbd.video_is_available():
self.__control_buttons(previous_frame=DISABLED,
play_backward=DISABLED,
play_forward=DISABLED,
next_frame=DISABLED,
process_all=DISABLED,
export=DISABLED)
_thread.start_new_thread(self.__sbd.detect,
(lambda progress, status: self.__set_progress(progress, status),
lambda: self.__finish()))
# self.__sbd.detect(lambda progress, status: self.__set_progress(progress, status),
# lambda: self.__finish())
else:
print("Video is not available!")
def __set_progress(self, progress, status):
self.progress_bar['value'] = progress
self.label_status['text'] = status
def __finish(self):
self.listbox_shot_boundaries.delete(0, END)
for index, shot in enumerate(self.__sbd.sb):
if shot['transition'] == 'cut':
self.listbox_shot_boundaries. \
insert(index,
"Cut: {h}:{m}:{s}.{ms} ({f})".format(h=shot['cut_time'].hour,
m=shot['cut_time'].minute,
s=shot['cut_time'].second,
ms=shot['cut_time'].microsecond,
f=shot['cut_frame']))
elif shot['transition'] == 'gradual':
self.listbox_shot_boundaries. \
insert(index,
"Gradual: {sh}:{sm}:{ss}.{sms} ({sf})"
" - {eh}:{em}:{es}.{ems} ({ef})".format(sh=shot['start_time'].hour,
sm=shot['start_time'].minute,
ss=shot['start_time'].second,
sms=shot['start_time'].microsecond,
sf=shot['start_frame'],
eh=shot['end_time'].hour,
em=shot['end_time'].minute,
es=shot['end_time'].second,
ems=shot['end_time'].microsecond,
ef=shot['end_frame'], ))
self.__set_progress(100, "Video is processed.")
self.__control_buttons(previous_frame=NORMAL,
play_backward=NORMAL,
play_forward=NORMAL,
next_frame=NORMAL,
process_all=NORMAL,
export=NORMAL)
# import datetime
# print("{s}: {t}".format(s="finish",
# t=datetime.datetime.now()))
# noinspection PyUnusedLocal
def __shot_selected(self, event):
if len(self.listbox_shot_boundaries.curselection()) > 0:
index = int(self.listbox_shot_boundaries.curselection()[0])
print(index)
if self.__sbd.sb[index]['transition'] == 'cut':
self.__sbd.set_frame(self.__sbd.sb[index]['cut_frame'])
elif self.__sbd.sb[index]['transition'] == 'gradual':
self.__sbd.set_frame(self.__sbd.sb[index]['start_frame'])
self.__next_frame()
def __export(self):
with open('test.csv', 'w', newline='') as f:
writer = csv.DictWriter(f, fieldnames=['Transition',
'Start Frame',
'Start Time',
'End Frame',
'End Time'])
writer.writeheader()
for shot in self.__sbd.sb:
if shot['transition'] == 'cut':
writer.writerow({'Transition': 'Cut',
'Start Frame': shot['cut_frame'],
'Start Time': "{h}:{m}:{s}.{ms}".format(h=shot['cut_time'].hour,
m=shot['cut_time'].minute,
s=shot['cut_time'].second,
ms=shot['cut_time'].microsecond),
'End Frame': '---',
'End Time': '---'})
elif shot['transition'] == 'gradual':
writer.writerow({'Transition': 'Gradual',
'Start Frame': shot['start_frame'],
'Start Time': "{h}:{m}:{s}.{ms}".format(h=shot['start_time'].hour,
m=shot['start_time'].minute,
s=shot['start_time'].second,
ms=shot['start_time'].microsecond),
'End Frame': shot['end_frame'],
'End Time': "{h}:{m}:{s}.{ms}".format(h=shot['end_time'].hour,
m=shot['end_time'].minute,
s=shot['end_time'].second,
ms=shot['end_time'].microsecond)})
def open_test(self):
self.__sbd.open_video("test.mov")
self.label_status['text'] = "Video is not processed!"
self.__button_process_all.config(state=NORMAL)
self.__next_frame()
if __name__ == "__main__":
sbdgui = SBDGUI()