Skip to content

Commit 53bebcc

Browse files
gguussJon Wayne Parrott
authored and
Jon Wayne Parrott
committed
Add video sample (#844)
1 parent bfd9aec commit 53bebcc

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

video/cloud-client/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Google Cloud Video Intelligence Sample
2+
3+
Demonstrates face detection, label detection, and shot change detection using
4+
the Google Cloud API..
5+
6+
## Setup
7+
Please follow the [Set Up Your Project](https://cloud.google.com/video-intelligence/docs/getting-started#set_up_your_project)
8+
steps in the Quickstart doc to create a project and enable the Google Cloud
9+
Video Intelligence API. Following those steps, make sure that you
10+
[Set Up a Service Account](https://cloud.google.com/video-intelligence/docs/common/auth#set_up_a_service_account),
11+
and export the following environment variable:
12+
13+
```
14+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your-project-credentials.json
15+
```
16+
17+
## Run the sample
18+
19+
Install [pip](https://pip.pypa.io/en/stable/installing) if not already installed.
20+
21+
Install the necessary libraries using pip:
22+
23+
```sh
24+
$ pip install -r requirements.txt
25+
```
26+
27+
Run the sample, for example:
28+
```
29+
python analyze.py faces gs://demomaker/google_gmail.mp4
30+
python analyze.py labels gs://demomaker/cat.mp4
31+
python analyze.py shots gs://demomaker/gbikes_dinosaur.mp4
32+
```

video/cloud-client/analyze.py

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google Inc. All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""This application demonstrates how to perform basic operations with the
18+
Google Cloud Video Intelligence API.
19+
20+
For more information, check out the documentation at
21+
https://cloud.google.com/videointelligence/docs.
22+
"""
23+
24+
import argparse
25+
import sys
26+
import time
27+
28+
from google.cloud.gapic.videointelligence.v1beta1 import enums
29+
from google.cloud.gapic.videointelligence.v1beta1 import (
30+
video_intelligence_service_client)
31+
32+
33+
def analyze_faces(path):
34+
""" Detects faces given a GCS path. """
35+
video_client = (video_intelligence_service_client.
36+
VideoIntelligenceServiceClient())
37+
features = [enums.Feature.FACE_DETECTION]
38+
operation = video_client.annotate_video(path, features)
39+
print('\nProcessing video for label annotations:')
40+
41+
while not operation.done():
42+
sys.stdout.write('.')
43+
sys.stdout.flush()
44+
time.sleep(1)
45+
46+
print('\nFinished processing.')
47+
48+
# first result is retrieved because a single video was processed
49+
face_annotations = (operation.result().annotation_results[0].
50+
face_annotations)
51+
52+
for face_id, face in enumerate(face_annotations):
53+
print('Thumbnail size: {}'.format(len(face.thumbnail)))
54+
55+
for segment_id, segment in enumerate(face.segments):
56+
print('Track {}: {} to {}'.format(
57+
segment_id,
58+
segment.start_time_offset,
59+
segment.end_time_offset))
60+
61+
62+
def analyze_labels(path):
63+
""" Detects labels given a GCS path. """
64+
video_client = (video_intelligence_service_client.
65+
VideoIntelligenceServiceClient())
66+
features = [enums.Feature.LABEL_DETECTION]
67+
operation = video_client.annotate_video(path, features)
68+
print('\nProcessing video for label annotations:')
69+
70+
while not operation.done():
71+
sys.stdout.write('.')
72+
sys.stdout.flush()
73+
time.sleep(1)
74+
75+
print('\nFinished processing.')
76+
77+
# first result is retrieved because a single video was processed
78+
results = operation.result().annotation_results[0]
79+
80+
for i, label in enumerate(results.label_annotations):
81+
print('Label description: {}'.format(label.description))
82+
print('Locations:')
83+
84+
for l, location in enumerate(label.locations):
85+
print('\t{}: {} to {}'.format(
86+
l,
87+
location.segment.start_time_offset,
88+
location.segment.end_time_offset))
89+
90+
91+
def analyze_shots(path):
92+
""" Detects camera shot changes. """
93+
video_client = (video_intelligence_service_client.
94+
VideoIntelligenceServiceClient())
95+
features = [enums.Feature.SHOT_CHANGE_DETECTION]
96+
operation = video_client.annotate_video(path, features)
97+
print('\nProcessing video for shot change annotations:')
98+
99+
while not operation.done():
100+
sys.stdout.write('.')
101+
sys.stdout.flush()
102+
time.sleep(1)
103+
104+
print('\nFinished processing.')
105+
106+
# first result is retrieved because a single video was processed
107+
shots = operation.result().annotation_results[0]
108+
109+
for note, shot in enumerate(shots.shot_annotations):
110+
print('Scene {}: {} to {}'.format(
111+
note,
112+
shot.start_time_offset,
113+
shot.end_time_offset))
114+
115+
116+
if __name__ == '__main__':
117+
parser = argparse.ArgumentParser(
118+
description=__doc__,
119+
formatter_class=argparse.RawDescriptionHelpFormatter)
120+
subparsers = parser.add_subparsers(dest='command')
121+
analyze_faces_parser = subparsers.add_parser(
122+
'faces', help=analyze_faces.__doc__)
123+
analyze_faces_parser.add_argument('path')
124+
analyze_labels_parser = subparsers.add_parser(
125+
'labels', help=analyze_labels.__doc__)
126+
analyze_labels_parser.add_argument('path')
127+
analyze_shots_parser = subparsers.add_parser(
128+
'shots', help=analyze_shots.__doc__)
129+
analyze_shots_parser.add_argument('path')
130+
131+
args = parser.parse_args()
132+
133+
if args.command == 'faces':
134+
analyze_faces(args.path)
135+
if args.command == 'labels':
136+
analyze_labels(args.path)
137+
if args.command == 'shots':
138+
analyze_shots(args.path)

video/cloud-client/analyze_test.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2017 Google, Inc
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
import pytest
18+
19+
import analyze
20+
21+
22+
LABELS_FILE_PATH = '/video/cat.mp4'
23+
FACES_FILE_PATH = '/video/gbike.mp4'
24+
SHOTS_FILE_PATH = '/video/gbikes_dinosaur.mp4'
25+
26+
27+
@pytest.mark.slow
28+
def test_cat_video_shots(capsys, cloud_config):
29+
analyze.analyze_shots(
30+
'gs://{}{}'.format(cloud_config.bucket, SHOTS_FILE_PATH))
31+
out, _ = capsys.readouterr()
32+
assert 'Scene 1:' in out
33+
34+
35+
@pytest.mark.slow
36+
def test_cat_video_faces(capsys, cloud_config):
37+
analyze.analyze_faces(
38+
'gs://{}{}'.format(cloud_config.bucket, FACES_FILE_PATH))
39+
out, _ = capsys.readouterr()
40+
assert 'Thumbnail' in out
41+
42+
43+
@pytest.mark.slow
44+
def test_cat_video_labels(capsys, cloud_config):
45+
analyze.analyze_labels(
46+
'gs://{}{}'.format(cloud_config.bucket, LABELS_FILE_PATH))
47+
out, _ = capsys.readouterr()
48+
assert 'Whiskers' in out

video/cloud-client/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://storage.googleapis.com/videointelligence-alpha/videointelligence-python.zip

0 commit comments

Comments
 (0)