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