From 010dec34eea930e70c1a95faee715ca0ca76d0d1 Mon Sep 17 00:00:00 2001 From: Patrick McGuire Date: Thu, 30 Sep 2021 19:23:13 -0400 Subject: [PATCH] more efficient birndet_analysis.service more efficient extraction.service cleaner birdnet_analysis logging cleaner birdnet.conf functioning species_notifier --- Birders_Guide_Installer.sh | 9 +---- birdnet.conf-defaults | 10 +---- scripts/birdnet_analysis.sh | 63 ++++++++++++++----------------- scripts/birdnet_recording.sh | 4 +- scripts/extract_new_birdsounds.sh | 1 + scripts/install_config.sh | 10 +---- scripts/install_services.sh | 2 +- scripts/species_notifier.sh | 7 ++-- scripts/update_species.sh | 2 - 9 files changed, 40 insertions(+), 68 deletions(-) diff --git a/Birders_Guide_Installer.sh b/Birders_Guide_Installer.sh index 5e045c8..5ab802d 100755 --- a/Birders_Guide_Installer.sh +++ b/Birders_Guide_Installer.sh @@ -390,14 +390,7 @@ CONFIDENCE="0.7" CHANNELS=2 -# Don't 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. diff --git a/birdnet.conf-defaults b/birdnet.conf-defaults index 25784d0..0690d3f 100644 --- a/birdnet.conf-defaults +++ b/birdnet.conf-defaults @@ -218,15 +218,7 @@ CONFIDENCE="0.7" CHANNELS=${CHANNELS} -# Don't 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. diff --git a/scripts/birdnet_analysis.sh b/scripts/birdnet_analysis.sh index 2ff4a37..28d9168 100755 --- a/scripts/birdnet_analysis.sh +++ b/scripts/birdnet_analysis.sh @@ -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 diff --git a/scripts/birdnet_recording.sh b/scripts/birdnet_recording.sh index c975d29..0a67078 100755 --- a/scripts/birdnet_recording.sh +++ b/scripts/birdnet_recording.sh @@ -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 diff --git a/scripts/extract_new_birdsounds.sh b/scripts/extract_new_birdsounds.sh index 946dd1c..0952a21 100755 --- a/scripts/extract_new_birdsounds.sh +++ b/scripts/extract_new_birdsounds.sh @@ -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 )) diff --git a/scripts/install_config.sh b/scripts/install_config.sh index dcf5016..cb116a8 100755 --- a/scripts/install_config.sh +++ b/scripts/install_config.sh @@ -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. diff --git a/scripts/install_services.sh b/scripts/install_services.sh index c86503f..bb829f9 100755 --- a/scripts/install_services.sh +++ b/scripts/install_services.sh @@ -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] diff --git a/scripts/species_notifier.sh b/scripts/species_notifier.sh index c62296a..089a4a9 100755 --- a/scripts/species_notifier.sh +++ b/scripts/species_notifier.sh @@ -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" \ diff --git a/scripts/update_species.sh b/scripts/update_species.sh index c895543..e96ebd2 100755 --- a/scripts/update_species.sh +++ b/scripts/update_species.sh @@ -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""}' \