Skip to content

Commit 0715f7d

Browse files
authored
video: adding face detection and person detection samples for beta (#2919)
* video: adding face detection and person detection samples for beta * updating requirements.txt * updating test names to faces * fixing region tag typo * responding to comments * reverted tabs to fix linting errors * responding to comments
1 parent 3e2ebaf commit 0715f7d

9 files changed

+487
-1
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
google-cloud-videointelligence==1.12.1
1+
google-cloud-videointelligence==1.13.0
22
google-cloud-storage==1.23.0
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Copyright 2020 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+
# https://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+
# [START video_detect_faces_beta]
16+
import io
17+
from google.cloud import videointelligence_v1p3beta1 as videointelligence
18+
19+
20+
def detect_faces(local_file_path="path/to/your/video-file.mp4"):
21+
"""Detects faces in a video from a local file."""
22+
23+
client = videointelligence.VideoIntelligenceServiceClient()
24+
25+
with io.open(local_file_path, "rb") as f:
26+
input_content = f.read()
27+
28+
# Configure the request
29+
config = videointelligence.types.FaceDetectionConfig(
30+
include_bounding_boxes=True, include_attributes=True
31+
)
32+
context = videointelligence.types.VideoContext(
33+
face_detection_config=config
34+
)
35+
36+
# Start the asynchronous request
37+
operation = client.annotate_video(
38+
input_content=input_content,
39+
features=[videointelligence.enums.Feature.FACE_DETECTION],
40+
video_context=context,
41+
)
42+
43+
print("\nProcessing video for face detection annotations.")
44+
result = operation.result(timeout=300)
45+
46+
print("\nFinished processing.\n")
47+
48+
# Retrieve the first result, because a single video was processed.
49+
annotation_result = result.annotation_results[0]
50+
51+
for annotation in annotation_result.face_detection_annotations:
52+
print("Face detected:")
53+
for track in annotation.tracks:
54+
print(
55+
"Segment: {}s to {}s".format(
56+
track.segment.start_time_offset.seconds
57+
+ track.segment.start_time_offset.nanos / 1e9,
58+
track.segment.end_time_offset.seconds
59+
+ track.segment.end_time_offset.nanos / 1e9,
60+
)
61+
)
62+
63+
# Each segment includes timestamped faces that include
64+
# characteristics of the face detected.
65+
# Grab the first timestamped face
66+
timestamped_object = track.timestamped_objects[0]
67+
box = timestamped_object.normalized_bounding_box
68+
print("Bounding box:")
69+
print("\tleft : {}".format(box.left))
70+
print("\ttop : {}".format(box.top))
71+
print("\tright : {}".format(box.right))
72+
print("\tbottom: {}".format(box.bottom))
73+
74+
# Attributes include glasses, headwear, facial hair, smiling,
75+
# direction of gaze, etc.
76+
print("Attributes:")
77+
for attribute in timestamped_object.attributes:
78+
print(
79+
"\t{}:{} {}".format(
80+
attribute.name, attribute.value, attribute.confidence
81+
)
82+
)
83+
84+
85+
# [END video_detect_faces_beta]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2020 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+
import os
16+
17+
import video_detect_faces_beta
18+
19+
RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
20+
21+
22+
def test_detect_faces(capsys):
23+
local_file_path = os.path.join(RESOURCES, "googlework_short.mp4")
24+
25+
video_detect_faces_beta.detect_faces(local_file_path=local_file_path)
26+
27+
out, _ = capsys.readouterr()
28+
29+
assert "Face detected:" in out
30+
assert "Attributes:" in out
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Copyright 2020 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+
# https://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+
# [START video_detect_faces_gcs_beta]
16+
from google.cloud import videointelligence_v1p3beta1 as videointelligence
17+
18+
19+
def detect_faces(gcs_uri="gs://YOUR_BUCKET_ID/path/to/your/video.mp4"):
20+
"""Detects faces in a video."""
21+
22+
client = videointelligence.VideoIntelligenceServiceClient()
23+
24+
# Configure the request
25+
config = videointelligence.types.FaceDetectionConfig(
26+
include_bounding_boxes=True, include_attributes=True
27+
)
28+
context = videointelligence.types.VideoContext(
29+
face_detection_config=config
30+
)
31+
32+
# Start the asynchronous request
33+
operation = client.annotate_video(
34+
input_uri=gcs_uri,
35+
features=[videointelligence.enums.Feature.FACE_DETECTION],
36+
video_context=context,
37+
)
38+
39+
print("\nProcessing video for face detection annotations.")
40+
result = operation.result(timeout=300)
41+
42+
print("\nFinished processing.\n")
43+
44+
# Retrieve the first result, because a single video was processed.
45+
annotation_result = result.annotation_results[0]
46+
47+
for annotation in annotation_result.face_detection_annotations:
48+
print("Face detected:")
49+
for track in annotation.tracks:
50+
print(
51+
"Segment: {}s to {}s".format(
52+
track.segment.start_time_offset.seconds
53+
+ track.segment.start_time_offset.nanos / 1e9,
54+
track.segment.end_time_offset.seconds
55+
+ track.segment.end_time_offset.nanos / 1e9,
56+
)
57+
)
58+
59+
# Each segment includes timestamped faces that include
60+
# characteristics of the face detected.
61+
# Grab the first timestamped face
62+
timestamped_object = track.timestamped_objects[0]
63+
box = timestamped_object.normalized_bounding_box
64+
print("Bounding box:")
65+
print("\tleft : {}".format(box.left))
66+
print("\ttop : {}".format(box.top))
67+
print("\tright : {}".format(box.right))
68+
print("\tbottom: {}".format(box.bottom))
69+
70+
# Attributes include glasses, headwear, facial hair, smiling,
71+
# direction of gaze, etc.
72+
print("Attributes:")
73+
for attribute in timestamped_object.attributes:
74+
print(
75+
"\t{}:{} {}".format(
76+
attribute.name, attribute.value, attribute.confidence
77+
)
78+
)
79+
80+
81+
# [END video_detect_faces_gcs_beta]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2020 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+
import os
16+
17+
import video_detect_faces_gcs_beta
18+
19+
RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
20+
21+
22+
def test_detect_faces(capsys):
23+
input_uri = "gs://cloud-samples-data/video/googlework_short.mp4"
24+
25+
video_detect_faces_gcs_beta.detect_faces(gcs_uri=input_uri)
26+
27+
out, _ = capsys.readouterr()
28+
29+
assert "Face detected:" in out
30+
assert "Attributes:" in out
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Copyright 2020 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+
# https://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+
# [START video_detect_person_beta]
16+
import io
17+
from google.cloud import videointelligence_v1p3beta1 as videointelligence
18+
19+
20+
def detect_person(local_file_path="path/to/your/video-file.mp4"):
21+
"""Detects people in a video from a local file."""
22+
23+
client = videointelligence.VideoIntelligenceServiceClient()
24+
25+
with io.open(local_file_path, "rb") as f:
26+
input_content = f.read()
27+
28+
# Configure the request
29+
config = videointelligence.types.PersonDetectionConfig(
30+
include_bounding_boxes=True,
31+
include_attributes=True,
32+
include_pose_landmarks=True,
33+
)
34+
context = videointelligence.types.VideoContext(
35+
person_detection_config=config
36+
)
37+
38+
# Start the asynchronous request
39+
operation = client.annotate_video(
40+
input_content=input_content,
41+
features=[videointelligence.enums.Feature.PERSON_DETECTION],
42+
video_context=context,
43+
)
44+
45+
print("\nProcessing video for person detection annotations.")
46+
result = operation.result(timeout=300)
47+
48+
print("\nFinished processing.\n")
49+
50+
# Retrieve the first result, because a single video was processed.
51+
annotation_result = result.annotation_results[0]
52+
53+
for annotation in annotation_result.person_detection_annotations:
54+
print("Person detected:")
55+
for track in annotation.tracks:
56+
print(
57+
"Segment: {}s to {}s".format(
58+
track.segment.start_time_offset.seconds
59+
+ track.segment.start_time_offset.nanos / 1e9,
60+
track.segment.end_time_offset.seconds
61+
+ track.segment.end_time_offset.nanos / 1e9,
62+
)
63+
)
64+
65+
# Each segment includes timestamped objects that include
66+
# characteristic - -e.g.clothes, posture of the person detected.
67+
# Grab the first timestamped object
68+
timestamped_object = track.timestamped_objects[0]
69+
box = timestamped_object.normalized_bounding_box
70+
print("Bounding box:")
71+
print("\tleft : {}".format(box.left))
72+
print("\ttop : {}".format(box.top))
73+
print("\tright : {}".format(box.right))
74+
print("\tbottom: {}".format(box.bottom))
75+
76+
# Attributes include unique pieces of clothing,
77+
# poses, or hair color.
78+
print("Attributes:")
79+
for attribute in timestamped_object.attributes:
80+
print(
81+
"\t{}:{} {}".format(
82+
attribute.name, attribute.value, attribute.confidence
83+
)
84+
)
85+
86+
# Landmarks in person detection include body parts such as
87+
# left_shoulder, right_ear, and right_ankle
88+
print("Landmarks:")
89+
for landmark in timestamped_object.landmarks:
90+
print(
91+
"\t{}: {} (x={}, y={})".format(
92+
landmark.name,
93+
landmark.confidence,
94+
landmark.point.x, # Normalized vertex
95+
landmark.point.y, # Normalized vertex
96+
)
97+
)
98+
99+
100+
# [END video_detect_person_beta]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright 2020 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+
import os
16+
17+
import video_detect_person_beta
18+
19+
RESOURCES = os.path.join(os.path.dirname(__file__), "resources")
20+
21+
22+
def test_detect_person(capsys):
23+
local_file_path = os.path.join(RESOURCES, "googlework_tiny.mp4")
24+
25+
video_detect_person_beta.detect_person(local_file_path=local_file_path)
26+
27+
out, _ = capsys.readouterr()
28+
29+
assert "Person detected:" in out
30+
assert "Attributes:" in out
31+
assert "x=" in out
32+
assert "y=" in out

0 commit comments

Comments
 (0)