Skip to content

Commit

Permalink
[ADD] hr and conf to loging
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCretois committed Nov 3, 2023
1 parent be55919 commit 9220e25
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
4 changes: 2 additions & 2 deletions cloud_analysis/key-file.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"type": "service_account",
"project_id": "snoskuter-akustikk",
"private_key_id": "74c1e4bcd4b00da4dcaf848c31b7f0e77f077163",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCSSaUWwH1vue/s\nw6fz1RVq+CIP6RSKwBLCBO3B4un6crj6BCZpE8uOIulMlkQbhCoesVWUS+QWJJmU\nvJqIypGJXgjOlsG3AfezGbk39xbQI/g5ZN1f3QPWDvZkF7raMphu7aiDHsGesNbI\nvT1ASWvUpKD8W54XvocIekGlCL3fDSuvtSFL2Uz/MoLk6P9hl9qCQMY89sEooXY5\ng/h008Mam+/SNfPukGAmkjtUpf9w49tWdj5b1QC4e9JiDwE7PAmckKynYyzUrfVr\nDsphjk2jDFBPRY9MlFiLZl0I9QaU/4dHOa8hSOZ7lXFie7MUgRPrbfJWY/Fcm+2k\n3bq+qAqxAgMBAAECggEAB8pIMlf51hNOxYr9zKxSQaoJFlPRNjpYRWDB0Itm7mFh\nnF7QNNRLCLyvM9bktZpqK8xBxG3uLYexbgxFJnezHF+XlHjOxBIkwLLj/Cj6XAcu\nVtAaQdqhM6sET/w7hRcgqveY+7KNmQfNyE7bjiraxS4nLIkseBM7WaY5DLLUeaZD\nRAmZi5KHv9hnINL3jVwMQTMXDwNCI4gvvTtWjfiWF5870cGaDHFcLiCuaWYrH1sc\nyuc5BUFkOSJx+TYxoYk+Rd/aTfj/4EVzBGLeDMI40QfAuNBYSqYbHX9Uv8oTDlUI\n7UA8SqhOhtwjwGMZhC4CPLDspFhIFyuhtWeFNJjsAQKBgQDLohEAe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bibXQXZgNWkrCxahhPyOPxA1wu\nucAjES/+UkbJYzeA1Gz6lq1xC9fVeprpOEmQGtzMrSLG6CejkchyaLjyftyzC99Z\nIvllRJcxjKGvXWUQNuCV2zJ6Oj6XuLHxLpySWQpV5Kmdnxflb6kCwHz6cQKBgB18\nZMrq7MKfP7hPIOnwRbcNMn3L1qtQ9+RRGHHc8SYNmcoyB32Bl5+RnBbSbhq64ffv\nIH5/kcmbeyWQiADiFIc61VfLUZz46T80Ig2rKrw/YOOjQl1RTn2OajisaeO4kkGm\nv4zcBaLrzq+lj0wGNVAAc9AqSI8DsxFc6EwDqqQ9AoGAXpRLsqLhIMYot0lV0b7f\nWukfsZiWz9KDMg9oIxUv9FqarLRme95M3g5r63udbGE4RGNuk9d0t2UHTTSUOmv1\nBUG9/TyIwipH6jL2YbcGghwIqB3HPWFcM1BZQ4mL6c8funIM7LCWAr+KntkQAoJJ\n4nSKnfx+p4b8hvK+LTCvYlE=\n-----END PRIVATE KEY-----\n",
"client_email": "[email protected]",
"client_id": "104912621642971586511",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
Expand Down
28 changes: 21 additions & 7 deletions cloud_analysis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def fetch_audio_data(bucket_name, blob_name):

return wav_file_object

def generate_signed_url(bucket_name, blob_name, expiration_time=600):
def generate_signed_url(bucket_name, blob_name, expiration_time=86400):
"""
Generates a signed URL for a GCS object.
Expand Down Expand Up @@ -164,6 +164,10 @@ def analyseAudioFile(
# List of results
results = []

# List of model confidence and HR
conf_arr = []
hr_arr = []

for item_audioclip, item_hr in zip(prob_audioclip_array, hr_array):

# Get the properties of the detection (start, end, label, confidence and harmonic ratio)
Expand All @@ -173,6 +177,10 @@ def analyseAudioFile(
confidence = conf.max()
hr = np.array(item_hr)

# Append the conf and hr of each segment
conf_arr.append(confidence)
hr_arr.append(hr)

# If the label is not "soundscape" then write the row:
if label != 0 and hr > min_hr and confidence > min_conf:
item_properties = [idx_begin, idx_end, confidence, hr]
Expand All @@ -181,7 +189,10 @@ def analyseAudioFile(
# Update the start time of the detection
idx_begin = idx_end

return results
# Get the max confidence and hr for the file analyzed
maxes = [max(conf_arr), max(hr_arr)]

return results, maxes

def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name: str, hr: float, conf:float):

Expand All @@ -190,16 +201,18 @@ def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name

# A call out to your code here. Optionally we can pass on the recorder coordinates
audio_file_object = fetch_audio_data(bucket_name, blob_name)
results = analyseAudioFile(audio_file_object, hr, conf)
results, maxes = analyseAudioFile(audio_file_object, hr, conf)
# The object results is a list containing detections in the form:
# [start, end, confidence, harmonic ratio]

# After processing we record each detection in the database.
# Each detection should have a start and end time which is used to create an audio clip later.
count = 0
detections = []

for r in results:
start, end, confidence, harmonic_ratio = r

if harmonic_ratio > hr and confidence > conf:
count += 1

Expand All @@ -214,7 +227,7 @@ def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name
# Add any other information you want to record here
})

return count
return count, maxes


@app.route('/process-audio', methods=['POST'])
Expand All @@ -227,9 +240,10 @@ def process_audio_endpoint():
hr = data['hr']
conf = data['conf']

results = on_process_audio(audio_id, audio_rec, bucket_name, blob_name, hr, conf)
results, maxes = on_process_audio(audio_id, audio_rec, bucket_name, blob_name, hr, conf)
max_conf, max_hr = maxes

if results > 0:
if results > 1:
# Create a signed URL
download_url = generate_signed_url(bucket_name, blob_name)

Expand All @@ -242,7 +256,7 @@ def process_audio_endpoint():
email_body += f"Download the audio file here: {download_url}"
send_email("Snowmobile Detection Alert", email_body)

return jsonify({"message": "Audio processing completed!"})
return jsonify({"message": f"file {blob_name} processed. Max CONF = {max_conf}, MAX HR = {max_hr}"})


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion cloud_analysis/test_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

curl -X POST \
-H "Content-Type: application/json" \
-d '{"hr": 0.00, "conf": 0.0001, "audio_id": "test-id", "audio_rec": {"location": {"latitude": 0, "longitude": 0}}, "bucket_name": "snoskuter-detector-test", "blob_name": "from_BUGG.mp3"}' \
-d '{"hr": 0.00, "conf": 0.0001, "audio_id": "test-id", "audio_rec": {"location": {"latitude": 0, "longitude": 0}}, "bucket_name": "snoskuter-detector-test", "blob_name": "other.mp3"}' \
http://localhost:8080/process-audio


Expand Down
36 changes: 0 additions & 36 deletions cloud_analysis/trigger_audio_analysis.py

This file was deleted.

0 comments on commit 9220e25

Please sign in to comment.