【问题标题】:Does google cloud vision api( source path- gcsSource) supports image detection (image contains text) in PDF file?google cloud vision api(source path-gcsSource)是否支持PDF文件中的图像检测(图像包含文本)?
【发布时间】:2021-11-26 20:04:28
【问题描述】:

我正在使用带有 TEXT_DETECTION 和 DOCUMENT_TEXT_DETECTION 的 OCR 来处理 pdf 文件(InputConfig mimeType-“application/pdf”)。当前图像在处理时被跳过。有什么方法可以处理 PDF 文件中的图像(有文本)?

【问题讨论】:

    标签: google-cloud-platform google-api google-cloud-vision google-apis-explorer


    【解决方案1】:

    要回答您的问题,是的,有一种方法可以处理带有 PDF 文件中的文本的图像。根据谷歌官方文档,通常是使用OCR DOCUMENT_TEXT_DETECTION [1]。

    Vision API 可以检测和转录存储在 Cloud Storage 中的 PDF 和 TIFF 文件中的文本。必须使用 files:asyncBatchAnnotate 函数请求来自 PDF 和 TIFF 的文档文本检测,该函数执行离线(异步)请求并使用操作资源提供其状态。 PDF/TIFF 请求的输出将写入在指定 Cloud Storage 存储分区中创建的 JSON 文件。[2]

    [1]https://cloud.google.com/vision/docs/ocr#optical_character_recognition_ocr [2]https://cloud.google.com/vision/docs/pdf#vision_text_detection_pdf_gcs-gcloud

    编辑

    我不知道您使用的是什么语言,但我尝试了这个 python 代码,它处理带有图像的 pdf 而不会跳过它们。

    你需要安装google-cloud-storagegoogle-cloud-vision

    gcs_source_uri 上,您必须指定您的存储桶名称和您正在使用的 pdf 文件。

    gcs_destination_uri 上,您只需指定您的存储桶名称,让pdf_result 保持原样。

    import os
    import re
    import json
    from google.cloud import vision
    from google.cloud import storage
     
    """
    #pip install --upgrade google-cloud-storage
    #pip install --upgrade google-cloud-vision
    """
    credential_path = 'your_path'
    os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = credential_path
     
    client = vision.ImageAnnotatorClient()
     
    batch_size = 2
    mime_type = 'application/pdf'
    feature = vision.Feature(
           type_=vision.Feature.Type.DOCUMENT_TEXT_DETECTION)
    gcs_source_uri= 'gs://your_bucketname/your_pdf_File.pdf'
    gcs_source = vision.GcsSource(uri=gcs_source_uri)
    input_config = vision.InputConfig(gcs_source=gcs_source, mime_type=mime_type)
     
    gcs_destination_uri = 'gs://your_bucketname/pdf_result'
    gcs_destination = vision.GcsDestination(uri=gcs_destination_uri)
    output_config = vision.OutputConfig(gcs_destination=gcs_destination, batch_size= batch_size)
    async_request = vision.AsyncAnnotateFileRequest(
       features=[feature], input_config=input_config, output_config=output_config
    )
    operation = client.async_batch_annotate_files(requests=[async_request])
     
    operation.result(timeout=180)
     
    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)
     
    #List object with the given prefix
    blob_list = list(bucket.list_blobs(prefix=prefix))
    print('Output files: ')
    for blob in blob_list:
       print(blob.name)
     
    output = blob_list[0]
    json_string = output.download_as_string()
    response = json.loads(json_string)
     
    first_page_response = response['responses'][0]
    annotation = first_page_response['fullTextAnnotation']
     
     
    print('Full text:\n')
    print(annotation['text'])
     
    

    【讨论】:

    • 我可以处理 pdf 文件中的文本,但 pdf 文件也有一些图像。通过这种方式,图像被跳过,只有文本得到处理。
    • @user3779846 我更新了我的答案 我尝试了一个 python 代码,它在 4 种情况下对我有用,如果这对你有帮助,请告诉我。
    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 2020-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 2020-05-08
    相关资源
    最近更新 更多