forked from pulumi/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video-label-processor.js
107 lines (91 loc) · 3.59 KB
/
video-label-processor.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
// Copyright 2016-2018, Pulumi Corporation. All rights reserved.
"use strict";
const cloud = require("@pulumi/cloud-aws");
const aws = require("@pulumi/aws");
class VideoLabelProcessor {
constructor() {
let topic = new cloud.Topic("AmazonRekognitionTopic");
let role = createRekognitionRole();
this.startRekognitionJob = (bucketName, filename) => {
var aws = require("aws-sdk");
var rekognition = new aws.Rekognition();
var params = {
Video: {
S3Object: {
Bucket: bucketName,
Name: filename,
}
},
NotificationChannel: {
RoleArn: role.arn.get(),
SNSTopicArn: topic.topic.arn.get(),
}
};
rekognition.startLabelDetection(params, (err, data) => {
if (!err) {
console.log(`*** Submitted Rekognition job for ${filename}`);
} else {
console.log(err, err.stack);
}
});
}
this.onLabelResult = (searchLabel, action) => {
topic.subscribe("labelResults", (jobStatus) => {
console.log("*** Rekognition job complete");
if (jobStatus.Status == "SUCCEEDED" && jobStatus.API == "StartLabelDetection") {
var aws = require("aws-sdk");
var rekognition = new aws.Rekognition();
rekognition.getLabelDetection( { JobId: jobStatus.JobId },
function (err, data) {
if (!err) {
// call callback to process the video at a timestamp
let timestamp = getTimestampForLabel(data.Labels, searchLabel).toString();
action(jobStatus.Video.S3ObjectName, timestamp);
} else {
console.log(err, err.stack);
}
}
);
}
});
}
}
}
function getTimestampForLabel(labels, filterName) {
console.log(`Raw label results: ${JSON.stringify(labels)}`);
let bestTimestamp = 0;
let highestConfidence = 0;
labels.forEach(element => {
if (element.Label.Name.toLowerCase() == filterName.toLowerCase() &&
element.Label.Confidence > highestConfidence) {
highestConfidence = element.Label.Confidence;
bestTimestamp = element.Timestamp;
console.log(` *** Found object ${element.Label.Name} at position ${bestTimestamp}. Confidence = ${highestConfidence}`);
}
});
return bestTimestamp / 1000; // convert to milliseconds
}
function createRekognitionRole() {
let policy = {
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "rekognition.amazonaws.com",
},
"Effect": "Allow",
"Sid": "",
},
],
};
let role = new aws.iam.Role("rekognition-role", {
assumeRolePolicy: JSON.stringify(policy),
});
let serviceRoleAccess = new aws.iam.RolePolicyAttachment("rekognition-access", {
role: role,
policyArn: "arn:aws:iam::aws:policy/service-role/AmazonRekognitionServiceRole", // use managed AWS policy
});
return role;
}
module.exports.VideoLabelProcessor = VideoLabelProcessor;