-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslideshow_maker.py
More file actions
328 lines (247 loc) · 10.6 KB
/
slideshow_maker.py
File metadata and controls
328 lines (247 loc) · 10.6 KB
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
import random
import copy
from slide import Slide
from score import transition_score
class SlideshowMaker(object):
def __init__(self):
# Maybe this will be useful
pass
def greedy_horizontal_make(self, pics):
slideshow = []
previous_slide = None
for i, pic in pics.items():
# First, only Horizontal
if pic.orientation == 1:
continue
current_slide = Slide(pic, None)
if current_slide.is_valid(): # Horizontal
if previous_slide is None:
slideshow.append(current_slide)
previous_slide = current_slide
else:
if transition_score(current_slide, previous_slide):
# Ok, current slide is valid, add it to slideshow
slideshow.append(current_slide)
previous_slide = current_slide
return slideshow
def greedy_random_horizontal_make(self, pics):
slideshow = []
previous_slide = None
index = list(range(len(pics)))
random.shuffle(index)
for i in index:
pic = pics[i]
# First, only Horizontal
if pic.orientation == 1:
continue
current_slide = Slide(pic, None)
if current_slide.is_valid(): # Horizontal
if previous_slide is None:
slideshow.append(current_slide)
previous_slide = current_slide
else:
if transition_score(current_slide, previous_slide):
# Ok, current slide is valid, add it to slideshow
slideshow.append(current_slide)
previous_slide = current_slide
return slideshow
def greedy_vertical_make(self, pics):
slideshow = []
previous_slide = None
current_slide = None
pics_copy = copy.copy(pics)
keep_going = True
while keep_going:
current_slideshow = []
for i, pic in pics_copy.items():
# First, only Vertical
if pic.orientation == 0:
continue
if current_slide is None:
current_slide = Slide(pic, None)
continue
else:
current_slide.add(pic)
if current_slide.is_valid():
if previous_slide is None:
slideshow.append(current_slide)
current_slideshow.append(current_slide)
previous_slide = current_slide
else:
if transition_score(current_slide, previous_slide):
# Ok, current slide is valid, add it to slideshow
slideshow.append(current_slide)
current_slideshow.append(current_slide)
previous_slide = current_slide
current_slide = None
if not current_slideshow:
# Stop when no more slideshow found
keep_going = False
else:
# print('Adding %s slides' % len(current_slideshow))
for slide in current_slideshow:
del pics_copy[slide.pic_a.id]
del pics_copy[slide.pic_b.id]
return slideshow
def greedy_random_vertical_make(self, pics):
slideshow = []
previous_slide = None
current_slide = None
pics_copy = copy.copy(pics)
keep_going = True
# print('Starting greedy slideshow maker...')
while keep_going:
current_slideshow = []
index = list(pics_copy.keys())
random.shuffle(index)
for i in index:
pic = pics_copy[i]
# First, only Vertical
if pic.orientation == 0:
continue
if current_slide is None:
current_slide = Slide(pic, None)
continue
else:
current_slide.add(pic)
if current_slide.is_valid():
if previous_slide is None:
slideshow.append(current_slide)
current_slideshow.append(current_slide)
previous_slide = current_slide
else:
if transition_score(current_slide, previous_slide):
# Ok, current slide is valid, add it to slideshow
slideshow.append(current_slide)
current_slideshow.append(current_slide)
previous_slide = current_slide
current_slide = None
if not current_slideshow:
# Stop when no more slideshow found
keep_going = False
else:
# print('Adding %s slides' % len(current_slideshow))
for slide in current_slideshow:
del pics_copy[slide.pic_a.id]
del pics_copy[slide.pic_b.id]
return slideshow
def greedy_random_make(self, slides):
slideshow = []
previous_slide = None
slides = copy.copy(slides)
keep_going = True
while keep_going:
current_slideshow = []
index = list(list(slides.keys()))
random.shuffle(index)
for i in index:
slide = slides[i]
if previous_slide is None:
slideshow.append(slide)
current_slideshow.append(i)
previous_slide = slide
else:
if transition_score(slide, previous_slide):
# Ok, current slide is valid, add it to slideshow
slideshow.append(slide)
current_slideshow.append(i)
previous_slide = slide
if not current_slideshow:
# Stop when no more slideshow found
keep_going = False
else:
# print('Adding %s slides' % len(current_slideshow))
for slide_id in current_slideshow:
del slides[slide_id]
return slideshow
def greedy_best_make(self, slides):
slideshow = []
slides = copy.copy(slides)
# Randomly pick first pick
index = list(slides.keys())
starting_index = random.choice(index)
previous_slide = slides.pop(starting_index)
slideshow.append(previous_slide)
keep_going = True
i = 0
while keep_going:
if i % 1000 == 0 and i != 0:
print('current slideshow is %s long...' % i)
current_slide_id = self._find_best_slide_transition(slides, previous_slide)
if current_slide_id is None:
# Stop when no more slideshow found
keep_going = False
else:
slideshow.append(slides[current_slide_id])
previous_slide = slides[current_slide_id]
del slides[current_slide_id]
i += 1
return slideshow
def _find_best_slide_transition(self, slides, previous_slide):
best_transition = -100000
best_transition_tag_count = 100000
best_slide_id = None
shuffled_slides = list(slides.keys())
random.shuffle(shuffled_slides)
for i in shuffled_slides:
max_target_score = 1000 # max(int(len(previous_slide.tags) / 2) + 10, 1) # Tweek here for better result
current_transition = transition_score(previous_slide, slides[i])
current_transition_tag_count = len(previous_slide.tags | slides[i].tags)
if current_transition > best_transition or \
(current_transition == best_transition and \
current_transition_tag_count < best_transition_tag_count):
best_transition = current_transition
best_transition_tag_count = current_transition_tag_count
best_slide_id = i
if best_transition >= max_target_score: # and best_transition_tag_count <= (max_target_score * 4) + 5:
break
return best_slide_id
def greedy_on_pics(self, pics):
slideshow = []
pics = copy.copy(pics)
# Randomly pick first pick
index = list(pics.keys())
starting_index = random.choice(index)
current_slide = Slide(pics.pop(starting_index))
previous_slide = None
keep_going = True
counter = 0
while keep_going:
if counter % 10000 == 0 and counter != 0:
print('currently consumed %s pics...' % counter)
if current_slide.is_valid():
slideshow.append(current_slide)
previous_slide = current_slide
current_slide_id = self._find_best_slide_transition(pics, previous_slide)
if current_slide_id is not None:
current_slide = Slide(pics[current_slide_id])
del pics[current_slide_id]
else:
break
else:
# Find another best pic and add it to current slide
second_pic_id = None
if previous_slide is not None:
pic_a = current_slide.pic_a
best_transition = -1
for i, pic_b in pics.items():
if pic_b.orientation == 1:
current_association = Slide(pic_a)
current_association.add(pic_b)
current_transition = transition_score(previous_slide, current_association)
if current_transition > best_transition:
best_transition = current_transition
second_pic_id = i
else:
for i, pic in pics.items():
if pic.orientation == 1 and not set(current_slide.tags) & set(pic.tags):
second_pic_id = i
break
if second_pic_id is not None:
current_slide.add(pics[second_pic_id])
del pics[second_pic_id]
else:
print('breaking at %s pics: cant find vertical match' % counter)
keep_going = False
counter += 1
return slideshow