【问题标题】:Is there a way to process PDF files in Blob Storage without downloading them locally using Python?有没有办法在 Blob 存储中处理 PDF 文件而不使用 Python 在本地下载它们?
【发布时间】:2020-12-09 03:40:10
【问题描述】:

总结问题:

我正在尝试将多个 PDF 处理为用 Python 编写的 OCR 程序。在本地开发期间,PDF 位于可以处理的本地目录中,但我无法在 Blob 存储中找出类似路径的文件系统。从技术上讲,我知道 Blob 中没有这样的文件系统,但我需要在 OCR 程序中传递这样的路径。我们有什么方法可以做到这一点?

我的尝试:

目前我有下面的代码来连接azure.py中的容器和blob:

import os
import glob
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, PublicAccess

# list input PDF files 
def ls_files(client, path, recursive=False):
    if not path == '' and not path.endswith('/'):
        path += '/'

    blob_list = client.list_blobs(name_starts_with=path)
    files = []
    for blob in blob_list:
        relative_path = os.path.relpath(blob.name, path) # blob.name is the name of blobs in containers
        if recursive or not '/' in relative_path:
            files.append(relative_path)
            files = [f for f in files if f.endswith('.pdf')] # look for PDF files 
    return files

# connection string to the storage account
connect_str = '<connection string>'
# same container but different folders for inputs and outputs 
container_name = 'ocr'

blob_service_client = BlobServiceClient.from_connection_string(connect_str)
client = blob_service_client.get_container_client(container_name)

input_files = ls_files(client, '', recursive=True) # This is the input PDF files 

for files in input_files:
    ############################
    # kick off OCR program here#
    ############################ 
    print('Processing ...', files, '\n')

main.py 文件中:

import azure as az 

input_directory = az.input_files # input_directory was like '/Users/xyz/path/to/local/dir'

# do regular OCR processing next 

执行脚本后,Python 无法识别 Blob 存储中的文件或路径。有没有办法可以在这里实现目标?提前致谢。

编辑 1:

我遇到了this sample code,但恐怕这是针对旧版本的 Python SDK 而不是 V12。也一直在看官方repo,但无济于事。

编辑 2:

好的。打开票 here 寻求 MSFT 团队的帮助,一旦我了解更多信息,将在此处更新。解决方法是 1) 将文件下载为内存流或 2) 在 Python 中创建一个临时文件作为占位符。欢迎提出任何建议。

【问题讨论】:

  • 这个 OCR 工具有点专有,或者我们可以看看 API?
  • 恐怕这是专有的:(@MarekPiotrowski
  • 那么,至少,它是一个单独的进程/应用程序还是只是另一个可以完全访问源代码的 python 模块?
  • 模块。基本上main.py 什么都做。 azure.py 此处只是为了获取该路径(如果 blob 存储中存在任何路径)。 @MarekPiotrowski
  • 所以我会尝试检查 OCR 接收二进制文件的位置,即在文件从大容量存储加载/删除到内存之后。然后我会尝试将 Azure blob(它已经在内存中,如果它甚至可以将其加载到内存而不是从 Azure 下载)“转换”为 OCR 接受的类似形式。

标签: python python-3.x azure azure-blob-storage


【解决方案1】:

除了使用 Azure 存储 BLOB,您还可以使用 Azure 存储文件共享,也可以使用用于 OCR 的 Azure 认知服务计算机视觉 API https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/concept-recognizing-text

【讨论】:

  • 啊,我昨天只是在查看有关 azure 存储文件共享的文档,今天想尝试一下。至于用于 OCR 的 API,我们希望保留现有的内容
猜你喜欢
  • 2023-02-25
  • 2021-06-09
  • 2021-01-17
  • 2015-07-15
  • 2013-08-08
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 2020-09-12
相关资源
最近更新 更多