forked from smarsland/AviaNZ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAviaNZ.py
More file actions
128 lines (110 loc) · 4.35 KB
/
AviaNZ.py
File metadata and controls
128 lines (110 loc) · 4.35 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
# Version 3.0 14/09/20
# Authors: Stephen Marsland, Nirosha Priyadarshani, Julius Juodakis, Virginia Listanti
# This is the script that starts AviaNZ. It processes command line options
# and then calls either part of the GUI, or runs on the command line directly.
# AviaNZ bioacoustic analysis program
# Copyright (C) 2017--2020
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Cut down by Ysobel Sims, University of Newcastle 2024
import json, shutil, os, sys, argparse
from jsonschema import validate
import util.SupportClasses as SupportClasses
import AviaNZ_batch
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--filename",
type=str,
help="Input sound to process",
)
parser.add_argument(
"-r",
"--recogniser",
type=str,
help="Recogniser name",
)
parser.add_argument("command", nargs="*", help="Command to execute")
args = parser.parse_args()
if getattr(sys, "frozen", False):
appdir = sys._MEIPASS
else:
appdir = os.path.dirname(os.path.abspath(__file__))
os.chdir(appdir)
configdir = os.path.expanduser("~/.avianz/")
# if config and bird files not found, copy from distributed backups.
# so these files will always exist on load (although they could be corrupt)
# (exceptions here not handled and should always result in crashes)
if not os.path.isdir(configdir):
# print("Creating config dir %s" % configdir)
try:
os.makedirs(configdir)
except Exception as e:
# print("ERROR: failed to make config dir")
# print(e)
raise
# pre-run check of config file validity
confloader = SupportClasses.ConfigLoader()
configschema = json.load(open("Config/config.schema"))
learnparschema = json.load(open("Config/learnpar.schema"))
try:
config = confloader.config(os.path.join(configdir, "AviaNZconfig.txt"))
validate(instance=config, schema=configschema)
learnpar = confloader.learningParams(os.path.join(configdir, "LearningParams.txt"))
validate(instance=learnpar, schema=learnparschema)
# print("successfully validated config file")
except Exception as e:
# print("Warning: config file failed validation with:")
# print(e)
try:
shutil.copy2("Config/AviaNZconfig.txt", configdir)
shutil.copy2("Config/LearningParams.txt", configdir)
except Exception as e:
# print("ERROR: failed to copy essential config files")
# print(e)
raise
# check and if needed copy any other necessary files
necessaryFiles = [
"ListCommonBirds.txt",
"ListDOCBirds.txt",
"ListBats.txt",
"LearningParams.txt",
]
for f in necessaryFiles:
if not os.path.isfile(os.path.join(configdir, f)):
# print("File %s not found in config dir, providing default" % f)
try:
shutil.copy2(os.path.join("Config", f), configdir)
except Exception as e:
# print("ERROR: failed to copy essential config files")
# print(e)
raise
# copy over filters to ~/.avianz/Filters/:
filterdir = os.path.join(configdir, "Filters/")
if not os.path.isdir(filterdir):
# print("Creating filter dir %s" % filterdir)
os.makedirs(filterdir)
for f in os.listdir("Filters"):
ff = os.path.join("Filters", f) # Kiwi.txt
if not os.path.isfile(os.path.join(filterdir, f)): # ~/.avianz/Filters/Kiwi.txt
# print("Recogniser %s not found, providing default" % f)
try:
shutil.copy2(ff, filterdir) # cp Filters/Kiwi.txt ~/.avianz/Filters/
except Exception as e:
print("Warning: failed to copy recogniser %s to %s" % (ff, filterdir))
print(e)
avianzbatch = AviaNZ_batch.AviaNZ_batchProcess(configdir, args.recogniser)
### THE DETECTION ###
detection = avianzbatch.detect(args.filename)
if len(detection) > 0:
print("Detected")
else:
print("Not detected")