This repository was archived by the owner on Jun 1, 2021. It is now read-only.
forked from ghowa/bandera-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_video_types.py
More file actions
executable file
·188 lines (155 loc) · 5.24 KB
/
Copy pathplot_video_types.py
File metadata and controls
executable file
·188 lines (155 loc) · 5.24 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Plot frame differences and highlight
video types
@author ghowa
"""
import sys
import scipy.stats
import pylab
import guess_language
from HTMLParser import HTMLParser
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def main(argv=None):
""" Main function """
if argv is None:
argv = sys.argv
points = []
labels = []
ru = []
en = []
uk = []
pl = []
en_labels = []
uk_labels = []
pl_labels = []
points_labels = []
title_labels = dict()
lang_labels = dict()
type_labels = dict()
type_file = open('../youTubeData/video_type', "r")
type_line = type_file.readline()
while not type_line == "":
type_labels[type_line.split(";")[0]] = type_line.split(";")[1].strip()
type_line = type_file.readline()
lang_file = open('../youTubeData/manually_recognized', "r")
lang_line = lang_file.readline()
while not lang_line == "":
lang_labels[lang_line.split(";")[0]] = lang_line.split(";")[1].strip()
lang_line = lang_file.readline()
title_file = open("../youTubeData/all_frames_stats_title", "r")
line = " "
manual = 0
while not line == "":
line = title_file.readline()
lbl = line.strip()
# .lower().replace("stepan bandera","en")
title = title_file.readline()
title_labels[lbl] = title
title_lang = guess_language.guessLanguage(strip_tags(title))
desc = title_file.readline()
try:
desc.split("No description available")[1]
desc = ""
except IndexError:
pass
desc_lang = guess_language.guessLanguage(strip_tags(desc))
line = title_file.readline()
lang = guess_language.guessLanguage(
strip_tags(title) + strip_tags(desc))
if lbl in lang_labels:
print lbl, " found"
continue
print lbl, " not found"
if lang in ['uk', 'ru', 'pl', 'en']:
lang_labels[lbl] = lang
else:
manual += 1
print "------------------------------------------------------------"
print title_lang, desc_lang
print title
print desc
print "------------------------------------------------------------"
l = raw_input("which language? ")
lang_labels[lbl] = l
print manual, " manually recognized"
lang_file.close()
lang_file_content = ""
print lang_labels
for key in lang_labels.keys():
lang_file_content += key + ";" + lang_labels[key] + "\r\n"
print lang_file_content
lang_file = open('../youTubeData/manually_recognized', "w")
lang_file.write(lang_file_content)
lang_file.close()
text_file = open('../youTubeData/all_frames_stats', "r")
line = " "
counter = 0
while not line == "":
line = text_file.readline()
counter = counter + 1
try:
values = map(float, line.split('\r\n')[0].split(';')[1:])
label = line.split(';')[0]
labels.append(label)
size, min_max, mean, variance, skew, kurt = scipy.stats.describe(
values)
# throw out vids under 20 seconds
if size < 20:
print "video too short: ", label
continue
# cut min variance at 0.001, otherwise the plot gets quite
# distorted
if variance < 0.001:
variance = 0.001
except ValueError:
print "error calculating stats for ", label
continue
point = [mean, variance]
try:
if type_labels[label] == "priv":
uk.append(point)
uk_labels.append(label)
elif type_labels[label] == "news" \
or type_labels[label] == "talk" \
or type_labels[label] == "docu":
en.append(point)
en_labels.append(label)
elif type_labels[label] == "slide":
pl.append(point)
pl_labels.append(label)
else:
points.append(point)
points_labels.append(label)
except:
print label, lang_labels[label]
print counter, " lines read."
print "number of labels ", str(len(labels))
print "number of labels ", str(len(lang))
pylab.show()
pylab.xlabel('Mean')
pylab.ylabel('Variance')
pylab.title("Frame Likenesses of Bandera Youtube Clips")
pylab.plot(*zip(*points), marker='o', color='w', ls='')
pylab.plot(*zip(*uk), marker='o', color='#99FFFF', ls='')
pylab.plot(*zip(*ru), marker='o', color='b', ls='')
pylab.plot(*zip(*pl), marker='o', color='#000066', ls='')
pylab.plot(*zip(*en), marker='o', color='#3399FF', ls='')
figure = pylab.gcf()
figure.set_size_inches(
figure.get_size_inches()[0] * 2, figure.get_size_inches()[1] * 2)
figure.savefig('video_types.png', bbox_inches='tight')
if __name__ == "__main__":
sys.exit(main())