Running jobs using the API
The Edge Impulse API gives programmatic access to all features in the studio, and many tasks that might normally have to be performed manually can thus be automated. In this tutorial you'll create a job that deploys your model (as a ZIP file), you monitor the job status, and then finally download the deployed model. The tutorial uses Python, but you can use any environment capable of scripting HTTP requests.
To run this script you'll need:
  • A recent version of Python 3.
  • The requests module:
1
pip3 install requests
Copied!
  • Your project ID (can be found on Dashboard in the Edge Impulse Studio).
  • An API Key (under Dashboard > Keys).
Then, create the following script build-model.py:
1
import requests, json, datetime, time, re
2
3
project_id = 1 # YOUR PROJECT ID
4
api_key = "ei_..." # YOUR API KEY
5
deploy_type = "zip" # CAN CHANGE TO DIFFERENT TYPE
6
7
def build_model():
8
url = f"https://studio.edgeimpulse.com/v1/api/{project_id}/jobs/build-ondevice-model"
9
querystring = {"type": deploy_type}
10
payload = {"engine": "tflite-eon"}
11
headers = {
12
"x-api-key": api_key,
13
"Accept": "application/json",
14
"Content-Type": "application/json",
15
}
16
response = requests.request("POST", url, json=payload, headers=headers, params=querystring)
17
body = json.loads(response.text)
18
if (not body['success']):
19
raise Exception(body['error'])
20
return body['id']
21
22
def get_stdout(job_id, skip_line_no):
23
url = f"https://studio.edgeimpulse.com/v1/api/{project_id}/jobs/{job_id}/stdout"
24
headers = {
25
"x-api-key": api_key,
26
"Accept": "application/json",
27
"Content-Type": "application/json",
28
}
29
response = requests.request("GET", url, headers=headers)
30
body = json.loads(response.text)
31
if (not body['success']):
32
raise Exception(body['error'])
33
stdout = body['stdout'][::-1] # reverse array so it's old -> new
34
return [ x['data'] for x in stdout[skip_line_no:] ]
35
36
def wait_for_job_completion(job_id):
37
skip_line_no = 0
38
39
url = f"https://studio.edgeimpulse.com/v1/api/{project_id}/jobs/{job_id}/status"
40
headers = {
41
"x-api-key": api_key,
42
"Accept": "application/json",
43
"Content-Type": "application/json",
44
}
45
while True:
46
response = requests.request("GET", url, headers=headers)
47
body = json.loads(response.text)
48
if (not body['success']):
49
raise Exception(body['error'])
50
51
stdout = get_stdout(job_id, skip_line_no)
52
for l in stdout:
53
print(l, end='')
54
skip_line_no = skip_line_no + len(stdout)
55
56
if (not 'finished' in body['job']):
57
# print('Job', job_id, 'is not finished yet...', body['job'])
58
time.sleep(1)
59
continue
60
if (not body['job']['finishedSuccessful']):
61
raise Exception('Job failed')
62
else:
63
break
64
65
def download_model():
66
url = f"https://studio.edgeimpulse.com/v1/api/{project_id}/deployment/download"
67
querystring = {"type": deploy_type}
68
headers = {
69
"x-api-key": api_key,
70
"Accept": "application/json",
71
"Content-Type": "application/json",
72
}
73
response = requests.request("GET", url, headers=headers, params=querystring)
74
75
d = response.headers['Content-Disposition']
76
fname = re.findall("filename\*?=(.+)", d)[0].replace('utf-8\'\'', '')
77
78
return fname, response.content
79
80
if __name__ == "__main__":
81
job_id = build_model()
82
print('Job ID is', job_id)
83
wait_for_job_completion(job_id)
84
print('Job', job_id, 'is finished')
85
bin_filename, bin_file = download_model()
86
print('Output file is', len(bin_file), 'bytes')
87
with open(bin_filename, 'wb') as f:
88
f.write(bin_file)
89
print('Written job output to', bin_filename)
Copied!
When you now run python3 download-model.py you'll see something like:
1
Job ID is 1486148
2
Writing templates OK
3
4
Scheduling job in cluster...
5
Compiling EON model...
6
Job started
7
Compiling EON model OK
8
Removing clutter...
9
Removing clutter OK
10
Copying output...
11
Copying output OK
12
13
Job 1486148 is finished
14
Output file is 4824580 bytes
15
Written job output to myawesomeproject-v63.zip
Copied!
🚀
Export as PDF
Copy link