Skip to content

Commit

Permalink
Remove Detection constructor dependency on temporary audio file
Browse files Browse the repository at this point in the history
This allows to decorrelate the Detection class and the .wav audio files that are temporarily stored in the "StreamData" folder.
  • Loading branch information
tvoirand committed Dec 6, 2024
1 parent 430c5f1 commit 3046512
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
5 changes: 2 additions & 3 deletions scripts/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
)
Expand Down
15 changes: 7 additions & 8 deletions scripts/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 19 additions & 8 deletions scripts/utils/reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3046512

Please sign in to comment.