-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathload_emodb.py
79 lines (68 loc) · 2.75 KB
/
load_emodb.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
#encoding:utf-8
import urllib
import zipfile
import os
import shutil
import sys
from six.moves import urllib
import tarfile
import path
def load_data(server_file,source_url, obj_directory):
if not os.path.exists(server_file):
tarpath = maybe_download(server_file,source_url)
if not os.path.exists(obj_directory):
untar(server_file, server_file.split('.')[0])
build_class_directories(server_file,obj_directory)
def maybe_download(filename, source_url):
datadir_root = path.DataDir.DataRoot
filepath = ''
if not os.path.exists(datadir_root):
os.makedirs(datadir_root)
if not os.path.exists(filename):
print("Downloading %s from %s, Please "
"wait..."%(filename,source_url))
filepath, _ = urllib.request.urlretrieve(source_url,
filename, reporthook)
statinfo = os.stat(filename)
print('Succesfully downloaded', filename, statinfo.st_size, 'bytes.')
return filepath
#reporthook from stackoverflow #13881092
def reporthook(blocknum, blocksize, totalsize):
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 1e2 / totalsize
s = "\r%5.1f%% %*d / %d" % (
percent, len(str(totalsize)), readsofar, totalsize)
sys.stderr.write(s)
if readsofar >= totalsize: # near the end
sys.stderr.write("\n")
else: # total size is unknown
sys.stderr.write("read %d\n" % (readsofar,))
def build_class_directories(filename,dir):
os.rename('%s/wav'%filename.split('.')[0],dir)
shutil.rmtree(filename.split('.')[0])
def classfier_dataset_todir(Dataset_path):
wave_filenames = os.listdir(Dataset_path)
for filename in wave_filenames:
if not os.path.exists('%s/%s/%s'%(Dataset_path,filename[:2],filename[5:6])):
os.makedirs('%s/%s/%s'%(Dataset_path,filename[:2],filename[5:6]))
#os.rename('%s/%s'%(Dataset_path,filename),'%s/%s/%s/%s'%(Dataset_path,filename[:2],filename[2:5],filename[5:12]))
def untar(fname, extract_dir):
if fname.endswith("tar.gz") or fname.endswith("tgz"):
tar = tarfile.open(fname)
tar.extractall(extract_dir)
tar.close()
print("File Extracted")
elif fname.endswith("zip"):
zip = zipfile.ZipFile(fname, 'r')
zip.extractall(extract_dir)
# for f in zip.namelist():
# print(f)
zip.close()
print("File Extracted")
else:
print("Not a tar.gz/tgz/zip file: '%s '" % sys.argv[0])
if __name__ == "__main__":
url = 'http://www.emodb.bilderbar.info/download/download.zip'
server_fname = url.split('/')[-1]
load_data(server_file=path.DataDir.DataRoot+server_fname,source_url=url,obj_directory=path.DataDir.wav)