more efficient birndet_analysis.service

more efficient extraction.service
cleaner birdnet_analysis logging
cleaner birdnet.conf
functioning species_notifier
This commit is contained in:
Patrick McGuire
2021-09-30 19:23:13 -04:00
parent 9de946d72d
commit 010dec34ee
9 changed files with 40 additions and 68 deletions
+29 -34
View File
@@ -1,17 +1,14 @@
#!/usr/bin/env bash
# Runs BirdNET in virtual environment
# Runs BirdNET-Lite
#set -x
source /etc/birdnet/birdnet.conf
CUSTOM_LIST="/home/pi/BirdNET-Lite/custom_species_list.txt"
DAYS=(
"today"
)
# Create an array of the day's audio files
# Uses 1st argument:
# Create an array of the audio files
# Takes one argument:
# - {DIRECTORY}
get_files() {
echo "Starting get_files() for ${1}"
echo "get_files() for ${1:19}"
files=($( find ${1} -maxdepth 1 -name '*wav' \
| sort \
| awk -F "/" '{print $NF}' ))
@@ -20,33 +17,33 @@ get_files() {
# Move all files that have been analyzed already into newly created "Analyzed"
# directory
# Uses 1st argument:
# Takes one argument:
# - {DIRECTORY}
move_analyzed() {
echo "Starting move_analyzed() for ${1}"
echo "Starting move_analyzed() for ${1:19}"
for i in "${files[@]}";do
j="$(echo "${i}" | cut -d'.' -f1-2).csv"
if [ -f "${1}/${j}" ];then
if [ ! -d "${1}-Analyzed" ];then
mkdir -vvvvvvvp "${1}-Analyzed" && echo "'Analyzed' directory created"
j="${i}.csv"
if [ -f "${1}/${j}" ];then
if [ ! -d "${1}-Analyzed" ];then
mkdir -p "${1}-Analyzed" && echo "'Analyzed' directory created"
fi
echo "Moving analyzed files to new directory"
mv "${1}/${i}" "${1}-Analyzed/"
mv "${1}/${j}" "${1}-Analyzed/"
fi
echo "Moving analyzed files to new directory"
mv "${1}/${i}" "${1}-Analyzed/"
mv "${1}/${j}" "${1}-Analyzed/"
fi
done
done
}
# Run BirdNET analysis on the remaining WAVE files for the day
# Uses 1st and 2nd arguments:
# Run BirdNET-Lite on the WAVE files from get_files()
# Uses one argument:
# - {DIRECTORY}
# - {"today", "yesterday", "2 days ago",...}
run_analysis() {
echo "Starting run_analysis() for ${1}"
echo "Starting run_analysis() for ${1:19}"
WEEK=$(date --date="${2}" +"%U")
cd ${HOME}/BirdNET-Lite || exit 1
for i in "${files[@]}";do
if [ -f ${1}/${i} ] && [ ! -f ${CUSTOM_LIST} ];then
set -x
python3 analyze.py \
--i "${1}/${i}" \
--o "${1}/${i}.csv" \
@@ -55,7 +52,9 @@ run_analysis() {
--week "${WEEK}" \
--overlap "${OVERLAP}" \
--min_conf "${CONFIDENCE}"
set +x
elif [ -f ${1}/${i} ] && [ -f ${CUSTOM_LIST} ];then
set -x
python3 analyze.py \
--i "${1}/${i}" \
--o "${1}/${i}.csv" \
@@ -65,6 +64,7 @@ run_analysis() {
--overlap "${OVERLAP}" \
--min_conf "${CONFIDENCE}" \
--custom_list "${CUSTOM_LIST}"
set +x
fi
done
}
@@ -74,22 +74,17 @@ run_analysis() {
# - {DIRECTORY}
# - {"today", "yesterday", "2 days ago",...}
run_birdnet() {
echo "Starting run_birdnet() in \"${1}\" for \""${2}"\""
sleep 1
echo "Starting run_birdnet() for \"${1:19}\""
get_files "${1}"
sleep 1
move_analyzed "${1}"
sleep 1
run_analysis "${1}" "${2}"
run_analysis "${1}"
}
if [ $(find ${RECS_DIR} -maxdepth 1 -name '*wav' | wc -l) -gt 0 ];then
run_birdnet "${RECS_DIR}" "today"
run_birdnet "${RECS_DIR}"
fi
for i in ${!DAYS[@]};do
DIRECTORY="$RECS_DIR/$(date --date="${DAYS[$i]}" "+%B-%Y/%d-%A")"
if [ $(find ${DIRECTORY} -name '*wav' | wc -l) -gt 0 ];then
run_birdnet "${DIRECTORY}" "${DAYS[$i]}"
fi
done
DIRECTORY="$RECS_DIR/$(date "+%B-%Y/%d-%A")"
if [ $(find ${DIRECTORY} -name '*wav' | wc -l) -gt 0 ];then
run_birdnet "${DIRECTORY}"
fi
+2 -2
View File
@@ -12,10 +12,10 @@ if pgrep arecord &> /dev/null ;then
echo "Recording"
else
if [ -z ${REC_CARD} ];then
arecord -f S16_LE -c${CHANNELS} -r48000 -t wav --max-file-time 9\
arecord -f S16_LE -c${CHANNELS} -r48000 -t wav --max-file-time 6\
--use-strftime ${RECS_DIR}/%B-%Y/%d-%A/%F-birdnet-${STAMP}.wav
else
arecord -f S16_LE -c${CHANNELS} -r48000 -t wav --max-file-time 9\
arecord -f S16_LE -c${CHANNELS} -r48000 -t wav --max-file-time 6\
-D "${REC_CARD}" --use-strftime \
${RECS_DIR}/%B-%Y/%d-%A/%F-birdnet-${STAMP}.wav
fi
+1
View File
@@ -13,6 +13,7 @@ source /etc/birdnet/birdnet.conf
# Set Variables
TMPFILE=$(mktemp)
ANALYZED=${RECS_DIR}/*/*Analyzed
# SCAN_DIRS are all directories marked "Analyzed"
SCAN_DIRS=($(find ${ANALYZED} -type d | sort ))
+1 -9
View File
@@ -411,15 +411,7 @@ CONFIDENCE="0.7"
CHANNELS=${CHANNELS}
# Don't touch the three below
## ANALYZED is where the extraction.service looks for audio and
## BirdNET.selection.txt files after they have been processed by the
## birdnet_analysis.service. This is NOT where the analyzed files are moved --
## analyzed files are always created within the same directory
## birdnet_analysis.service finds them.
ANALYZED=${RECS_DIR}/*/*Analyzed
# Don't touch the variables below
## SYSTEMD_MOUNT is created from the RECS_DIR variable to comply with systemd
## mount naming requirements.
+1 -1
View File
@@ -23,7 +23,7 @@ Description=BirdNET Analysis
Restart=always
RuntimeMaxSec=10800
Type=simple
RestartSec=1
RestartSec=2
User=${USER}
ExecStart=/usr/local/bin/birdnet_analysis.sh
[Install]
+4 -3
View File
@@ -10,7 +10,7 @@ TMPFILE=$(mktemp)
[ -f ${IDFILE} ] || touch ${IDFILE}
cat "${IDFILE}" > "${TMPFILE}"
/usr/local/bin/update_species.sh &> /dev/null
/usr/local/bin/update_species.sh > /dev/null
if ! diff "${IDFILE}" "${TMPFILE}" &> /dev/null; then
SPECIES=("$(diff "${IDFILE}" "${TMPFILE}" \
@@ -19,9 +19,10 @@ if ! diff "${IDFILE}" "${TMPFILE}" &> /dev/null; then
| awk '{for(i=4;i<=NF;++i)printf $i""FS ; print ""}')")
NOTIFICATION="New Species Detection: "${SPECIES[@]}""
echo "Sending the following notification:
${NOTIFICATION}"
if [ ! -z ${PUSHED_APP_KEY} ];then
curl -X POST -s \
curl -X POST \
--form-string "app_key=${PUSHED_APP_KEY}" \
--form-string "app_secret=${PUSHED_APP_SECRET}" \
--form-string "target_type=app" \
-2
View File
@@ -9,8 +9,6 @@ TMPFILE=$(mktemp) || exit 1
[ -f ${IDFILE} ] || touch ${IDFILE}
IDFILEBAKUP="${IDFILE}.bak"
if [ $(find ${PROCESSED} -name '*csv' | wc -l) -ge 1 ];then
sort $(find ${PROCESSED} ${ANALYZED} ${EXTRACTED} -name '*csv') \
| awk -F\; '!/Scientific/ {print"Common Name: " $4 "\nScientific Name: " $3""}' \