Skip to content

A live bird collage card for your Home Assistant dashboard.

fix: write json file: spectrogram.php needs it

frederik1f7aad78739d
diff --git a/scripts/birdnet_analysis.py b/scripts/birdnet_analysis.py
index ec34132a..e34aaf11 100644
--- a/scripts/birdnet_analysis.py
+++ b/scripts/birdnet_analysis.py
@@ -13,7 +13,8 @@ from inotify.constants import IN_CLOSE_WRITE
from server import load_global_model, run_analysis
from utils.helpers import get_settings, ParseFileName, get_wav_files, write_settings, ANALYZING_NOW
-from utils.reporting import extract_detection, summary, write_to_file, write_to_db, apprise, bird_weather, heartbeat
+from utils.reporting import extract_detection, summary, write_to_file, write_to_db, apprise, bird_weather, heartbeat, \
+ update_json_file
shutdown = False
@@ -108,6 +109,7 @@ def handle_reporting_queue(queue):
file, detections = msg
try:
+ update_json_file(file, detections)
for detection in detections:
detection.file_name_extr = extract_detection(file, detection)
log.info('%s;%s', summary(file, detection), os.path.basename(detection.file_name_extr))
diff --git a/scripts/utils/reporting.py b/scripts/utils/reporting.py
index 8e3dd749..56bc23c8 100644
--- a/scripts/utils/reporting.py
+++ b/scripts/utils/reporting.py
@@ -1,5 +1,7 @@
import datetime
+import glob
import gzip
+import json
import logging
import os
import sqlite3
@@ -114,6 +116,29 @@ def write_to_file(file: ParseFileName, detection: Detection):
rfile.write(f'{summary(file, detection)}\n')
+def update_json_file(file: ParseFileName, detections: [Detection]):
+ if file.RTSP_id is None:
+ mask = f'{os.path.dirname(file.file_name)}/*.json'
+ else:
+ mask = f'{os.path.dirname(file.file_name)}/*{file.RTSP_id}*.json'
+ for f in glob.glob(mask):
+ log.debug(f'deleting {f}')
+ os.remove(f)
+ write_to_json_file(file, detections)
+
+
+def write_to_json_file(file: ParseFileName, detections: [Detection]):
+ conf = get_settings()
+ json_file = f'{file.file_name}.json'
+ log.debug(f'WRITING RESULTS TO {json_file}')
+ dets = {'file_name': os.path.basename(json_file), 'timestamp': file.iso8601, 'delay': conf['RECORDING_LENGTH'],
+ 'detections': [{"start": det.start, "common_name": det.common_name, "confidence": det.confidence} for det in
+ detections]}
+ with open(json_file, 'w') as rfile:
+ rfile.write(json.dumps(dets))
+ log.debug(f'DONE! WROTE {len(detections)} RESULTS.')
+
+
def apprise(file: ParseFileName, detections: [Detection]):
species_apprised_this_run = []
conf = get_settings()