Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.
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
29 changes: 19 additions & 10 deletions cognitive-services/cognitive-services/computervision.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script type="text/javascript">
RED.nodes.registerType('Computer Vision', {
category: 'analysis',
category: 'Microsoft Cognitive Services',
color: '#049580',
defaults: {
operation: {
value: "description",
value: "Description",
required: true
},
endpoint: {
value: "https://westus.api.cognitive.microsoft.com/",
required: true
},
name: {
Expand All @@ -27,18 +31,23 @@

<script type="text/x-red" data-template-name="Computer Vision">
<div class="form-row">
<label for="node-input-operation"><i class="icon-tag"></i> Operation (<a href="https://dev.projectoxford.ai/docs/services/56f91f2d778daf23d8ec6739/operations/56f91f2e778daf14a499e1fa" target="_blank">See details</a>)</label>
<label for="node-input-operation"><i class="icon-tag"></i> Operation (<a href="https://westcentralus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-1-ga/operations/56f91f2e778daf14a499f21b" target="_blank">Documentation</a>)</label>
<select type="text" id="node-input-operation">
<option value="tags">Tags</option>
<option value="description">Description</option>
<option value="age">Face Age</option>
<option value="gender">Face Gender</option>
<option value="adult">Adult Score</option>
<option value="Adult">Adult Score</option>
<option value="Color">Color</option>
<option value="Description">Description</option>
<option value="Faces Age">Face Age</option>
<option value="Faces Gender">Face Gender</option>
<option value="Tags">Tags</option>
</select>
</div>
<div class="form-row">
<label for="node-input-key"><i class="icon-tag"></i> Subscription (<a href="https://www.microsoft.com/cognitive-services" target="_blank">Get key</a>)</label>
<input type="password" id="node-input-key" placeholder="Paste subscription key of West US region">
<label for="node-input-endpoint"><i class="icon-tag"></i> Endpoint </label>
<input type="text" id="node-input-endpoint" placeholder="Paste endpoint url from 'Keys and Endpoints'">
</div>
<div class="form-row">
<label for="node-input-key"><i class="icon-tag"></i> Key </label>
<input type="password" id="node-input-key" placeholder="Paste key from 'Keys and Endpoints'">
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name (Optional)</label>
Expand Down
50 changes: 41 additions & 9 deletions cognitive-services/cognitive-services/computervision.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// https://github.com/Azure/node-red-contrib-azure/blob/master/cognitive-services/cognitive-services/computervision.js
// https://westcentralus.dev.cognitive.microsoft.com/docs/services/computer-vision-v3-1-ga/operations/56f91f2e778daf14a499f21b

var request = require('request');

module.exports = function(RED)
Expand All @@ -15,13 +18,25 @@ module.exports = function(RED)
node.status({fill: "red", shape: "ring", text: "Error"});
console.log("Input subscription key");
}
// 2020-12-31 DM
else if (config.endpoint == null || config.endpoint == "")
{
node.error("Input endpoint address", msg);
node.status({fill: "red", shape: "rint", text: "Error"});
console.log("Input endpoint address");
}
else
{
var options = null;
// 2020-12-31 DM
//var endpoint = "https://westus.api.cognitive.microsoft.com/";
var endpoint = config.endpoint;
//var visualFeatures = "Categories,Tags,Description,Faces,ImageType,Color,Adult";
var visualFeatures = config.operation.split(" ")[0];
if (Buffer.isBuffer(msg.payload))
{
options = {
url: 'https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories,Tags,Description,Faces,ImageType,Color,Adult',
url: endpoint + 'vision/v3.1/analyze?visualFeatures=' + visualFeatures,
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': this.credentials.key,
Expand All @@ -33,7 +48,7 @@ module.exports = function(RED)
else if (typeof(msg.payload) == 'string' && (msg.payload.indexOf('http://') === 0 || msg.payload.indexOf('https://') === 0))
{
options = {
url: 'https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze?visualFeatures=Categories,Tags,Description,Faces,ImageType,Color,Adult',
url: endpoint + 'vision/v3.1/analyze?visualFeatures=' + visualFeatures,
method: 'POST',
headers: {
'Ocp-Apim-Subscription-Key': this.credentials.key,
Expand All @@ -58,9 +73,9 @@ module.exports = function(RED)
console.log("response.statusCode=" + response.statusCode + ", body=" + JSON.stringify(body));
if (response.statusCode == 200 && body != null)
{
if (config.operation == "tags") // Tags
if (config.operation == "Tags") // Tags
{
if (body.tags.length > 0 && body.categories[0].name != null)
if (body.tags.length > 0)
{
var tmp = body.tags.sort(function(a, b) {
return b.confidence - a.confidence;
Expand All @@ -80,7 +95,7 @@ module.exports = function(RED)
node.send(msg);
node.status({});
}
else if (config.operation == "description") // Description
else if (config.operation == "Description") // Description
{
if (body != null && body.description != null && body.description.captions != null && body.description.captions.length > 0)
{
Expand All @@ -97,7 +112,7 @@ module.exports = function(RED)
node.send(msg);
node.status({});
}
else if (config.operation == "age") // Faces(age)
else if (config.operation == "Faces Age") // Faces(age)
{
if (body.faces != null && body.faces.length > 0 && body.faces[0].age != null)
{
Expand All @@ -111,7 +126,7 @@ module.exports = function(RED)
node.send(msg);
node.status({});
}
else if (config.operation == "gender") // Faces(gender)
else if (config.operation == "Faces Gender") // Faces(gender)
{
if (body.faces != null && body.faces.length > 0 && body.faces[0].gender != null)
{
Expand All @@ -125,7 +140,7 @@ module.exports = function(RED)
node.send(msg);
node.status({});
}
else if (config.operation == "adult") // Adult
else if (config.operation == "Adult") // Adult
{
if (body.adult != null && body.adult.adultScore != null)
{
Expand All @@ -139,6 +154,23 @@ module.exports = function(RED)
node.send(msg);
node.status({});
}
else if (config.operation == "Color")
{
if (body.color != null && body.color.dominantColorForeground != null && body.color.dominantColorBackground != null)
{
msg.payload = {
foregroundColor: body.color.dominantColorForeground,
backgroundColor: body.color.dominantColorBackground
};
}
else
{
msg.payload = null;
}
msg.detail = body;
node.send(msg);
node.status({});
}
else
{
node.error("Unsupported operation: " + config.operation);
Expand Down Expand Up @@ -181,4 +213,4 @@ module.exports = function(RED)
}
}
});
}
}