BirdNET-stream/daemon/birdnet_analyzis.sh

102 lines
2.8 KiB
Bash
Raw Normal View History

2022-08-12 18:01:01 +02:00
#! /usr/bin/env bash
set -e
DEBUG=${DEBUG:-1}
debug() {
if [ $DEBUG -eq 1 ]; then
echo "$1"
fi
}
2022-08-12 18:01:01 +02:00
config_filepath="./config/analyzer.conf"
if [ -f "$config_filepath" ]; then
source "$config_filepath"
else
echo "Config file not found: $config_filepath"
exit 1
fi
PYTHON_EXECUTABLE="${PYTHON_VENV}/bin/python3"
check_prerequisites() {
if [[ -z ${LATITUDE} ]]; then
echo "LATITUDE is not set"
exit 1
fi
if [[ -z ${LONGITUDE} ]]; then
echo "LONGITUDE is not set"
exit 1
fi
if [[ -z ${CHUNK_FOLDER} ]]; then
echo "CHUNK_FOLDER is not set"
exit 1
else
if [[ ! -d "${CHUNK_FOLDER}" ]]; then
echo "CHUNK_FOLDER does not exist: ${CHUNK_FOLDER}"
exit 1
else
if [[ ! -d "${CHUNK_FOLDER}/in" ]]; then
echo "Input dir does not exist: ${CHUNK_FOLDER}/in"
exit 1
else
if [[ ! -d "${CHUNK_FOLDER}/out" ]]; then
echo "Output dir does not exist: ${CHUNK_FOLDER}/out"
echo "Creating output dir"
mkdir -p "${CHUNK_FOLDER}/out"
fi
fi
fi
fi
2022-08-13 07:48:06 +02:00
if [[ -z ${SPECIES_LIST} ]]; then
2022-08-12 18:01:01 +02:00
echo "SPECIES_LIST is not set"
exit 1
fi
2022-08-13 07:48:06 +02:00
if [[ -f ${PYTHON_EXECUTABLE} ]]; then
2022-08-12 18:01:01 +02:00
if $verbose; then
2022-08-13 07:48:06 +02:00
echo "Python executable found: ${PYTHON_EXECUTABLE}"
2022-08-12 18:01:01 +02:00
fi
else
2022-08-13 07:48:06 +02:00
echo "Python executable not found: ${PYTHON_EXECUTABLE}"
2022-08-12 18:01:01 +02:00
exit 1
fi
}
# Get array of audio chunks to be processed
get_chunk_list() {
2022-08-13 07:48:06 +02:00
find "${CHUNK_FOLDER}/in" -type f -name '*.wav' -exec basename {} \; ! -size 0 | sort
2022-08-12 18:01:01 +02:00
}
# Perform audio chunk analysis on one chunk
analyze_chunk() {
chunk_name=$1
2022-08-13 07:48:06 +02:00
chunk_path="${CHUNK_FOLDER}/in/$chunk_name"
output_dir="${CHUNK_FOLDER}/out/$chunk_name.d"
2022-08-12 18:01:01 +02:00
mkdir -p "$output_dir"
date=$(echo $chunk_name | cut -d'_' -f2)
week=$(./daemon/weekof.sh $date)
$PYTHON_EXECUTABLE ./analyzer/analyze.py --i $chunk_path --o "$output_dir/model.out.csv" --lat $LATITUDE --lon $LONGITUDE --week $week --min_conf $CONFIDENCE --threads 4 --rtype csv
debug "Model output written to $output_dir/model.out.csv"
2022-08-12 18:01:01 +02:00
}
# Perform audio chunk analysis on all recorded chunks
analyze_chunks() {
for chunk_name in $(get_chunk_list); do
if [[ -f "${CHUNK_FOLDER}/out/$chunk_name.d/model.out.csv" ]]; then
debug "Skipping $chunk_name, as it has already been analyzed"
else
analyze_chunk $chunk_name
fi
2022-08-13 07:48:06 +02:00
chunk_path="${CHUNK_FOLDER}/in/$chunk_name"
mv $chunk_path "${CHUNK_FOLDER}/out/$chunk_name"
2022-08-12 18:01:01 +02:00
done
}
2022-08-16 05:21:53 +02:00
check_prerequisites
2022-08-12 18:01:01 +02:00
# Get list of current chunk in working directory
chunks=$(get_chunk_list)
# Analyze all chunks in working directory
2022-08-13 07:48:06 +02:00
analyze_chunks $chunks