A live bird collage card for your Home Assistant dashboard.
@timserc's analyze.py for BirdWeather
Patrick McGuireb16ad201b542
diff --git a/analyze.py b/analyze.pyindex 054f5d40..9b755f0f 100644--- a/analyze.py+++ b/analyze.py@@ -20,6 +20,8 @@ import requestsimport mysql.connector###############################################################################import datetime+import pytz+from tzlocal import get_localzonefrom pathlib import Pathdef loadModel():@@ -192,7 +194,6 @@ def main():# Parse passed argumentsparser = argparse.ArgumentParser()- parser.add_argument('--s', type=int, default=99999, help='BirdWeather station id.')parser.add_argument('--i', help='Path to input file.')parser.add_argument('--o', default='result.csv', help='Path to output file. Defaults to result.csv.')parser.add_argument('--lat', type=float, default=-1, help='Recording location latitude. Set -1 to ignore.')@@ -202,7 +203,7 @@ def main():parser.add_argument('--sensitivity', type=float, default=1.0, help='Detection sensitivity; Higher values result in higher sensitivity. Values in [0.5, 1.5]. Defaults to 1.0.')parser.add_argument('--min_conf', type=float, default=0.1, help='Minimum confidence threshold. Values in [0.01, 0.99]. Defaults to 0.1.')parser.add_argument('--custom_list', default='', help='Path to text file containing a list of species. Not used if not provided.')- parser.add_argument('--meta_data', default='Testing', help='Location meta_data for BirdWeather station.')+ parser.add_argument('--birdweather_id', default='99999', help='Private Station ID for BirdWeather.')args = parser.parse_args()@@ -215,8 +216,7 @@ def main():else:WHITE_LIST = []- station_id = args.s- location_meta_data = args.meta_data+ birdweather_id = args.birdweather_id# Read audio dataaudioData = readAudioData(args.i, args.overlap)@@ -235,7 +235,7 @@ def main():now = date_time_objcurrent_date = now.strftime("%Y/%m/%d")current_time = now.strftime("%H:%M:%S")- current_iso8601 = now.isoformat()+ current_iso8601 = now.astimezone(get_localzone()).isoformat()week_number = int(now.strftime("%V"))week = max(1, min(week_number, 48))@@ -299,28 +299,27 @@ def main():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) + '\n')- if station_id != 99999:+ if birdweather_id != "99999":if soundscape_uploaded is False:# POST soundscape to server- post_url = "https://app.birdweather.com/api/v1/soundscapes" + "?timestamp=" + current_iso8601+ soundscape_url = "https://app.birdweather.com/api/v1/stations/" + birdweather_id + "/soundscapes" + "?timestamp=" + current_iso8601with open(args.i, 'rb') as f:wav_data = f.read()- response = requests.post(url=post_url, data=wav_data, headers={'Content-Type': 'application/octet-stream'})+ response = requests.post(url=soundscape_url, data=wav_data, headers={'Content-Type': 'application/octet-stream'})print("Soundscape POST Response Status - ", response.status_code)sdata = response.json()soundscape_id = sdata['soundscape']['id']soundscape_uploaded = True# POST detection to server- api_url = "https://app.birdweather.com/api/v1/detections"+ detection_url = "https://app.birdweather.com/api/v1/stations/" + birdweather_id + "/detections"start_time = d.split(';')[0]end_time = d.split(';')[1]post_begin = "{ "- post_station_id = "\"stationId\": \"" + str(station_id) + "\","now_p_start = now + datetime.timedelta(seconds=float(start_time))- current_iso8601 = now_p_start.isoformat();+ current_iso8601 = now_p_start.astimezone(get_localzone()).isoformat()post_timestamp = "\"timestamp\": \"" + current_iso8601 + "\","post_lat = "\"lat\": " + str(args.lat) + ","post_lon = "\"lon\": " + str(args.lon) + ","@@ -330,13 +329,12 @@ def main():post_commonName = "\"commonName\": \"" + entry[0].split('_')[1] + "\","post_scientificName = "\"scientificName\": \"" + entry[0].split('_')[0] + "\","post_algorithm = "\"algorithm\": " + "\"alpha\"" + ","- post_confidence = "\"confidence\": " + str(entry[1]) + ","- post_metadata = "\"metadata\": { \"location\": \"" + location_meta_data + "\" }"+ post_confidence = "\"confidence\": " + str(entry[1])post_end = " }"- post_json = post_begin + post_station_id + 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_metadata + 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_endprint(post_json)- response = requests.post(api_url, json=json.loads(post_json))+ response = requests.post(detection_url, json=json.loads(post_json))print("Detection POST Response Status - ", response.status_code)#time.sleep(3)