Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active December 26, 2018 14:36
Show Gist options
  • Save wmakeev/56e5e718a5242538fec17e758b5379be to your computer and use it in GitHub Desktop.
Save wmakeev/56e5e718a5242538fec17e758b5379be to your computer and use it in GitHub Desktop.
Google Cloud #google #cloud #automl
# https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu
# Create environment variable for correct distribution
export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"
# Add the Cloud SDK distribution URI as a package source
echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list
# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
# Update the package list and install the Cloud SDK
sudo apt-get update && sudo apt-get install google-cloud-sdk
'use strict'
const fs = require('fs')
const path = require('path')
const fetch = require('node-fetch')
const { shell } = require('execa')
const { promisify } = require('util')
const sh = cmd => shell(cmd).then(out => out.stdout)
const readFile = promisify(fs.readFile)
// process.env.GOOGLE_APPLICATION_CREDENTIALS == 'path/to/google-credentials.json'
const MODEL_ID = 'ICN...' // AutoML model id
const AUTOML_URL =
`https://automl.googleapis.com/v1beta1/projects/vensi-cloud/locations/us-central1/models/${MODEL_ID}:predict`
async function main () {
const [accessToken, thumbBuffer] = await Promise.all([
sh(`gcloud auth application-default print-access-token`),
readFile(path.join(__dirname, 'thumb.jpg')) // read sample image
])
const response = await fetch(AUTOML_URL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
payload: {
image: {
imageBytes: thumbBuffer.toString('base64')
}
}
})
})
// {"payload":[{"classification":{"score":0.9999933},"displayName":"head"}]}
const predictions = await response.json().then(it => it.payload)
const displayName = predictions.reduce((res, item) => {
return item.classification.score > res.classification.score
? item : res
}).displayName
console.log(displayName)
}
main()
'use strict'
const fs = require('fs')
const path = require('path')
const { promisify } = require('util')
// https://cloud.google.com/nodejs/docs/reference/automl/0.1.x/v1beta1.PredictionServiceClient
const Automl = require('@google-cloud/automl')
const predictionService = new Automl.PredictionServiceClient()
const readFile = promisify(fs.readFile)
// process.env.GOOGLE_APPLICATION_CREDENTIALS == 'path/to/google-credentials.json'
async function main () {
const thumbBuffer = await readFile(path.join(__dirname, 'thumb.jpg'))
const payload = {
image: {
imageBytes: thumbBuffer.toString('base64')
}
}
const formattedName = predictionService.modelPath(
'my_prj', // project
'us-central1', // location
'ICN6576...') // model
// [{"classification":{"score":0.9999933},"displayName":"head"}]
const predictions = await predictionService.predict({
name: formattedName,
payload
}).then(resp => resp[0].payload)
const displayName = predictions.reduce((res, item) => {
return item.classification.score > res.classification.score
? item : res
}).displayName
console.log(displayName)
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment