Skip to content

Instantly share code, notes, and snippets.

@takurx
Created Apr 19, 2021
Embed
What would you like to do?
test for google cloud api
chino@kafu:~/Desktop/210419_google_cloud_vision$ export GOOGLE_APPLICATION_CREDENTIALS="/home/chino/Desktop/210419_google_cloud_vision/synthetic-nova-114514-23e4b6aa4a23.json"
chino@kafu:~/Desktop/210419_google_cloud_vision$ curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-335.0.0-linux-x86_64.tar.gz
chino@kafu:~/Desktop/210419_google_cloud_vision$ tar zxvf ./google-cloud-sdk-335.0.0-linux-x86_64.tar.gz
chino@kafu:~/Desktop/210419_google_cloud_vision$ ./google-cloud-sdk/install.sh 
Welcome to the Google Cloud SDK!

To help improve the quality of this product, we collect anonymized usage data
and anonymized stacktraces when crashes are encountered; additional information
is available at <https://cloud.google.com/sdk/usage-statistics>. This data is
handled in accordance with our privacy policy
<https://cloud.google.com/terms/cloud-privacy-notice>. You may choose to opt in this
collection now (by choosing 'Y' at the below prompt), or at any time in the
future by running the following command:

    gcloud config set disable_usage_reporting false

Do you want to help improve the Google Cloud SDK (y/N)?  y


Your current Cloud SDK version is: 335.0.0
The latest available version is: 336.0.0

┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                   Components                                                  │
├──────────────────┬──────────────────────────────────────────────────────┬──────────────────────────┬──────────┤
│      Status      │                         Name                         │            ID            │   Size   │
├──────────────────┼──────────────────────────────────────────────────────┼──────────────────────────┼──────────┤
│ Update Available │ Cloud SDK Core Libraries                             │ core                     │ 17.9 MiB │
│ Update Available │ Cloud Storage Command Line Tool                      │ gsutil                   │  3.9 MiB │
│ Not Installed    │ App Engine Go Extensions                             │ app-engine-go            │  4.9 MiB │
│ Not Installed    │ Appctl                                               │ appctl                   │ 21.0 MiB │
│ Not Installed    │ Cloud Bigtable Command Line Tool                     │ cbt                      │  7.7 MiB │
│ Not Installed    │ Cloud Bigtable Emulator                              │ bigtable                 │  6.6 MiB │
│ Not Installed    │ Cloud Datalab Command Line Tool                      │ datalab                  │  < 1 MiB │
│ Not Installed    │ Cloud Datastore Emulator                             │ cloud-datastore-emulator │ 18.4 MiB │
│ Not Installed    │ Cloud Firestore Emulator                             │ cloud-firestore-emulator │ 41.9 MiB │
│ Not Installed    │ Cloud Pub/Sub Emulator                               │ pubsub-emulator          │ 60.4 MiB │
│ Not Installed    │ Cloud SQL Proxy                                      │ cloud_sql_proxy          │  7.6 MiB │
│ Not Installed    │ Cloud Spanner Emulator                               │ cloud-spanner-emulator   │ 21.8 MiB │
│ Not Installed    │ Emulator Reverse Proxy                               │ emulator-reverse-proxy   │ 14.5 MiB │
│ Not Installed    │ Google Cloud Build Local Builder                     │ cloud-build-local        │  6.3 MiB │
│ Not Installed    │ Google Container Registry's Docker credential helper │ docker-credential-gcr    │  1.8 MiB │
│ Not Installed    │ Kustomize                                            │ kustomize                │ 25.9 MiB │
│ Not Installed    │ Minikube                                             │ minikube                 │ 23.9 MiB │
│ Not Installed    │ Nomos CLI                                            │ nomos                    │ 22.9 MiB │
│ Not Installed    │ On-Demand Scanning API extraction helper             │ local-extract            │ 13.5 MiB │
│ Not Installed    │ Skaffold                                             │ skaffold                 │ 16.6 MiB │
│ Not Installed    │ anthos-auth                                          │ anthos-auth              │ 16.4 MiB │
│ Not Installed    │ config-connector                                     │ config-connector         │ 44.4 MiB │
│ Not Installed    │ gcloud Alpha Commands                                │ alpha                    │  < 1 MiB │
│ Not Installed    │ gcloud Beta Commands                                 │ beta                     │  < 1 MiB │
│ Not Installed    │ gcloud app Java Extensions                           │ app-engine-java          │ 53.1 MiB │
│ Not Installed    │ gcloud app Python Extensions                         │ app-engine-python        │  6.1 MiB │
│ Not Installed    │ gcloud app Python Extensions (Extra Libraries)       │ app-engine-python-extras │ 27.1 MiB │
│ Not Installed    │ kpt                                                  │ kpt                      │ 11.7 MiB │
│ Not Installed    │ kubectl                                              │ kubectl                  │  < 1 MiB │
│ Not Installed    │ kubectl-oidc                                         │ kubectl-oidc             │ 16.4 MiB │
│ Not Installed    │ pkg                                                  │ pkg                      │          │
│ Installed        │ BigQuery Command Line Tool                           │ bq                       │  < 1 MiB │
└──────────────────┴──────────────────────────────────────────────────────┴──────────────────────────┴──────────┘
To install or remove components at your current SDK version [335.0.0], run:
  $ gcloud components install COMPONENT_ID
  $ gcloud components remove COMPONENT_ID

To update your SDK installation to the latest version [336.0.0], run:
  $ gcloud components update


Modify profile to update your $PATH and enable shell command 
completion?

Do you want to continue (Y/n)?  Y

The Google Cloud SDK installer will now prompt you to update an rc 
file to bring the Google Cloud CLIs into your environment.

Enter a path to an rc file to update, or leave blank to use 
[/home/chino/.bashrc]:  
Backing up [/home/chino/.bashrc] to [/home/chino/.bashrc.backup].
[/home/chino/.bashrc] has been updated.

==> Start a new shell for the changes to take effect.


For more information on how to get started, please visit:
  https://cloud.google.com/sdk/docs/quickstarts


chino@kafu:~/Desktop/210419_google_cloud_vision$ ./google-cloud-sdk/bin/gcloud init
Welcome! This command will take you through the configuration of gcloud.

Your current configuration has been set to: [default]

You can skip diagnostics next time by using the following flag:
  gcloud init --skip-diagnostics

Network diagnostic detects and fixes local network connection issues.
Checking network connection...done.                                            
Reachability Check passed.
Network diagnostic passed (1/1 checks passed).

You must log in to continue. Would you like to log in (Y/n)?  Y

Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=11451481019.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2F&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.admin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcompute+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&state=8yF2IZARwSFzFGNAJdTAyrCiiy7SIx&access_type=offline&code_challenge=TfLWVL9W1RB5rfCSuVPbN3lGfcYHH0POkwF37TopVUQ&code_challenge_method=S256



Updates are available for some Cloud SDK components.  To install them,
please run:
  $ gcloud components update

You are logged in as: [chino_kafu@gmail.com].

Pick cloud project to use: 
 [1] synthetic-nova-114514
 [2] test-aprs-1145141919454
 [3] Create a new project
Please enter numeric choice or text value (must exactly match list 
item):  1

Your current project has been set to: [synthetic-nova-114514].

Not setting default zone/region (this feature makes it easier to use
[gcloud compute] by setting an appropriate default value for the
--zone and --region flag).
See https://cloud.google.com/compute/docs/gcloud-compute section on how to set
default compute region and zone manually. If you would like [gcloud init] to be
able to do this for you the next time you run it, make sure the
Compute Engine API is enabled for your project on the
https://console.developers.google.com/apis page.

Created a default .boto configuration file at [/home/chino/.boto]. See this file and
[https://cloud.google.com/storage/docs/gsutil/commands/config] for more
information about configuring Google Cloud Storage.
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use chino_kafu@gmail.com by default
* Commands will reference project `synthetic-nova-114514` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default]. You can create additional configurations if you work with multiple accounts and/or projects.
Run `gcloud topic configurations` to learn more.

Some things to try next:

* Run `gcloud --help` to see the Cloud Platform services you can interact with. And run `gcloud help COMMAND` to get help on any gcloud command.
* Run `gcloud topic --help` to learn about advanced features of the SDK like arg files and output formatting
chino@kafu:~/Desktop/210419_google_cloud_vision$ source ~/.bashrc
chino@kafu:~/Desktop/210419_google_cloud_vision$ gcloud auth application-default print-access-token
ya29.c.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
chino@kafu:~/Desktop/210419_google_cloud_vision$ pip install --upgrade google-cloud-vision
Defaulting to user installation because normal site-packages is not writeable
Collecting google-cloud-vision
  Downloading google_cloud_vision-2.3.1-py2.py3-none-any.whl (461 kB)
     |████████████████████████████████| 461 kB 10.1 MB/s 
Collecting proto-plus>=1.15.0
  Downloading proto_plus-1.18.1-py3-none-any.whl (42 kB)
     |████████████████████████████████| 42 kB 260 kB/s 
Collecting google-api-core[grpc]<2.0.0dev,>=1.22.2
  Downloading google_api_core-1.26.3-py2.py3-none-any.whl (93 kB)
     |████████████████████████████████| 93 kB 165 kB/s 
Collecting protobuf>=3.12.0
  Downloading protobuf-3.15.8-cp38-cp38-manylinux1_x86_64.whl (1.0 MB)
     |████████████████████████████████| 1.0 MB 84.3 MB/s 
Collecting packaging>=14.3
  Downloading packaging-20.9-py2.py3-none-any.whl (40 kB)
     |████████████████████████████████| 40 kB 939 kB/s 
Requirement already satisfied, skipping upgrade: pytz in /usr/lib/python3/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-vision) (2019.3)
Requirement already satisfied, skipping upgrade: six>=1.13.0 in /usr/lib/python3/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-vision) (1.14.0)
Requirement already satisfied, skipping upgrade: setuptools>=40.3.0 in /usr/lib/python3/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-vision) (45.2.0)
Collecting googleapis-common-protos<2.0dev,>=1.6.0
  Downloading googleapis_common_protos-1.53.0-py2.py3-none-any.whl (198 kB)
     |████████████████████████████████| 198 kB 102.1 MB/s 
Requirement already satisfied, skipping upgrade: requests<3.0.0dev,>=2.18.0 in /usr/lib/python3/dist-packages (from google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-vision) (2.22.0)
Collecting google-auth<2.0dev,>=1.21.1
  Downloading google_auth-1.29.0-py2.py3-none-any.whl (142 kB)
     |████████████████████████████████| 142 kB 70.1 MB/s 
Collecting grpcio<2.0dev,>=1.29.0; extra == "grpc"
  Downloading grpcio-1.37.0-cp38-cp38-manylinux2014_x86_64.whl (4.2 MB)
     |████████████████████████████████| 4.2 MB 93.9 MB/s 
Requirement already satisfied, skipping upgrade: pyparsing>=2.0.2 in /home/chino/.local/lib/python3.8/site-packages (from packaging>=14.3->google-api-core[grpc]<2.0.0dev,>=1.22.2->google-cloud-vision) (2.4.7)
Collecting rsa<5,>=3.1.4; python_version >= "3.6"
  Downloading rsa-4.7.2-py3-none-any.whl (34 kB)
Collecting cachetools<5.0,>=2.0.0
  Downloading cachetools-4.2.1-py3-none-any.whl (12 kB)
Collecting pyasn1-modules>=0.2.1
  Downloading pyasn1_modules-0.2.8-py2.py3-none-any.whl (155 kB)
     |████████████████████████████████| 155 kB 86.0 MB/s 
Collecting pyasn1>=0.1.3
  Downloading pyasn1-0.4.8-py2.py3-none-any.whl (77 kB)
     |████████████████████████████████| 77 kB 1.1 MB/s 
Installing collected packages: protobuf, proto-plus, packaging, googleapis-common-protos, pyasn1, rsa, cachetools, pyasn1-modules, google-auth, grpcio, google-api-core, google-cloud-vision
Successfully installed cachetools-4.2.1 google-api-core-1.26.3 google-auth-1.29.0 google-cloud-vision-2.3.1 googleapis-common-protos-1.53.0 grpcio-1.37.0 packaging-20.9 proto-plus-1.18.1 protobuf-3.15.8 pyasn1-0.4.8 pyasn1-modules-0.2.8 rsa-4.7.2
WARNING: You are using pip version 20.2.4; however, version 21.0.1 is available.
You should consider upgrading via the '/usr/bin/python3 -m pip install --upgrade pip' command.
chino@kafu:~$ cd Works/uploader_flask/
chino@kafu:~/Works/uploader_flask$ python3 ./test_google_cloud_vison_api.py 
Traceback (most recent call last):
  File "./test_google_cloud_vison_api.py", line 9, in <module>
    client = vision.ImageAnnotatorClient()
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_v1/services/image_annotator/client.py", line 357, in __init__
    self._transport = Transport(
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_v1/services/image_annotator/transports/grpc.py", line 153, in __init__
    super().__init__(
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_v1/services/image_annotator/transports/base.py", line 102, in __init__
    credentials, _ = auth.default(
  File "/home/chino/.local/lib/python3.8/site-packages/google/auth/_default.py", line 483, in default
    raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
chino@kafu:~/Works/uploader_flask$ export GOOGLE_APPLICATION_CREDENTIALS="/home/chino/Desktop/210419_google_cloud_vision/synthetic-nova-114514-23e4b6aa4a23.json"
chino@kafu:~/Works/uploader_flask$ python3 ./test_google_cloud_vison_api.py 
Traceback (most recent call last):
  File "./test_google_cloud_vison_api.py", line 16, in <module>
    with io.open(file_name, 'rb') as image_file:
FileNotFoundError: [Errno 2] No such file or directory: '/home/chino/Works/uploader_flask/resources/wakeupcat.jpg'
chino@kafu:~/Works/uploader_flask$ wget https://raw.githubusercontent.com/googleapis/python-vision/master/samples/snippets/quickstart/resources/wakeupcat.jpg
--2021-04-19 14:37:08--  https://raw.githubusercontent.com/googleapis/python-vision/master/samples/snippets/quickstart/resources/wakeupcat.jpg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.111.133, 185.199.110.133, 185.199.109.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.111.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 64892 (63K) [image/jpeg]
Saving to: ‘wakeupcat.jpg’

wakeupcat.jpg       100%[===================>]  63.37K  --.-KB/s    in 0.007s  

2021-04-19 14:37:08 (8.64 MB/s) - ‘wakeupcat.jpg’ saved [64892/64892]

chino@kafu:~/Works/uploader_flask$ python3 ./test_google_cloud_vison_api.py 
Traceback (most recent call last):
  File "./test_google_cloud_vison_api.py", line 16, in <module>
    with io.open(file_name, 'rb') as image_file:
FileNotFoundError: [Errno 2] No such file or directory: '/home/chino/Works/uploader_flask/resources/wakeupcat.jpg'
chino@kafu:~/Works/uploader_flask$ mkdir resources
chino@kafu:~/Works/uploader_flask$ mv wakeupcat.jpg resources/
chino@kafu:~/Works/uploader_flask$ python3 ./test_google_cloud_vison_api.py 
Traceback (most recent call last):
  File "/home/chino/.local/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 73, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "/home/chino/.local/lib/python3.8/site-packages/grpc/_channel.py", line 946, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "/home/chino/.local/lib/python3.8/site-packages/grpc/_channel.py", line 849, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
	status = StatusCode.PERMISSION_DENIED
	details = "This API method requires billing to be enabled. Please enable billing on project #114514191945 by visiting https://console.developers.google.com/billing/enable?project=114514191945 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry."
	debug_error_string = "{"created":"@1618810657.763351920","description":"Error received from peer ipv4:172.217.25.202:443","file":"src/core/lib/surface/call.cc","file_line":1067,"grpc_message":"This API method requires billing to be enabled. Please enable billing on project #114514191945 by visiting https://console.developers.google.com/billing/enable?project=114514191945 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.","grpc_status":7}"
>

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./test_google_cloud_vison_api.py", line 22, in <module>
    response = client.label_detection(image=image)
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_helpers/decorators.py", line 112, in inner
    response = self.annotate_image(
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_helpers/__init__.py", line 76, in annotate_image
    r = self.batch_annotate_images(
  File "/home/chino/.local/lib/python3.8/site-packages/google/cloud/vision_v1/services/image_annotator/client.py", line 434, in batch_annotate_images
    response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
  File "/home/chino/.local/lib/python3.8/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "/home/chino/.local/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 75, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.PermissionDenied: 403 This API method requires billing to be enabled. Please enable billing on project #114514191945 by visiting https://console.developers.google.com/billing/enable?project=114514191945 then retry. If you enabled billing for this project recently, wait a few minutes for the action to propagate to our systems and retry.
chino@kafu:~/Works/uploader_flask$ python3 ./test_google_cloud_vison_api.py 
Labels:
Cat
Window
Felidae
Carnivore
Jaw
Ear
Small to medium-sized cats
Window blind
Gesture
Whiskers
chino@kafu:~/Works/uploader_flask$ wget https://github.com/googleapis/python-vision/raw/master/samples/snippets/detect/detect.py
--2021-04-19 15:09:12--  https://github.com/googleapis/python-vision/raw/master/samples/snippets/detect/detect.py
Resolving github.com (github.com)... 52.69.186.44
Connecting to github.com (github.com)|52.69.186.44|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/googleapis/python-vision/master/samples/snippets/detect/detect.py [following]
--2021-04-19 15:09:12--  https://raw.githubusercontent.com/googleapis/python-vision/master/samples/snippets/detect/detect.py
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 38142 (37K) [text/plain]
Saving to: ‘detect.py’

detect.py                    100%[============================================>]  37.25K  --.-KB/s    in 0.006s  

2021-04-19 15:09:12 (6.27 MB/s) - ‘detect.py’ saved [38142/38142]

chino@kafu:~/Works/uploader_flask$ python3 ./detect.py text ./resources/wakeupcat.jpg
Texts:

"Wake up human!
"
bounds: (30,396),(567,396),(567,465),(30,465)

"Wake"
bounds: (30,396),(209,396),(209,457),(30,457)

"up"
bounds: (230,406),(305,406),(305,463),(230,463)

"human!"
bounds: (326,396),(567,396),(567,465),(326,465)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment