-
Notifications
You must be signed in to change notification settings - Fork 1
/
EIDownload.py
157 lines (128 loc) · 5.5 KB
/
EIDownload.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import requests, json, time, re, os
import logging
logging.basicConfig()
logger = logging.getLogger("EIDownload")
logger.setLevel(logging.INFO)
class EIDownload:
def __init__(self, api_key, project_id = None):
self.api_key = api_key
if project_id is None:
self.project_id = self.set_project_id()
logger.info("Project ID is " + str(self.project_id))
else:
self.project_id = project_id
def get_project_id(self):
return self.project_id
def set_project_id(self):
url = f"https://studio.edgeimpulse.com/v1/api/projects"
headers = {
"x-api-key": self.api_key,
"Accept": "application/json",
"Content-Type": "application/json",
}
response = requests.request("GET", url, headers=headers)
body = json.loads(response.text)
if (not body['success']):
raise Exception(body['error'])
return body['projects'][0]['id']
def download_model(self, out_directory, eon=True, quantized=True, force_build=False):
if self.project_id is None:
raise Exception('Project ID is not set')
if eon:
engine = 'tflite-eon'
else:
engine = 'tflite'
if quantized:
model_type = 'int8'
else:
model_type = 'float32'
# Check if build is available first
if force_build or not self.build_available(engine, model_type):
logger.info("No build artefact found for project " + str(self.project_id) + ", will build library first.")
job_id = self.build_model(engine, model_type)
self.wait_for_job_completion(job_id)
logger.info('Build OK')
url = f"https://studio.edgeimpulse.com/v1/api/{self.project_id}/deployment/download"
querystring = {
"type": "zip",
"modelType": model_type,
"engine": engine
}
headers = {
"x-api-key": self.api_key,
"Accept": "application/zip",
"Content-Type": "application/json",
}
response = requests.request("GET", url, headers=headers, params=querystring)
d = response.headers['Content-Disposition']
fname = re.findall("filename\*?=(.+)", d)[0].replace('utf-8\'\'', '')
with open(os.path.join(out_directory, fname), 'wb') as f:
f.write(response.content)
logger.info('Export ZIP saved in: ' + os.path.join(out_directory, fname) + ' (' + str(len(response.content)) + ' Bytes)')
return os.path.join(out_directory, fname)
def build_available(self, engine, model_type):
url = f"https://studio.edgeimpulse.com/v1/api/{self.project_id}/deployment"
querystring = {"type": "zip", "modelType": model_type, "engine": engine}
headers = {
"x-api-key": self.api_key,
"Accept": "application/json",
"Content-Type": "application/json",
}
response = requests.request("GET", url, headers=headers, params=querystring)
body = json.loads(response.text)
if (not body['success']):
raise Exception(body['error'])
return body['hasDeployment']
def build_model(self, engine, model_type):
url = f"https://studio.edgeimpulse.com/v1/api/{self.project_id}/jobs/build-ondevice-model"
querystring = {"type": "zip"}
payload = {"engine": engine, "modelType": model_type}
headers = {
"x-api-key": self.api_key,
"Accept": "application/json",
"Content-Type": "application/json",
}
response = requests.request("POST", url, json=payload, headers=headers, params=querystring)
body = json.loads(response.text)
if (not body['success']):
raise Exception(body['error'])
return body['id']
def get_stdout(self, job_id, skip_line_no):
url = f"https://studio.edgeimpulse.com/v1/api/{self.project_id}/jobs/{job_id}/stdout"
headers = {
"x-api-key": self.api_key,
"Accept": "application/json",
"Content-Type": "application/json",
}
response = requests.request("GET", url, headers=headers)
body = json.loads(response.text)
if (not body['success']):
raise Exception(body['error'])
stdout = body['stdout'][::-1] # reverse array so it's old -> new
return [ x['data'] for x in stdout[skip_line_no:] ]
def wait_for_job_completion(self, job_id):
skip_line_no = 0
url = f"https://studio.edgeimpulse.com/v1/api/{self.project_id}/jobs/{job_id}/status"
headers = {
"x-api-key": self.api_key,
"Accept": "application/json",
"Content-Type": "application/json",
}
while True:
response = requests.request("GET", url, headers=headers)
body = json.loads(response.text)
if (not body['success']):
raise Exception(body['error'])
stdout = self.get_stdout(job_id, skip_line_no)
for l in stdout:
logger.info(l)
skip_line_no = skip_line_no + len(stdout)
if (not 'finished' in body['job']):
logger.info('Still building...')
time.sleep(1)
continue
if (not body['job']['finishedSuccessful']):
logger.error(f"Job did not finish successfully. Response: {response.text}")
raise Exception('Job failed')
else:
break