-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_proc.py
66 lines (56 loc) · 2.66 KB
/
post_proc.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
import csv
import numpy as np
import os
import argparse
def post_processing(val_path, evaluation_file, new_evaluation_file, n_shots=5):
'''Post processing of a prediction file by removing all events that have shorter duration
than 60% of the minimum duration of the shots for that audio file.
Parameters
----------
val_path: path to validation set folder containing subfolders with wav audio files and csv annotations
evaluation_file: .csv file of predictions to be processed
new_evaluation_file: .csv file to be saved with predictions after post processing
n_shots: number of available shots
'''
dict_duration = {}
folders = os.listdir(val_path)
for folder in folders:
files = os.listdir(val_path+folder)
for file in files:
if file[-4:] == '.csv':
audiofile = file[:-4]+'.wav'
annotation = file
events = []
with open(val_path+folder+'/'+annotation) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
for row in csv_reader:
if row[-1] == 'POS' and len(events) < n_shots:
events.append(row)
min_duration = 10000
for event in events:
if float(event[2])-float(event[1]) < min_duration: # 计算5个shot中,时间最短的一个shot
min_duration = float(event[2])-float(event[1])
dict_duration[audiofile] = min_duration
results = []
with open(evaluation_file, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader, None) # skip the headers
for row in reader:
results.append(row)
new_results = [['Audiofilename', 'Starttime', 'Endtime']]
for event in results:
audiofile = event[0]
min_dur = dict_duration[audiofile]
if float(event[2])-float(event[1]) >= 0.6*min_dur:
new_results.append(event)
with open(new_evaluation_file, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(new_results)
return
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('-val_path', type=str, help='path to validation folder with wav and csv files')
parser.add_argument('-evaluation_file', type=str, help='path and name of prediction file')
parser.add_argument('-new_evaluation_file', type=str, help="name of prost processed prediction file to be saved")
args = parser.parse_args()
post_processing( args.val_path, args.evaluation_file, args.new_evaluation_file)