Linting scripts/server.py
This commit is contained in:
+104
-69
@@ -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 socket
|
||||||
import threading
|
import threading
|
||||||
import os
|
import os
|
||||||
@@ -6,26 +18,9 @@ os.environ['CUDA_VISIBLE_DEVICES'] = ''
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
import tflite_runtime.interpreter as tflite
|
import tflite_runtime.interpreter as tflite
|
||||||
except:
|
except BaseException:
|
||||||
from tensorflow import lite as tflite
|
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
|
HEADER = 64
|
||||||
PORT = 5050
|
PORT = 5050
|
||||||
@@ -37,18 +32,17 @@ DISCONNECT_MESSAGE = "!DISCONNECT"
|
|||||||
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
try:
|
try:
|
||||||
server.bind(ADDR)
|
server.bind(ADDR)
|
||||||
except:
|
except BaseException:
|
||||||
print("Waiting on socket")
|
print("Waiting on socket")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Open most recent Configuration and grab DB_PWD as a python variable
|
# Open most recent Configuration and grab DB_PWD as a python variable
|
||||||
userDir = os.path.expanduser('~')
|
userDir = os.path.expanduser('~')
|
||||||
with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
|
with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
|
||||||
this_run = f.readlines()
|
this_run = f.readlines()
|
||||||
audiofmt = "." + str(str(str([i for i in this_run if i.startswith('AUDIOFMT')]).split('=')[1]).split('\\')[0])
|
audiofmt = "." + str(str(str([i for i in this_run if i.startswith('AUDIOFMT')]).split('=')[1]).split('\\')[0])
|
||||||
priv_thresh = float("." + str(str(str([i for i in this_run if i.startswith('PRIVACY_THRESHOLD')]).split('=')[1]).split('\\')[0]))/10
|
priv_thresh = float("." + str(str(str([i for i in this_run if i.startswith('PRIVACY_THRESHOLD')]).split('=')[1]).split('\\')[0])) / 10
|
||||||
|
|
||||||
|
|
||||||
def loadModel():
|
def loadModel():
|
||||||
@@ -62,7 +56,7 @@ def loadModel():
|
|||||||
|
|
||||||
# Load TFLite model and allocate tensors.
|
# Load TFLite model and allocate tensors.
|
||||||
modelpath = userDir + '/BirdNET-Pi/model/BirdNET_6K_GLOBAL_MODEL.tflite'
|
modelpath = userDir + '/BirdNET-Pi/model/BirdNET_6K_GLOBAL_MODEL.tflite'
|
||||||
myinterpreter = tflite.Interpreter(model_path=modelpath,num_threads=2)
|
myinterpreter = tflite.Interpreter(model_path=modelpath, num_threads=2)
|
||||||
myinterpreter.allocate_tensors()
|
myinterpreter.allocate_tensors()
|
||||||
|
|
||||||
# Get input and output tensors.
|
# Get input and output tensors.
|
||||||
@@ -85,6 +79,7 @@ def loadModel():
|
|||||||
|
|
||||||
return myinterpreter
|
return myinterpreter
|
||||||
|
|
||||||
|
|
||||||
def loadCustomSpeciesList(path):
|
def loadCustomSpeciesList(path):
|
||||||
|
|
||||||
slist = []
|
slist = []
|
||||||
@@ -95,6 +90,7 @@ def loadCustomSpeciesList(path):
|
|||||||
|
|
||||||
return slist
|
return slist
|
||||||
|
|
||||||
|
|
||||||
def splitSignal(sig, rate, overlap, seconds=3.0, minlen=1.5):
|
def splitSignal(sig, rate, overlap, seconds=3.0, minlen=1.5):
|
||||||
|
|
||||||
# Split signal with overlap
|
# Split signal with overlap
|
||||||
@@ -116,6 +112,7 @@ def splitSignal(sig, rate, overlap, seconds=3.0, minlen=1.5):
|
|||||||
|
|
||||||
return sig_splits
|
return sig_splits
|
||||||
|
|
||||||
|
|
||||||
def readAudioData(path, overlap, sample_rate=48000):
|
def readAudioData(path, overlap, sample_rate=48000):
|
||||||
|
|
||||||
print('READING AUDIO DATA...', end=' ', flush=True)
|
print('READING AUDIO DATA...', end=' ', flush=True)
|
||||||
@@ -130,6 +127,7 @@ def readAudioData(path, overlap, sample_rate=48000):
|
|||||||
|
|
||||||
return chunks
|
return chunks
|
||||||
|
|
||||||
|
|
||||||
def convertMetadata(m):
|
def convertMetadata(m):
|
||||||
|
|
||||||
# Convert week to cosine
|
# Convert week to cosine
|
||||||
@@ -147,9 +145,11 @@ def convertMetadata(m):
|
|||||||
|
|
||||||
return np.concatenate([m, mask])
|
return np.concatenate([m, mask])
|
||||||
|
|
||||||
|
|
||||||
def custom_sigmoid(x, sensitivity=1.0):
|
def custom_sigmoid(x, sensitivity=1.0):
|
||||||
return 1 / (1.0 + np.exp(-sensitivity * x))
|
return 1 / (1.0 + np.exp(-sensitivity * x))
|
||||||
|
|
||||||
|
|
||||||
def predict(sample, sensitivity):
|
def predict(sample, sensitivity):
|
||||||
global INTERPRETER
|
global INTERPRETER
|
||||||
# Make a prediction
|
# Make a prediction
|
||||||
@@ -167,20 +167,21 @@ def predict(sample, sensitivity):
|
|||||||
# Sort by score
|
# Sort by score
|
||||||
p_sorted = sorted(p_labels.items(), key=operator.itemgetter(1), reverse=True)
|
p_sorted = sorted(p_labels.items(), key=operator.itemgetter(1), reverse=True)
|
||||||
|
|
||||||
# #print("DATABASE SIZE:", len(p_sorted))
|
# # print("DATABASE SIZE:", len(p_sorted))
|
||||||
# #print("HUMAN-CUTOFF AT:", int(len(p_sorted)*priv_thresh)/10)
|
# # print("HUMAN-CUTOFF AT:", int(len(p_sorted)*priv_thresh)/10)
|
||||||
#
|
#
|
||||||
# # Remove species that are on blacklist
|
# # Remove species that are on blacklist
|
||||||
|
|
||||||
human_cutoff = max(10,int(len(p_sorted)*priv_thresh))
|
human_cutoff = max(10, int(len(p_sorted) * priv_thresh))
|
||||||
|
|
||||||
for i in range(min(10, len(p_sorted))):
|
for i in range(min(10, len(p_sorted))):
|
||||||
if p_sorted[i][0]=='Human_Human':
|
if p_sorted[i][0] == 'Human_Human':
|
||||||
with open(userDir + '/BirdNET-Pi/HUMAN.txt', 'a') as rfile:
|
with open(userDir + '/BirdNET-Pi/HUMAN.txt', 'a') as rfile:
|
||||||
rfile.write(str(datetime.datetime.now())+str(p_sorted[i])+ ' ' + str(human_cutoff)+ '\n')
|
rfile.write(str(datetime.datetime.now()) + str(p_sorted[i]) + ' ' + str(human_cutoff) + '\n')
|
||||||
|
|
||||||
return p_sorted[:human_cutoff]
|
return p_sorted[:human_cutoff]
|
||||||
|
|
||||||
|
|
||||||
def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
||||||
global INTERPRETER
|
global INTERPRETER
|
||||||
|
|
||||||
@@ -202,19 +203,19 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
|||||||
# Make prediction
|
# Make prediction
|
||||||
p = predict([sig, mdata], sensitivity)
|
p = predict([sig, mdata], sensitivity)
|
||||||
# print("PPPPP",p)
|
# print("PPPPP",p)
|
||||||
HUMAN_DETECTED=False
|
HUMAN_DETECTED = False
|
||||||
|
|
||||||
#Catch if Human is recognized
|
# Catch if Human is recognized
|
||||||
for x in range(len(p)):
|
for x in range(len(p)):
|
||||||
if "Human" in p[x][0]:
|
if "Human" in p[x][0]:
|
||||||
HUMAN_DETECTED=True
|
HUMAN_DETECTED = True
|
||||||
|
|
||||||
# Save result and timestamp
|
# Save result and timestamp
|
||||||
pred_end = pred_start + 3.0
|
pred_end = pred_start + 3.0
|
||||||
|
|
||||||
#If human detected set all detections to human to make sure voices are not saved
|
# 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
|
p = [('Human_Human', 0.0)] * 10
|
||||||
|
|
||||||
detections[str(pred_start) + ';' + str(pred_end)] = p
|
detections[str(pred_start) + ';' + str(pred_end)] = p
|
||||||
|
|
||||||
@@ -224,7 +225,8 @@ def analyzeAudioData(chunks, lat, lon, week, sensitivity, overlap,):
|
|||||||
# print('DETECTIONS:::::',detections)
|
# print('DETECTIONS:::::',detections)
|
||||||
return detections
|
return detections
|
||||||
|
|
||||||
def sendAppriseNotifications(species,confidence):
|
|
||||||
|
def sendAppriseNotifications(species, confidence):
|
||||||
if os.path.exists(userDir + '/BirdNET-Pi/apprise.txt') and os.path.getsize(userDir + '/BirdNET-Pi/apprise.txt') > 0:
|
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:
|
with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
|
||||||
this_run = f.readlines()
|
this_run = f.readlines()
|
||||||
@@ -239,7 +241,7 @@ def sendAppriseNotifications(species,confidence):
|
|||||||
apobj.add(config)
|
apobj.add(config)
|
||||||
|
|
||||||
apobj.notify(
|
apobj.notify(
|
||||||
body=body.replace("$sciname",species.split("_")[0]).replace("$comname",species.split("_")[1]).replace("$confidence",confidence),
|
body=body.replace("$sciname", species.split("_")[0]).replace("$comname", species.split("_")[1]).replace("$confidence", confidence),
|
||||||
title=title,
|
title=title,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -250,10 +252,10 @@ def sendAppriseNotifications(species,confidence):
|
|||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("SELECT DISTINCT(Com_Name) FROM detections")
|
cur.execute("SELECT DISTINCT(Com_Name) FROM detections")
|
||||||
known_species = cur.fetchall()
|
known_species = cur.fetchall()
|
||||||
sciName,comName = species.split("_")
|
sciName, comName = species.split("_")
|
||||||
|
|
||||||
print("\ncomName: ",comName)
|
print("\ncomName: ", comName)
|
||||||
print("\nknown_species: ",known_species)
|
print("\nknown_species: ", known_species)
|
||||||
if comName not in known_species:
|
if comName not in known_species:
|
||||||
apobj = apprise.Apprise()
|
apobj = apprise.Apprise()
|
||||||
config = apprise.AppriseConfig()
|
config = apprise.AppriseConfig()
|
||||||
@@ -261,15 +263,16 @@ def sendAppriseNotifications(species,confidence):
|
|||||||
apobj.add(config)
|
apobj.add(config)
|
||||||
|
|
||||||
apobj.notify(
|
apobj.notify(
|
||||||
body=body.replace("$sciname",species.split("_")[0]).replace("$comname",species.split("_")[1]).replace("$confidence",confidence),
|
body=body.replace("$sciname", species.split("_")[0]).replace("$comname", species.split("_")[1]).replace("$confidence", confidence),
|
||||||
title=title,
|
title=title,
|
||||||
)
|
)
|
||||||
|
|
||||||
con.close()
|
con.close()
|
||||||
except:
|
except BaseException:
|
||||||
print("Database busy")
|
print("Database busy")
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
|
|
||||||
def writeResultsToFile(detections, min_conf, path):
|
def writeResultsToFile(detections, min_conf, path):
|
||||||
|
|
||||||
print('WRITING RESULTS TO', path, '...', end=' ')
|
print('WRITING RESULTS TO', path, '...', end=' ')
|
||||||
@@ -278,17 +281,18 @@ def writeResultsToFile(detections, min_conf, path):
|
|||||||
rfile.write('Start (s);End (s);Scientific name;Common name;Confidence\n')
|
rfile.write('Start (s);End (s);Scientific name;Common name;Confidence\n')
|
||||||
for d in detections:
|
for d in detections:
|
||||||
for entry in detections[d]:
|
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) ):
|
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')
|
rfile.write(d + ';' + entry[0].replace('_', ';') + ';' + str(entry[1]) + '\n')
|
||||||
rcnt += 1
|
rcnt += 1
|
||||||
print('DONE! WROTE', rcnt, 'RESULTS.')
|
print('DONE! WROTE', rcnt, 'RESULTS.')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
def handle_client(conn, addr):
|
def handle_client(conn, addr):
|
||||||
global INCLUDE_LIST
|
global INCLUDE_LIST
|
||||||
global EXCLUDE_LIST
|
global EXCLUDE_LIST
|
||||||
#print(f"[NEW CONNECTION] {addr} connected.")
|
# print(f"[NEW CONNECTION] {addr} connected.")
|
||||||
|
|
||||||
connected = True
|
connected = True
|
||||||
while connected:
|
while connected:
|
||||||
@@ -299,7 +303,7 @@ def handle_client(conn, addr):
|
|||||||
if msg == DISCONNECT_MESSAGE:
|
if msg == DISCONNECT_MESSAGE:
|
||||||
connected = False
|
connected = False
|
||||||
else:
|
else:
|
||||||
#print(f"[{addr}] {msg}")
|
# print(f"[{addr}] {msg}")
|
||||||
|
|
||||||
args = type('', (), {})()
|
args = type('', (), {})()
|
||||||
|
|
||||||
@@ -315,7 +319,6 @@ def handle_client(conn, addr):
|
|||||||
args.lat = -1
|
args.lat = -1
|
||||||
args.lon = -1
|
args.lon = -1
|
||||||
|
|
||||||
|
|
||||||
for line in msg.split('||'):
|
for line in msg.split('||'):
|
||||||
inputvars = line.split('=')
|
inputvars = line.split('=')
|
||||||
if inputvars[0] == 'i':
|
if inputvars[0] == 'i':
|
||||||
@@ -341,8 +344,6 @@ def handle_client(conn, addr):
|
|||||||
elif inputvars[0] == 'lon':
|
elif inputvars[0] == 'lon':
|
||||||
args.lon = float(inputvars[1])
|
args.lon = float(inputvars[1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Load custom species lists - INCLUDED and EXCLUDED
|
# Load custom species lists - INCLUDED and EXCLUDED
|
||||||
if not args.include_list == 'null':
|
if not args.include_list == 'null':
|
||||||
INCLUDE_LIST = loadCustomSpeciesList(args.include_list)
|
INCLUDE_LIST = loadCustomSpeciesList(args.include_list)
|
||||||
@@ -360,16 +361,16 @@ def handle_client(conn, addr):
|
|||||||
audioData = readAudioData(args.i, args.overlap)
|
audioData = readAudioData(args.i, args.overlap)
|
||||||
|
|
||||||
# Get Date/Time from filename in case Pi gets behind
|
# Get Date/Time from filename in case Pi gets behind
|
||||||
#now = datetime.now()
|
# now = datetime.now()
|
||||||
full_file_name = args.i
|
full_file_name = args.i
|
||||||
#print('FULL FILENAME: -' + full_file_name + '-')
|
# print('FULL FILENAME: -' + full_file_name + '-')
|
||||||
file_name = Path(full_file_name).stem
|
file_name = Path(full_file_name).stem
|
||||||
file_date = file_name.split('-birdnet-')[0]
|
file_date = file_name.split('-birdnet-')[0]
|
||||||
file_time = file_name.split('-birdnet-')[1]
|
file_time = file_name.split('-birdnet-')[1]
|
||||||
date_time_str = file_date + ' ' + file_time
|
date_time_str = file_date + ' ' + file_time
|
||||||
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
|
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
|
||||||
#print('Date:', date_time_obj.date())
|
# print('Date:', date_time_obj.date())
|
||||||
#print('Time:', date_time_obj.time())
|
# print('Time:', date_time_obj.time())
|
||||||
print('Date-time:', date_time_obj)
|
print('Date-time:', date_time_obj)
|
||||||
now = date_time_obj
|
now = date_time_obj
|
||||||
current_date = now.strftime("%Y-%m-%d")
|
current_date = now.strftime("%Y-%m-%d")
|
||||||
@@ -398,21 +399,21 @@ def handle_client(conn, addr):
|
|||||||
for i in detections:
|
for i in detections:
|
||||||
myReturn += str(i) + '-' + str(detections[i][0]) + '\n'
|
myReturn += str(i) + '-' + str(detections[i][0]) + '\n'
|
||||||
|
|
||||||
|
|
||||||
with open(userDir + '/BirdNET-Pi/BirdDB.txt', 'a') as rfile:
|
with open(userDir + '/BirdNET-Pi/BirdDB.txt', 'a') as rfile:
|
||||||
for d in detections:
|
for d in detections:
|
||||||
for entry in detections[d]:
|
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) ):
|
if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0)
|
||||||
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';') + ';' \
|
and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0)):
|
||||||
+ str(entry[1]) +";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';' \
|
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';') + ';'
|
||||||
+ str(args.sensitivity) +';' + str(args.overlap) + '\n')
|
+ 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)
|
Date = str(current_date)
|
||||||
Time = str(current_time)
|
Time = str(current_time)
|
||||||
species = entry[0]
|
species = entry[0]
|
||||||
Sci_Name,Com_Name = species.split('_')
|
Sci_Name, Com_Name = species.split('_')
|
||||||
score = entry[1]
|
score = entry[1]
|
||||||
Confidence = str(round(score*100))
|
Confidence = str(round(score * 100))
|
||||||
Lat = str(args.lat)
|
Lat = str(args.lat)
|
||||||
Lon = str(args.lon)
|
Lon = str(args.lon)
|
||||||
Cutoff = str(args.min_conf)
|
Cutoff = str(args.min_conf)
|
||||||
@@ -423,28 +424,60 @@ def handle_client(conn, addr):
|
|||||||
File_Name = Com_Name.replace(" ", "_") + '-' + Confidence + '-' + \
|
File_Name = Com_Name.replace(" ", "_") + '-' + Confidence + '-' + \
|
||||||
Date.replace("/", "-") + '-birdnet-' + Time + audiofmt
|
Date.replace("/", "-") + '-birdnet-' + Time + audiofmt
|
||||||
|
|
||||||
#Connect to SQLite Database
|
# Connect to SQLite Database
|
||||||
for attempt_number in range(3):
|
for attempt_number in range(3):
|
||||||
try:
|
try:
|
||||||
con = sqlite3.connect(userDir + '/BirdNET-Pi/scripts/birds.db')
|
con = sqlite3.connect(userDir + '/BirdNET-Pi/scripts/birds.db')
|
||||||
cur = con.cursor()
|
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.commit()
|
||||||
con.close()
|
con.close()
|
||||||
break
|
break
|
||||||
except:
|
except BaseException:
|
||||||
print("Database busy")
|
print("Database busy")
|
||||||
time.sleep(2)
|
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":
|
if birdweather_id != "99999":
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if soundscape_uploaded is False:
|
if soundscape_uploaded is False:
|
||||||
# POST soundscape to server
|
# 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:
|
with open(args.i, 'rb') as f:
|
||||||
wav_data = f.read()
|
wav_data = f.read()
|
||||||
@@ -473,30 +506,32 @@ def handle_client(conn, addr):
|
|||||||
post_confidence = "\"confidence\": " + str(entry[1])
|
post_confidence = "\"confidence\": " + str(entry[1])
|
||||||
post_end = " }"
|
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)
|
print(post_json)
|
||||||
response = requests.post(detection_url, json=json.loads(post_json))
|
response = requests.post(detection_url, json=json.loads(post_json))
|
||||||
print("Detection POST Response Status - ", response.status_code)
|
print("Detection POST Response Status - ", response.status_code)
|
||||||
except:
|
except BaseException:
|
||||||
print("Cannot POST right now")
|
print("Cannot POST right now")
|
||||||
conn.send(myReturn.encode(FORMAT))
|
conn.send(myReturn.encode(FORMAT))
|
||||||
|
|
||||||
#time.sleep(3)
|
# time.sleep(3)
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
||||||
def start():
|
def start():
|
||||||
# Load model
|
# Load model
|
||||||
global INTERPRETER, INCLUDE_LIST, EXCLUDE_LIST
|
global INTERPRETER, INCLUDE_LIST, EXCLUDE_LIST
|
||||||
INTERPRETER = loadModel()
|
INTERPRETER = loadModel()
|
||||||
server.listen()
|
server.listen()
|
||||||
#print(f"[LISTENING] Server is listening on {SERVER}")
|
# print(f"[LISTENING] Server is listening on {SERVER}")
|
||||||
while True:
|
while True:
|
||||||
conn, addr = server.accept()
|
conn, addr = server.accept()
|
||||||
thread = threading.Thread(target=handle_client, args=(conn, addr))
|
thread = threading.Thread(target=handle_client, args=(conn, addr))
|
||||||
thread.start()
|
thread.start()
|
||||||
#print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
|
# print(f"[ACTIVE CONNECTIONS] {threading.activeCount() - 1}")
|
||||||
|
|
||||||
|
|
||||||
#print("[STARTING] server is starting...")
|
# print("[STARTING] server is starting...")
|
||||||
start()
|
start()
|
||||||
|
|||||||
Reference in New Issue
Block a user