Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CV2-4187-Determine-what-percentage-of-an-image-is-text #445

Merged
Merged
8 changes: 3 additions & 5 deletions app/main/controller/image_ocr_controller.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import json
from flask import request, current_app as app
from urllib3 import Retry
from flask_restplus import Resource, Namespace, fields
from google.cloud import vision
import tenacity
import json
from google.protobuf.json_format import MessageToJson


from app.main.lib.google_client import get_credentialed_google_client
from app.main.lib.google_client import convert_text_annotation_to_json
ahmednasserswe marked this conversation as resolved.
Show resolved Hide resolved

api = Namespace('ocr', description='ocr operations')
ocr_request = api.model('ocr_request', {
Expand Down Expand Up @@ -39,9 +38,8 @@ def post(self):
if not texts:
return

res_dict = MessageToDict(response, preserving_proto_field_name=True)
app.logger.info(
ahmednasserswe marked this conversation as resolved.
Show resolved Hide resolved
f"[Alegre OCR] [image_uri {image.source.image_uri}] Image OCR response package looks like {json.dumps(res_dict)}")
f"[Alegre OCR] [image_uri {image.source.image_uri}] Image OCR response package looks like {convert_text_annotation_to_json(texts[0])}")

return {
'text': texts[0].description
Expand Down
16 changes: 16 additions & 0 deletions app/main/lib/google_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import json
from google.oauth2 import service_account
from flask import current_app as app
ahmednasserswe marked this conversation as resolved.
Show resolved Hide resolved

def get_credentialed_google_client(client):
default_values = {}
Expand All @@ -26,3 +27,18 @@ def get_credentialed_google_client(client):
except ValueError as e:
print(f"Couldn't authenticate to google client: {str(e)}")
return None
def convert_text_annotation_to_json(text_annotation):
try:
text_json = {}
text_json['description'] = text_annotation.description
text_json['locale'] = text_annotation.locale
text_json['bounding_poly'] = []
for a_vertice in text_annotation.bounding_poly.vertices:
vertice_json = {}
vertice_json['x'] = a_vertice.x
vertice_json['y'] = a_vertice.y
text_json['bounding_poly'] += [vertice_json]
text_json = json.dumps(text_json)
return text_json
except:
ahmednasserswe marked this conversation as resolved.
Show resolved Hide resolved
pass
Loading