-
Notifications
You must be signed in to change notification settings - Fork 2
/
textrecog.py
32 lines (21 loc) · 927 Bytes
/
textrecog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from google.cloud import vision
import io
from google.oauth2 import service_account
GOOGLE_APPLICATION_CREDENTIALS = './key/CodeBoard-eb1f5c042ada.json'
credentials = service_account.Credentials.from_service_account_file(GOOGLE_APPLICATION_CREDENTIALS)
def detect_text(path):
"""Detects text in the file."""
client = vision.ImageAnnotatorClient(credentials=credentials)
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)))
imgPath = './images/image1.jpg'
detect_text(imgPath)