我在使用 Python 和 Google Vision 检测 PDF 文件上的文本时收到 JSON 解码错误



我正在尝试使用Google Vision和Python。 我正在使用示例文件,但不断收到相同的错误消息:

Traceback (most recent call last):
  File "C:Program Files (x86)Python37-32libsite-packagesgoogleprotobufjso
n_format.py", line 416, in Parse
    js = json.loads(text, object_pairs_hook=_DuplicateChecker)
  File "C:Program Files (x86)Python37-32libjson__init__.py", line 361, in l
oads
    return cls(**kw).decode(s)
  File "C:Program Files (x86)Python37-32libjsondecoder.py", line 338, in de
code
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:Program Files (x86)Python37-32libjsondecoder.py", line 356, in ra
w_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "sample.py", line 72, in <module>
    async_detect_document('gs://matr/file_1035.pdf','gs://matr/output/')
  File "sample.py", line 59, in async_detect_document
    json_string, vision.types.AnnotateFileResponse())
  File "C:Program Files (x86)Python37-32libsite-packagesgoogleprotobufjso
n_format.py", line 418, in Parse
    raise ParseError('Failed to load JSON: {0}.'.format(str(e)))
google.protobuf.json_format.ParseError: Failed to load JSON: Expecting value: li
ne 1 column 1 (char 0).

我猜它与生成的 JSON 文件有关。 它确实会生成一个 JSON 文件,但我想它应该将其打印到命令行。 以下是 JSON 文件的前几行:

{
    "inputConfig": {
        "gcsSource": {
            "uri": "gs://python-docs-samples-tests/HodgeConj.pdf"
        },
        "mimeType": "application/pdf"
    },

我生成的文件确实使用 JSON 对象加载到 JSON 对象中

data = json.load(jsonfile)

我已经尝试过print (json_string)但我只得到b'placeholder'

我怎样才能让它工作? 我正在使用Python 3.7.2

我的代码如下:

def async_detect_document(gcs_source_uri, gcs_destination_uri):
    """OCR with PDF/TIFF as source files on GCS"""
    from google.cloud import vision
    from google.cloud import storage
    from google.protobuf import json_format
    import re
    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'application/pdf'
    # How many pages should be grouped into each json output file.
    batch_size = 2
    client = vision.ImageAnnotatorClient()
    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(
        gcs_source=gcs_source, mime_type=mime_type)
    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(
        gcs_destination=gcs_destination, batch_size=batch_size)
    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature], input_config=input_config,
        output_config=output_config)
    operation = client.async_batch_annotate_files(
        requests=[async_request])
    print('Waiting for the operation to finish.')
    operation.result(timeout=180)
    # Once the request has completed and the output has been
    # written to GCS, we can list all the output files.
    storage_client = storage.Client()
    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)
    bucket = storage_client.get_bucket(bucket_name=bucket_name)
    # List objects with the given prefix.
    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files:')
    for blob in blob_list:
        print(blob.name)
    # Process the first output file from GCS.
    # Since we specified batch_size=2, the first response contains
    # the first two pages of the input file.
    output = blob_list[0]
    json_string = output.download_as_string()
    response = json_format.Parse(
        json_string, vision.types.AnnotateFileResponse())
    # The actual response for the first page of the input file.
    first_page_response = response.responses[0]
    annotation = first_page_response.full_text_annotation
    # Here we print the full text from the first page.
    # The response contains more information:
    # annotation/pages/blocks/paragraphs/words/symbols
    # including confidence scores and bounding boxes
    print(u'Full text:n{}'.format(
        annotation.text))
async_detect_document('gs://my_bucket/file_1035.pdf','gs://my_bucket/output/')

我在 github 页面上收到了用户的回答。https://github.com/GoogleCloudPlatform/python-docs-samples/issues/2086#issuecomment-487635159

我遇到了这个问题,并确定它是由前缀作为 blob 列表的一部分迭代引起的。我可以看到"output/"在您的输出中列为文件,随后尝试解析导致错误。

尝试对前缀进行硬编码,例如前缀 = 'output/out',该文件夹将不会包含在列表中。

The demo code should probably be modified to handle this simple case a little better.


import re

def async_detect_document(gcs_source_uri, gcs_destination_uri):
    """OCR with PDF/TIFF as source files on GCS"""
    from google.cloud import vision
    from google.cloud import storage
    from google.protobuf import json_format
    # Supported mime_types are: 'application/pdf' and 'image/tiff'
    mime_type = 'application/pdf'
    # How many pages should be grouped into each json output file.
    batch_size = 2
    client = vision.ImageAnnotatorClient()
    feature = vision.types.Feature(
        type=vision.enums.Feature.Type.DOCUMENT_TEXT_DETECTION)
    gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
    input_config = vision.types.InputConfig(
        gcs_source=gcs_source, mime_type=mime_type)
    gcs_destination = vision.types.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.types.OutputConfig(
        gcs_destination=gcs_destination, batch_size=batch_size)
    async_request = vision.types.AsyncAnnotateFileRequest(
        features=[feature], input_config=input_config,
        output_config=output_config)
    operation = client.async_batch_annotate_files(
        requests=[async_request])
    print('Waiting for the operation to finish.')
    operation.result(timeout=180)
    # Once the request has completed and the output has been
    # written to GCS, we can list all the output files.
    storage_client = storage.Client()
    match = re.match(r'gs://([^/]+)/(.+)', gcs_destination_uri)
    bucket_name = match.group(1)
    prefix = match.group(2)
    bucket = storage_client.get_bucket(bucket_name=bucket_name)
    print ('prefix: ' + prefix)
    prefix = 'output/out'
    print ('prefix new: ' + prefix)

    # List objects with the given prefix.
    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files:')
    for blob in blob_list:
        print(blob.name)
    # Process the first output file from GCS.
    # Since we specified batch_size=2, the first response contains
    # the first two pages of the input file.
    output = blob_list[0]
    json_string = output.download_as_string()
    response = json_format.Parse(
        json_string, vision.types.AnnotateFileResponse())
    # The actual response for the first page of the input file.
    first_page_response = response.responses[0]
    annotation = first_page_response.full_text_annotation
    # Here we print the full text from the first page.
    # The response contains more information:
    # annotation/pages/blocks/paragraphs/words/symbols
    # including confidence scores and bounding boxes
    print(u'Full text:n{}'.format(
        annotation.text))

async_detect_document('gs://my_bucket/my_file.pdf','gs://my_bucket/output/out')

相关内容

  • 没有找到相关文章

最新更新