0

The following piece of code comes from Google's Vision API Documentation, the only modification I've made is adding the argument parser for the function at the bottom.

import argparse
import os
from google.cloud import vision
import io

def detect_text(path):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
    content = image_file.read()

image = vision.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))

    vertices = (['({},{})'.format(vertex.x, vertex.y)
                for vertex in text.bounding_poly.vertices])

    print('bounds: {}'.format(','.join(vertices)))

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str,
    help="path to input image")
args = vars(ap.parse_args())
detect_text(args)

If I run it from a terminal like below, I get this invalid file error:

PS C:\VisionTest> python visionTest.py --image C:\VisionTest\test.png
Traceback (most recent call last):
  File "visionTest.py", line 31, in <module>
    detect_text(args)
  File "visionTest.py", line 10, in detect_text
    with io.open(path, 'rb') as image_file:
TypeError: invalid file: {'image': 'C:\\VisionTest\\test.png'}

I've tried with various images and image types as well as running the code from different locations with no success.

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
AlToy
  • 1

1 Answers1

0

Seems like either the file doesn't exist or is corrupt since it isn't even read. Can you try another image and validate it is in the location you expect?

Brendan
  • 1,017
  • 5
  • 7