I am trying to run the quick start demo by Google Vision APIs on MacOS Sierra.
def run_quickstart():
# [START vision_quickstart]
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
vision_client = vision.Client()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'resources/wakeupcat.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(
content=content)
# Performs label detection on the image file
labels = image.detect_labels()
print('Labels:')
for label in labels:
print(label.description)
# [END vision_quickstart]
if __name__ == '__main__':
run_quickstart()
Script looks as above. I am using Service Account key file to authenticate. As document suggested I have installed google-vision dependencies via pip and set up an environment variable with,
export GOOGLE_APPLICATION_CREDENTIALS=/my_credentials.json
Environment variable is correctly set. Still script raises,
oauth2client.client.HttpAccessTokenRefreshError: invalid_grant: Invalid JWT Signature.
There are similar questions asked when using API keys, when using Service account file was not mentioned.
Related
I am trying to extract text data from images using Google Cloud Vision API. My Initial starting point was here After Enabling Vision API and Creating service account,generating json file, I created a script by referring this example.
Here's my code
from google.cloud import vision
from google.cloud.vision_v1 import types
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'generated_after_creating_sec_key.json'
image_path = 'images\\image_1.png'
vision_client = vision.ImageAnnotatorClient()
print(vision_client)
image = types.Image()
image.source.image_path = image_path
response = vision_client.text_detection(image=image)
for text in response.text_annotations:
print(text.description)
The only difference between the example shown in google page and my code is that the image uploaded in the example is on gcloud while mine happens to be on local storage.
Here's the complete stacktrace.
<google.cloud.vision_v1.ImageAnnotatorClient object at 0x000001DF861D7970>
Traceback (most recent call last):
File "text_detection.py", line 10, in <module>
image.source.image_path = image_path
File "C:\Users\user\ProjectFolder\ProjName\venv\lib\site-packages\proto\message.py", line 677, in __setattr__
pb_type = self._meta.fields[key].pb_type
KeyError: 'image_path'
What is the root cause of this error? Please help!
Per the Google Cloud Vision docs, you want image.source.image_uri instead.
According to the Google Cloud Vision docs, you should use
image.source.image_uri instead.
I'm playing around with some scripts in python and trying to find if these image returns results. However when running python doesn't print anything. I don't get an error but can't seem to figure it out.
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
def run(annotations):
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'static/123456.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images retrieved'.format(
len(annotations.pages_with_matching_images)))
matching = annotations.pages_with_matching_images
print matching
I'm basing the work on these examples
https://cloud.google.com/vision/docs/quickstart-client-libraries#client-libraries-install-python
https://cloud.google.com/vision/docs/internet-detection
You are missing some key parts:
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
def run(): # remove the argument since you aren't using it
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.join(
os.path.dirname(__file__),
'static/123456.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content) # dedent this
web_detection = client.web_detection(image=image).web_detection
""" annotations doesn't exist in your script as is...
if annotations.pages_with_matching_images:
print('\n{} Pages with matching images retrieved'.format(
len(annotations.pages_with_matching_images)))
"""
# replace above with this
if web_detection.pages_with_matching_images:
print('\n{} Pages with matching images retrieved'.format(
len(web_detection.pages_with_matching_images)))
if __name__ == '__main__':
run()
Some of the key issues you need to watch out for when editing tutorial scripts is following your objects along. You can't use an object that is called in the tutorial in your own script if you don't first create that object.
I need (or at least I think I need) to create a file (could be a temp file but for now it does not work while I was testing it) where I can copy a file stored in google cloud storage.
This file is a geojson file and after load the file i will read it using geopandas.
The code will be run it inside a Kubernete in Google Cloud
The code:
def geoalarm(self,input):
from shapely.geometry import Point
import uuid
from google.cloud import storage
import geopandas as gpd
fp = open("XXX.geojson", "w+")
storage_client = storage.Client()
bucket = storage_client.get_bucket('YYY')
blob = bucket.get_blob('ZZZ.geojson')
blob.download_to_file(fp)
fp.seek(0)
PAIS = gpd.read_file(fp.name)
(dictionaryframe,_)=input
try:
place = Point((float(dictionaryframe["lon"])/100000), (float(dictionaryframe["lat"]) / 100000))
<...>
The questions are:
How could I create the file in kubernetes?
Or, how could I use the content of the file as string (if I use download_as_string) in geopandas to do the equivalent of geopanda.read_file(name)?
Extra
I tried using:
PAIS = gpd.read_file("gs://bucket/xxx.geojson")
But I have the following error:
DriverError: '/vsigs/bucket/xxx.geojson' does not exist in the file system, and is not recognized as a supported dataset name.
A VERY general overview of the pattern:
You can start by putting the code on a git repository. On Kubernetes, create a deployment/pod with the ubuntu image and make sure you install python, your python dependencies and pull your code in an initialization script, with the final line invoking python to run your code. In the "command" attribute of your pod template, you should use /bin/bash to run your script. Assuming you have the correct credentials, you will be able to grab the file from Google Storage and process it. To debug, you can attach to the running container using "kubectl exec".
Hope this helps!
A solution to avoid to create a file in kubernetes:
storage_client = storage.Client()
bucket = storage_client.get_bucket('buckte')
blob = bucket.get_blob('file.geojson')
string = blob.download_as_string()
PAIS = gpd.GeoDataFrame.from_features(json.loads(string))
Have a python code running on cloud function using cloud function python.
I'm processing an image on cloud. Now I want to save that image on google-cloud-storage
from google.cloud import storage
import cv2
from tempfile import NamedTemporaryFile
import os
client = storage.Client()
bucket = client.get_bucket('document')
image = cv2.imread('15.png')
with NamedTemporaryFile() as temp:
print(type(temp.name)," ",temp.name)
iName = "".join([str(temp.name),".jpg"])
print(iName)
cv2.imwrite(iName,image)
print(os.path.exists(str(iName)))
blob = bucket.blob('document/Test15.jpg')
blob.upload_from_filename(iName)
My output
< class 'str' >
/var/folders/5j/bqk11bqj4r55f8975mn__72m0000gq/T/tmplcgzaucx
/var/folders/5j/bqk11bqj4r55f8975mn__72m0000gq/T/tmplcgzaucx.jpg
True
Don't know what's going wrong
can anyone suggest a solution?
Silly mistake, turns out
blob = bucket.blob('document/Test15.jpg')
creates another folder document inside the bucket document
so the actual path would be document/document/Test15.jpg
I'mt trying to use goole vision api but i can't run my python script without getting the following error:
google.auth.exceptions.DefaultCredentialsError: ('File /root/GoogleCloudStaff/apikey.json is not a valid json file.', ValueError('Invalid control character at: line 5 column 37 (char 172)',))
My python script:
import io
from google.cloud import vision
vision_client = vision.Client()
#file_name = "/var/www/FlaskApp/FlaskApp/static/"#'375px-Guido_van_Rossum_OSCON_2006_cropped.png'
file_name = '1200px-Guido_van_Rossum_OSCON_2006.jpg'
#file_name = "/var/www/FlaskApp/FlaskApp/static/cyou_pic_folders/cyou_folder_2017_11_16_10_26_18/pi_pic_lc_2017_11_16_10_26_1800049.png"
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision_client.image(
content=content, )
labels = image.detect_labels()
for label in labels:
print(label.description)
Thanks very much!
DefaultCredentialsError indicates that you failed acquiring default credentials.Have you done initial set up in a proper manner?
Take a look at vision
The error you are facing might be attributed to an issue with the service account key itself. \n is a control character present in the key which signifies a new line, which might be causing the issue. To solve the error, you can either validate the content of the JSON file or you can download the key from Google Cloud again. The key can be downloaded by following these instructions.
After acquiring the service account key, the environment variable GOOGLE_APPLICATION_CREDENTIALS has to be set depending on the Operating System used.
As the final step, run the following Python code which performs a labeling task using the Cloud Vision API. The service account key will be automatically used to authenticate the labeling request.
import io
import os
# Imports the Google Cloud client library
from google.cloud import vision
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath('path/to/file/sample.jpg')
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
response = client.label_detection(image=image)
labels = response.label_annotations
print('Labels:')
for label in labels:
print(label.description)
The code prints out the labels which the API returns. The Vision API can detect and extract information about entities in an image, across a broad group of categories.
You seem to be missing the authentication config. From Using the client library:
Using the client library
To run the client library, you must first set up authentication.