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

Adjust timestamps to reflect actual detection time instead of audio file start time #235

Merged
merged 1 commit into from
Nov 20, 2024
Merged
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
8 changes: 7 additions & 1 deletion scripts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ def run_analysis(file):
elif entry[0] not in PREDICTED_SPECIES_LIST and len(PREDICTED_SPECIES_LIST) != 0:
log.warning("Excluded as below Species Occurrence Frequency Threshold: %s", entry[0])
else:
d = Detection(time_slot.split(';')[0], time_slot.split(';')[1], entry[0], entry[1])
d = Detection(
file.file_date,
time_slot.split(';')[0],
time_slot.split(';')[1],
entry[0],
entry[1],
)
confident_detections.append(d)
return confident_detections
17 changes: 6 additions & 11 deletions scripts/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ def get_settings(settings_path='/etc/birdnet/birdnet.conf', force_reload=False):


class Detection:
def __init__(self, start_time, stop_time, species, confidence):
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]
self.confidence = round(float(confidence), 4)
self.confidence_pct = round(self.confidence * 100)
self.species = species
Expand All @@ -66,16 +71,6 @@ def __init__(self, file_name):
ident_match = re.search("RTSP_[0-9]+-", file_name)
self.RTSP_id = ident_match.group() if ident_match is not None else ""

@property
def date(self):
current_date = self.file_date.strftime("%Y-%m-%d")
return current_date

@property
def time(self):
current_time = self.file_date.strftime("%H:%M:%S")
return current_time

@property
def iso8601(self):
current_iso8601 = self.file_date.astimezone(get_localzone()).isoformat()
Expand Down
21 changes: 8 additions & 13 deletions scripts/utils/reporting.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime
import glob
import gzip
import json
Expand All @@ -9,7 +8,6 @@
from time import sleep

import requests
from tzlocal import get_localzone

from .helpers import get_settings, ParseFileName, Detection, DB_PATH
from .notifications import sendAppriseNotifications
Expand Down Expand Up @@ -65,8 +63,8 @@ def spectrogram(in_file, title, comment, raw=False):

def extract_detection(file: ParseFileName, detection: Detection):
conf = get_settings()
new_file_name = f'{detection.common_name_safe}-{detection.confidence_pct}-{file.root}.{conf["AUDIOFMT"]}'
new_dir = os.path.join(conf['EXTRACTED'], 'By_Date', f'{file.date}', f'{detection.common_name_safe}')
new_file_name = f'{detection.common_name_safe}-{detection.confidence_pct}-{detection.date}-birdnet-{detection.time}.{conf["AUDIOFMT"]}'
new_dir = os.path.join(conf['EXTRACTED'], 'By_Date', f'{detection.date}', f'{detection.common_name_safe}')
new_file = os.path.join(new_dir, new_file_name)
if os.path.isfile(new_file):
log.warning('Extraction exists. Moving on: %s', new_file)
Expand All @@ -85,8 +83,8 @@ def write_to_db(file: ParseFileName, detection: Detection):
con = sqlite3.connect(DB_PATH)
cur = con.cursor()
cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
(file.date, file.time, detection.scientific_name, detection.common_name, detection.confidence,
conf['LATITUDE'], conf['LONGITUDE'], conf['CONFIDENCE'], str(file.week), conf['SENSITIVITY'],
(detection.date, detection.time, detection.scientific_name, detection.common_name, detection.confidence,
conf['LATITUDE'], conf['LONGITUDE'], conf['CONFIDENCE'], str(detection.week), conf['SENSITIVITY'],
conf['OVERLAP'], os.path.basename(detection.file_name_extr)))
# (Date, Time, Sci_Name, Com_Name, str(score),
# Lat, Lon, Cutoff, Week, Sens,
Expand All @@ -104,9 +102,9 @@ def summary(file: ParseFileName, detection: Detection):
# Date;Time;Sci_Name;Com_Name;Confidence;Lat;Lon;Cutoff;Week;Sens;Overlap
# 2023-03-03;12:48:01;Phleocryptes melanops;Wren-like Rushbird;0.76950216;-1;-1;0.7;9;1.25;0.0
conf = get_settings()
s = (f'{file.date};{file.time};{detection.scientific_name};{detection.common_name};'
s = (f'{detection.date};{detection.time};{detection.scientific_name};{detection.common_name};'
f'{detection.confidence};'
f'{conf["LATITUDE"]};{conf["LONGITUDE"]};{conf["CONFIDENCE"]};{file.week};{conf["SENSITIVITY"]};'
f'{conf["LATITUDE"]};{conf["LONGITUDE"]};{conf["CONFIDENCE"]};{detection.week};{conf["SENSITIVITY"]};'
f'{conf["OVERLAP"]}')
return s

Expand Down Expand Up @@ -148,7 +146,7 @@ def apprise(file: ParseFileName, detections: [Detection]):
if detection.species not in species_apprised_this_run:
try:
sendAppriseNotifications(detection.species, str(detection.confidence), str(detection.confidence_pct),
os.path.basename(detection.file_name_extr), file.date, file.time, str(file.week),
os.path.basename(detection.file_name_extr), detection.date, detection.time, str(detection.week),
conf['LATITUDE'], conf['LONGITUDE'], conf['CONFIDENCE'], conf['SENSITIVITY'],
conf['OVERLAP'], dict(conf), DB_PATH)
except BaseException as e:
Expand Down Expand Up @@ -186,10 +184,7 @@ 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'

start = file.file_date + datetime.timedelta(seconds=detection.start)
current_iso8601 = start.astimezone(get_localzone()).isoformat()

data = {'timestamp': current_iso8601, 'lat': conf['LATITUDE'], 'lon': conf['LONGITUDE'],
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,
Expand Down
Loading