Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Add "processed" folder mode #89

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions scripts/birdnet_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import threading
from queue import Queue
from subprocess import CalledProcessError
import glob
import time

import inotify.adapters
from inotify.constants import IN_CLOSE_WRITE
Expand Down Expand Up @@ -117,7 +119,11 @@ def handle_reporting_queue(queue):
apprise(file, detections)
bird_weather(file, detections)
heartbeat()
os.remove(file.file_name)
processed_size = get_processed_size()
if processed_size > 0:
move_to_processed(file.file_name, processed_size)
else:
os.remove(file.file_name)
except BaseException as e:
stderr = e.stderr.decode('utf-8') if isinstance(e, CalledProcessError) else ""
log.exception(f'Unexpected error: {stderr}', exc_info=e)
Expand All @@ -129,6 +135,23 @@ def handle_reporting_queue(queue):
log.info('handle_reporting_queue done')


def get_processed_size():
try:
processed_size = get_settings('PROCESSED_SIZE')
return processed_size if isinstance(processed_size, int) else 0
except (ValueError, TypeError):
return 0


def move_to_processed(file_name, processed_size):
processed_dir = os.path.join(get_settings()['RECS_DIR'], 'Processed')
os.rename(file_name, os.path.join(processed_dir, os.path.basename(file_name)))
files = glob.glob(os.path.join(processed_dir, '*'))
files.sort(key=os.path.getmtime)
while len(files) > processed_size:
os.remove(files.pop(0))


def setup_logging():
logger = logging.getLogger()
formatter = logging.Formatter("[%(name)s][%(levelname)s] %(message)s")
Expand All @@ -139,7 +162,6 @@ def setup_logging():
global log
log = logging.getLogger('birdnet_analysis')


if __name__ == '__main__':
signal.signal(signal.SIGINT, sig_handler)
signal.signal(signal.SIGTERM, sig_handler)
Expand Down
21 changes: 21 additions & 0 deletions scripts/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function syslog_shell_exec($cmd, $sudo_user = null) {
$flickr_filter_email = $_GET["flickr_filter_email"];
$language = $_GET["language"];
$info_site = $_GET["info_site"];
$processed_size = $_GET["processed_size"];
$timezone = $_GET["timezone"];
$model = $_GET["model"];
$sf_thresh = $_GET["sf_thresh"];
Expand Down Expand Up @@ -156,6 +157,7 @@ function() {
$contents = preg_replace("/FLICKR_API_KEY=.*/", "FLICKR_API_KEY=$flickr_api_key", $contents);
$contents = preg_replace("/DATABASE_LANG=.*/", "DATABASE_LANG=$language", $contents);
$contents = preg_replace("/INFO_SITE=.*/", "INFO_SITE=$info_site", $contents);
$contents = preg_replace("/PROCESSED_SIZE=.*/", "PROCESSED_SIZE=$processed_size", $contents);
$contents = preg_replace("/FLICKR_FILTER_EMAIL=.*/", "FLICKR_FILTER_EMAIL=$flickr_filter_email", $contents);
$contents = preg_replace("/APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=.*/", "APPRISE_MINIMUM_SECONDS_BETWEEN_NOTIFICATIONS_PER_SPECIES=$minimum_time_limit", $contents);
$contents = preg_replace("/MODEL=.*/", "MODEL=$model", $contents);
Expand Down Expand Up @@ -607,6 +609,16 @@ function runProcess() {
</td></tr></table>
<br>

<table class="settingstable"><tr><td>
<h2>Processed folder management </h2>
<label for="processed_size">Amount of files to keep after analysis :</label>
<input name="processed_size" type="number" style="width:6em;" max="90" min="0" step="1" value="<?php print($config['PROCESSED_SIZE']);?>"/>
</td></tr><tr><td>
Processed is the directory where the formerly 'Analyzed' files are moved after extractions, mostly for troubleshooting purposes.<br>
This value defines the maximum amount of files that are kept before replacement with new files.<br>
</td></tr></table>
<br>

<table class="settingstable"><tr><td>
<h2>Additional Info </h2>
<label for="info_site">Site to pull additional species info from: </label>
Expand Down Expand Up @@ -659,6 +671,15 @@ function handleChange(checkbox) {
}
?>
<table class="settingstable"><tr><td>
<h2>Processed folder management </h2>
<label for="processed_size">Amount of files to keep after analysis :</label>
<input name="processed_size" type="number" style="width:6em;" max="90" min="0" step="1" value="<?php print($config['PROCESSED_SIZE']);?>"/>
</td></tr><tr><td>
Processed is the directory where the formerly 'Analyzed' files are moved after extractions, mostly for troubleshooting purposes.<br>
This value defines the maximum amount of files that are kept before replacement with new files.<br>
</td></tr></table>
<br>
<table class="settingstable"><tr><td>
<h2>Time and Date</h2>
<span>If connected to the internet, retrieve time automatically?</span>
<input type="checkbox" onchange='handleChange(this)' <?php echo $checkedvalue; ?> ><br>
Expand Down
2 changes: 2 additions & 0 deletions scripts/install_config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,10 @@ REC_CARD=default
## PROCESSED is the directory where the formerly 'Analyzed' files are moved
## after extractions have been made from them. This includes both WAVE and
## BirdNET.selection.txt files.
## PROCESSED_SIZE is the number of files kept in this directory, default is 0

PROCESSED=$HOME/BirdSongs/Processed
PROCESSED_SIZE=0

## EXTRACTED is the directory where the extracted audio selections are moved.

Expand Down
Loading