Merge pull request #965 from srd424/serverpy-fix-2

Simplify handle_client loop / indent, check for EOF when reading from…
This commit is contained in:
ehpersonal38
2023-07-02 18:18:48 -04:00
committed by GitHub
+238 -235
View File
@@ -341,276 +341,279 @@ def handle_client(conn, addr):
global EXCLUDE_LIST global EXCLUDE_LIST
# print(f"[NEW CONNECTION] {addr} connected.") # print(f"[NEW CONNECTION] {addr} connected.")
connected = True while True:
while connected:
msg_length = conn.recv(HEADER).decode(FORMAT) msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length: if not msg_length:
msg_length = int(msg_length) break
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
connected = False
else:
# print(f"[{addr}] {msg}")
args = type('', (), {})() msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if not msg:
break
if msg == DISCONNECT_MESSAGE:
break
args.i = '' # print(f"[{addr}] {msg}")
args.o = ''
args.birdweather_id = '99999'
args.include_list = 'null'
args.exclude_list = 'null'
args.overlap = 0.0
args.week = -1
args.sensitivity = 1.25
args.min_conf = 0.70
args.lat = -1
args.lon = -1
for line in msg.split('||'): args = type('', (), {})()
inputvars = line.split('=')
if inputvars[0] == 'i':
args.i = inputvars[1]
elif inputvars[0] == 'o':
args.o = inputvars[1]
elif inputvars[0] == 'birdweather_id':
args.birdweather_id = inputvars[1]
elif inputvars[0] == 'include_list':
args.include_list = inputvars[1]
elif inputvars[0] == 'exclude_list':
args.exclude_list = inputvars[1]
elif inputvars[0] == 'overlap':
args.overlap = float(inputvars[1])
elif inputvars[0] == 'week':
args.week = int(inputvars[1])
elif inputvars[0] == 'sensitivity':
args.sensitivity = float(inputvars[1])
elif inputvars[0] == 'min_conf':
args.min_conf = float(inputvars[1])
elif inputvars[0] == 'lat':
args.lat = float(inputvars[1])
elif inputvars[0] == 'lon':
args.lon = float(inputvars[1])
# Load custom species lists - INCLUDED and EXCLUDED args.i = ''
if not args.include_list == 'null': args.o = ''
INCLUDE_LIST = loadCustomSpeciesList(args.include_list) args.birdweather_id = '99999'
else: args.include_list = 'null'
INCLUDE_LIST = [] args.exclude_list = 'null'
args.overlap = 0.0
args.week = -1
args.sensitivity = 1.25
args.min_conf = 0.70
args.lat = -1
args.lon = -1
if not args.exclude_list == 'null': for line in msg.split('||'):
EXCLUDE_LIST = loadCustomSpeciesList(args.exclude_list) inputvars = line.split('=')
else: if inputvars[0] == 'i':
EXCLUDE_LIST = [] args.i = inputvars[1]
elif inputvars[0] == 'o':
args.o = inputvars[1]
elif inputvars[0] == 'birdweather_id':
args.birdweather_id = inputvars[1]
elif inputvars[0] == 'include_list':
args.include_list = inputvars[1]
elif inputvars[0] == 'exclude_list':
args.exclude_list = inputvars[1]
elif inputvars[0] == 'overlap':
args.overlap = float(inputvars[1])
elif inputvars[0] == 'week':
args.week = int(inputvars[1])
elif inputvars[0] == 'sensitivity':
args.sensitivity = float(inputvars[1])
elif inputvars[0] == 'min_conf':
args.min_conf = float(inputvars[1])
elif inputvars[0] == 'lat':
args.lat = float(inputvars[1])
elif inputvars[0] == 'lon':
args.lon = float(inputvars[1])
birdweather_id = args.birdweather_id # Load custom species lists - INCLUDED and EXCLUDED
if not args.include_list == 'null':
INCLUDE_LIST = loadCustomSpeciesList(args.include_list)
else:
INCLUDE_LIST = []
# Read audio data & handle errors if not args.exclude_list == 'null':
try: EXCLUDE_LIST = loadCustomSpeciesList(args.exclude_list)
audioData = readAudioData(args.i, args.overlap) else:
EXCLUDE_LIST = []
except (NameError, TypeError) as e: birdweather_id = args.birdweather_id
print(f"Error with the following info: {e}")
open('~/BirdNET-Pi/analyzing_now.txt', 'w').close()
finally: # Read audio data & handle errors
pass try:
audioData = readAudioData(args.i, args.overlap)
# Get Date/Time from filename in case Pi gets behind except (NameError, TypeError) as e:
# now = datetime.now() print(f"Error with the following info: {e}")
full_file_name = args.i open('~/BirdNET-Pi/analyzing_now.txt', 'w').close()
# print('FULL FILENAME: -' + full_file_name + '-')
file_name = Path(full_file_name).stem
# Get the RSTP stream identifier from the filename if it exists finally:
RTSP_ident_for_fn = "" pass
RTSP_ident = re.search("RTSP_[0-9]+-", file_name)
if RTSP_ident is not None:
RTSP_ident_for_fn = RTSP_ident.group()
# Find and remove the identifier for the RSTP stream url it was from that is added when more than one # Get Date/Time from filename in case Pi gets behind
# RSTP stream is recorded simultaneously, in order to make the filenames unique as filenames are all # now = datetime.now()
# generated at the same time full_file_name = args.i
file_name = re.sub("RTSP_[0-9]+-", "", file_name) # print('FULL FILENAME: -' + full_file_name + '-')
file_name = Path(full_file_name).stem
# Now we can read the date and time as normal # Get the RSTP stream identifier from the filename if it exists
# First portion of the filename contaning the date in Y m d RTSP_ident_for_fn = ""
file_date = file_name.split('-birdnet-')[0] RTSP_ident = re.search("RTSP_[0-9]+-", file_name)
# Second portion of the filename containing the time in H:M:S if RTSP_ident is not None:
file_time = file_name.split('-birdnet-')[1] RTSP_ident_for_fn = RTSP_ident.group()
# Join the date and time together to get a complete string representing when the audio was recorded
date_time_str = file_date + ' ' + file_time
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
# print('Date:', date_time_obj.date())
# print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
now = date_time_obj
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
current_iso8601 = now.astimezone(get_localzone()).isoformat()
week_number = int(now.strftime("%V")) # Find and remove the identifier for the RSTP stream url it was from that is added when more than one
week = max(1, min(week_number, 48)) # RSTP stream is recorded simultaneously, in order to make the filenames unique as filenames are all
# generated at the same time
file_name = re.sub("RTSP_[0-9]+-", "", file_name)
sensitivity = max(0.5, min(1.0 - (args.sensitivity - 1.0), 1.5)) # Now we can read the date and time as normal
# First portion of the filename contaning the date in Y m d
file_date = file_name.split('-birdnet-')[0]
# Second portion of the filename containing the time in H:M:S
file_time = file_name.split('-birdnet-')[1]
# Join the date and time together to get a complete string representing when the audio was recorded
date_time_str = file_date + ' ' + file_time
date_time_obj = datetime.datetime.strptime(date_time_str, '%Y-%m-%d %H:%M:%S')
# print('Date:', date_time_obj.date())
# print('Time:', date_time_obj.time())
print('Date-time:', date_time_obj)
now = date_time_obj
current_date = now.strftime("%Y-%m-%d")
current_time = now.strftime("%H:%M:%S")
current_iso8601 = now.astimezone(get_localzone()).isoformat()
# Process audio data and get detections week_number = int(now.strftime("%V"))
detections = analyzeAudioData(audioData, args.lat, args.lon, week, sensitivity, args.overlap) week = max(1, min(week_number, 48))
# Write detections to output file sensitivity = max(0.5, min(1.0 - (args.sensitivity - 1.0), 1.5))
min_conf = max(0.01, min(args.min_conf, 0.99))
writeResultsToFile(detections, min_conf, args.o)
############################################################################### # Process audio data and get detections
############################################################################### detections = analyzeAudioData(audioData, args.lat, args.lon, week, sensitivity, args.overlap)
soundscape_uploaded = False # Write detections to output file
min_conf = max(0.01, min(args.min_conf, 0.99))
writeResultsToFile(detections, min_conf, args.o)
# Write detections to Database ###############################################################################
myReturn = '' ###############################################################################
for i in detections:
myReturn += str(i) + '-' + str(detections[i][0]) + '\n'
with open(userDir + '/BirdNET-Pi/BirdDB.txt', 'a') as rfile: soundscape_uploaded = False
for d in detections:
species_apprised_this_run = []
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)
and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0)):
# Write to text file.
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';').split("/")[0] + ';'
+ str(entry[1]) + ";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';'
+ str(args.sensitivity) + ';' + str(args.overlap) + '\n')
# Write to database # Write detections to Database
Date = str(current_date) myReturn = ''
Time = str(current_time) for i in detections:
species = entry[0].split("/")[0] myReturn += str(i) + '-' + str(detections[i][0]) + '\n'
Sci_Name, Com_Name = species.split('_')
score = entry[1]
Confidence = str(round(score * 100))
Lat = str(args.lat)
Lon = str(args.lon)
Cutoff = str(args.min_conf)
Week = str(args.week)
Sens = str(args.sensitivity)
Overlap = str(args.overlap)
Com_Name = Com_Name.replace("'", "")
File_Name = Com_Name.replace(" ", "_") + '-' + Confidence + '-' + \
Date.replace("/", "-") + '-birdnet-' + RTSP_ident_for_fn + Time + audiofmt
# Connect to SQLite Database with open(userDir + '/BirdNET-Pi/BirdDB.txt', 'a') as rfile:
for attempt_number in range(3): for d in detections:
try: species_apprised_this_run = []
con = sqlite3.connect(DB_PATH) for entry in detections[d]:
cur = con.cursor() if entry[1] >= min_conf and ((entry[0] in INCLUDE_LIST or len(INCLUDE_LIST) == 0)
cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (Date, Time, and (entry[0] not in EXCLUDE_LIST or len(EXCLUDE_LIST) == 0)
Sci_Name, Com_Name, str(score), Lat, Lon, Cutoff, Week, Sens, Overlap, File_Name)) and (entry[0] in PREDICTED_SPECIES_LIST or len(PREDICTED_SPECIES_LIST) == 0)):
# Write to text file.
rfile.write(str(current_date) + ';' + str(current_time) + ';' + entry[0].replace('_', ';').split("/")[0] + ';'
+ str(entry[1]) + ";" + str(args.lat) + ';' + str(args.lon) + ';' + str(min_conf) + ';' + str(week) + ';'
+ str(args.sensitivity) + ';' + str(args.overlap) + '\n')
con.commit() # Write to database
con.close() Date = str(current_date)
break Time = str(current_time)
except BaseException: species = entry[0].split("/")[0]
print("Database busy") Sci_Name, Com_Name = species.split('_')
time.sleep(2) score = entry[1]
Confidence = str(round(score * 100))
Lat = str(args.lat)
Lon = str(args.lon)
Cutoff = str(args.min_conf)
Week = str(args.week)
Sens = str(args.sensitivity)
Overlap = str(args.overlap)
Com_Name = Com_Name.replace("'", "")
File_Name = Com_Name.replace(" ", "_") + '-' + Confidence + '-' + \
Date.replace("/", "-") + '-birdnet-' + RTSP_ident_for_fn + Time + audiofmt
# Apprise of detection if not already alerted this run. # Connect to SQLite Database
if not entry[0] in species_apprised_this_run: for attempt_number in range(3):
settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt') try:
sendAppriseNotifications(species, con = sqlite3.connect(DB_PATH)
str(score), cur = con.cursor()
str(round(score * 100)), cur.execute("INSERT INTO detections VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", (Date, Time,
File_Name, Sci_Name, Com_Name, str(score), Lat, Lon, Cutoff, Week, Sens, Overlap, File_Name))
Date,
Time,
Week,
Lat,
Lon,
Cutoff,
Sens,
Overlap,
settings_dict,
DB_PATH)
species_apprised_this_run.append(entry[0])
print(str(current_date) + con.commit()
';' + con.close()
str(current_time) + break
';' + except BaseException:
entry[0].replace('_', ';') + print("Database busy")
';' + time.sleep(2)
str(entry[1]) +
';' +
str(args.lat) +
';' +
str(args.lon) +
';' +
str(min_conf) +
';' +
str(week) +
';' +
str(args.sensitivity) +
';' +
str(args.overlap) +
';' +
File_Name +
'\n')
if birdweather_id != "99999": # Apprise of detection if not already alerted this run.
try: if not entry[0] in species_apprised_this_run:
settings_dict = config_to_settings(userDir + '/BirdNET-Pi/scripts/thisrun.txt')
sendAppriseNotifications(species,
str(score),
str(round(score * 100)),
File_Name,
Date,
Time,
Week,
Lat,
Lon,
Cutoff,
Sens,
Overlap,
settings_dict,
DB_PATH)
species_apprised_this_run.append(entry[0])
if soundscape_uploaded is False: print(str(current_date) +
# POST soundscape to server ';' +
soundscape_url = 'https://app.birdweather.com/api/v1/stations/' + \ str(current_time) +
birdweather_id + \ ';' +
'/soundscapes' + \ entry[0].replace('_', ';') +
'?timestamp=' + \ ';' +
current_iso8601 str(entry[1]) +
';' +
str(args.lat) +
';' +
str(args.lon) +
';' +
str(min_conf) +
';' +
str(week) +
';' +
str(args.sensitivity) +
';' +
str(args.overlap) +
';' +
File_Name +
'\n')
with open(args.i, 'rb') as f: if birdweather_id != "99999":
wav_data = f.read() try:
gzip_wav_data = gzip.compress(wav_data)
response = requests.post(url=soundscape_url, data=gzip_wav_data, headers={'Content-Type': 'application/octet-stream',
'Content-Encoding': 'gzip'})
print("Soundscape POST Response Status - ", response.status_code)
sdata = response.json()
soundscape_id = sdata['soundscape']['id']
soundscape_uploaded = True
# POST detection to server if soundscape_uploaded is False:
detection_url = "https://app.birdweather.com/api/v1/stations/" + birdweather_id + "/detections" # POST soundscape to server
start_time = d.split(';')[0] soundscape_url = 'https://app.birdweather.com/api/v1/stations/' + \
end_time = d.split(';')[1] birdweather_id + \
post_begin = "{ " '/soundscapes' + \
now_p_start = now + datetime.timedelta(seconds=float(start_time)) '?timestamp=' + \
current_iso8601 = now_p_start.astimezone(get_localzone()).isoformat() current_iso8601
post_timestamp = "\"timestamp\": \"" + current_iso8601 + "\","
post_lat = "\"lat\": " + str(args.lat) + ","
post_lon = "\"lon\": " + str(args.lon) + ","
post_soundscape_id = "\"soundscapeId\": " + str(soundscape_id) + ","
post_soundscape_start_time = "\"soundscapeStartTime\": " + start_time + ","
post_soundscape_end_time = "\"soundscapeEndTime\": " + end_time + ","
post_commonName = "\"commonName\": \"" + entry[0].split('_')[1].split("/")[0] + "\","
post_scientificName = "\"scientificName\": \"" + entry[0].split('_')[0] + "\","
if model == "BirdNET_GLOBAL_6K_V2.4_Model_FP16": with open(args.i, 'rb') as f:
post_algorithm = "\"algorithm\": " + "\"2p4\"" + "," wav_data = f.read()
else: gzip_wav_data = gzip.compress(wav_data)
post_algorithm = "\"algorithm\": " + "\"alpha\"" + "," response = requests.post(url=soundscape_url, data=gzip_wav_data, headers={'Content-Type': 'application/octet-stream',
'Content-Encoding': 'gzip'})
print("Soundscape POST Response Status - ", response.status_code)
sdata = response.json()
soundscape_id = sdata['soundscape']['id']
soundscape_uploaded = True
post_confidence = "\"confidence\": " + str(entry[1]) # POST detection to server
post_end = " }" detection_url = "https://app.birdweather.com/api/v1/stations/" + birdweather_id + "/detections"
start_time = d.split(';')[0]
end_time = d.split(';')[1]
post_begin = "{ "
now_p_start = now + datetime.timedelta(seconds=float(start_time))
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) + ","
post_soundscape_id = "\"soundscapeId\": " + str(soundscape_id) + ","
post_soundscape_start_time = "\"soundscapeStartTime\": " + start_time + ","
post_soundscape_end_time = "\"soundscapeEndTime\": " + end_time + ","
post_commonName = "\"commonName\": \"" + entry[0].split('_')[1].split("/")[0] + "\","
post_scientificName = "\"scientificName\": \"" + entry[0].split('_')[0] + "\","
post_json = post_begin + post_timestamp + post_lat + post_lon + post_soundscape_id + post_soundscape_start_time + \ if model == "BirdNET_GLOBAL_6K_V2.4_Model_FP16":
post_soundscape_end_time + post_commonName + post_scientificName + post_algorithm + post_confidence + post_end post_algorithm = "\"algorithm\": " + "\"2p4\"" + ","
print(post_json) else:
response = requests.post(detection_url, json=json.loads(post_json)) post_algorithm = "\"algorithm\": " + "\"alpha\"" + ","
print("Detection POST Response Status - ", response.status_code)
except BaseException:
print("Cannot POST right now")
conn.send(myReturn.encode(FORMAT))
# time.sleep(3) 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
print(post_json)
response = requests.post(detection_url, json=json.loads(post_json))
print("Detection POST Response Status - ", response.status_code)
except BaseException:
print("Cannot POST right now")
conn.send(myReturn.encode(FORMAT))
# time.sleep(3)
conn.close() conn.close()