Prepare before you start
for the next quick start.
Since 1 to 3 are project settings, they are as instructed in the quick start, but 4 and 5 are slightly different.
Make sure the project you created in step 1 is linked to your billing account. (Validity of billing is at your own risk.)
When you select the Enable
button, the screen for selecting a project is displayed. Select the project created in 1 and Continue
.
(In the past, there were times when the API couldn't be enabled, but when I recreated the project, it worked fine. I don't know the cause.)
First of all, what is a service account?
A service account is a special Google account that belongs to an application or virtual machine (VM) rather than an individual end user. Applications can use their service account to call Google's service API without the need for user involvement.
For example, if a service account runs a Compute Engine VM, you can give that account access to the resources you need. The service account thus becomes the identity of the service, and the privileges of the service account control the resources that the service can access.
By setting the permissions of the service account, you control the resources that the service can access, so
Here we want to create a service account with only the view
permission on the storage
.
(To upload the video to Google Cloud Storage
once and have it loaded from there.)
When I created it from the quick start link, I didn't know the cause, but it didn't work, so
-Select + Create Service Account
from Service Accounts – IAM and Administration – Google Cloud Platform
--Enter service account name
and select create
--Select Storage> Storage Object Viewer
from Role
--Select Continue
--Select Create
from` Create Key``
--Download JSON file with pop-up
Since it is necessary to specify the file path of the JSON file, the environment variable is set in the quick start, Here, the path is specified directly from the Colaboratory, so no setting is required.
Go to Storage Browser – Storage – Google Cloud Platform (https://console.cloud.google.com/storage/browser?hl=ja) and select Create Bucket
.
Enter a name for your bucket and select Create
.
(I don't care about regions here.)
Once you've created your bucket, upload your video.
I uploaded dog.mp4
directly under the bucket.
Please be careful about the free tier of storage capacity and download capacity.
Cloud Storage pricing| Cloud Storage | Google Cloud
On Colaboratory,
--File
> Python 3 new notebook
Make a new notebook from.
Install the Cloud Video Intelligence API
Python package.
!pip install -U google-cloud-videointelligence
Upload the JSON file that you downloaded when you created the service account from File
in the left pane of Colaboratory.
import json
from google.oauth2 import service_account
service_account_key_name = <JSON file name>
info = json.load(open(service_account_key_name))
creds = service_account.Credentials.from_service_account_info(info)
Create the client by specifying the certificate here.
from google.cloud import videointelligence
video_client = videointelligence.VideoIntelligenceServiceClient(credentials=creds)
This time, I uploaded the video file directly under the bucket, so it will be as follows.
video_url = "gs://<Bucket name>/<Video file name>"
From here, it's the same as the quick start.
features = [videointelligence.enums.Feature.LABEL_DETECTION]
operation = video_client.annotate_video(
video_url, features=features)
print('\nProcessing video for label annotations:')
result = operation.result(timeout=120)
print('\nFinished processing.')
# first result is retrieved because a single video was processed
segment_labels = result.annotation_results[0].segment_label_annotations
for i, segment_label in enumerate(segment_labels):
print('Video label description: {}'.format(
segment_label.entity.description))
for category_entity in segment_label.category_entities:
print('\tLabel category description: {}'.format(
category_entity.description))
for i, segment in enumerate(segment_label.segments):
start_time = (segment.segment.start_time_offset.seconds +
segment.segment.start_time_offset.nanos / 1e9)
end_time = (segment.segment.end_time_offset.seconds +
segment.segment.end_time_offset.nanos / 1e9)
positions = '{}s to {}s'.format(start_time, end_time)
confidence = segment.confidence
print('\tSegment {}: {}'.format(i, positions))
print('\tConfidence: {}'.format(confidence))
print('\n')
The analysis result is displayed safely.
PermissionDenied: 403 The caller does not have permission
Removed permissions granted to members (service accounts) from the IAM-IAM and Administration-Google Cloud Platform (https://console.cloud.google.com/iam-admin/iam?hl=ja) page. After that, I recreated the service account and it worked. However, the cause is unknown.
Recommended Posts