Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump hoek from 4.2.0 to 4.2.1 in /azure-demo-function-text #1

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Dependency directory
azure-demo-function-face/node_modules
azure-demo-function-image/node_modules
azure-demo-function-text/node_modules
# Dependency directory
azure-demo-function-face/node_modules
azure-demo-function-image/node_modules
azure-demo-function-text/node_modules
obj
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
![ANS](Images/ans_logo_small.png)
# Azure Bootcamp
For more information on the ANS Azure Bootcamp visit https://www.ans.co.uk

# Node.JS Azure Function
Node.JS code for the function app deployed in the ANS Azure Bootcamp.

![ANS](Images/ans_logo_small.png)
# Azure Bootcamp
For more information on the ANS Azure Bootcamp visit https://www.ans.co.uk
# Node.JS Azure Function v2
Node.JS code for the function app deployed in the ANS Azure Bootcamp.
![Diagram](Images/Serverless-Middleware.jpg)
54 changes: 27 additions & 27 deletions azure-demo-function-face/function.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"dataType": "binary",
"direction": "in",
"path": "face/{name}",
"connection": "AzureWebJobsStorage"
},
{
"type": "table",
"name": "imageTableInfo",
"tableName": "imageTable",
"connection": "AzureWebJobsStorage",
"direction": "out"
},
{
"name": "outputBlob",
"type": "blob",
"dataType": "binary",
"direction": "out",
"path": "thumbs/{name}",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
{
"bindings": [
{
"name": "myBlob",
"type": "blobTrigger",
"dataType": "binary",
"direction": "in",
"path": "face/{name}",
"connection": "AzureWebJobsStorage"
},
{
"type": "table",
"name": "imageTableInfo",
"tableName": "imageTable",
"connection": "AzureWebJobsStorage",
"direction": "out"
},
{
"name": "outputBlob",
"type": "blob",
"dataType": "binary",
"direction": "out",
"path": "thumbs/{name}",
"connection": "AzureWebJobsStorage"
}
],
"disabled": false
}
256 changes: 131 additions & 125 deletions azure-demo-function-face/index.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,131 @@
module.exports = function (context, myBlob) {

var FaceAPIClient = require('azure-cognitiveservices-face');
var CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
var azure = require('azure-storage');
var request = require("request");

var imageUri = context.bindingData.uri;
context.log(imageUri);

//Split https:// from url
var imageUriArray = imageUri.split("//");
//Split url path
imageUriArray = imageUriArray[1].split("/")

//Create Blob Service
var blobService = azure.createBlobService();

//Creates container if not exists
blobService.createContainerIfNotExists('thumbs', {publicAccessLevel : 'blob'}, function(error) {
if(error) {
context.log(error);
};
});

//Replace "images" container to "thumbs"
imageUriArray[1] = "thumbs"

//Build url path
var thumbsPath = imageUriArray.join("/");
var thumbUri = "https://" + thumbsPath;
context.log(thumbUri);

var keyVar = 'AZURE_COMPUTER_VISION_KEY';
var keyVarFace = 'AZURE_COMPUTER_VISION_FACE_KEY';
var keyRegion = 'AZURE_COMPUTER_VISION_REGION';

if (!process.env[keyVar] || !process.env[keyVarFace] || !process.env[keyRegion]) {
throw new Error('please set/export the following environment variables: ' + keyVar + ' ' + keyVarFace + ' ' + keyRegion);
}

let serviceKey = process.env[keyVar];
let serviceKeyFace = process.env[keyVarFace];
let region = process.env[keyRegion];

let credentials = new CognitiveServicesCredentials(serviceKeyFace);
let client = new FaceAPIClient(credentials, region);

context.log("Image name: " + context.bindingData.name);

imageQuery();

//image query
function imageQuery(){
client.face.detectInStream(myBlob, {returnFaceAttributes: ['age','gender','smile','facialHair','glasses','emotion','hair','makeup']})

.then(function(data){
// write to azure table
context.log("data: " + JSON.stringify(data));
context.bindings.imageTableInfo = [];
context.bindings.imageTableInfo.push({
PartitionKey: 'face',
RowKey: context.bindingData.name,
data: {
"api" : "face",
"imageUri" : imageUri,
"thumbUri" : thumbUri,
"faceAttributes" : data[0].faceAttributes
}
})

thumbnail(imageUri, function (error, outputBlob) {

if (error) {
context.log("No Output Blob");
context.log("Error: "+ error);
context.done(null, error);
}
else {
context.log("Output Blob")
context.bindings.outputBlob = outputBlob;
context.done(null);
};
})
})

.catch(function(err) {
context.log(`Error: ${err}`);
context.done(null, err);
})

};

//create thumbnails
function thumbnail(imageUri, callback) {
var options = { method: 'POST',
url: 'https://'+region+'.api.cognitive.microsoft.com/vision/v1.0/generateThumbnail',
qs: { width: '95', height: '95', smartCropping: 'true' },
headers:
{
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': serviceKey,
'Content-Type': 'application/json' },
body: { url: imageUri },
encoding: null,
json: true
};

request(options, function (error, response, body) {

if (error){

// Call the callback and pass in the error
callback(error, null);
}
else {

context.log("Status Code: " + response.statusCode);

// Call the callback and pass in the body
callback(null, body);
};
});
};
};
module.exports = async function (context, myBlob) {

var Vision = require('azure-cognitiveservices-vision');
var CognitiveServicesCredentials = require('ms-rest-azure').CognitiveServicesCredentials;
var azure = require('azure-storage');
var request = require("request");

var imageUri = context.bindingData.uri;
context.log(imageUri);

//Split https:// from url
var imageUriArray = imageUri.split("//");
//Split url path
imageUriArray = imageUriArray[1].split("/")

//Create Blob Service
var blobService = azure.createBlobService();

//Creates container if not exists
blobService.createContainerIfNotExists('thumbs', {publicAccessLevel : 'blob'}, function(error) {
if(error) {
context.log(error);
};
});

//Replace "images" container to "thumbs"
imageUriArray[1] = "thumbs"

//Build url path
var thumbsPath = imageUriArray.join("/");
var thumbUri = "https://" + thumbsPath;
context.log(thumbUri);


var keyCognitive = 'AZURE_COGNITIVE_SERVICES_KEY';
var keyRegion = 'AZURE_COGNITIVE_SERVICES_REGION';

if (!process.env[keyCognitive] || !process.env[keyRegion]) {
throw new Error('please set/export the following environment variables: ' + keyCognitive + ' ' + keyRegion);
}

let serviceKey = process.env[keyCognitive];
let region =process.env[keyRegion];

let endpoint = 'https://'+region +'.api.cognitive.microsoft.com';

let credentials = new CognitiveServicesCredentials(serviceKey);
let faceApiClient = new Vision.FaceAPIClient(credentials, endpoint);

context.log("Image name: " + context.bindingData.name);

//image query
function imageQuery(){
context.log("Calling Face API")

faceApiClient.face.detectWithStream(myBlob, {returnFaceAttributes: ['age','gender','smile','facialHair','glasses','emotion','hair','makeup']})

.then(function(data){
// write to azure table
context.log("data: " + JSON.stringify(data));
context.bindings.imageTableInfo = [];
context.bindings.imageTableInfo.push({
PartitionKey: 'face',
RowKey: context.bindingData.name,
data: {
"api" : "face",
"imageUri" : imageUri,
"thumbUri" : thumbUri,
"faceAttributes" : data[0].faceAttributes
}
})

thumbnail(imageUri, function (error, outputBlob) {

if (error) {
context.log("No Output Blob");
context.log("Error: "+ error);
context.done(null, error);
}
else {
context.log("Output Blob")
context.bindings.outputBlob = outputBlob;
context.done(null);
};
})
})

.catch(function(err) {
context.log(`Error: ${err}`);
context.done(null, err);
})

};

//create thumbnails
function thumbnail(imageUri, callback) {
context.log("Calling Thumbnail API")

var options = { method: 'POST',
url: 'https://'+region+'.api.cognitive.microsoft.com/vision/v1.0/generateThumbnail',
qs: { width: '95', height: '95', smartCropping: 'true' },
headers:
{
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': serviceKey,
'Content-Type': 'application/json' },
body: { url: imageUri },
encoding: null,
json: true
};

request(options, function (error, response, body) {

if (error){

// Call the callback and pass in the error
callback(error, null);
}
else {

context.log("Status Code: " + response.statusCode);

// Call the callback and pass in the body
callback(null, body);
};
});
};

imageQuery();

};
Loading