Last active
February 7, 2019 22:35
-
-
Save ptitzler/51ccdf7937e14530580e8799f26c9a5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
class MyClass(GeneratedClass): | |
# ... | |
def onInput_picture(self, p): | |
''' | |
Identify objects in an image by calling an external microservice | |
Input: location of an image that was taken using the camera | |
''' | |
self.log('Image location is {}'.format(p)) | |
with open(p) as f: | |
# formdata payload: the captured image | |
files = {'image' : f} | |
print('Sending inference request to {} ...'.format(resnet_inference_url)) | |
# send a POST request to the service's /model/predict endpoint | |
r = requests.post(resnet_inference_url, files = files) | |
if r.status_code > 200: | |
self.logger.error('Inference request returned HTTP code {} and message {}'.format(r.status_code, r.text)) | |
else: | |
# HTTP 200 response returns JSON; parse and extract 'predictions' array | |
objects = r.json()['predictions'] | |
if len(objects) == 0: | |
# ... no objects could be identified | |
# sad response, e.g. | |
self.tts.say('I saw something but I don\'t know what it is.') | |
else: | |
for object in objects: | |
# label: identifies the object type | |
# probability: quantifies the confidence [0...1], 1 = highest confidence | |
self.log('Object: {} probability: {}'.format(object['label'], object['probability'])) | |
if object['probability'] > 0.05: | |
self.tts.say('I see a {}'.format(object['label'])) | |
else: | |
self.tts.say('I think there is a {}? '.format(object['label'])) | |
# ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment