Skip to content

Commit 1326415

Browse files
morgandugcf-owl-bot[bot]
authored and
Ace Nassri
committed
samples: add streaming_automl_object_tracking sample (#547)
* samples: add streaming_automl_object_tracking sample * fix: skip checking doc link before merged * 🦉 Updates from OwlBot Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent d4db78a commit 1326415

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main(
18+
path = 'YOUR_LOCAL_FILE',
19+
projectId = 'YOUR_GCP_PROJECT',
20+
modelId = 'YOUR_AUTOML_MODELID'
21+
) {
22+
// [START video_streaming_automl_object_tracking_beta]
23+
/**
24+
* TODO(developer): Uncomment these variables before running the sample.
25+
*/
26+
// const path = 'Local file to analyze, e.g. ./my-file.mp4';
27+
// const modelId = 'AutoML model'
28+
// const projectId = 'Your GCP Project'
29+
30+
const {StreamingVideoIntelligenceServiceClient} =
31+
require('@google-cloud/video-intelligence').v1p3beta1;
32+
const fs = require('fs');
33+
34+
// Instantiates a client
35+
const client = new StreamingVideoIntelligenceServiceClient();
36+
37+
// Streaming configuration
38+
const modelName = `projects/${projectId}/locations/us-central1/models/${modelId}`;
39+
const configRequest = {
40+
videoConfig: {
41+
feature: 'STREAMING_AUTOML_OBJECT_TRACKING',
42+
automlObjectTrackingConfig: {
43+
modelName: modelName,
44+
},
45+
},
46+
};
47+
48+
const readStream = fs.createReadStream(path, {
49+
highWaterMark: 5 * 1024 * 1024, //chunk size set to 5MB (recommended less than 10MB)
50+
encoding: 'base64',
51+
});
52+
//Load file content
53+
// Note: Input videos must have supported video codecs. See
54+
// https://cloud.google.com/video-intelligence/docs/streaming/streaming#supported_video_codecs
55+
// for more details.
56+
const chunks = [];
57+
readStream
58+
.on('data', chunk => {
59+
const request = {
60+
inputContent: chunk.toString(),
61+
};
62+
chunks.push(request);
63+
})
64+
.on('close', () => {
65+
// configRequest should be the first in the stream of requests
66+
stream.write(configRequest);
67+
for (let i = 0; i < chunks.length; i++) {
68+
stream.write(chunks[i]);
69+
}
70+
stream.end();
71+
});
72+
73+
const stream = client.streamingAnnotateVideo().on('data', response => {
74+
//Gets annotations for video
75+
const annotations = response.annotationResults;
76+
const objects = annotations.objectAnnotations;
77+
objects.forEach(object => {
78+
console.log(`Entity description: ${object.entity.description}`);
79+
console.log(`Entity id: ${object.entity.entityId}`);
80+
console.log(`Track id: ${object.trackId}`);
81+
console.log(`Confidence: ${object.confidence}`);
82+
console.log(
83+
`Time offset for the frame: ${
84+
object.frames[0].timeOffset.seconds || 0
85+
}` + `.${(object.frames[0].timeOffset.nanos / 1e6).toFixed(0)}s`
86+
);
87+
//Every annotation has only one frame.
88+
const box = object.frames[0].normalizedBoundingBox;
89+
console.log('Bounding box position:');
90+
console.log(`\tleft: ${box.left}`);
91+
console.log(`\ttop: ${box.top}`);
92+
console.log(`\tright: ${box.right}`);
93+
console.log(`\tbottom: ${box.bottom}`);
94+
});
95+
});
96+
// [END video_streaming_automl_object_tracking_beta]
97+
}
98+
main(...process.argv.slice(2)).catch(console.error());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const cp = require('child_process');
18+
const {assert} = require('chai');
19+
const {describe, it} = require('mocha');
20+
21+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
22+
23+
const cmd = 'node analyze-streaming-automl-object-tracking.js';
24+
const modelId = 'VOT409893536788381696';
25+
const project = process.env.GCLOUD_PROJECT;
26+
const file = 'resources/googlework_short.mp4';
27+
28+
describe('streaming automl object tracking', function () {
29+
this.retries(3);
30+
it('should track an object in a streaming video', async () => {
31+
const output = execSync(`${cmd} ${file} ${project} ${modelId}`);
32+
assert.match(output, /Track id/);
33+
});
34+
});

0 commit comments

Comments
 (0)