-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First-draft of Services infrastructure (#2985)
* begin services work * flesh out services impl * `service` method moved to Particle class * dump unused ServiceRequestCallback * better description * accommodate tslint * move `waltbird` into repo as particle asset * enable shorthand service invocation * do a fandango to appease lint * fix argument
- Loading branch information
Scott J. Miles
authored
May 9, 2019
1 parent
1bef847
commit 7837959
Showing
18 changed files
with
201 additions
and
31 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
shells/services/dynamic-import.js |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import 'particles/ServiceTest.particle' | ||
|
||
recipe ServiceTest | ||
ServiceTest |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
particle ServiceTest in './js/ServiceTest.js' | ||
consume root |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright (c) 2019 Google Inc. All rights reserved. | ||
* This code may only be used under the BSD style license found at | ||
* http://polymer.github.io/LICENSE.txt | ||
* Code distributed by Google as part of this project is also | ||
* subject to an additional IP rights grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
|
||
'use strict'; | ||
|
||
defineParticle(({DomParticle, log, html, resolver}) => { | ||
|
||
const template = html` | ||
<div> | ||
<img style="max-width: 240px;" src="{{imageUrl}}"><br> | ||
<div> | ||
<div>Label: </span><span>{{label}}</div> | ||
<div>Confidence: </span><span>{{probability}}</div> | ||
</div> | ||
</div> | ||
`; | ||
|
||
const url = resolver(`ServiceTest/../../assets/waltbird.jpg`); | ||
|
||
return class extends DomParticle { | ||
get template() { | ||
return template; | ||
} | ||
update({}, state) { | ||
if (!state.classified) { | ||
state.classified = true; | ||
this.classify(url); | ||
} | ||
} | ||
async classify(imageUrl) { | ||
const response = await this.service({call: 'ml5.classifyImage', imageUrl}); | ||
this.setState({response}); | ||
} | ||
render({}, {response}) { | ||
response = response || {label: '<working>', probability: '<working>'}; | ||
return { | ||
label: response.label, | ||
probability: response.probability, | ||
imageUrl: url | ||
}; | ||
} | ||
}; | ||
|
||
}); |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Copyright (c) 2019 Google Inc. All rights reserved. | ||
* This code may only be used under the BSD style license found at | ||
* http://polymer.github.io/LICENSE.txt | ||
* Code distributed by Google as part of this project is also | ||
* subject to an additional IP rights grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
export const dynamicImport = path => import(path); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Copyright (c) 2019 Google Inc. All rights reserved. | ||
* This code may only be used under the BSD style license found at | ||
* http://polymer.github.io/LICENSE.txt | ||
* Code distributed by Google as part of this project is also | ||
* subject to an additional IP rights grant found at | ||
* http://polymer.github.io/PATENTS.txt | ||
*/ | ||
import {dynamicImport} from './dynamic-import.js'; | ||
import {Services} from '../../build/runtime/services.js'; | ||
|
||
const requireMl5 = async () => { | ||
if (!window.ml5) { | ||
await dynamicImport('https://unpkg.com/[email protected]/dist/ml5.min.js'); | ||
} | ||
}; | ||
|
||
const classifyImage = async ({imageUrl}) => { | ||
console.log('classifying...'); | ||
await requireMl5(); | ||
const image = await loadImage(imageUrl); | ||
const classifier = await window.ml5.imageClassifier('MobileNet'); | ||
const results = await classifier.classify(image); | ||
const result = results.shift(); | ||
console.log('classifying done.'); | ||
return { | ||
label: result.label, | ||
probability: result.confidence.toFixed(4) | ||
}; | ||
}; | ||
|
||
const loadImage = async url => { | ||
return new Promise((resolve) => { | ||
const image = new Image(); | ||
image.src = url; | ||
image.onload = async () => resolve(image); | ||
}); | ||
}; | ||
|
||
Services.register('ml5', { | ||
classifyImage | ||
}); |
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
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
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
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
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
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
Oops, something went wrong.