forked from kahst/BirdNET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotate.py
76 lines (67 loc) · 2.5 KB
/
annotate.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
import sys
import config as cfg
from pathlib import Path
FILENAME = 'annotations.csv'
POINTSPERSEC = 1000 # 1000 10^-3 10 for 10^-1
ROUNDDIGITS = 3 # corresponds to POINTSPERSEC
FRAMESIZE = 3.0 # predefined by BirdNET
SKIPKEY = 's'
YESKEY = 'y'
NOKEY = 'n'
MINCONF = 0.0
MAXCONF = 1.0
print('*** Audio annotatation utility ***')
print('')
print('Enter start and end points of bird voices in seconds or enter s to skip.')
print('')
def process_file(path):
done = NOKEY
lines = ''
prev_end = 0.0
prev_start = 0.0
print('Annotating file ', path)
selection_id = 1
while done != YESKEY:
start = input('Please enter startpoint or skip: ')
prev_start = start
if start == SKIPKEY:
done = input('Done? y or n: ')
if done == YESKEY:
break
else:
continue
if float(start) <= prev_end:
print('Please list annotations sorted. Start has to be greater than previus End. Abborting')
break
end = float(input('Please enter endpoint: '))
prev_end = end
if end <= float(start):
print('End has to be greater than Start. Abborting')
break
code = input('Please enter eBird Species Code:')
for i in range(int(abs(end - float(start)) * POINTSPERSEC)):
lines += f'{selection_id}{cfg.CSV_DLIM}Spectogram_1{cfg.CSV_DLIM}1{cfg.CSV_DLIM}{path}{cfg.CSV_DLIM}{start}{cfg.CSV_DLIM}{round(float(start) + FRAMESIZE, ROUNDDIGITS)}{cfg.CSV_DLIM}{-1}{cfg.CSV_DLIM}{-1}{cfg.CSV_DLIM}{code}{cfg.CSV_DLIM}{path}{cfg.CSV_DLIM}1.0{cfg.CSV_DLIM}1{cfg.CSV_DLIM}{cfg.SPEC_OVERLAP}\n'
start = float(start)
start += 1/POINTSPERSEC
start = round(start, ROUNDDIGITS)
selection_id += 1
print(f'Added {code} between {str(prev_start)} and {str(end)}.')
return lines
path_str = sys.argv[-1]
csv_f = Path(path_str)
if len(sys.argv[1:]) >= 3:
for p in sys.argv[1:-1]:
lines = process_file(p)
if csv_f.is_file():
print(f'Created file {path_str}')
print(f'Wrote annotations for {p} to file {path_str}')
with open(path_str, 'a') as csvfile:
csvfile.write(lines)
else:
print(f'Wrote annotations for {p} to file {path_str}')
with open(path_str, 'w') as csvfile:
csvfile.write(lines)
else:
print('usage: $ python3 annotate.py [FILESPATHS]⁺ [OUTPUTPATH]')
print('')
print('Done')