fix: write json file: spectrogram.php needs it

This commit is contained in:
frederik
2024-03-11 12:24:20 +01:00
parent 5c3b2d5691
commit 1f7aad7873
2 changed files with 28 additions and 1 deletions
+3 -1
View File
@@ -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))
+25
View File
@@ -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()