I have the follow function that passes a image url to google vision service and returns the letters and numbers (characters) in the image. It works fine with general web urls but I'm calling it to access files stored in Google storage, it doesn't work. How can i get this to work? I've looked at examples from googling but I cant work out how to do this?
If its not possible to use google storage, is there a way you can just upload the image rather than storing in on a file system? I have no need for storing the image, all i care about is the returned characters.
def detect_text_uri(uri):
"""Detects text in the file located in Google Cloud Storage or on the Web.
"""
from google.cloud import vision
client = vision.ImageAnnotatorClient()
image = vision.types.Image()
image.source.image_uri = uri
image.source.gcs_image_uri = uri
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
print('\n"{}"'.format(text.description))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
return texts
{
This line doesn't work which should read an image I've placed in google storage, all thats returned is a blank responce:
detect_text_uri("'source': {'image_uri': 'gs://ocr_storage/meter_reader.jpg'}")
This line works fine :
detect_text_uri('https://upload.wikimedia.org/wikipedia/commons/thumb/4/4a/Transparent_Electricity_Meter_found_in_Israel.JPG/220px-Transparent_Electricity_Meter_found_in_Israel.JPG')
Your function is just expecting the gcs uri
detect_text_uri('gs://ocr_storage/meter_reader.jpg')
What that function is waiting for, is the URI from google storage. But before running that cell you need to log in with the json file which contains your credentials:
os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r"credentials.json"
You can use a similar function to read text from a local image.
The code is in this link: https://cloud.google.com/vision/docs/ocr#vision_text_detection_gcs-python
Related
storage_client = storage.Client.from_service_account_json('service-account-file.json')
bucket = storage_client.get_bucket(my_bucket)
blob = bucket.blob(my_blob)
blob.upload_from_string(img)
It depends from where you are uploading the image or in better explanation from where you store the image... if you wan to get the image from specific link from the web you can scrape using python module selinium or scrapy depends from where you do ... if you want upload using code you might more read the documentation and use the api key to upload ..
I see your code you may need to use scrapy instead of javascript ...
I am trying to create an image thumbnail creation function using python, running in a google cloud platform's function. The image is sent as a base64 string to the cloud function, manipulated and made smaller with Python's Pillow package. It is then uploaded as an image, going from a Pillow Image object, to a BytesIO object, then saved to google cloud storage. This is all done successfully.
The problem here is very strange: Google Cloud Storage does not recognize the image until an access token is created manually. Otherwise, the image is left in an infinite loop, never loading, and never being able to be used.
I have reviewed this SO post, which has a very similar problem to mine (the image here shows exactly my problem: an uploaded file cannot be loaded properly), but it differs in two imporant categories: 1) They are manipulating the image array directly, while my code never touches it and 2) they are working in Node.js, where the Firebase SDK is different than in Python.
The code to generate the image is as follows:
def thumbnailCreator(request):
# Setting up the resourcse we are going to use
storage_client = storage.Client()
stor_bucket = storage_client.bucket(BUCKET_LINK)
# Retriving the Data
sent_data = request.get_json()['data']
name = sent_data['name']
userID = sent_data['userID']
# Process to go between base64 string to bytes, to a file object
imageString = stor_bucket.blob(PATH_TO_FULL_SIZE_IMAGE).download_as_string()
imageFile = BytesIO(imageString)
image = Image.open(imageFile)
# Resizing the image is the goal
image = image.resize(THUMBNAIL_SIZE)
# Go between pillow Image object to a file
imageFile = BytesIO()
image.save(imageFile, format='PNG')
imageBytes = imageFile.getvalue()
image64 = base64.b64encode(imageBytes)
imageFile.seek(0)
# Uploading the Data
other_blob = stor_bucket.blob(PATH_FOR_THUMBNAIL_IMAGE)
other_blob.upload_from_file(imageFile, content_type = 'image/png')
return {'data': {'response': 'ok', 'status': 200}}
Again, this works. I have a feeling there is something wrong with the MIME type. I am a novice when it comes to this type of programming/networking/image manipulation, so I'm always looking for a better way to do this. Anyway, thanks for any and all help.
It appears that the premise of this question - that a access token must be made manually for the image to work - is not accurate. After further testing, the error came from other parts of the code base I was working in. The above python script does work for image manipulation. An access token to the image can be generated via code, and be provided client-side.
Leaving this up in case someone stumbles upon it in the future when they need to work with Pillow/PIL in the Google Cloud Platform.
I developed an Image Classification Model in CustomVision.AI
I want to download all of the training images used to train the model
I used the training API and was able to retrieve the "HTML" location of all the images however I'd like to use a script to actually download the image to a local drive from the HTML location but am not great with writing scripts as much as running them.
I was also trying to figure out if the images are stored in an azure resource or are custom vision uses its own load storage the images, I'd like to move the images over to an azure blob.
I'm not really a "Programmer" really more of a high-level technology manager but I am comfortable in running scripts and some python code.
I was also trying to figure out if the images are stored in an azure
resource
I suppose it's , cause the images are stored in blob after I checked the image uri. You could use browser developer tool(F12) directly to check the picture uri. The uri format is blob url with image id.
If you want to get all images uri you could use this api: GetTaggedImages and GetUntaggedImages, it will return all images information including the uri.
I am fairly new to the Google Cloud Vision API so my apologies if there is an obvious answer to this. I am noticing that for some images I am getting different OCR results between the Google Cloud Vision API Drag and Drop (https://cloud.google.com/vision/docs/drag-and-drop) and from local image detection in python.
My code is as follows
import io
# Imports the Google Cloud client library
from google.cloud import vision
from google.cloud.vision import types
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = "./test0004a.jpg"
# Loads the image into memory
with io.open(file_name, 'rb') as image_file:
content = image_file.read()
image = types.Image(content=content)
response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')
for text in texts:
# print('\n"{}"'.format(text.description.encode('utf-8')))
print('\n"{}"'.format(text.description.encode('ascii','ignore')))
vertices = (['({},{})'.format(vertex.x, vertex.y)
for vertex in text.bounding_poly.vertices])
print('bounds: {}'.format(','.join(vertices)))
A sample image that highlights this is attached Sample Image
The python code above doesn't return anything, but in the browser using drag and drop it correctly identifies "2340" as the text.
Shouldn't both python and the browser return the same result?. And if not, why not?, Do I need to include additional parameters in the code?.
The issue here is that you are using TEXT_DETECTION instead of DOCUMENT_TEXT_DETECTION, which is the feature being used in the Drag and Drop example page that you shared.
By changing the method (to document_text_detection()), you should obtain the desired results (I have tested it with your code, and it did work):
# Using TEXT_DETECTION
response = client.text_detection(image=image)
# Using DOCUMENT_TEXT_DETECTION
response = client.document_text_detection(image=image)
Although both methods can be used for OCR, as presented in the documentation, DOCUMENT_TEXT_DETECTION is optimized for dense text and documents. The image you shared is not a really high-quality one, and the text is not clear, therefore it may be that for this type of images, DOCUMENT_TEXT_DETECTION offers a better performance than TEXT_DETECTION.
See some other examples where DOCUMENT_TEXT_DETECTION worked better than TEXT_DETECTION. In any case, please note that it might not always be the situation, and TEXT_DETECTION may still have better results under certain conditions:
Getting Different Data on using Demo and Actual API
Google Vision API text detection strange behaviour
Has someone a working example how to serve images dynamically without Blobstore ? Blobstore is deprecated and I would like to use the Images API in order to crop, resize and serve images which are stored in a google cloud bucket.
At the moment I am using cloud-storage in order to serve images but it was not possible to include the Images API. An example url looks like: https://storage.googleapis.com/orbital-views.appspot.com/blabla2.png?Signature=OHLKMc64WzThGSOU3Rv8MMWz919Pjl%2FYs9Nhw4abqVTX04h4aYniC%2B%2FCtMIeh2stA70KJFdKY1YNuwhBWxDQ4CaiXy0Onn3Fy7tkW1kXw0P5EhDhKQQ27ufgUmOkYxOULo65YkfdW7fVoVeruwdTahk3I94ac4Et3Bvs4nXHW4PQqk31dLVKwYLlTkAvUArg3oiaWsAwAZUKWjfYfw9z8i68QrZubDIB8zski%2FAukPgbT5c%2B8eq8BBPbdkcS1YiL9Wz6Yl1y8VCJxM%2BVMg81IIwCfF9qoJk8Cus20PBXrDYyO7sLvLx5Dy%2BfdMxqmi%2BIZZ2JAhfdjM3oZul5cj%2B9%2Bg%3D%3D&GoogleAccessId=orbital-views%40appspot.gserviceaccount.com&Expires=1504477278
This url is generated using a signed url approach. https://cloud.google.com/storage/docs/access-control/create-signed-urls-program
My aim is to serve an image with the Images API Python.
I tried the following methods but without success.
url = images.get_serving_url(None,filename=’/gs/xxx.appspot.com/’)
blobstore_filename = '/gs{}'.format(filename)
blob_key = blobstore.create_gs_key(blobstore_filename)
url2 = images.get_serving_url(blob_key)