-
Notifications
You must be signed in to change notification settings - Fork 82
/
build_labels.py
58 lines (46 loc) · 1.7 KB
/
build_labels.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
"""
Given a folder of images and commercial start/end times, build our features
for training.
"""
import glob
import pickle
from shutil import copyfile
from commercials import commercials
def label_frames(batch, commercials, copyimage=True):
"""Label our frames."""
# Get all our images.
images = sorted(glob.glob('./images/' + str(batch) + '/*'))
num_images = len(images)
print("Labelling %d frames." % num_images)
# Loop through our images and set our labels.
labeled_images = []
num_commercials = 0
for image in images:
# Get the timestamp.
timestamp = image.replace('.jpg', '').split('/')[-1]
# What is it?
label = get_label(timestamp, commercials)
# Save it.
labeled_images.append([timestamp, label])
# Copy it.
if copyimage:
copyfile(image, './images/classifications/' + label + '/' + timestamp + '.jpg')
# Info.
if label == 'ad':
num_commercials += 1
print("Done labelling, with %d commercial frames and %d not." %
(num_commercials, num_images - num_commercials))
with open('data/labeled-frames-' + str(batch) + '.pkl', 'wb') as fout:
pickle.dump(labeled_images, fout)
return labeled_images
def get_label(timestamp, commercials):
"""Given a timestamp and the commercials list, return if
this frame is a commercial or not. If not, return label."""
for com in commercials['commercials']:
if com['start'] <= timestamp <= com['end']:
return 'ad'
return commercials['class']
if __name__ == '__main__':
batches = ['1']
for batch in batches:
label_frames(batch, commercials[str(batch)], copyimage=False)