0 second wav file uploaded to studio with ingestion API

I’ve been working through using the ingestion API today using Python with some success, until I examine an uploaded wav file in studio. The wav file itself looks correct when I print the wav_file content to the console, but it ends up as a 0 second sample in studio.

Here is my full source…I must be missing something simple…?

import json
import time
import hmac
import hashlib
import requests
from scipy.io import wavfile

HMAC_KEY = "..."
API_KEY = "..."

samplerate, wav_data = wavfile.read('final.wav')
wav_data = wav_data.tolist()

emptySignature = ''.join(['0'] * 64)

data = {
    "protected": {
        "ver": "v1",
        "alg": "HS256",
        "iat": time.time()
    },
    "signature": emptySignature,
    "payload": {
        "device_name": "DC:A6:32:BC:0A:00",
        "device_type": "RASPBERRY_PI",
        "interval_ms": 1000 / samplerate,
        "sensors": [{"name": "audio", "units": "wav"}],
        "values": [wav_data]
    }
}

# encode in JSON
encoded = json.dumps(data)

# sign message
signature = hmac.new(bytes(HMAC_KEY, 'utf-8'),
                     msg=encoded.encode('utf-8'), digestmod=hashlib.sha256).hexdigest()

# set the signature again in the message, and encode again
data['signature'] = signature
encoded = json.dumps(data)

# and upload the file
res = requests.post(url='https://ingestion.edgeimpulse.com/api/training/data',
                    data=encoded,
                    headers={
                        'Content-Type': 'application/json',
                        'x-file-name': 'major_failure',
                        'x-label': 'major failure',
                        'x-api-key': API_KEY
                    })
if (res.status_code == 200):
    print('Uploaded file to Edge Impulse', res.status_code, res.content)
else:
    print('Failed to upload file to Edge Impulse', res.status_code, res.content)

Oh my…to make this work I just had to change the values param to:

"values": wav_data

3 Likes

Python is tricky like that haha, glad you figured it out!

1 Like