Skip to content

Commit

Permalink
Create request.py
Browse files Browse the repository at this point in the history
  • Loading branch information
RubensZimbres authored Mar 15, 2021
1 parent eb12a1b commit 5b68b14
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Google-Cloud-OCR/request.py
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))

0 comments on commit 5b68b14

Please sign in to comment.