Skip to content

Commit 8b27e0d

Browse files
chore(video_intelligence): Move samples from the ruby-docs-samples repo
1 parent 0492cc2 commit 8b27e0d

8 files changed

Lines changed: 711 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
source "https://rubygems.org"
16+
17+
master = ENV["GOOGLE_CLOUD_SAMPLES_TEST"] == "master"
18+
19+
gem "google-cloud-video_intelligence", path: master ? "../../google-cloud-video_intelligence" : nil
20+
gem "google-cloud-video_intelligence-v1", path: master ? "../../google-cloud-video_intelligence-v1" : nil
21+
gem "google-cloud-video_intelligence-v1p1beta1", path: master ? "../../google-cloud-video_intelligence-v1p1beta1" : nil
22+
gem "google-cloud-video_intelligence-v1p2beta1", path: master ? "../../google-cloud-video_intelligence-v1p2beta1" : nil
23+
24+
group :test do
25+
gem "google-cloud-storage"
26+
gem "minitest", "~> 5.14"
27+
gem "minitest-focus", "~> 1.1"
28+
gem "minitest-hooks", "~>1.5"
29+
gem "rake", ">= 12.0"
30+
gem "pry"
31+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Video Intelligence Ruby Samples
4+
5+
The [Google Cloud Video Intelligence API](https://cloud.google.com/video-intelligence/)
6+
enables easy integration of Google speech recognition technologies into
7+
developer applications.
8+
9+
## Setup
10+
11+
### Authentication
12+
13+
Authentication is typically done through [Application Default Credentials](https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow)
14+
, which means you do not have to change the code to authenticate as long as your
15+
environment has credentials. You have a few options for setting up
16+
authentication:
17+
18+
1. When running locally, use the [Google Cloud SDK](https://cloud.google.com/sdk/)
19+
20+
`gcloud auth application-default login`
21+
22+
1. When running on App Engine or Compute Engine, credentials are already set-up.
23+
However, you may need to configure your Compute Engine instance with
24+
[additional scopes](https://cloud.google.com/compute/docs/authentication#using).
25+
26+
1. You can create a [Service Account key file](https://cloud.google.com/docs/authentication#service_accounts)
27+
. This file can be used to authenticate to Google Cloud Platform services from
28+
any environment. To use the file, set the `GOOGLE_APPLICATION_CREDENTIALS`
29+
environment variable to the path to the key file, for example:
30+
31+
`export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json`
32+
33+
### Install Dependencies
34+
35+
1. Install the [Bundler](http://bundler.io/) gem.
36+
37+
1. Install dependencies using:
38+
39+
`bundle install`
40+
41+
## Run Quickstart
42+
43+
bundle exec ruby quickstart.rb
44+
45+
## Run Samples
46+
47+
Usage: bundle exec ruby video_samples.rb [command] [arguments]
48+
49+
Commands:
50+
analyze_labels <gcs_path> Detects labels given a GCS path.
51+
analyze_labels_local <local_path> Detects labels given file path.
52+
analyze_shots <gcs_path> Detects camera shot changes given a GCS path.
53+
analyze_explicit_content <gcs_path> Detects explicit content given a GCS path.
54+
speech_transcription <gcs_path> Transcribes speech given a GCS path.
55+
detect_text_gcs <gcs_path> Detects text given a GCS path.
56+
detect_text_local <local_path> Detects text given file path.
57+
track_objects_gcs <gcs_path> Track objects given a GCS path.
58+
track_objects_local <local_path> Track objects given file path.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
require "rake/testtask"
16+
17+
Rake::TestTask.new "test" do |t|
18+
t.test_files = FileList["**/*_test.rb"]
19+
t.warning = false
20+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2020 Google, Inc
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+
require "minitest/autorun"
16+
17+
describe "Video Intelligence API Quickstart" do
18+
it "can find labels for a cat video" do
19+
assert_output(/Label description: animal/) do
20+
load File.expand_path("../quickstart.rb", __dir__)
21+
end
22+
end
23+
end
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Copyright 2020 Google, Inc
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+
require_relative "../video_samples"
16+
require "minitest/autorun"
17+
require "minitest/focus"
18+
require "minitest/hooks/default"
19+
require "net/http"
20+
require "tempfile"
21+
require "uri"
22+
23+
describe "Google Cloud Video API sample" do
24+
before :all do
25+
@labels_file = "cloud-samples-data/video/cat.mp4"
26+
@shots_file = "cloud-samples-data/video/gbikes_dinosaur.mp4"
27+
@safe_search_file = "cloud-samples-data/video/pizza.mp4"
28+
@transcription_file = "cloud-samples-data/video/googlework_short.mp4"
29+
end
30+
31+
it "can analyze labels from a gcs file" do
32+
assert_output(/Label description: animal/) do
33+
analyze_labels_gcs path: "gs://#{@labels_file}"
34+
end
35+
end
36+
37+
it "can analyze labels from a local file" do
38+
begin
39+
local_tempfile = Tempfile.new "temp_video"
40+
File.open local_tempfile.path, "w" do |file|
41+
file_contents = Net::HTTP.get URI("http://storage.googleapis.com/#{@transcription_file}")
42+
file.write file_contents
43+
file.flush
44+
end
45+
assert_output(/Finished Processing./) do
46+
analyze_labels_local path: local_tempfile.path
47+
end
48+
ensure
49+
local_tempfile.close
50+
local_tempfile.unlink
51+
end
52+
end
53+
54+
it "can analyze explicit content from a gcs file" do
55+
assert_output(/pornography: VERY_UNLIKELY/) do
56+
analyze_explicit_content path: "gs://#{@safe_search_file}"
57+
end
58+
end
59+
60+
it "can analyze shots from a gcs file" do
61+
assert_output(/0.0 to 5/) do
62+
analyze_shots path: "gs://#{@shots_file}"
63+
end
64+
end
65+
66+
it "can transcribe speech from a gcs file" do
67+
assert_output(/cultural/) do
68+
transcribe_speech_gcs path: "gs://#{@transcription_file}"
69+
end
70+
end
71+
72+
it "can detect texts from a gcs file" do
73+
assert_output(/GOOGLE/) do
74+
detect_text_gcs path: "gs://#{@transcription_file}"
75+
end
76+
end
77+
78+
it "can detect texts from a local file" do
79+
begin
80+
local_tempfile = Tempfile.new "temp_video"
81+
File.open local_tempfile.path, "w" do |file|
82+
file_contents = Net::HTTP.get URI("http://storage.googleapis.com/#{@transcription_file}")
83+
file.write file_contents
84+
file.flush
85+
end
86+
assert_output(/GOOGLE/) do
87+
detect_text_local path: local_tempfile.path
88+
end
89+
ensure
90+
local_tempfile.close
91+
local_tempfile.unlink
92+
end
93+
end
94+
95+
it "can track objects from a gcs file" do
96+
assert_output(/cat/) do
97+
track_objects_gcs path: "gs://#{@labels_file}"
98+
end
99+
end
100+
101+
it "can track objects from a local file" do
102+
begin
103+
local_tempfile = Tempfile.new "temp_video"
104+
File.open local_tempfile.path, "w" do |file|
105+
file_contents = Net::HTTP.get URI("http://storage.googleapis.com/#{@transcription_file}")
106+
file.write file_contents
107+
file.flush
108+
end
109+
assert_output(/Finished Processing./) do
110+
track_objects_local path: local_tempfile.path
111+
end
112+
ensure
113+
local_tempfile.close
114+
local_tempfile.unlink
115+
end
116+
end
117+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Copyright 2017 Google, Inc
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+
# [START video_quickstart]
16+
require "google/cloud/video_intelligence"
17+
18+
video_client = Google::Cloud::VideoIntelligence.video_intelligence_service
19+
features = [:LABEL_DETECTION]
20+
path = "gs://cloud-samples-data/video/cat.mp4"
21+
22+
# Register a callback during the method call
23+
operation = video_client.annotate_video features: features, input_uri: path
24+
25+
puts "Processing video for label annotations:"
26+
operation.wait_until_done!
27+
28+
raise operation.results.message? if operation.error?
29+
puts "Finished Processing."
30+
31+
labels = operation.results.annotation_results.first.segment_label_annotations
32+
33+
labels.each do |label|
34+
puts "Label description: #{label.entity.description}"
35+
36+
label.category_entities.each do |category_entity|
37+
puts "Label category description: #{category_entity.description}"
38+
end
39+
40+
label.segments.each do |segment|
41+
start_time = (segment.segment.start_time_offset.seconds +
42+
segment.segment.start_time_offset.nanos / 1e9)
43+
end_time = (segment.segment.end_time_offset.seconds +
44+
segment.segment.end_time_offset.nanos / 1e9)
45+
46+
puts "Segment: #{start_time} to #{end_time}"
47+
puts "Confidence: #{segment.confidence}"
48+
end
49+
end
50+
# [END video_quickstart]

0 commit comments

Comments
 (0)