Various optimisations

This commit is contained in:
Alexandre
2024-06-07 09:24:23 +02:00
parent df0c36d197
commit 3f87a2c32e
9 changed files with 31 additions and 6690 deletions

View File

@@ -1,4 +1,4 @@
- Fix timedatectl for time setting in options
- Fix : time setting in options (don't forget that the timezone is set from addon options)
- New addon option : LIVESTREAM_BOOT_ENABLED enables livestream from boot. Disable to save resources
## 0.13-50 (04-06-2024)

View File

@@ -52,8 +52,8 @@ Options can be configured through three ways :
```yaml
BIRDSONGS_FOLDER: folder to store birdsongs file # It should be an ssd if you want to avoid clogging of analysis
LIVESTREAM_BOOT_ENABLED: start livestream from boot, or from settings
SPECIES_CONVERTER: if yes, you need to put in the file /config/convert_species_list.txt the list of species you want to convert (example : Falco subbuteo_Faucon hobereau;Falco tinnunculus_Faucon Crécerelle). It will convert on the fly the specie when detected
Processed_Buffer : defines the number of last wav files that will be saved in the temporary folder "/tmp/Processed" within the tmpfs (so no disk wear) in case you want to retrieve them. This amount can be adapted from the addon options
SPECIES_CONVERTER_ENABLED: true/false. if enabled, will create a new setting in the birdnet options where you can specify birds to convert. It will convert on the fly the specie when detected
PROCESSED_FOLDER_ENABLED : if enabled, you need to set in the birdnet.conf (or the setting of birdnet) the number of last wav files that will be saved in the temporary folder "/tmp/Processed" within the tmpfs (so no disk wear) in case you want to retrieve them. This amount can be adapted from the addon options
TZ: Etc/UTC specify a timezone to use, see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List
pi_password: set the user password to access the web terminal
localdisks: sda1 #put the hardware name of your drive to mount separated by commas, or its label. ex. sda1, sdb1, MYNAS...

View File

@@ -93,8 +93,8 @@
],
"schema": {
"BIRDSONGS_FOLDER": "str?",
"Processed_Buffer": "int?",
"SPECIES_CONVERTER": "bool?",
"PROCESSED_FOLDER_ENABLED": "bool?",
"SPECIES_CONVERTER_ENABLED": "bool?",
"LIVESTREAM_BOOT_ENABLED": "bool",
"TZ": "str?",
"certfile": "str",

View File

@@ -25,7 +25,7 @@ fi || true
# Add species conversion system
###############################
if bashio::config.true "SPECIES_CONVERTER"; then
if bashio::config.true "SPECIES_CONVERTER_ENABLED"; then
echo "... adding feature of SPECIES_CONVERTER, a new tab is added to your Tools"
touch /config/convert_species_list.txt
chown pi:pi /config/convert_species_list.txt
@@ -87,10 +87,10 @@ if bashio::config.true "SPECIES_CONVERTER"; then
fi
fi || true
exit 0
# Enable the Processed folder
#############################
if ! grep -q "processed_size" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py; then
if bashio::config.true "PROCESSED_FOLDER_ENABLED" && ! grep -q "processed_size" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py; then
echo "... Enabling the Processed folder : the last 15 wav files will be stored there"
# Adapt config.php
sed -i "/GET\[\"info_site\"\]/a\ \$processed_size = \$_GET\[\"processed_size\"\];" "$HOME"/BirdNET-Pi/scripts/config.php
@@ -102,10 +102,10 @@ if ! grep -q "processed_size" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py; th
sed -i "/\"success\"/i </td></tr><tr><td>" "$HOME"/BirdNET-Pi/scripts/config.php
sed -i "/\"success\"/i Processed is the directory where the formerly 'Analyzed' files are moved after extractions, mostly for troubleshooting purposes.<br>" "$HOME"/BirdNET-Pi/scripts/config.php
sed -i "/\"success\"/i This value defines the maximum amount of files that are kept before replacement with new files.<br>" "$HOME"/BirdNET-Pi/scripts/config.php
sed -i "/\"success\"/i </td></tr></table>" "$HOME"/BirdNET-Pi/scripts/config.php
sed -i "/\"success\"/i </td></tr></table>" "$HOME"/BirdNET-Pi/scripts/config.php
sed -i "/\"success\"/i\ <br>" "$HOME"/BirdNET-Pi/scripts/config.php
# Adapt birdnet_analysis.py - move_to_processed
sed -i "/log.info('handle_reporting_queue done')/a\ os.remove(files.pop(0))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
sed -i "/log.info('handle_reporting_queue done')/a\ os.remove(files.pop(0))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
sed -i "/log.info('handle_reporting_queue done')/a\ while len(files) > processed_size:" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
sed -i "/log.info('handle_reporting_queue done')/a\ files.sort(key=os.path.getmtime)" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py
sed -i "/log.info('handle_reporting_queue done')/a\ files = glob.glob(os.path.join(processed_dir, '*'))" "$HOME"/BirdNET-Pi/scripts/birdnet_analysis.py

View File

@@ -57,7 +57,7 @@ bashio::log.info "Starting BirdNET-Pi services"
chmod +x "$HOME"/BirdNET-Pi/scripts/restart_services.sh
"$HOME"/BirdNET-Pi/scripts/restart_services.sh
if bashio::config.true LIVESTREAM_DEFAULT_ENABLED; then
if bashio::config.true LIVESTREAM_BOOT_ENABLED; then
echo "... starting livestream"
sudo systemctl enable icecast2
sudo systemctl start icecast2.service

View File

@@ -1,30 +0,0 @@
# Code to modify the birdnet_analysis.py and restore the Processed folder
# The last 30 wav files are stored in a dynamic manner in the Processed folder
import re
def modify_code(original_file, modified_file):
with open(original_file, 'r') as f:
lines = f.readlines()
# Define the modifications
modifications = {
'os.remove(file.file_name)': 'move_to_processed(file.file_name)',
'def handle_reporting_queue(queue):': 'def handle_reporting_queue(queue):\n conf = get_settings()\n processed_dir = os.path.join(conf[\'RECS_DIR\'], \'Processed\')\n os.makedirs(processed_dir, exist_ok=True)\n user_id = pwd.getpwnam(os.getenv(\'USER\')).pw_uid\n os.chown(processed_dir, user_id, user_id)',
'import threading\nfrom queue import Queue\nfrom subprocess import CalledProcessError': 'import threading\nfrom queue import Queue\nfrom subprocess import CalledProcessError\nimport glob\nimport time\nimport pwd',
'def move_to_processed(file_name):': 'def move_to_processed(file_name):\n conf = get_settings()\n processed_dir = os.path.join(conf[\'RECS_DIR\'], \'Processed\')\n os.rename(file_name, os.path.join(processed_dir, os.path.basename(file_name)))\n files = glob.glob(os.path.join(processed_dir, \'*\'))\n files.sort(key=os.path.getmtime)\n buffer_size = int(os.getenv(\'Processed_Buffer\', 30))\n while len(files) > buffer_size:\n os.remove(files.pop(0))'
}
# Apply the modifications
modified_lines = []
for line in lines:
for original, modified in modifications.items():
line = line.replace(original, modified)
modified_lines.append(line)
# Write the modified code to the new file
with open(modified_file, 'w') as f:
f.writelines(modified_lines)
# Use the function
modify_code('/home/pi/BirdNET-Pi/scripts/birdnet_analysis.py', '/home/pi/BirdNET-Pi/scripts/birdnet_analysis.py')

View File

@@ -1,117 +0,0 @@
#!/usr/bin/env bash
# This scripts allows to change the identification of a Birdnet-pi detection
#################
# SET VARIABLES #
#################
HOME="${HOME:-/home/pi}"
source /etc/birdnet/birdnet.conf &>/dev/null
# Get arguments
OLDNAME="$1" #OLDNAME="Mésange_charbonnière-78-2024-05-02-birdnet-RTSP_1-18:14:08.mp3"
NEWNAME="$2" #NEWNAME="Lapinus atricapilla_Lapinu à tête noire"
# Set log level
OUTPUT_TYPE="${3:-debug}" # Set 3rd argument to debug to have all outputs
# Ask for user input if no arguments
if [ -z "$OLDNAME" ]; then read -r -p 'OLDNAME (finishing by mp3): ' OLDNAME; fi
if [ -z "$NEWNAME" ]; then read -r -p 'NEWNAME (sciname_commoname): ' NEWNAME; fi
# Fixed values
LABELS_FILE="$HOME/BirdNET-Pi/model/labels.txt"
DB_FILE="$HOME/BirdNET-Pi/scripts/birds.db"
DETECTIONS_TABLE="detections"
###################
# VALIDITY CHECKS #
###################
# Check if files exist
if [ ! -f "$LABELS_FILE" ]; then echo "$LABELS_FILE doesn't exist, exiting" && exit 1; fi
if [ ! -f "$DB_FILE" ]; then echo "$DB_FILE doesn't exist, exiting" && exit 1; fi
# Check if inputs are valid
if [[ "$1" != *".mp3" ]]; then
echo "The first argument should be a filename starting with the common name of the bird and finishing by mp3!"
echo "Instead, it is : $1"
exit 1
elif [[ "$2" != *"_"* ]]; then
echo "The second argument should be in the format : \"scientific name_common name\""
echo "Instead, it is : $2"
exit 1
fi
# Check if $NEWNAME is found in the file $LABELS_FILE
if ! grep -q "$NEWNAME" "$LABELS_FILE"; then
echo "Error: $NEWNAME not found in $LABELS_FILE"
exit 1
fi
# Check if the common name as the same as the first
OLDNAME_space="${OLDNAME//_/ }"
if [[ "${OLDNAME_space%%-*}" == "${NEWNAME#*_}" ]]; then
echo "Error: $OLDNAME has the same common name as $NEWNAME"
exit 1
fi
##################
# EXECUTE SCRIPT #
##################
# Intro
[[ "$OUTPUT_TYPE" == "debug" ]] && echo "Starting to modify $OLDNAME to $NEWNAME"
# Get the line where the column "File_Name" matches exactly $OLDNAME
IFS='|' read -r OLDNAME_sciname OLDNAME_comname OLDNAME_date < <(sqlite3 "$DB_FILE" "SELECT Sci_Name, Com_Name, Date FROM $DETECTIONS_TABLE WHERE File_Name = '$OLDNAME' LIMIT 1;")
if [[ -z "$OLDNAME_sciname" ]]; then
echo "Error: No line matching $OLDNAME in $DB_FILE"
exit 1
fi
# Extract the part before the _ from $NEWNAME
NEWNAME_comname="${NEWNAME#*_}"
NEWNAME_sciname="${NEWNAME%%_*}"
# Replace spaces with underscores
NEWNAME_comname_safe="$(echo "$NEWNAME_comname" | tr -d "'" | tr ' ' '_')"
OLDNAME_comname_safe="$(echo "$OLDNAME_comname" | tr -d "'" | tr ' ' '_')"
# Replace OLDNAME_comname_safe with NEWNAME_comname_safe in OLDNAME
NEWNAME_filename="${OLDNAME//$OLDNAME_comname_safe/$NEWNAME_comname_safe}"
[[ "$OUTPUT_TYPE" == "debug" ]] && echo "This script will change the identification $OLDNAME from $OLDNAME_comname to ${NEWNAME#*_}"
########################
# EXECUTE : MOVE FILES #
########################
# Check if the file exists
FILE_PATH="$HOME/BirdSongs/Extracted/By_Date/$OLDNAME_date/$OLDNAME_comname_safe/$OLDNAME"
if [[ -f $FILE_PATH ]]; then
# Ensure the new directory exists
NEW_DIR="$HOME/BirdSongs/Extracted/By_Date/$OLDNAME_date/$NEWNAME_comname_safe"
mkdir -p "$NEW_DIR"
# Move and rename the file
mv "$FILE_PATH" "$NEW_DIR/$NEWNAME_filename"
mv "$FILE_PATH".png "$NEW_DIR/$NEWNAME_filename".png
[[ "$OUTPUT_TYPE" == "debug" ]] && echo "Files moved!"
else
echo "Error: File $FILE_PATH does not exist"
fi
###################################
# EXECUTE : UPDATE DATABASE FILES #
###################################
# Update the database
sqlite3 "$DB_FILE" "UPDATE $DETECTIONS_TABLE SET Sci_Name = '$NEWNAME_sciname', Com_Name = '$NEWNAME_comname', File_Name = '$NEWNAME_filename' WHERE File_Name = '$OLDNAME';"
[[ "$OUTPUT_TYPE" == "debug" ]] && echo "Database entry removed"
[[ "$OUTPUT_TYPE" == "debug" ]] && echo "All done!"

File diff suppressed because it is too large Load Diff

View File

@@ -42,20 +42,30 @@ case "$1" in
;;
esac
;;
"show")
case "$2" in
"--value"|"--property=Timezone")
show_timezone
;;
*)
echo "Invalid argument for show. Use '--value --property=Timezone'."
;;
esac
"show --value --property=Timezone")
show_timezone
;;
"set-timezone")
set_timezone "$2"
;;
*)
echo "Usage: $0 {set-ntp [false|true] | show [--value --property=Timezone] | set-timezone <timezone>}"
# Get values
local_time="$(date)"
utc_time="$(date -u)"
time_zone="$(show_timezone)"
# Check if NTP is used
if sudo systemctl status systemd-timesyncd | grep -q "active"; then
ntp_status="yes"
ntp_service="active"
else
ntp_status="no"
ntp_service="inactive"
fi
# Print the information
echo "Local time: $local_time"
echo "Universal time: $utc_time"
echo "Time zone: $time_zone"
echo "Network time on: $ntp_status"
echo "NTP service: $ntp_service"
;;
esac