ms903 commited on
Commit
2bec0b1
1 Parent(s): c812e58

Upload draw-old2.py

Browse files
Files changed (1) hide show
  1. draw-old2.py +102 -0
draw-old2.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import tqdm
3
+ import matplotlib.pyplot as plt
4
+ import os
5
+ import shutil
6
+ import wave
7
+
8
+ WAV_MIN_LENGTH = 2 # wav文件的最短时长 / The minimum duration of wav files
9
+ SAMPLE_RATE = 1 # 抽取文件数量的百分比 / The percentage of files to be extracted
10
+ SAMPLE_MIN = 5 # 抽取的文件数量下限 / The lower limit of the number of files to be extracted
11
+ SAMPLE_MAX = 5 # 抽取的文件数量上限 / The upper limit of the number of files to be extracted
12
+
13
+
14
+ # 定义一个函数,用于检查wav文件的时长是否大于最短时长
15
+ def check_duration(wav_file):
16
+ # 打开wav文件
17
+ f = wave.open(wav_file, "rb")
18
+ # 获取帧数和帧率
19
+ frames = f.getnframes()
20
+ rate = f.getframerate()
21
+ # 计算时长(秒)
22
+ duration = frames / float(rate)
23
+ # 关闭文件
24
+ f.close()
25
+ # 返回时长是否大于最短时长的布尔值
26
+ return duration > WAV_MIN_LENGTH
27
+
28
+ # 定义一个函数,用于从给定的目录中随机抽取一定比例的wav文件,并剪切到另一个目录中,保留数据结构
29
+ def split_data(src_dir, dst_dir, ratio):
30
+ # 创建目标目录(如果不存在)
31
+ if not os.path.exists(dst_dir):
32
+ os.makedirs(dst_dir)
33
+
34
+ # 获取源目录下所有的子目录和文件名
35
+ subdirs, files, subfiles = [], [], []
36
+ for item in os.listdir(src_dir):
37
+ item_path = os.path.join(src_dir, item)
38
+ if os.path.isdir(item_path):
39
+ subdirs.append(item)
40
+ for subitem in os.listdir(item_path):
41
+ subitem_path = os.path.join(item_path, subitem)
42
+ if os.path.isfile(subitem_path) and subitem.endswith(".wav"):
43
+ subfiles.append(subitem)
44
+ elif os.path.isfile(item_path) and item.endswith(".wav"):
45
+ files.append(item)
46
+
47
+ # 如果源目录下没有任何wav文件,则报错并退出函数
48
+ if len(files) == 0:
49
+ if len(subfiles) == 0:
50
+ print(f"Error: No wav files found in {src_dir}")
51
+ return
52
+
53
+ # 计算需要抽取的wav文件数量
54
+ num_files = int(len(files) * ratio)
55
+ num_files = max(SAMPLE_MIN, min(SAMPLE_MAX, num_files))
56
+
57
+ # 随机打乱文件名列表,并取出前num_files个作为抽取结果
58
+ np.random.shuffle(files)
59
+ selected_files = files[:num_files]
60
+
61
+ # 创建一个进度条对象,用于显示程序的运行进度
62
+ pbar = tqdm.tqdm(total=num_files)
63
+
64
+ # 遍历抽取结果中的每个文件名,检查是否大于2秒
65
+ for file in selected_files:
66
+ src_file = os.path.join(src_dir, file)
67
+ # 检查源文件的时长是否大于2秒,如果不是,则打印源文件的文件名,并跳过该文件
68
+ if not check_duration(src_file):
69
+ print(f"Skipped {src_file} because its duration is less than 2 seconds.")
70
+ continue
71
+ # 拼接源文件和目标文件的完整路径,移动文件,并更新进度条
72
+ dst_file = os.path.join(dst_dir, file)
73
+ shutil.move(src_file, dst_file)
74
+ pbar.update(1)
75
+
76
+ pbar.close()
77
+
78
+ # 遍历源目录下所有的子目录(如果有)
79
+ for subdir in subdirs:
80
+ # 拼接子目录在源目录和目标目录中的完整路径
81
+ src_subdir = os.path.join(src_dir, subdir)
82
+ dst_subdir = os.path.join(dst_dir, subdir)
83
+ # 递归地调用本函数,对子目录中的wav文件进行同样的操作,保留数据结构
84
+ split_data(src_subdir, dst_subdir, ratio)
85
+
86
+ # 定义主函数,用于获取用户输入并调用上述函数
87
+
88
+ def main():
89
+ root_dir = os.path.abspath('.')
90
+ dst_dir = root_dir + "/data/val/audio/1"
91
+ # 抽取比例,默认为1
92
+ ratio = float(SAMPLE_RATE) / 100
93
+
94
+ # 固定源目录为根目录下/data/train/audio目录
95
+ src_dir = root_dir + "/data/train/audio/1"
96
+
97
+ # 调用split_data函数,对源目录中的wav文件进行抽取,并剪切到目标目录中,保留数据结构
98
+ split_data(src_dir, dst_dir, ratio)
99
+
100
+ # 如果本模块是主模块,则执行主函数
101
+ if __name__ == "__main__":
102
+ main()