Код: Выделить всё
from google.protobuff.json_format import *
import json
import numpy as np
from google.cloud import vision
import io
import os
def detect_document(path):
client = vision.ImageAnnotatorClient()
if type(path) == bytes:
content = path
else:
with io.open(path, 'rb') as image_file:
content = image_file.read()
image = vision.Image(content=content)
response = client.document_text_detection(image = image)
return response
def save_to_json(json, outfile):
serialized = MessageToJson(json)
with open(outfile, 'w+') as f:
f.write(serialized)
def detect_doc_save(path, save_path = '/home/jupyter/results/', save_data = True):
response = detect_document(path)
if save_data:
try:
save_pdf_to_json(response, os.path.join(save_path, os.path.basename('path')))
except Exception as e:
print("Failed saving the response from: {} Error: {}".format(path,e))
texts = response.text_annotations
text_data = []
for text in texts:
vertices = ([[vertex.x],[vertex.y] for vertex in text.bounding_poly.vertices])
tex_data.append((text.description, vertices))
if response.error.message:
raise Exception(
'{}\n For more info on error messages check: https://cloud.google.com/apis/design/errors'.format(response.error.message)
)
return [text[0] for text in text_data], np.array([text[1] for text in text_data]).ravel()
Я пробовал обновить файлDetect_document функция для включения
Код: Выделить всё
texts = proto.MessageToJson(response)
texts_json = json.dump(texts)
Затем я попытался обновить save_to_json, так как знаю, что именно здесь возникает моя ошибка при вызове MessageToJson() и пытался включить:
response = MessageToDict(json._pb)
но все равно получил ту же ошибку. Любые рекомендации будут оценены
Подробнее здесь: https://stackoverflow.com/questions/790 ... descriptor