Improve query accuracy for today's birds
This commit is contained in:
+19
-10
@@ -8,7 +8,6 @@ from datetime import datetime, timedelta
|
|||||||
|
|
||||||
userDir = os.path.expanduser('~')
|
userDir = os.path.expanduser('~')
|
||||||
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
|
APPRISE_CONFIG = userDir + '/BirdNET-Pi/apprise.txt'
|
||||||
THIS_RUN_PATH = userDir + '/BirdNET-Pi/scripts/thisrun.txt'
|
|
||||||
DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db'
|
DB_PATH = userDir + '/BirdNET-Pi/scripts/birds.db'
|
||||||
|
|
||||||
def notify(body, title):
|
def notify(body, title):
|
||||||
@@ -22,7 +21,8 @@ def notify(body, title):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=DB_PATH):
|
def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=DB_PATH):
|
||||||
# print(settings_dict)
|
print(sendAppriseNotifications)
|
||||||
|
print(settings_dict)
|
||||||
if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0:
|
if os.path.exists(APPRISE_CONFIG) and os.path.getsize(APPRISE_CONFIG) > 0:
|
||||||
|
|
||||||
title = settings_dict.get('APPRISE_NOTIFICATION_TITLE')
|
title = settings_dict.get('APPRISE_NOTIFICATION_TITLE')
|
||||||
@@ -48,12 +48,17 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D
|
|||||||
try:
|
try:
|
||||||
con = sqlite3.connect(db_path)
|
con = sqlite3.connect(db_path)
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE date > (SELECT DATETIME('now', '-1 day')) GROUP BY Com_Name")
|
today = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date = date('{today}') GROUP BY Com_Name")
|
||||||
known_species = cur.fetchall()
|
known_species = cur.fetchall()
|
||||||
numberDetections = [d[1] for d in known_species if d[0] == comName.replace("'","")][0]
|
detections = [d[1] for d in known_species if d[0] == comName.replace("'","")]
|
||||||
if numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
|
numberDetections = 0
|
||||||
notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times today)",
|
if len(detections):
|
||||||
notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times today)",
|
numberDetections = detections[0]
|
||||||
|
if numberDetections > 0 and numberDetections <= APPRISE_NOTIFICATION_NEW_SPECIES_DAILY_COUNT_LIMIT:
|
||||||
|
print("send the notification")
|
||||||
|
notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (first time today)"
|
||||||
|
notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (first time today)"
|
||||||
notify(notify_body, notify_title)
|
notify(notify_body, notify_title)
|
||||||
con.close()
|
con.close()
|
||||||
except sqlite3.Error as e:
|
except sqlite3.Error as e:
|
||||||
@@ -65,10 +70,14 @@ def sendAppriseNotifications(species, confidence, path, settings_dict, db_path=D
|
|||||||
try:
|
try:
|
||||||
con = sqlite3.connect(db_path)
|
con = sqlite3.connect(db_path)
|
||||||
cur = con.cursor()
|
cur = con.cursor()
|
||||||
cur.execute("SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE date > (SELECT DATETIME('now', '-7 day')) GROUP BY Com_Name")
|
today = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
cur.execute(f"SELECT DISTINCT(Com_Name), count(Com_Name) FROM detections WHERE Date >= date('{today}', '-7 day') GROUP BY Com_Name")
|
||||||
known_species = cur.fetchall()
|
known_species = cur.fetchall()
|
||||||
numberDetections = [d[1] for d in known_species if d[0] == comName.replace("'","")][0]
|
detections = [d[1] for d in known_species if d[0] == comName.replace("'","")]
|
||||||
if numberDetections <= 5:
|
numberDetections = 0
|
||||||
|
if len(detections):
|
||||||
|
numberDetections = detections[0]
|
||||||
|
if numberDetections > 0 and numberDetections <= 5:
|
||||||
notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
|
notify_body=body.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
|
||||||
notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
|
notify_title=title.replace("$sciname", sciName).replace("$comname", comName).replace("$confidence", confidence).replace("$listenurl", listenurl) + " (only seen "+str(int(numberDetections))+" times in last 7d)"
|
||||||
notify(notify_body, notify_title)
|
notify(notify_body, notify_title)
|
||||||
|
|||||||
+3
-5
@@ -50,8 +50,6 @@ with open(userDir + '/BirdNET-Pi/scripts/thisrun.txt', 'r') as f:
|
|||||||
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
|
||||||
|
|
||||||
settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt')
|
|
||||||
|
|
||||||
def loadModel():
|
def loadModel():
|
||||||
|
|
||||||
global INPUT_LAYER_INDEX
|
global INPUT_LAYER_INDEX
|
||||||
@@ -402,10 +400,10 @@ def handle_client(conn, addr):
|
|||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
# Apprise of detection if not already alerted this run.
|
# Apprise of detection if not already alerted this run.
|
||||||
if not str(entry[0]) in species_apprised_this_run:
|
if not entry[0] in species_apprised_this_run:
|
||||||
|
settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt')
|
||||||
sendAppriseNotifications(str(entry[0]), str(entry[1]), File_Name, settings_dict)
|
sendAppriseNotifications(str(entry[0]), str(entry[1]), File_Name, settings_dict)
|
||||||
|
species_apprised_this_run.append(entry[0])
|
||||||
species_apprised_this_run.append(str(entry[0]))
|
|
||||||
|
|
||||||
print(str(current_date) +
|
print(str(current_date) +
|
||||||
';' +
|
';' +
|
||||||
|
|||||||
@@ -22,7 +22,8 @@ def create_test_db(db_file):
|
|||||||
VALUES(?,?) '''
|
VALUES(?,?) '''
|
||||||
|
|
||||||
cur = conn.cursor()
|
cur = conn.cursor()
|
||||||
cur.execute(sql, ["Great Crested Flycatcher", datetime.now()])
|
today = datetime.now().strftime("%Y-%m-%d") # SQLite stores date as YYYY-MM-DD
|
||||||
|
cur.execute(sql, ["Great Crested Flycatcher", today])
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
@@ -58,7 +59,7 @@ def test_notifications(mocker):
|
|||||||
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
|
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
|
||||||
assert(notify_call.call_count == 1)
|
assert(notify_call.call_count == 1)
|
||||||
assert(
|
assert(
|
||||||
notify_call.call_args_list[0][0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times today)"
|
notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
|
||||||
)
|
)
|
||||||
|
|
||||||
# Add new species notification.
|
# Add new species notification.
|
||||||
@@ -67,7 +68,7 @@ def test_notifications(mocker):
|
|||||||
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
|
sendAppriseNotifications("Myiarchus crinitus_Great Crested Flycatcher", "91", "filename", settings_dict, "test.db")
|
||||||
assert(notify_call.call_count == 2)
|
assert(notify_call.call_count == 2)
|
||||||
assert(
|
assert(
|
||||||
notify_call.call_args_list[0][0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times today)"
|
notify_call.call_args_list[0][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (first time today)"
|
||||||
)
|
)
|
||||||
assert(
|
assert(
|
||||||
notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times in last 7d)"
|
notify_call.call_args_list[1][0][0] == "A Great Crested Flycatcher (Myiarchus crinitus) was just detected with a confidence of 91 (only seen 1 times in last 7d)"
|
||||||
|
|||||||
Reference in New Issue
Block a user