Skip to content

Commit f2c7ea3

Browse files
gogascabshaffer
authored andcommitted
Add support for Video Intelligence Text OCR (GoogleCloudPlatform#854)
1 parent 5da12f8 commit f2c7ea3

File tree

8 files changed

+210
-12
lines changed

8 files changed

+210
-12
lines changed

video/README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ Video Intelligence API from PHP.
3030
5. Run `php video.php`. The following commands are available:
3131

3232
```
33-
faces Detect faces changes in video using the Video Intelligence API
34-
help Displays help for a command
35-
labels Detect labels in video using the Video Intelligence API
36-
labels-in-file Detect labels in a file using the Video Intelligence API
37-
list Lists commands
38-
safe-search Detect safe search in video using the Video Intelligence API
39-
shots Detect shots in video using the Video Intelligence API
40-
transcription Transcribe speech from a video
33+
faces Detect faces changes in video using the Video Intelligence API
34+
help Displays help for a command
35+
labels Detect labels in video using the Video Intelligence API
36+
labels-in-file Detect labels in a file using the Video Intelligence API
37+
list Lists commands
38+
safe-search Detect safe search in video using the Video Intelligence API
39+
shots Detect shots in video using the Video Intelligence API
40+
transcription Transcribe speech from a video
41+
text-detection Detects OCR text in video using the Video Intelligence API
42+
text-detection-file Detects OCR text in a file using the Video Intelligence API
4143
```
4244
4345
Example:

video/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33
"type": "project",
44
"require": {
55
"symfony/console": "^3.1",
6-
"google/cloud-videointelligence": "^1.2"
6+
"google/cloud-videointelligence": "^1.3.1"
77
},
88
"autoload": {
99
"files": [
1010
"src/analyze_labels.php",
1111
"src/analyze_labels_file.php",
1212
"src/analyze_explicit_content.php",
1313
"src/analyze_shots.php",
14-
"src/analyze_transcription.php"
14+
"src/analyze_transcription.php",
15+
"src/analyze_text_detection.php",
16+
"src/analyze_text_detection_file.php"
1517
]
1618
},
1719
"require-dev": {

video/src/analyze_text_detection.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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_text_detection]
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_text_detection($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::TEXT_DETECTION]
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+
50+
# Process video/segment level label annotations
51+
foreach ($results->getTextAnnotations() as $text) {
52+
printf('Video text description: %s' . PHP_EOL, $text->getText());
53+
foreach ($text->getSegments() as $segment) {
54+
$startTimeOffset = $segment->getSegment()->getStartTimeOffset();
55+
$startSeconds = $startTimeOffset->getSeconds();
56+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
57+
$startTime = $startSeconds + $startNanoseconds;
58+
$endTimeOffset = $segment->getSegment()->getEndTimeOffset();
59+
$endSeconds = $endTimeOffset->getSeconds();
60+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
61+
$endTime = $endSeconds + $endNanoseconds;
62+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
63+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
64+
}
65+
}
66+
print(PHP_EOL);
67+
} else {
68+
print_r($operation->getError());
69+
}
70+
}
71+
// [END video_analyze_text_detection]
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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_text_detection_file]
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_text_detection_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::TEXT_DETECTION]
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+
53+
# Process video/segment level label annotations
54+
foreach ($results->getTextAnnotations() as $text) {
55+
printf('Video text description: %s' . PHP_EOL, $text->getText());
56+
foreach ($text->getSegments() as $segment) {
57+
$startTimeOffset = $segment->getSegment()->getStartTimeOffset();
58+
$startSeconds = $startTimeOffset->getSeconds();
59+
$startNanoseconds = floatval($startTimeOffset->getNanos())/1000000000.00;
60+
$startTime = $startSeconds + $startNanoseconds;
61+
$endTimeOffset = $segment->getSegment()->getEndTimeOffset();
62+
$endSeconds = $endTimeOffset->getSeconds();
63+
$endNanoseconds = floatval($endTimeOffset->getNanos())/1000000000.00;
64+
$endTime = $endSeconds + $endNanoseconds;
65+
printf(' Segment: %ss to %ss' . PHP_EOL, $startTime, $endTime);
66+
printf(' Confidence: %f' . PHP_EOL, $segment->getConfidence());
67+
}
68+
}
69+
print(PHP_EOL);
70+
} else {
71+
print_r($operation->getError());
72+
}
73+
}
74+
// [END video_analyze_text_detection_file]

video/src/analyze_transcription.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ function analyze_transcription($uri, array $options = [])
7373

7474
print('Word level information:');
7575
foreach ($alternative->getWords() as $wordInfo) {
76-
printf('%s s - %s s: %s' . PHP_EOL,
76+
printf(
77+
'%s s - %s s: %s' . PHP_EOL,
7778
$wordInfo->getStartTime()->getSeconds(),
7879
$wordInfo->getEndTime()->getSeconds(),
79-
$wordInfo->getWord());
80+
$wordInfo->getWord()
81+
);
8082
}
8183
}
8284
}

video/test/data/googlework_short.mp4

1.42 MB
Binary file not shown.

video/test/videoTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ public function testTranscription()
9696
$this->assertContains('France', $output);
9797
}
9898

99+
public function testAnalyzeText()
100+
{
101+
$output = $this->runCommand('text-detection', [
102+
'uri' => $this->gcsUriTwo(),
103+
'--polling-interval-seconds' => 10,
104+
]);
105+
$this->assertContains('GOOGLE', $output);
106+
$this->assertContains('Video text description:', $output);
107+
$this->assertContains('Segment:', $output);
108+
$this->assertContains('Confidence:', $output);
109+
}
110+
111+
public function testAnalyzeTextInFile()
112+
{
113+
$output = $this->runCommand('text-detection-file', [
114+
'file' => __DIR__ . '/data/googlework_short.mp4',
115+
'--polling-interval-seconds' => 10,
116+
]);
117+
$this->assertContains('GOOGLE', $output);
118+
$this->assertContains('Video text description:', $output);
119+
$this->assertContains('Segment:', $output);
120+
$this->assertContains('Confidence:', $output);
121+
}
122+
99123
private function gcsUri()
100124
{
101125
return sprintf(

video/video.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,29 @@
9494
);
9595
});
9696

97+
$application->add(new Command('text-detection'))
98+
->setDefinition($inputDefinition)
99+
->setDescription('Detect text in video using the Video Intelligence API')
100+
->setCode(function ($input, $output) {
101+
analyze_text_detection(
102+
$input->getArgument('uri'),
103+
['pollingIntervalSeconds' => $input->getOption('polling-interval-seconds')]
104+
);
105+
});
106+
107+
$application->add(new Command('text-detection-file'))
108+
->addArgument('file', InputArgument::REQUIRED,
109+
'Path to a local video file.')
110+
->addOption('polling-interval-seconds', '', InputOption::VALUE_REQUIRED,
111+
'Polling interval in seconds to waiting for a Video API response.')
112+
->setDescription('Detect texts in a file using the Video Intelligence API')
113+
->setCode(function ($input, $output) {
114+
analyze_text_detection_file(
115+
$input->getArgument('file'),
116+
['pollingIntervalSeconds' => $input->getOption('polling-interval-seconds')]
117+
);
118+
});
119+
97120
// for testing
98121
if (getenv('PHPUNIT_TESTS') === '1') {
99122
return $application;

0 commit comments

Comments
 (0)