From 30465124c5d2f371650e201094965abbd1cb9852 Mon Sep 17 00:00:00 2001 From: Thibaut Voirand Date: Sat, 30 Nov 2024 15:44:50 +0100 Subject: [PATCH] Remove Detection constructor dependency on temporary audio file This allows to decorrelate the Detection class and the .wav audio files that are temporarily stored in the "StreamData" folder. --- scripts/server.py | 5 ++--- scripts/utils/helpers.py | 15 +++++++-------- scripts/utils/reporting.py | 27 +++++++++++++++++++-------- 3 files changed, 28 insertions(+), 19 deletions(-) diff --git a/scripts/server.py b/scripts/server.py index 85522349..5b673c61 100644 --- a/scripts/server.py +++ b/scripts/server.py @@ -339,9 +339,8 @@ def run_analysis(file): log.warning("Excluded as below Species Occurrence Frequency Threshold: %s", entry[0]) else: d = Detection( - file.file_date, - time_slot.split(';')[0], - time_slot.split(';')[1], + file.file_date + datetime.timedelta(seconds=float(time_slot.split(';')[0])), + file.file_date + datetime.timedelta(seconds=float(time_slot.split(';')[1])), entry[0], entry[1], ) diff --git a/scripts/utils/helpers.py b/scripts/utils/helpers.py index 864f0157..0ef50ff7 100644 --- a/scripts/utils/helpers.py +++ b/scripts/utils/helpers.py @@ -42,14 +42,13 @@ def get_settings(settings_path='/etc/birdnet/birdnet.conf', force_reload=False): class Detection: - def __init__(self, file_date, start_time, stop_time, species, confidence): - self.start = float(start_time) - self.stop = float(stop_time) - self.datetime = file_date + datetime.timedelta(seconds=self.start) - self.date = self.datetime.strftime("%Y-%m-%d") - self.time = self.datetime.strftime("%H:%M:%S") - self.iso8601 = self.datetime.astimezone(get_localzone()).isoformat() - self.week = self.datetime.isocalendar()[1] + def __init__(self, start_datetime, stop_datetime, species, confidence): + self.start_datetime = start_datetime + self.stop_datetime = stop_datetime + self.date = self.start_datetime.strftime("%Y-%m-%d") + self.time = self.start_datetime.strftime("%H:%M:%S") + self.iso8601 = self.start_datetime.astimezone(get_localzone()).isoformat() + self.week = self.start_datetime.isocalendar()[1] self.confidence = round(float(confidence), 4) self.confidence_pct = round(self.confidence * 100) self.species = species diff --git a/scripts/utils/reporting.py b/scripts/utils/reporting.py index ac0e0099..ea8f920c 100644 --- a/scripts/utils/reporting.py +++ b/scripts/utils/reporting.py @@ -70,7 +70,12 @@ def extract_detection(file: ParseFileName, detection: Detection): log.warning('Extraction exists. Moving on: %s', new_file) else: os.makedirs(new_dir, exist_ok=True) - extract_safe(file.file_name, new_file, detection.start, detection.stop) + extract_safe( + file.file_name, + new_file, + (detection.start_datetime - file.file_date).seconds, + (detection.stop_datetime - file.file_date).seconds, + ) spectrogram(new_file, detection.common_name, new_file.replace(os.path.expanduser('~/'), '')) return new_file @@ -130,7 +135,7 @@ def write_to_json_file(file: ParseFileName, detections: [Detection]): 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': [{"start": (det.start_datetime - file.file_date).seconds, "common_name": det.common_name, "confidence": det.confidence} for det in detections]} with open(json_file, 'w') as rfile: rfile.write(json.dumps(dets)) @@ -184,12 +189,18 @@ def bird_weather(file: ParseFileName, detections: [Detection]): # POST detection to server detection_url = f'https://app.birdweather.com/api/v1/stations/{conf["BIRDWEATHER_ID"]}/detections' - data = {'timestamp': detection.iso8601, 'lat': conf['LATITUDE'], 'lon': conf['LONGITUDE'], - 'soundscapeId': soundscape_id, - 'soundscapeStartTime': detection.start, 'soundscapeEndTime': detection.stop, - 'commonName': detection.common_name, 'scientificName': detection.scientific_name, - 'algorithm': '2p4' if conf['MODEL'] == 'BirdNET_GLOBAL_6K_V2.4_Model_FP16' else 'alpha', - 'confidence': detection.confidence} + data = { + 'timestamp': detection.iso8601, + 'lat': conf['LATITUDE'], + 'lon': conf['LONGITUDE'], + 'soundscapeId': soundscape_id, + 'soundscapeStartTime': (detection.start_datetime - file.file_date).seconds, + 'soundscapeEndTime': (detection.stop_datetime - file.file_date).seconds, + 'commonName': detection.common_name, + 'scientificName': detection.scientific_name, + 'algorithm': '2p4' if conf['MODEL'] == 'BirdNET_GLOBAL_6K_V2.4_Model_FP16' else 'alpha', + 'confidence': detection.confidence, + } log.debug(data) try: