-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathapp.js
441 lines (382 loc) · 17.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
const express = require("express");
const app = express();
const multer = require("multer");
const upload = multer({
storage: multer.diskStorage({}),
fileFilter: (req, file, cb) => {
let ext = path.extname(file.originalname);
if (ext !== ".jpg" && ext !== ".jpeg" && ext !== ".png") {
cb(new Error("File type is not supported"), false);
return;
}
cb(null, true);
},
});
//MS Specific
const axios = require("axios").default;
const async = require("async");
const fs = require("fs");
const https = require("https");
const path = require("path");
const createReadStream = require("fs").createReadStream;
const sleep = require("util").promisify(setTimeout);
const ComputerVisionClient =
require("@azure/cognitiveservices-computervision").ComputerVisionClient;
const ApiKeyCredentials = require("@azure/ms-rest-js").ApiKeyCredentials;
require("dotenv").config({ path: "./config/.env" });
const cloudinary = require("cloudinary").v2;
cloudinary.config({
cloud_name: process.env.CLOUD_NAME,
api_key: process.env.CLOUD_API_KEY,
api_secret: process.env.CLOUD_API_SECRET,
});
const key = process.env.MS_COMPUTER_VISION_SUBSCRIPTION_KEY;
const endpoint = process.env.MS_COMPUTER_VISION_ENDPOINT;
const faceEndpoint = process.env.MS_FACE_ENDPOINT;
const subscriptionKey = process.env.MS_FACE_SUB_KEY;
const computerVisionClient = new ComputerVisionClient(
new ApiKeyCredentials({ inHeader: { "Ocp-Apim-Subscription-Key": key } }),
endpoint
);
//Server Setup
app.set("view engine", "ejs");
app.use(express.static("public"));
//Routes
app.get("/", (req, res) => {
res.render("index.ejs");
});
app.post("/", upload.single("file-to-upload"), async (req, res) => {
try {
// Upload image to cloudinary
const result = await cloudinary.uploader.upload(req.file.path);
const brandURLImage = result.secure_url;
async.series([
async function () {
// </snippet_functiondef_begin>
/**
* DESCRIBE IMAGE
* Describes what the main objects or themes are in an image.
* Describes both a URL and a local image.
*/
// console.log('-------------------------------------------------');
// console.log('DESCRIBE IMAGE');
// console.log();
// // <snippet_describe_image>
// const describeURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/celebrities.jpg';
// // </snippet_describe_image>
// // const describeImagePath = __dirname + '\\celebrities.jpeg';
// // try {
// // await downloadFilesToLocal(describeURL, describeImagePath);
// // } catch {
// // console.log('>>> Download sample file failed. Sample cannot continue');
// // process.exit(1);
// // }
// // <snippet_describe>
// // Analyze URL image
// console.log('Analyzing URL image to describe...', describeURL.split('/').pop());
// const caption = (await computerVisionClient.describeImage(describeURL)).captions[0];
// console.log(`This may be ${caption.text} (${caption.confidence.toFixed(2)} confidence)`);
// // </snippet_describe>
// // Analyze local image
// console.log('\nAnalyzing local image to describe...', path.basename(describeImagePath));
// // DescribeImageInStream takes a function that returns a ReadableStream, NOT just a ReadableStream instance.
// const captionLocal = (await computerVisionClient.describeImageInStream(
// () => createReadStream(describeImagePath))).captions[0];
// console.log(`This may be ${caption.text} (${captionLocal.confidence.toFixed(2)} confidence)`);
// /**
// * END - Describe Image
// */
// console.log();
/**
* DETECT FACES
* This example detects faces and returns its:
* gender, age, location of face (bounding box), confidence score, and size of face.
*/
console.log('-------------------------------------------------');
console.log('DETECT FACES');
console.log();
// <snippet_faces>
const facesImageURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/faces.jpg';
// Analyze URL image.
console.log('Analyzing faces in image...', facesImageURL.split('/').pop());
// Get the visual feature for 'Faces' only.
const faces = (await computerVisionClient.analyzeImage(facesImageURL, { visualFeatures: ['Faces'] })).faces;
// Print the bounding box, gender, and age from the faces.
if (faces.length) {
console.log(`${faces.length} face${faces.length == 1 ? '' : 's'} found:`);
for (const face of faces) {
console.log(` Gender: ${face.gender}`.padEnd(20)
+ ` Age: ${face.age}`.padEnd(10) + `at ${formatRectFaces(face.faceRectangle)}`);
}
} else { console.log('No faces found.'); }
// </snippet_faces>
// <snippet_formatfaces>
// Formats the bounding box
function formatRectFaces(rect) {
return `top=${rect.top}`.padEnd(10) + `left=${rect.left}`.padEnd(10) + `bottom=${rect.top + rect.height}`.padEnd(12)
+ `right=${rect.left + rect.width}`.padEnd(10) + `(${rect.width}x${rect.height})`;
}
// </snippet_formatfaces>
/**
* END - Detect Faces
*/
console.log();
/**
* DETECT OBJECTS
* Detects objects in URL image:
* gives confidence score, shows location of object in image (bounding box), and object size.
*/
console.log('-------------------------------------------------');
console.log('DETECT OBJECTS');
console.log();
// <snippet_objects>
// Image of a dog
const objectURL = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-node-sdk-samples/master/Data/image.jpg';
// Analyze a URL image
console.log('Analyzing objects in image...', objectURL.split('/').pop());
const objects = (await computerVisionClient.analyzeImage(objectURL, { visualFeatures: ['Objects'] })).objects;
console.log();
// Print objects bounding box and confidence
if (objects.length) {
console.log(`${objects.length} object${objects.length == 1 ? '' : 's'} found:`);
for (const obj of objects) { console.log(` ${obj.object} (${obj.confidence.toFixed(2)}) at ${formatRectObjects(obj.rectangle)}`); }
} else { console.log('No objects found.'); }
// </snippet_objects>
// <snippet_objectformat>
// Formats the bounding box
function formatRectObjects(rect) {
return `top=${rect.y}`.padEnd(10) + `left=${rect.x}`.padEnd(10) + `bottom=${rect.y + rect.h}`.padEnd(12)
+ `right=${rect.x + rect.w}`.padEnd(10) + `(${rect.w}x${rect.h})`;
}
// </snippet_objectformat>
/**
* END - Detect Objects
*/
console.log();
/**
* DETECT TAGS
* Detects tags for an image, which returns:
* all objects in image and confidence score.
*/
// <snippet_tags>
console.log('-------------------------------------------------');
console.log('DETECT TAGS');
console.log();
// Image of different kind of dog.
const tagsURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';
// Analyze URL image
console.log('Analyzing tags in image...', tagsURL.split('/').pop());
const tags = (await computerVisionClient.analyzeImage(tagsURL, { visualFeatures: ['Tags'] })).tags;
console.log(`Tags: ${formatTags(tags)}`);
// </snippet_tags>
// <snippet_tagsformat>
// Format tags for display
function formatTags(tags) {
return tags.map(tag => (`${tag.name} (${tag.confidence.toFixed(2)})`)).join(', ');
}
// </snippet_tagsformat>
/**
* END - Detect Tags
*/
console.log();
/**
* DETECT TYPE
* Detects the type of image, says whether it is clip art, a line drawing, or photograph).
*/
console.log('-------------------------------------------------');
console.log('DETECT TYPE');
console.log();
// <snippet_imagetype>
const typeURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-python-sdk-samples/master/samples/vision/images/make_things_happen.jpg';
// Analyze URL image
console.log('Analyzing type in image...', typeURLImage.split('/').pop());
const types = (await computerVisionClient.analyzeImage(typeURLImage, { visualFeatures: ['ImageType'] })).imageType;
console.log(`Image appears to be ${describeType(types)}`);
// </snippet_imagetype>
// <snippet_imagetype_describe>
function describeType(imageType) {
if (imageType.clipArtType && imageType.clipArtType > imageType.lineDrawingType) return 'clip art';
if (imageType.lineDrawingType && imageType.clipArtType < imageType.lineDrawingType) return 'a line drawing';
return 'a photograph';
}
// </snippet_imagetype_describe>
/**
* END - Detect Type
*/
console.log();
/**
* DETECT CATEGORY
* Detects the categories of an image. Two different images are used to show the scope of the features.
*/
console.log('-------------------------------------------------');
console.log('DETECT CATEGORY');
console.log();
// <snippet_categories>
const categoryURLImage = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';
// Analyze URL image
console.log('Analyzing category in image...', categoryURLImage.split('/').pop());
const categories = (await computerVisionClient.analyzeImage(categoryURLImage)).categories;
console.log(`Categories: ${formatCategories(categories)}`);
// </snippet_categories>
// <snippet_categories_format>
// Formats the image categories
function formatCategories(categories) {
categories.sort((a, b) => b.score - a.score);
return categories.map(cat => `${cat.name} (${cat.score.toFixed(2)})`).join(', ');
}
// </snippet_categories_format>
/**
* END - Detect Categories
*/
console.log();
/**
* DETECT BRAND
* Detects brands and logos that appear in an image.
*/
console.log('-------------------------------------------------');
console.log('DETECT BRAND');
console.log();
// <snippet_brands>
const brandURLImage = 'https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/images/red-shirt-logo.jpg';
// Analyze URL image
console.log('Analyzing brands in image...', brandURLImage.split('/').pop());
const brands = (await computerVisionClient.analyzeImage(brandURLImage, { visualFeatures: ['Brands'] })).brands;
// Print the brands found
if (brands.length) {
console.log(`${brands.length} brand${brands.length != 1 ? 's' : ''} found:`);
for (const brand of brands) {
console.log(` ${brand.name} (${brand.confidence.toFixed(2)} confidence)`);
}
} else { console.log(`No brands found.`); }
// </snippet_brands>
console.log();
/**
* DETECT COLOR SCHEME
* Detects the color scheme of an image, including foreground, background, dominant, and accent colors.
*/
console.log('-------------------------------------------------');
console.log('DETECT COLOR SCHEME');
console.log();
// <snippet_colors>
const colorURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/celebrities.jpg';
// Analyze URL image
console.log('Analyzing image for color scheme...', colorURLImage.split('/').pop());
console.log();
const color = (await computerVisionClient.analyzeImage(colorURLImage, { visualFeatures: ['Color'] })).color;
printColorScheme(color);
// </snippet_colors>
// <snippet_colors_print>
// Print a detected color scheme
function printColorScheme(colors) {
console.log(`Image is in ${colors.isBwImg ? 'black and white' : 'color'}`);
console.log(`Dominant colors: ${colors.dominantColors.join(', ')}`);
console.log(`Dominant foreground color: ${colors.dominantColorForeground}`);
console.log(`Dominant background color: ${colors.dominantColorBackground}`);
console.log(`Suggested accent color: #${colors.accentColor}`);
}
// </snippet_colors_print>
/**
* END - Detect Color Scheme
*/
console.log();
/**
* GENERATE THUMBNAIL
* This example generates a thumbnail image of a specified size, from a URL and a local image.
*/
console.log('-------------------------------------------------');
console.log('GENERATE THUMBNAIL');
console.log();
// Image of a dog.
const dogURL = 'https://moderatorsampleimages.blob.core.windows.net/samples/sample16.png';
console.log('Generating thumbnail...')
await computerVisionClient.generateThumbnail(100, 100, dogURL, { smartCropping: true })
.then((thumbResponse) => {
const destination = fs.createWriteStream("thumb.png")
thumbResponse.readableStreamBody.pipe(destination)
console.log('Thumbnail saved.') // Saves into root folder
})
console.log();
/**
* END - Generate Thumbnail
*/
/**
* DETECT DOMAIN-SPECIFIC CONTENT
* Detects landmarks or celebrities.
*/
console.log('-------------------------------------------------');
console.log('DETECT DOMAIN-SPECIFIC CONTENT');
console.log();
// <snippet_domain_image>
const domainURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/landmark.jpg';
// </snippet_domain_image>
// <snippet_landmarks>
// Analyze URL image
console.log('Analyzing image for landmarks...', domainURLImage.split('/').pop());
const domain = (await computerVisionClient.analyzeImageByDomain('landmarks', domainURLImage)).result.landmarks;
// Prints domain-specific, recognized objects
if (domain.length) {
console.log(`${domain.length} ${domain.length == 1 ? 'landmark' : 'landmarks'} found:`);
for (const obj of domain) {
console.log(` ${obj.name}`.padEnd(20) + `(${obj.confidence.toFixed(2)} confidence)`.padEnd(20) + `${formatRectDomain(obj.faceRectangle)}`);
}
} else {
console.log('No landmarks found.');
}
// </snippet_landmarks>
// <snippet_landmarks_rect>
// Formats bounding box
function formatRectDomain(rect) {
if (!rect) return '';
return `top=${rect.top}`.padEnd(10) + `left=${rect.left}`.padEnd(10) + `bottom=${rect.top + rect.height}`.padEnd(12) +
`right=${rect.left + rect.width}`.padEnd(10) + `(${rect.width}x${rect.height})`;
}
// </snippet_landmarks_rect>
console.log();
/**
* DETECT ADULT CONTENT
* Detects "adult" or "racy" content that may be found in images.
* The score closer to 1.0 indicates racy/adult content.
* Detection for both local and URL images.
*/
console.log('-------------------------------------------------');
console.log('DETECT ADULT CONTENT');
console.log();
// <snippet_adult_image>
// The URL image and local images are not racy/adult.
// Try your own racy/adult images for a more effective result.
const adultURLImage = 'https://raw.githubusercontent.com/Azure-Samples/cognitive-services-sample-data-files/master/ComputerVision/Images/celebrities.jpg';
// </snippet_adult_image>
// <snippet_adult>
// Function to confirm racy or not
const isIt = flag => flag ? 'is' : "isn't";
// Analyze URL image
console.log('Analyzing image for racy/adult content...', adultURLImage.split('/').pop());
const adult = (await computerVisionClient.analyzeImage(adultURLImage, {
visualFeatures: ['Adult']
})).adult;
console.log(`This probably ${isIt(adult.isAdultContent)} adult content (${adult.adultScore.toFixed(4)} score)`);
console.log(`This probably ${isIt(adult.isRacyContent)} racy content (${adult.racyScore.toFixed(4)} score)`);
// </snippet_adult>
console.log();
/**
* END - Detect Adult Content
*/
console.log();
console.log('-------------------------------------------------');
console.log('End of quickstart.');
// <snippet_functiondef_end>
},
function () {
return new Promise((resolve) => {
resolve();
})
}
], (err) => {
throw (err);
});
res.render("result.ejs", { brands: brands, img: brandURLImage });
} catch (err) {
console.log(err);
}
});
app.listen(process.env.PORT || 8000);