-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb12a1b
commit 5b68b14
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
from google.cloud import storage | ||
from google.cloud import vision | ||
import base64 | ||
import json | ||
import os | ||
import re | ||
|
||
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.Image() | ||
image.source.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))) | ||
|
||
if response.error.message: | ||
raise Exception( | ||
'{}\nFor more info on error messages, check: ' | ||
'https://cloud.google.com/apis/design/errors'.format( | ||
response.error.message)) | ||
|
||
|
||
def detect_text(path): | ||
"""Detects text in the file.""" | ||
texto=[] | ||
from google.cloud import vision | ||
import io | ||
client = vision.ImageAnnotatorClient() | ||
|
||
with io.open(path, 'rb') as image_file: | ||
content = image_file.read() | ||
|
||
image = vision.Image(content=content) | ||
|
||
response = client.text_detection(image=image) | ||
texts = response.text_annotations | ||
print('Texts:') | ||
|
||
for text in texts: | ||
print('\n"{}"'.format(text.description)) | ||
texto.append(text.description) | ||
vertices = (['({},{})'.format(vertex.x, vertex.y) | ||
for vertex in text.bounding_poly.vertices]) | ||
|
||
print('bounds: {}'.format(','.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)) | ||
return re.sub("\n","",' '.join(texto)) |