Linting scripts/server.py
This commit is contained in:
+71
-36
@@ -1,3 +1,15 @@
|
||||
import apprise
|
||||
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 operator
|
||||
import socket
|
||||
import threading
|
||||
import os
|
||||
@@ -6,26 +18,9 @@ os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
||||
|
||||
try:
|
||||
import tflite_runtime.interpreter as tflite
|
||||
except:
|
||||
except BaseException:
|
||||
from tensorflow import lite as tflite
|
||||
|
||||
import argparse
|
||||
import operator
|
||||
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
|
||||
|
||||
|
||||
HEADER = 64
|
||||
PORT = 5050
|
||||
@@ -37,12 +32,11 @@ DISCONNECT_MESSAGE = "!DISCONNECT"
|
||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
try:
|
||||
server.bind(ADDR)
|
||||
except:
|
||||
except BaseException:
|
||||
print("Waiting on socket")
|
||||
time.sleep(5)
|
||||
|
||||
|
||||
|
||||
# Open most recent Configuration and grab DB_PWD as a python variable
|
||||
userDir = os.path.expanduser('~')
|
||||
with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
|
||||
@@ -85,6 +79,7 @@ def loadModel():
|
||||
|
||||
return myinterpreter
|
||||
|
||||
|
||||
def loadCustomSpeciesList(path):
|
||||
|
||||
slist = []
|
||||
@@ -95,6 +90,7 @@ def loadCustomSpeciesList(path):
|
||||
|
||||
return slist
|
||||
|
||||
|
||||
def splitSignal(sig, rate, overlap, seconds=3.0, minlen=1.5):
|
||||
|
||||
# Split signal with overlap
|
||||
@@ -116,6 +112,7 @@ def splitSignal(sig, rate, overlap, seconds=3.0, minlen=1.5):
|
||||
|
||||
return sig_splits
|
||||
|
||||
|
||||
def readAudioData(path, overlap, sample_rate=48000):
|
||||
|
||||
print('READING AUDIO DATA...', end=' ', flush=True)
|
||||
@@ -130,6 +127,7 @@ def readAudioData(path, overlap, sample_rate=48000):
|
||||
|
||||
return chunks
|
||||
|
||||
|
||||
def convertMetadata(m):
|
||||
|
||||
# Convert week to cosine
|
||||
@@ -147,9 +145,11 @@ def convertMetadata(m):
|
||||
|
||||
return np.concatenate([m, mask])
|
||||
|
||||
|
||||
def custom_sigmoid(x, sensitivity=1.0):
|
||||
return 1 / (1.0 + np.exp(-sensitivity * x))
|
||||
|
||||
|
||||
def predict(sample, sensitivity):
|
||||
global INTERPRETER
|
||||
# Make a prediction
|
||||
@@ -181,6 +181,7 @@ def predict(sample, sensitivity):
|
||||
|
||||
return p_sorted[:human_cutoff]
|
||||
|
||||
|
||||
def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
||||
global INTERPRETER
|
||||
|
||||
@@ -213,7 +214,7 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
||||
pred_end = pred_start + 3.0
|
||||
|
||||
# If human detected set all detections to human to make sure voices are not saved
|
||||
if HUMAN_DETECTED == True:
|
||||
if HUMAN_DETECTED is True:
|
||||
p = [('Human_Human', 0.0)] * 10
|
||||
|
||||
detections[str(pred_start) + ';' + str(pred_end)] = p
|
||||
@@ -224,6 +225,7 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
||||
# 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:
|
||||
@@ -266,10 +268,11 @@ def sendAppriseNotifications(species,confidence):
|
||||
)
|
||||
|
||||
con.close()
|
||||
except:
|
||||
except BaseException:
|
||||
print("Database busy")
|
||||
time.sleep(2)
|
||||
|
||||
|
||||
def writeResultsToFile(detections, min_conf, path):
|
||||
|
||||
print('WRITING RESULTS TO', path, '...', end=' ')
|
||||
@@ -279,12 +282,13 @@ def writeResultsToFile(detections, min_conf, path):
|
||||
for d in detections:
|
||||
for entry in detections[d]:
|
||||
if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0)):
|
||||
sendAppriseNotifications(str(entry[0]),str(entry[1]));
|
||||
sendAppriseNotifications(str(entry[0]), str(entry[1]))
|
||||
rfile.write(d + ';' + entry[0].replace('_', ';') + ';' + str(entry[1]) + '\n')
|
||||
rcnt += 1
|
||||
print('DONE! WROTE', rcnt, 'RESULTS.')
|
||||
return
|
||||
|
||||
|
||||
def handle_client(conn, addr):
|
||||
global INCLUDE_LIST
|
||||
global EXCLUDE_LIST
|
||||
@@ -315,7 +319,6 @@ def handle_client(conn, addr):
|
||||
args.lat = -1
|
||||
args.lon = -1
|
||||
|
||||
|
||||
for line in msg.split('||'):
|
||||
inputvars = line.split('=')
|
||||
if inputvars[0] == 'i':
|
||||
@@ -341,8 +344,6 @@ def handle_client(conn, addr):
|
||||
elif inputvars[0] == 'lon':
|
||||
args.lon = float(inputvars[1])
|
||||
|
||||
|
||||
|
||||
# Load custom species lists - INCLUDED and EXCLUDED
|
||||
if not args.include_list == 'null':
|
||||
INCLUDE_LIST = loadCustomSpeciesList(args.include_list)
|
||||
@@ -398,13 +399,13 @@ def handle_client(conn, addr):
|
||||
for i in detections:
|
||||
myReturn += str(i) + '-' + str(detections[i][0]) + '\n'
|
||||
|
||||
|
||||
with open(userDir + '/BirdNET-Pi/BirdDB.txt', 'a') as rfile:
|
||||
for d in detections:
|
||||
for entry in detections[d]:
|
||||
if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0) and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0) ):
|
||||
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';') + ';' \
|
||||
+ str(entry[1]) +";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';' \
|
||||
if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0)
|
||||
and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0)):
|
||||
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';') + ';'
|
||||
+ str(entry[1]) + ";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';'
|
||||
+ str(args.sensitivity) + ';' + str(args.overlap) + '\n')
|
||||
|
||||
Date = str(current_date)
|
||||
@@ -428,23 +429,55 @@ def handle_client(conn, addr):
|
||||
try:
|
||||
con = sqlite3.connect(userDir + '/BirdNET-Pi/scripts/birds.db')
|
||||
cur = con.cursor()
|
||||
cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (Date, Time, Sci_Name, Com_Name, str(score), Lat, Lon, Cutoff, Week, Sens, Overlap, File_Name))
|
||||
cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (Date, Time,
|
||||
Sci_Name, Com_Name, str(score), Lat, Lon, Cutoff, Week, Sens, Overlap, File_Name))
|
||||
|
||||
con.commit()
|
||||
con.close()
|
||||
break
|
||||
except:
|
||||
except BaseException:
|
||||
print("Database busy")
|
||||
time.sleep(2)
|
||||
|
||||
print(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';') + ';' + str(entry[1]) + ';' + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';' + str(args.sensitivity) +';' + str(args.overlap) + Com_Name.replace(" ", "_") + '-' + str(score) + '-' + str(current_date) + '-birdnet-' + str(current_time) + audiofmt + '\n')
|
||||
print(str(current_date) +
|
||||
';' +
|
||||
str(current_time) +
|
||||
';' +
|
||||
entry[0].replace('_', ';') +
|
||||
';' +
|
||||
str(entry[1]) +
|
||||
';' +
|
||||
str(args.lat) +
|
||||
';' +
|
||||
str(args.lon) +
|
||||
';' +
|
||||
str(min_conf) +
|
||||
';' +
|
||||
str(week) +
|
||||
';' +
|
||||
str(args.sensitivity) +
|
||||
';' +
|
||||
str(args.overlap) +
|
||||
Com_Name.replace(" ", "_") +
|
||||
'-' +
|
||||
str(score) +
|
||||
'-' +
|
||||
str(current_date) +
|
||||
'-birdnet-' +
|
||||
str(current_time) +
|
||||
audiofmt +
|
||||
'\n')
|
||||
|
||||
if birdweather_id != "99999":
|
||||
try:
|
||||
|
||||
if soundscape_uploaded is False:
|
||||
# POST soundscape to server
|
||||
soundscape_url = "https://app.birdweather.com/api/v1/stations/" + birdweather_id + "/soundscapes" + "?timestamp=" + current_iso8601
|
||||
soundscape_url = 'https://app.birdweather.com/api/v1/stations/' + \
|
||||
birdweather_id + \
|
||||
'/soundscapes' + \
|
||||
'?timestamp=' + \
|
||||
current_iso8601
|
||||
|
||||
with open(args.i, 'rb') as f:
|
||||
wav_data = f.read()
|
||||
@@ -473,11 +506,12 @@ def handle_client(conn, addr):
|
||||
post_confidence = "\"confidence\": " + str(entry[1])
|
||||
post_end = " }"
|
||||
|
||||
post_json = post_begin + post_timestamp + post_lat + post_lon + post_soundscape_id + post_soundscape_start_time + post_soundscape_end_time + post_commonName + post_scientificName + post_algorithm + post_confidence + post_end
|
||||
post_json = post_begin + post_timestamp + post_lat + post_lon + post_soundscape_id + post_soundscape_start_time + \
|
||||
post_soundscape_end_time + post_commonName + post_scientificName + post_algorithm + post_confidence + post_end
|
||||
print(post_json)
|
||||
response = requests.post(detection_url, json=json.loads(post_json))
|
||||
print("Detection POST Response Status - ", response.status_code)
|
||||
except:
|
||||
except BaseException:
|
||||
print("Cannot POST right now")
|
||||
conn.send(myReturn.encode(FORMAT))
|
||||
|
||||
@@ -485,6 +519,7 @@ def handle_client(conn, addr):
|
||||
|
||||
conn.close()
|
||||
|
||||
|
||||
def start():
|
||||
# Load model
|
||||
global INTERPRETER, INCLUDE_LIST, EXCLUDE_LIST
|
||||
|
||||
Reference in New Issue
Block a user