Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flows for Image recognition
{
"id": "",
"revisionId": "",
"trigger": "onReceivedConversationMessage",
"options": {
"callbacks": [],
"targets": []
},
"metadata": {
"title": "Image recognition",
"isDraft": false,
"triggerIntent": "onReceivedTelegramMessage"
},
"steps": [
{
"id": "0c3e4f35-0950-44dd-8682-0a21a111de77",
"action": "switch",
"options": {
"cases": [
{
"conditions": [
{
"variable": "{{messageImage}}",
"operator": "isEmptyOrNotSet",
"value": "",
"options": {
"intent": "custom"
}
}
],
"steps": [
{
"id": "ffd13531-a3b9-41de-a2fa-0e515feed2fe",
"action": "sendConversationMessage",
"options": {
"content": {
"text": "Please send an image.",
"image": {
"url": ""
},
"audio": {
"url": ""
},
"video": {
"url": ""
},
"file": {
"url": ""
},
"location": {
"latitude": "",
"longitude": ""
},
"email": {
"from": {
"name": "",
"address": ""
},
"subject": "",
"content": {},
"headers": null
}
},
"type": "text",
"recipients": {
"conversationIds": [
"{{conversationId}}"
]
},
"intent": "replyConversationMessage",
"label": "Ask to send an image",
"highThroughput": false
}
},
{
"id": "3d752bc6-cf35-4971-8155-44a2bea4bb49",
"action": "endFlow",
"options": {
"intent": "endFlow"
}
}
],
"id": "aa_QVqjIn9"
}
],
"defaultCase": {
"steps": [
{
"id": "8f3748cf-9059-44fb-a177-bc0dab194e7b",
"action": "sendConversationMessage",
"options": {
"content": {
"text": "Thank you for the image! We started to detect a hotdog on the image.",
"image": {
"url": ""
},
"audio": {
"url": ""
},
"video": {
"url": ""
},
"file": {
"url": ""
},
"location": {
"latitude": "",
"longitude": ""
},
"email": {
"from": {
"name": "",
"address": ""
},
"subject": "",
"content": {},
"headers": null
}
},
"type": "text",
"recipients": {
"conversationIds": [
"{{conversationId}}"
]
},
"intent": "replyConversationMessage",
"label": "Send \"Thanks for the image\"",
"highThroughput": false
}
},
{
"id": "808debc0-974d-4b3f-bd4f-ed4efb30a499",
"action": "fetchVariables",
"options": {
"url": "https://your-function-address.cloudfunctions.net/HotDogOrNot",
"variableKeys": [
"isHotDog"
],
"intent": "fetchVariables",
"label": "Send image to VisionAPI",
"method": "POST",
"body": "{\"url\":\"{{messageImage}}\"}",
"contentType": "application/json"
}
},
{
"id": "c9f771fb-06ff-4362-b783-07e4bd3ff53d",
"action": "switch",
"options": {
"cases": [
{
"conditions": [
{
"variable": "{{isHotDog}}",
"operator": "==",
"value": "true",
"options": {
"intent": "custom"
}
}
],
"steps": [
{
"id": "02629417-e3ac-4bfa-94a9-83047c250d54",
"action": "sendConversationMessage",
"options": {
"content": {
"text": "There is a hotdog on the image. Thank you!",
"image": {
"url": ""
},
"audio": {
"url": ""
},
"video": {
"url": ""
},
"file": {
"url": ""
},
"location": {
"latitude": "",
"longitude": ""
},
"email": {
"from": {
"name": "",
"address": ""
},
"subject": "",
"content": {},
"headers": null
}
},
"type": "text",
"recipients": {
"conversationIds": [
"{{conversationId}}"
]
},
"intent": "replyConversationMessage",
"label": "Send \"we detected a hotdog!\"",
"highThroughput": false
}
}
],
"id": "AWzLv6jksY"
}
],
"defaultCase": {
"steps": [
{
"id": "b00034ce-db49-4ddf-bf8f-2be006e3fbbd",
"action": "sendConversationMessage",
"options": {
"content": {
"text": "Sorry, we didn't detect a hotdog on the image. Please try again.",
"image": {
"url": ""
},
"audio": {
"url": ""
},
"video": {
"url": ""
},
"file": {
"url": ""
},
"location": {
"latitude": "",
"longitude": ""
},
"email": {
"from": {
"name": "",
"address": ""
},
"subject": "",
"content": {},
"headers": null
}
},
"type": "text",
"recipients": {
"conversationIds": [
"{{conversationId}}"
]
},
"intent": "replyConversationMessage",
"label": "Notify that we didn't detect a hotdog",
"highThroughput": false
}
}
],
"id": "mwk5RoiCo"
},
"intent": "smsConditional"
}
},
{
"id": "8778c563-c045-4aa6-80e5-4c2a29b38b3f",
"action": "endFlow",
"options": {
"intent": "endFlow"
}
}
],
"id": "iuFXBNrWTr"
},
"intent": "smsConditional",
"label": "Check if user sent an image"
}
}
],
"published": true,
"createdAt": "2020-08-28T18:25:19.665815708Z",
"updatedAt": "2020-08-29T01:25:15.614170299Z",
"revisionCount": 26
}
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (request, response) => {
var url = request.query.url || request.body.url;
if (url == null || url == "" ) {
response.status(400).json({ error: "Must include a 'url' query parameter." });
}
getImageLabels(url)
.then(labels => {
// You can use 'console.log(labels);' command to check labels you got
// We filter all labels if they contain "hot dog" in label description
// And have a score > 0.6, which mean that VisionAPI is at least 60% sure that there is a hotdog on a picture
labels = labels.filter(function(label) {
return label.description.toLowerCase().includes("hot dog") && label.score > 0.6;
});
// If labels array contains at least 1 element, then we found a hot-dog!
if (labels.length > 0) {
response.status(200).json({isHotDog: true, error: ""});
} else {
response.status(200).json({isHotDog: false, error: ""});
}
})
.catch(err => {
response.status(500).json({ error: err });
});
};
async function getImageLabels(imageUrl) {
// Imports the Google Cloud client library
const vision = require('@google-cloud/vision');
// Creates a client
const client = new vision.ImageAnnotatorClient();
// Performs label detection on the image file
const [result] = await client.labelDetection(imageUrl);
const labels = result.labelAnnotations;
return labels
}
{
"name": "sample-http",
"version": "0.0.1",
"dependencies": {
"@google-cloud/vision": "^1.11.0"
}
}
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.helloWorld = (req, res) => {
let message = req.query.url ? "yes" : "no";
res.setHeader('Content-Type', 'application/json');
res.status(200).send(JSON.stringify({ isHotDog: message }));
};
{
"id": "",
"revisionId": "",
"trigger": "onReceivedConversationMessage",
"options": {
"callbacks": [],
"targets": []
},
"metadata": {
"title": "Image recognition",
"isDraft": false,
"triggerIntent": "onReceivedTelegramMessage"
},
"steps": [
{
"id": "19c3560f-a8d0-4787-8714-37c698108b69",
"action": "fetchVariables",
"options": {
"url": "https://your-function-address.cloudfunctions.net/HotDogOrNot",
"variableKeys": [
"isHotDog"
],
"intent": "fetchVariables",
"label": "Is there a hotdog on the image?",
"method": "POST",
"body": "{\"url\":\"{{messageImage}}\"}",
"contentType": "application/json"
}
},
{
"id": "ca9314a2-2f9d-489c-b4b1-50fc7a0b2cb6",
"action": "sendConversationMessage",
"options": {
"content": {
"text": "Hotdog on the image? {{isHotDog}}",
"image": {
"url": ""
},
"audio": {
"url": ""
},
"video": {
"url": ""
},
"file": {
"url": ""
},
"location": {
"latitude": "",
"longitude": ""
},
"email": {
"from": {
"name": "",
"address": ""
},
"subject": "",
"content": {},
"headers": null
}
},
"type": "text",
"recipients": {
"conversationIds": [
"{{conversationId}}"
]
},
"intent": "replyConversationMessage",
"highThroughput": false
}
}
],
"published": true,
"createdAt": "2020-08-28T18:25:19.665815708Z",
"updatedAt": "2020-08-29T01:15:43.669252097Z",
"revisionCount": 22
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.