简体   繁体   中英

using google storage and google vision API

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')

你的函数只是期待 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM