Skip to content

Commit

Permalink
[UPDATE] hr and conf as parameters to the POST request
Browse files Browse the repository at this point in the history
  • Loading branch information
BenCretois committed Oct 5, 2023
1 parent 08644a1 commit 28ddc11
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 10 deletions.
7 changes: 4 additions & 3 deletions cloud_analysis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 \
Expand Down
13 changes: 13 additions & 0 deletions cloud_analysis/key-file.json
Original file line number Diff line number Diff line change
@@ -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/g5ZN1f3QPWDvZkF7raMphu7aiDHsGesNbI\nvT1ASWvUpKD8W54XvocIekGlCL3fDSuvtSFL2Uz/MoLk6P9hl9qCQMY89sEooXY5\ng/h008Mam+/SNfPukGAmkjtUpf9w49tWdj5b1QC4e9JiDwE7PAmckKynYyzUrfVr\nDsphjk2jDFBPRY9MlFiLZl0I9QaU/4dHOa8h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8SBVyNVz8gIVDs7A3AHUn2VyoiKcTkdo\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"
}
12 changes: 7 additions & 5 deletions cloud_analysis/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]

Expand All @@ -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
Expand All @@ -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!")
Expand Down
6 changes: 5 additions & 1 deletion cloud_analysis/test_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion cloud_function_directory/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down

0 comments on commit 28ddc11

Please sign in to comment.