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)))
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 need to save/read a file from this location -
bucket name - 'bucket_name'
folder - 'user/test'
I need to know how to connect to this location & read/write files.
Here is a complete tutorial for how to read a file from your Cloud Storage in your AI Jupyter Notebook
And you can also follow this step by step tutorial to read/Write files.
import google.datalab.storage as storage
import pandas as pd from io
import BytesIO
def get_bucket(bucket_target):
shared_bucket = storage.Bucket(str(bucket_name))
return shared_bucket
def read_file_from_bucket(file_target):
shared_bucket = get_bucket(bucket_target)
sample_object = shared_bucket.object(str(file_target)
sample_text = sample_object.read_stream()
return sample_text
Or you can do this :
import tensorflow as tf
GCS_PATTERN = 'gs://my_bucket/folder/*.tfrec'
filenames = tf.gfile.Glob(GCS_PATTERN)
The google cloud vision api works fine on one pdf pdf1 but returns absolutely nothing on the other pdf pdf2. I'm unable to make sense of this behavior as both the pdfs are very similar and have almost the same font.Please help.
I'm using the code given in their examples section by uploading these files in a google cloud bucket.
def async_detect_document(gcs_source_uri, gcs_destination_uri):
"""OCR with PDF/TIFF as source files on GCS"""
from google.cloud import vision
from google.cloud import storage
from google.protobuf import json_format
# Supported mime_types are: 'application/pdf' and 'image/tiff'
mime_type = 'application/pdf'
# How many pages should be grouped into each json output file.
batch_size = 2
client = vision.ImageAnnotatorClient()
feature = vision.types.Feature(
type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
input_config = vision.types.InputConfig(
gcs_source=gcs_source, mime_type=mime_type)
gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
output_config = vision.types.OutputConfig(
gcs_destination=gcs_destination, batch_size=batch_size)
async_request = vision.types.AsyncAnnotateFileRequest(
features=[feature], input_config=input_config,
output_config=output_config)
operation = client.async_batch_annotate_files(
requests=[async_request])
print('Waiting for the operation to finish.')
operation.result(timeout=180)
# Once the request has completed and the output has been
# written to GCS, we can list all the output files.
storage_client = storage.Client()
match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
bucket_name = match.group(1)
prefix = match.group(2)
bucket = storage_client.get_bucket(bucket_name=bucket_name)
# List objects with the given prefix.
blob_list = list(bucket.list_blobs(prefix=prefix))
print('Output files:')
for blob in blob_list:
print(blob.name)
# Process the first output file from GCS.
# Since we specified batch_size=2, the first response contains
# the first two pages of the input file.
output = blob_list[0]
json_string = output.download_as_string()
response = json_format.Parse(
json_string, vision.types.AnnotateFileResponse())
# The actual response for the first page of the input file.
first_page_response = response.responses[0]
annotation = first_page_response.full_text_annotation
# Here we print the full text from the first page.
# The response contains more information:
# annotation/pages/blocks/paragraphs/words/symbols
# including confidence scores and bounding boxes
print(u'Full text:\n{}'.format(
annotation.text))
It probably has nothing to do with the GCloud API, I tried uploading your pdf to the vision drag and drop website and it returns expected results. Maybe at some point in your pipeline, the pdf is corrupted in any way? what does it look like in gcloud storage?
we also faced this issue and after doing few experiments it seems to me this is due to some font google vision not able to support.
To solve this generate pdf to image and then send image to process will provide result.
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.
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