From 28ddc118f7d79cfff907e1d04d21fb562a6e1308 Mon Sep 17 00:00:00 2001 From: Benjamin Cretois Date: Thu, 5 Oct 2023 12:28:04 +0200 Subject: [PATCH] [UPDATE] hr and conf as parameters to the POST request --- cloud_analysis/README.md | 7 ++++--- cloud_analysis/key-file.json | 13 +++++++++++++ cloud_analysis/main.py | 12 +++++++----- cloud_analysis/test_local.sh | 6 +++++- cloud_function_directory/main.py | 6 +++++- 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 cloud_analysis/key-file.json diff --git a/cloud_analysis/README.md b/cloud_analysis/README.md index e9e4294..e2b7780 100644 --- a/cloud_analysis/README.md +++ b/cloud_analysis/README.md @@ -123,9 +123,10 @@ docker push gcr.io/snoskuter-akustikk/snowmobile_cloud:main gcloud run deploy \ model \ --image gcr.io/snoskuter-akustikk/snowmobile_cloud:main \ - --memory 4Gi \ + --memory 8Gi \ --region europe-north1 \ - --platform managed + --platform managed \ + --cpus 4 ``` Be sure to allocate enough memory for the service to be able to process the files and run the model. @@ -156,7 +157,7 @@ Because of the structure of the `main.py`, the `key-file.json` that has been cre Once this is done you can deploy the function using: ```bash -gcloud functions deploy trigger_audio_analysis \ +gcloud functions deploy trigger_audio_analysis \ --runtime python310 \ --trigger-resource snoskuter-detector-test \ --trigger-event google.storage.object.finalize \ diff --git a/cloud_analysis/key-file.json b/cloud_analysis/key-file.json new file mode 100644 index 0000000..1276273 --- /dev/null +++ b/cloud_analysis/key-file.json @@ -0,0 +1,13 @@ +{ + "type": "service_account", + "project_id": "snoskuter-akustikk", + "private_key_id": "74c1e4bcd4b00da4dcaf848c31b7f0e77f077163", + "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCSSaUWwH1vue/s\nw6fz1RVq+CIP6RSKwBLCBO3B4un6crj6BCZpE8uOIulMlkQbhCoesVWUS+QWJJmU\nvJqIypGJXgjOlsG3AfezGbk39xbQI/g5ZN1f3Q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3e1rtTwv9iYTZ+L2/\nM5gb3RCzeEiu2/3U8SErogZcQD3Zpe9lQtB4FS9toHp3xaRyGsRFqJXRAoGBAJiS\nMQ579HeP8tuQ/+Cth02NyBg7QlbFh5kbSdnhR9u/p6iVvFNpCMwbgWx/k7OpmxeU\nR1M0gy1wZ9UB5OHGJQxZXMGawZ0b8EPr8SBVyNVz8gIVDs7A3AHUn2VyoiKcTkdo\npZpajDv5SO6iMBXgtev90ts77MP/NP6sv6+Vc53BAoGAUgLZ5dO7jOuMeF5aF93v\nd4EjNsxkWDh+1tznSc/zcZxB7BytVsb61HRDn/6HPwSGSvqz7RLKqtynZOhGUlLS\ndtw9TXUgwhMM/iKLUvZQKghm0CfnnsGjEkNowAHTnS7mRBNeClnpYFWxWkE/Za6k\nFnQZmUYKgliaLaVJBXc3W8E=\n-----END PRIVATE KEY-----\n", + "client_email": "snoskuter-akustikk@appspot.gserviceaccount.com", + "client_id": "104912621642971586511", + "auth_uri": "https://accounts.google.com/o/oauth2/auth", + "token_uri": "https://oauth2.googleapis.com/token", + "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", + "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/snoskuter-akustikk%40appspot.gserviceaccount.com", + "universe_domain": "googleapis.com" +} \ No newline at end of file diff --git a/cloud_analysis/main.py b/cloud_analysis/main.py index 93ac772..f39dee8 100644 --- a/cloud_analysis/main.py +++ b/cloud_analysis/main.py @@ -110,7 +110,7 @@ def fetch_audio_data(bucket_name, blob_name): def analyseAudioFile( - audio_file_object, batch_size=1, num_workers=4, min_hr = 0.1, min_conf = 0.99 + audio_file_object, min_hr, min_conf, batch_size=1, num_workers=2, ): # Initiate model @@ -148,14 +148,14 @@ def analyseAudioFile( return results -def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name: str): +def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name: str, hr: float, conf:float): print(f"PROCESSING audioId={audio_id}") location = audio_rec["location"] # 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) + results = analyseAudioFile(audio_file_object, hr, conf) # The object results is a list containing detections in the form: # [start, end, confidence, harmonic ratio] @@ -165,7 +165,7 @@ def on_process_audio(audio_id: str, audio_rec: dict, bucket_name: str, blob_name detections = [] for r in results: start, end, confidence, harmonic_ratio = r - if harmonic_ratio > 0.1 and confidence > 0.99: + if harmonic_ratio > hr and confidence > conf: count += 1 # create the detections dataset @@ -189,8 +189,10 @@ def process_audio_endpoint(): blob_name = data['blob_name'] audio_id = data['audio_id'] audio_rec = data['audio_rec'] + hr = data['hr'] + conf = data['conf'] - results = on_process_audio(audio_id, audio_rec, bucket_name, blob_name) + results = on_process_audio(audio_id, audio_rec, bucket_name, blob_name, hr, conf) if results > 0: send_email("Snowmobile Detection Alert", f"{results} snowmobile detections were made in the audio file!") diff --git a/cloud_analysis/test_local.sh b/cloud_analysis/test_local.sh index b34689a..8e99c33 100755 --- a/cloud_analysis/test_local.sh +++ b/cloud_analysis/test_local.sh @@ -2,5 +2,9 @@ curl -X POST \ -H "Content-Type: application/json" \ - -d '{"audio_id": "test-id", "audio_rec": {"location": {"latitude": 0, "longitude": 0}}, "bucket_name": "snoskuter-detector-test", "blob_name": "example_audio.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": "from_BUGG.mp3"}' \ http://localhost:8080/process-audio + + +# proj_snowmobile/bugg_RPiID-10000000f8e69c84/conf_6f40914/2023-10-04T11_28_44.579Z.mp3 +# other.mp3 \ No newline at end of file diff --git a/cloud_function_directory/main.py b/cloud_function_directory/main.py index f720ce5..eeba5cf 100644 --- a/cloud_function_directory/main.py +++ b/cloud_function_directory/main.py @@ -56,6 +56,8 @@ def fetch_service_account_key(bucket_name, blob_name): def trigger_audio_analysis(data, context): file_name = data['name'] bucket_name = data['bucket'] + harmonic_ratio = 0.05 + confidence = 0.80 # URL of your Cloud Run service cloud_run_url = "https://model-4uhtnq5xla-lz.a.run.app/process-audio" @@ -65,7 +67,9 @@ def trigger_audio_analysis(data, context): "bucket_name": bucket_name, "blob_name": file_name, "audio_id": "example-audio-id", - "audio_rec": {"location": {"latitude": 0, "longitude": 0}} + "audio_rec": {"location": {"latitude": 0, "longitude": 0}}, + "hr": harmonic_ratio, + "conf": confidence } # Specify the GCS bucket and blob name of the service account key file