sndid/sndid.py

157 lines
3.4 KiB
Python
Raw Normal View History

2023-09-21 13:04:11 -06:00
#!/usr/bin/env python
"""
sndid
Copyright 2022, 2023, Joe Weiss <joe.weiss@gmail.com>
Copyright 2023, Jeff Moe <moe@spacecruft.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
2023-09-21 14:03:59 -06:00
https://joeweiss.github.io/birdnetlib/getting-started/
2023-09-21 13:04:11 -06:00
"""
2023-09-21 14:03:59 -06:00
2023-09-21 13:49:58 -06:00
from birdnetlib import Recording
from birdnetlib.analyzer import Analyzer
2023-09-24 10:10:36 -06:00
from datetime import datetime, date
2023-09-21 14:41:14 -06:00
import argparse
2023-09-24 10:10:36 -06:00
from contextlib import redirect_stdout, redirect_stderr
import io
TODAY = date.today()
YEAR = TODAY.year
MONTH = TODAY.month
DAY = TODAY.day
2023-09-21 14:41:14 -06:00
parser = argparse.ArgumentParser(description="Run sndid")
parser.add_argument(
"-i",
"--input",
help="Input filename to process (default samples/sample.wav)",
type=str,
required=False,
default="samples/sample.wav",
)
parser.add_argument(
"-t",
"--latitude",
help="Latitude (default 40.57)",
type=str,
required=False,
default="40.57",
)
parser.add_argument(
"-n",
"--longitude",
help="Longitude (default -105.23)",
type=str,
required=False,
default="-105.23",
)
parser.add_argument(
"-y",
"--year",
2023-09-24 10:10:36 -06:00
help="Year (default today)",
2023-09-21 14:41:14 -06:00
type=int,
required=False,
2023-09-24 10:10:36 -06:00
default=YEAR,
2023-09-21 14:41:14 -06:00
)
parser.add_argument(
"-m",
"--month",
2023-09-24 10:10:36 -06:00
help="Month (default today)",
2023-09-21 14:41:14 -06:00
type=int,
required=False,
2023-09-24 10:10:36 -06:00
default=MONTH,
2023-09-21 14:41:14 -06:00
)
parser.add_argument(
"-d",
"--day",
2023-09-24 10:10:36 -06:00
help="Day (default today)",
2023-09-21 14:41:14 -06:00
type=int,
required=False,
2023-09-24 10:10:36 -06:00
default=DAY,
2023-09-21 14:41:14 -06:00
)
parser.add_argument(
"-c",
"--confidence",
help="Minimum Confidence (default 0.50)",
type=float,
required=False,
default="0.50",
)
2023-09-24 10:32:28 -06:00
parser.add_argument(
"-l",
"--list",
help="Output as human readable list not terse (default True)",
action=argparse.BooleanOptionalAction,
type=bool,
required=False,
default=True,
)
2023-09-21 14:41:14 -06:00
args = parser.parse_args()
INFILE = args.input
LAT = args.latitude
LON = args.longitude
YEAR = args.year
MONTH = args.month
DAY = args.day
CONFIDENCE = args.confidence
2023-09-24 10:32:28 -06:00
LIST = args.list
2023-09-21 13:49:58 -06:00
2023-09-24 10:10:36 -06:00
f = io.StringIO()
with redirect_stdout(f), redirect_stderr(f):
analyzer = Analyzer()
2023-09-21 13:49:58 -06:00
recording = Recording(
analyzer,
2023-09-21 14:41:14 -06:00
INFILE,
lat=LAT,
lon=LON,
date=datetime(year=YEAR, month=MONTH, day=DAY),
min_conf=CONFIDENCE,
2023-09-21 13:49:58 -06:00
)
2023-09-24 10:10:36 -06:00
f = io.StringIO()
with redirect_stdout(f), redirect_stderr(f):
recording.analyze()
2023-09-24 10:32:28 -06:00
if LIST:
i = 0
species_sort = ""
for i in range(0, len(recording.detections)):
species_sort = species_sort + (
recording.detections[i]["common_name"]
+ ", "
+ recording.detections[i]["scientific_name"]
+ ", "
+ str(recording.detections[i]["start_time"])
+ ", "
+ str(recording.detections[i]["end_time"])
+ ", "
+ str(recording.detections[i]["confidence"])
+ "\n"
)
species_out = sorted(species_sort.split("\n"))
i = 0
for i in range(1, len(species_out)):
print(species_out[i])
else:
print(recording.detections)