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.
Related
I am a beginner in programming, and this is my first little try. I'm currently facing a bottleneck, I would like to ask for the help. Any advice will be welcome. Thank you in advance!
Here is what I want to do:
To make a text detection application and extract the text for the further usage(for instance, to map some of the other relevant information in a data). So, I devided into two steps:
1.first, to detect the text
2.extract the text and use the regular expression to rearrange it for the data mapping.
For the first step, I use google vision api, so I have no probelm reading the image from google cloud storage(code reference 1):
However, when it comes to step two, I need a PIL module to open the file for drawing the text. When useing the methodImage.open(), it requries a path`. My question is how do I call the path? (code reference 2):
code reference 1:
from google.cloud import vision
image_uri = 'gs://img_platecapture/img_001.jpg'
client = vision.ImageAnnotatorClient()
image = vision.Image()
image.source.image_uri = image_uri ## <- THE PATH ##
response = client.text_detection(image=image)
for text in response.text_annotations:
print('=' * 30)
print(text.description)
vertices = ['(%s,%s)' % (v.x, v.y) for v in text.bounding_poly.vertices]
print('bounds:', ",".join(vertices))
if response.error.message:
raise Exception(
'{}\nFor more info on error messages, check: '
'https://cloud.google.com/apis/design/errors'.format(
response.error.message))
code reference 2:
from PIL import Image, ImageDraw
from PIL import ImageFont
import re
img = Image.open(?) <- THE PATH ##
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("simsun.ttc", 18)
for text in response.text_annotations[1::]:
ocr = text.description
bound=text.bounding_poly
draw.text((bound.vertices[0].x-25, bound.vertices[0].y-25),ocr,fill=(255,0,0),font=font)
draw.polygon(
[
bound.vertices[0].x,
bound.vertices[0].y,
bound.vertices[1].x,
bound.vertices[1].y,
bound.vertices[2].x,
bound.vertices[2].y,
bound.vertices[3].x,
bound.vertices[3].y,
],
None,
'yellow',
)
texts=response.text_annotations
a=str(texts[0].description.split())
b=re.sub(u"([^\u4e00-\u9fa5\u0030-u0039])","",a)
b1="".join(b)
regex1 = re.search(r"\D{1,2}Dist.",b)
if regex1:
regex1="{}".format(regex1.group(0))
.........
PIL does not have built in ability to automatically open files from GCS. you will need to either
Download the file to local storage and point PIL to that file or
Give PIL a BlobReader which it can use to access the data:
from PIL import Image
from google.cloud import storage
storage_client = storage.Client()
bucket = storage_client.bucket('img_platecapture')
blob = bucket.get_blob('img_001.jpg') # use get_blob to fix generation number, so we don't get corruption if blob is overwritten while we read it.
with blob.open() as file:
img = Image.open(file)
# ...
I have the exact same 1300x1000 image locally on my HD and in my google bucket. Using the code below, Google Vision finds two objects when I use the image in the bucket, and zero objects when I use the image on my HD. What am I missing?
Code for local upload:
from google.cloud import vision
import numpy as np
import cv2
import base64
client = vision.ImageAnnotatorClient()
local_image = cv2.imread(IMG_PATH,cv2.IMREAD_COLOR)
upload_image = base64.b64encode(local_image)
upload_image = vision.types.Image(content=upload_image)
obj_annot = client.object_localization(image = upload_image).localized_object_annotations
print('Number of objects found: {}'.format(len(obj_annot)))
This finds zero objects.
The google bucket code on the other hand, finds two objects.
client = vision.ImageAnnotatorClient()
bucket_image = {'source': {'image_uri': GS_PATH}}
obj_annot = client.object_localization(image = bucket_image).localized_object_annotations
print('Number of objects found: {}'.format(len(obj_annot)))
I am working on a script that would Convert a PDF from the internet (without saving it to disk) to a series of jpegs, then save the JPGs to AWS s3.
Unfortunately, the code below only saves the first page of the PDF as JPG to AWS. Any ideas on how I can modify it to save images to AWS with sequential file names?
from urllib2 import urlopen
from wand.image import Image
from io import BytesIO
import boto3
s3 = boto3.client(
's3',
aws_access_key_id='mykey',
aws_secret_access_key='mykey'
)
bucket_name = 'testbucketAWS323'
#location on disk
#file prefix
test_id = 'example'
f = urlopen("https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf")
bytes_io_file = BytesIO()
with Image(file=f) as img:
print('pages = ', len(img.sequence))
with img.convert('png') as converted:
bytes_io_file = BytesIO(converted.make_blob('jpeg'))
#code below should take 'converted' object, and save it to AWS as jpg.
s3.upload_fileobj(bytes_io_file, bucket_name, "assssd.jpg")
print 'done'
Just enumerate over the document pages (wand.image.Image.sequence) to get the page number & resource. With the page resource copied to a new instance of Image, export blob directly, and don't worry about intermediate conversions.
from urllib2 import urlopen
from wand.image import Image
from io import BytesIO
import boto3
# ....
url = 'https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf'
resource = urlopen(url)
with Image(file=resource) as document:
for page_number, page in enumerate(document.sequence):
with Image(page) as img:
bytes_io_file = BytesIO(img.make_blob('JPEG'))
filename = 'output_{0}.jpg'.format(page_number)
s3.upload_fileobj(bytes_io_file, bucket_name, filename)
What about using the upload_fileobj method on converted?
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.
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.