Skip to content

Commit 52bed4b

Browse files
authored
Merge pull request GoogleCloudPlatform#864 from gogasca/object-tracking
Add Object Tracking support for Video
2 parents fcfc66d + b609eca commit 52bed4b

File tree

6 files changed

+218
-2
lines changed

6 files changed

+218
-2
lines changed

video/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Video Intelligence API from PHP.
4040
transcription Transcribe speech from a video
4141
text-detection Detects OCR text in video using the Video Intelligence API
4242
text-detection-file Detects OCR text in a file using the Video Intelligence API
43+
object-tracking Object tracking tracks multiple objects detected in an input video
44+
object-tracking-file Object tracking tracks multiple objects detected in a file
4345
```
4446
4547
Example:

video/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "project",
44
"require": {
55
"symfony/console": "^3.1",
6-
"google/cloud-videointelligence": "^1.3.1"
6+
"google/cloud-videointelligence": "^1.4.0"
77
},
88
"autoload": {
99
"files": [
@@ -13,7 +13,9 @@
1313
"src/analyze_shots.php",
1414
"src/analyze_transcription.php",
1515
"src/analyze_text_detection.php",
16-
"src/analyze_text_detection_file.php"
16+
"src/analyze_text_detection_file.php",
17+
"src/analyze_object_tracking.php",
18+
"src/analyze_object_tracking_file.php"
1719
]
1820
},
1921
"require-dev": {

video/src/analyze_object_tracking.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Video;
19+
20+
// [START video_analyze_object_tracking]
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\VideoIntelligence\V1\Feature;
23+
24+
/**
25+
* Finds labels in the video.
26+
*
27+
* @param string $path File path to a video file to analyze.
28+
* @param array $options optional Array of options to pass to
29+
* OperationResponse::pollUntilComplete. This is useful
30+
* for increasing the "pollingIntervalSeconds" option.
31+
*/
32+
function analyze_object_tracking($uri, array $options = [])
33+
{
34+
# Instantiate a client.
35+
$video = new VideoIntelligenceServiceClient();
36+
37+
# Execute a request.
38+
$operation = $video->annotateVideo([
39+
'inputUri' => $uri,
40+
'features' => [Feature::OBJECT_TRACKING]
41+
]);
42+
43+
# Wait for the request to complete.
44+
$operation->pollUntilComplete($options);
45+
46+
# Print the results.
47+
if ($operation->operationSucceeded()) {
48+
$results = $operation->getResult()->getAnnotationResults()[0];
49+
# Process video/segment level label annotations
50+
$objectEntity = $results->getObjectAnnotations()[0];
51+
52+
printf('Video object entity: %s' . PHP_EOL, $objectEntity->getEntity()->getEntityId());
53+
printf('Video object description: %s' . PHP_EOL, $objectEntity->getEntity()->getDescription());
54+
55+
$startTimeOffset = $objectEntity->getSegment()->getStartTimeOffset();
56+
$startSeconds = $startTimeOffset->getSeconds();
57+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
58+
$startTime = $startSeconds + $startNanoseconds;
59+
$endTimeOffset = $objectEntity->getSegment()->getEndTimeOffset();
60+
$endSeconds = $endTimeOffset->getSeconds();
61+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
62+
$endTime = $endSeconds + $endNanoseconds;
63+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
64+
printf(' Confidence: %f' . PHP_EOL, $objectEntity->getConfidence());
65+
66+
foreach ($objectEntity->getFrames() as $objectEntityFrame) {
67+
$startSeconds = $objectEntityFrame->getTimeOffset()->getSeconds();
68+
$startNanoseconds = $objectEntityFrame->getTimeOffset()->getNanos();
69+
$timeOffSet = $startSeconds + $startNanoseconds/1000000000.00;
70+
printf(' Time offset: %ss' . PHP_EOL, $timeOffSet);
71+
$boundingBox = $objectEntityFrame->getNormalizedBoundingBox();
72+
printf(' Bounding box position:' . PHP_EOL);
73+
printf(' Left: %s', $boundingBox->getLeft());
74+
printf(' Top: %s', $boundingBox->getTop());
75+
printf(' Right: %s', $boundingBox->getRight());
76+
printf(' Bottom: %s', $boundingBox->getBottom());
77+
}
78+
print(PHP_EOL);
79+
} else {
80+
print_r($operation->getError());
81+
}
82+
}
83+
// [END video_analyze_object_tracking]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
namespace Google\Cloud\Samples\Video;
19+
20+
// [START video_analyze_object_tracking]
21+
use Google\Cloud\VideoIntelligence\V1\VideoIntelligenceServiceClient;
22+
use Google\Cloud\VideoIntelligence\V1\Feature;
23+
24+
/**
25+
* Finds labels in the video.
26+
*
27+
* @param string $path File path to a video file to analyze.
28+
* @param array $options optional Array of options to pass to
29+
* OperationResponse::pollUntilComplete. This is useful
30+
* for increasing the "pollingIntervalSeconds" option.
31+
*/
32+
function analyze_object_tracking_file($path, array $options = [])
33+
{
34+
# Instantiate a client.
35+
$video = new VideoIntelligenceServiceClient();
36+
37+
# Read the local video file
38+
$inputContent = file_get_contents($path);
39+
40+
# Execute a request.
41+
$operation = $video->annotateVideo([
42+
'inputContent' => $inputContent,
43+
'features' => [Feature::OBJECT_TRACKING]
44+
]);
45+
46+
# Wait for the request to complete.
47+
$operation->pollUntilComplete($options);
48+
49+
# Print the results.
50+
if ($operation->operationSucceeded()) {
51+
$results = $operation->getResult()->getAnnotationResults()[0];
52+
# Process video/segment level label annotations
53+
$objectEntity = $results->getObjectAnnotations()[0];
54+
55+
printf('Video object entity: %s' . PHP_EOL, $objectEntity->getEntity()->getEntityId());
56+
printf('Video object description: %s' . PHP_EOL, $objectEntity->getEntity()->getDescription());
57+
58+
$startTimeOffset = $objectEntity->getSegment()->getStartTimeOffset();
59+
$startSeconds = $startTimeOffset->getSeconds();
60+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
61+
$startTime = $startSeconds + $startNanoseconds;
62+
$endTimeOffset = $objectEntity->getSegment()->getEndTimeOffset();
63+
$endSeconds = $endTimeOffset->getSeconds();
64+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
65+
$endTime = $endSeconds + $endNanoseconds;
66+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
67+
printf(' Confidence: %f' . PHP_EOL, $objectEntity->getConfidence());
68+
69+
foreach ($objectEntity->getFrames() as $objectEntityFrame) {
70+
$startSeconds = $objectEntityFrame->getTimeOffset()->getSeconds();
71+
$startNanoseconds = $objectEntityFrame->getTimeOffset()->getNanos();
72+
$timeOffSet = $startSeconds + $startNanoseconds/1000000000.00;
73+
printf(' Time offset: %ss' . PHP_EOL, $timeOffSet);
74+
$boundingBox = $objectEntityFrame->getNormalizedBoundingBox();
75+
printf(' Bounding box position:' . PHP_EOL);
76+
printf(' Left: %s', $boundingBox->getLeft());
77+
printf(' Top: %s', $boundingBox->getTop());
78+
printf(' Right: %s', $boundingBox->getRight());
79+
printf(' Bottom: %s', $boundingBox->getBottom());
80+
}
81+
print(PHP_EOL);
82+
} else {
83+
print_r($operation->getError());
84+
}
85+
}
86+
// [END video_analyze_object_tracking]

video/test/videoTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,26 @@ public function testAnalyzeTextInFile()
120120
$this->assertContains('Confidence:', $output);
121121
}
122122

123+
public function testObjectTracking()
124+
{
125+
$output = $this->runCommand('object-tracking', [
126+
'uri' => $this->gcsUriTwo(),
127+
'--polling-interval-seconds' => 10,
128+
]);
129+
$this->assertContains('/m/01g317', $output);
130+
$this->assertContains('person', $output);
131+
}
132+
133+
public function testObjectTrackingFile()
134+
{
135+
$output = $this->runCommand('object-tracking-file', [
136+
'file' => __DIR__ . '/data/googlework_short.mp4',
137+
'--polling-interval-seconds' => 10,
138+
]);
139+
$this->assertContains('/m/01g317', $output);
140+
$this->assertContains('person', $output);
141+
}
142+
123143
private function gcsUri()
124144
{
125145
return sprintf(

video/video.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,29 @@
117117
);
118118
});
119119

120+
$application->add(new Command('object-tracking'))
121+
->setDefinition($inputDefinition)
122+
->setDescription('Detect objects in video using the Video Intelligence API')
123+
->setCode(function ($input, $output) {
124+
analyze_object_tracking(
125+
$input->getArgument('uri'),
126+
['pollingIntervalSeconds' => $input->getOption('polling-interval-seconds')]
127+
);
128+
});
129+
130+
$application->add(new Command('object-tracking-file'))
131+
->addArgument('file', InputArgument::REQUIRED,
132+
'Path to a local video file.')
133+
->addOption('polling-interval-seconds', '', InputOption::VALUE_REQUIRED,
134+
'Polling interval in seconds to waiting for a Video API response.')
135+
->setDescription('Detect objects in a file using the Video Intelligence API')
136+
->setCode(function ($input, $output) {
137+
analyze_object_tracking_file(
138+
$input->getArgument('file'),
139+
['pollingIntervalSeconds' => $input->getOption('polling-interval-seconds')]
140+
);
141+
});
142+
120143
// for testing
121144
if (getenv('PHPUNIT_TESTS') === '1') {
122145
return $application;

0 commit comments

Comments
 (0)