Repository navigation
Expand file tree
/
Copy pathupdate.py
More file actions
361 lines (310 loc) · 11.2 KB
/
Copy pathupdate.py
File metadata and controls
361 lines (310 loc) · 11.2 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
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
359
360
361
#!/usr/bin/env python
import csv
import commands
from compiler.ast import flatten
import os
import re
import cPickle as cp
import json
'''
read [class_labels_indics] file
result:(lid,lname) and (lname,lid)
'''
def psrcsv(file_path, tag):
with open(file_path) as csvfile:
reader = csv.reader(csvfile)
headings = next(reader)
print "The headings is like this:",headings,'\n'
point = 0
if tag=="cli":
lid_lname_dict = {}
lname_lid_dict = {}
for re in reader:
point+=1
if point>=1:
lid = re[1]
lname = re[2].strip()
lid_lname_dict[lid] = lname
lname_lid_dict[lname] = lid
csvfile.close()
return lid_lname_dict, lname_lid_dict
elif tag=="bt":
vid_lid_dict = {}
vid_startend_dict = {}
for _line in reader:
point+=1
lst = []
if point<3:
print _line,point
else:
mid = _line[0].strip()
start, end = _line[1].strip(), _line[2].strip()
vid_startend_dict[mid] = start,end
labels = _line[3:] # lid list
[lst.append(_item.replace('"','').strip()) for _item in labels]
vid_lid_dict[mid] = lst
csvfile.close()
return vid_lid_dict, vid_startend_dict
def AudiosetStart():
path = '/home/zeng/Audio_tensorflow/audioset/'
lid_lname_dict,lname_lid_dict = psrcsv(path+'class_labels_indices.csv', 'cli')
print "The output result of [class_labels_indices.csv], for example:\n1.",\
dict(lid_lname_dict.items()[:10]),"\n2.",dict(lname_lid_dict.items()[:10]),'\n'
dict1, dict2 = psrcsv(path+'label/balanced_train_segments.csv','bt')
dict3, dict4 = psrcsv(path+'label/eval_segments.csv','bt')
vid_lid_dict = dict(dict1, **dict3)
vid_startend_dict = dict(dict2, **dict4)
print '\nThe vid_lid_dict is like this:\n',\
dict(vid_lid_dict.items()[:5])
return lid_lname_dict,lname_lid_dict, vid_lid_dict, vid_startend_dict
def get_label_lst(path):
dicts = {}
with open(path) as fs:
lines = fs.readlines()
fs.close()
for lin in lines:
ln = lin.rstrip('\n').split('\t')
dicts[ln[0]]=ln[2]
return dicts
def updating(vid_lid_dict,lid_lname_dict,inputs):
vid_lname_dict={}
if type(inputs) is dict:
lsts = inputs.keys()
elif type(inputs) is list:
lsts = inputs
for item,label in vid_lid_dict.items():
lab = []
for ii in label:
name=lid_lname_dict[ii]
lab.append(name)
if set(lab).issubset(set(lsts)):
vid_lname_dict[item]=lab
return vid_lname_dict
def get_multi_only(vid_lid_dict,lid_lname_dict,dicts):
multi_dict = {}
onlyone_dict = {}
vid_lname_dict = updating(vid_lid_dict, lid_lname_dict,dicts)
for item,label in vid_lname_dict.items():
if len(label)>1 and '' not in label:
multi_dict[item]=label
else:
onlyone_dict[item]=label
with open("doc/multi_dict.csv","w") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['index', 'vid', 'label'])
for index,(vid,label) in enumerate(multi_dict.items()):
writer.writerow([str(index),str(vid),str(';'.join(label))])
csvfile.close()
with open("doc/onlyone_dict.csv","w") as csvfiles:
writers = csv.writer(csvfiles)
writers.writerow(['index', 'vid', 'label'])
for indexs,(vids,labels) in enumerate(onlyone_dict.items()):
writers.writerow([str(indexs),str(vids),str(''.join(labels))])
csvfiles.close()
lname_vid_mdict = {}
lname_vid_odict = {}
for item in dicts.keys():
lname_vid_mdict[item]=[]
for items in dicts.keys():
lname_vid_odict[items]=[]
for vid,lablst in multi_dict.items():
for lab in lablst:
if lname_vid_mdict.has_key(lab):
lname_vid_mdict[lab].append(vid)
else:
lname_vid_mdict[lab]=[]
for ks,vs in onlyone_dict.items():
labels = ''.join(vs)
if lname_vid_odict.has_key(labels):
lname_vid_odict[labels].append(ks)
else:
lname_vid_odict[labels]=[]
with open("doc/lname_vid_mdict.txt",'w') as f:
for _lname, _vidlst in lname_vid_mdict.items():
line = _lname+":"+'<SEP>'.join(_vidlst)+'\n'
f.write(line)
f.close()
with open("doc/lname_vid_odict.txt",'w') as fs:
for _lname, _vidlst in lname_vid_odict.items():
lines = _lname+":"+'<SEP>'.join(_vidlst)+'\n'
fs.write(lines)
fs.close()
return multi_dict, onlyone_dict,lname_vid_mdict,\
lname_vid_odict,onlyone_dict,multi_dict
def getlid(path):
'''get all 100% labels'''
ranks_lst = []
id_label_lst = []
rank_dict={}
pattern_rule=re.compile(r'\t|:')
with open(path) as g:
rank_lst = g.readlines()
for item1 in rank_lst:
item1_str = re.sub(pattern_rule, '=', item1)
item1_lst = item1_str.split('=')
assert item1_str.count('=')==2
ranks_lst.append(item1_lst[1])## ranks_lst contain all 79 classes names
rank_dict[item1_lst[1]]=item1_lst[2].strip()
g.close()
return rank_dict
def getAudios():
orignal = "/home/zeng/Audioset/videoset/20video/"
target = "/home/zeng/Audioset/videoset/20audio/"
lsts = []
for dirpaths, dirnames, filenames in os.walk(orignal):
lsts.extend(dirnames)
if len(dirpaths.split("/")[-1])>0:
lname=dirpaths.split("/")[-1]
os.chdir(target)
comm="mkdir "+lname
commands.getstatusoutput(comm)
os.chdir(orignal+lname)
for filename in filenames:
inputs = filename
outputs = target+lname+"/"+filename.replace(".mkv", ".mp3")
print outputs
line = "ffmpeg -i "+inputs+" -f mp3 -vn "+outputs
try:
commands.getstatusoutput(line)
except:
print item
def getvidlst(path):
lists=[]
for dirpaths, dirnames, filenames in os.walk(path):
for files in filenames:
files = files.replace(".mkv", "")
lists.append(files)
return lists
def json_reading():
with open("ontology.json", 'r') as f:
data = json.load(f)
f.close()
return data
###################################################################################
'''Use id and name to search related informations'''
def id_search_name(mid):
data = json_reading()
for item in range(len(data)):
index = data[item]
if index['id']==mid:
return [index['name'],index['description'], index['citation_uri'], \
index['positive_examples'], index['child_ids'],\
index['restrictions']]
def name_search_id(name):
data = json_reading()
for item in range(len(data)):
index = data[item]
if index['name']==name:
return [index['id'],index['description'], index['citation_uri'], \
index['positive_examples'], index['child_ids'],\
index['restrictions']]
############################getting children process#########################
def getchild(father): ## father is video id
children = []
if '/' in father:
try:
child = id_search_name(father)[4]
except:
print 'Please! input a right string format! instead of',type(father)
return 0
else:
child = name_search_id(father)[4]
for chi in child:
children.append(id_search_name(chi)[0])
return list(set(children))
def getChildren(father): ## father is video id
lsts = []
childlst = []
lee = []
data = json_reading()
lst = getchild(father)
if len(lst)>0:
childlst.extend(lst)
for ls in lst:
child = getAllchild(ls)
for _ch in child:
if _ch:
lsts.append(_ch)
return childlst, lsts
def getAllchild(father):
lists = []
childlst, lsts = getChildren(father)
lists.extend(childlst)
lists.extend(lsts)
return list(set(lists))
def getOffspring(classlst):
labelSet = []
for item in classlst:
child = getAllchild(name_search_id(item)[0])
if child:
labelSet.extend(child)
return labelSet
def moving(orignal, target, dicts):
pattern_rule=re.compile(r',')
name = target.split('/')[-1]
for vid in dicts:
os.chdir(target.replace(name,''))
comm="mkdir "+name
commands.getstatusoutput(comm)
os.chdir(orignal)
line = 'cp '+vid+'.mkv '+target
try:
commands.getstatusoutput(line)
except:
print vid
def get_video(vid_lst, loadfile):
'''
Use youtube id list to crawl fulltime videos
You need input youtube id list and your expected video path
'''
filename = ''
for item in vid_lst:
if loadfile[-1]=="/":
filename = loadfile+str(item)
else:
filename = loadfile+"/"+str(item)
path = "http://www.youtube.com/watch?v="+item
line = "youtube-dl -o "+filename+' '+path
try:
commands.getstatusoutput(line)
except:
print item
def formats(seconds):
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
forms = "%02d:%02d:%02d" % (h, m, s)
return forms
def getSegment(vid_startend_dict, fvideofile, videofile):
for vid, (start, end) in vid_startend_dict.items():
start = int(start.split(".")[0])
end = int(end.split(".")[0])
duration = end-start
start = formats(start)
name = ""+vid+".mkv"
segment = videofile+name
os.chdir(fvideofile)
line = "ffmpeg -y -i "+name+" -ss "+str(start)+" -t "+str(duration)+" -c copy -avoid_negative_ts 1 "+segment
try:
commands.getstatusoutput(line)
except:
print item
def getaudios(orignal, target):
lsts = []
for dirpaths, dirnames, filenames in os.walk(orignal):
lsts.extend(dirnames)
if len(dirpaths.split("/")[-1])>0:
lname=dirpaths.split("/")[-1]
os.chdir(target)
comm="mkdir "+lname
commands.getstatusoutput(comm)
os.chdir(orignal+lname)
for filename in filenames:
inputs = filename
outputs = target+lname+"/"+filename.replace(".mkv", ".mp3")
## print outputs
line = "ffmpeg -i "+inputs+" -f mp3 -vn "+outputs
try:
commands.getstatusoutput(line)
except:
print item
###########################end getting children process#######################