A live bird collage card for your Home Assistant dashboard.
Merge branch 'main' into python-lint
Jake Herbst0db7c12ac41d
diff --cc scripts/server.pyindex dbb27f11,6c96e123..715b349c--- a/scripts/server.py+++ b/scripts/server.py@@@ -1,26 -1,32 +1,28 @@@- from pathlib import Path- from tzlocal import get_localzone- import datetime- import sqlite3- import requests- import json- import time- import math- import numpy as np- import librosa-import socket-import threading-import os-os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'-os.environ['CUDA_VISIBLE_DEVICES'] = ''--try:- import tflite_runtime.interpreter as tflite-except:- from tensorflow import lite as tflite-+ import argparseimport operator- import socket- import threading- import os+ import librosa+ import numpy as np+ import math+ import time+ from decimal import Decimal+ import json+ import requests+ import sqlite3+ import datetime+ from time import sleep+ import pytz+ from tzlocal import get_localzone+ from pathlib import Path+ import apprise++os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'+os.environ['CUDA_VISIBLE_DEVICES'] = ''++try:+ import tflite_runtime.interpreter as tflite+except BaseException:+ from tensorflow import lite as tflite-HEADER = 64PORT = 5050SERVER = socket.gethostbyname(socket.gethostname())@@@ -164,16 -166,21 +167,22 @@@ def predict(sample, sensitivity)# Sort by scorep_sorted = sorted(p_labels.items(), key=operator.itemgetter(1), reverse=True)-+- # Remove species that are on blacklist+ # #print("DATABASE SIZE:", len(p_sorted))+ # #print("HUMAN-CUTOFF AT:", int(len(p_sorted)*priv_thresh)/10)-#++#+ # # Remove species that are on blacklist++ human_cutoff = max(10,int(len(p_sorted)*priv_thresh))+for i in range(min(10, len(p_sorted))):- if p_sorted[i][0] in ['Human_Human', 'Non-bird_Non-bird', 'Noise_Noise']:- p_sorted[i] = (p_sorted[i][0], 0.0)+ if p_sorted[i][0]=='Human_Human':+ with open(userDir + '/BirdNET-Pi/HUMAN.txt', 'a') as rfile:+ rfile.write(str(datetime.datetime.now())+str(p_sorted[i])+ ' ' + str(human_cutoff)+ '\n')- # Only return first the top ten results- return p_sorted[:10]+ return p_sorted[:human_cutoff]+def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):global INTERPRETER@@@ -194,16 -201,47 +203,47 @@@# Make predictionp = predict([sig, mdata], sensitivity)+ # print("PPPPP",p)+ HUMAN_DETECTED=False-+++ #Catch if Human is recognized+ for x in range(len(p)):+ if "Human" in p[x][0]:+ HUMAN_DETECTED=True-+# Save result and timestamppred_end = pred_start + 3.0-+++ #If human detected set all detections to human to make sure voices are not saved+ if HUMAN_DETECTED == True:+ p=[('Human_Human',0.0)]*10+detections[str(pred_start) + ';' + str(pred_end)] = p-++pred_start = pred_end - overlapprint('DONE! Time', int((time.time() - start) * 10) / 10.0, 'SECONDS')-+ # print('DETECTIONS:::::',detections)return detections+ def sendAppriseNotifications(species,confidence):+ if os.path.exists(userDir + '/BirdNET-Pi/apprise.txt') and os.path.getsize(userDir + '/BirdNET-Pi/apprise.txt') > 0:+ with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:+ this_run = f.readlines()+ title = str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFICATION_TITLE')]).split('=')[1]).split('\\')[0]).replace('"', '')+ body = str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFICATION_BODY')]).split('=')[1]).split('\\')[0]).replace('"', '')++ if str(str(str([i for i in this_run if i.startswith('APPRISE_NOTIFY_EACH_DETECTION')]).split('=')[1]).split('\\')[0]) == "1":++ apobj = apprise.Apprise()+ config = apprise.AppriseConfig()+ config.add(userDir + '/BirdNET-Pi/apprise.txt')+ apobj.add(config)-+++ apobj.notify(+ body=body.replace("$sciname",species.split("_")[0]).replace("$comname",species.split("_")[1]).replace("$confidence",confidence),+ title=title,+ )def writeResultsToFile(detections, min_conf, path):