-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
32 lines (24 loc) · 1.01 KB
/
main.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
import json
import base64
from PIL import Image
import io
from model_loader import ModelLoader
import numpy as np
import yaml
def init_context(context):
context.logger.info("Init context... 0%")
functionconfig = yaml.safe_load(open("/opt/nuclio/function.yaml"))
labels_spec = functionconfig['metadata']['annotations']['spec']
labels = {item['id']: item['name'] for item in json.loads(labels_spec)}
model_handler = ModelLoader(labels)
setattr(context.user_data, 'model_handler', model_handler)
context.logger.info("Init context...100%")
def handler(context, event):
context.logger.info("Run tf.matterport.mask_rcnn model")
data = event.body
buf = io.BytesIO(base64.b64decode(data["image"].encode('utf-8')))
threshold = float(data.get("threshold", 0.2))
image = Image.open(buf)
results = context.user_data.model_handler.infer(np.array(image), threshold)
return context.Response(body=json.dumps(results), headers={},
content_type='application/json', status_code=200)