我正在使用谷歌OCR API,我正在阅读图像和PDF文件,我能够读取和处理图像文件,但是,对于PDF文件,根据Google OCR API documentation,他们已经提到,我们需要将我们的文档存储到谷歌云服务。
话虽如此,由于数据的保密性,我不能将我的数据存储到Google Cloud中,我想从我的本地系统上传我的PDF,以便从PDF文件中读取文本。是否可以从本地磁盘上传PDF,然后进行处理,而不是将文件上传到Google Cloud?
发布于 2018-08-24 21:10:21
发布于 2020-03-20 04:43:54
本地存储的文件的代码不在文档特定部分下,而在此处:https://cloud.google.com/vision/docs/file-small-batch
我在下面总结了GCP和本地选项的代码。
# imports
from google.cloud import vision
from google.cloud.vision_v1 import enums
import io
# Set up Vision API
from google.cloud import vision
client = vision.ImageAnnotatorClient()
features = [{"type": enums.Feature.Type.DOCUMENT_TEXT_DETECTION}]
mime_type = 'application/pdf'
# from GCP
gcs_source_uri = "gs://bk-bucketname/example.pdf"
gcs_source = vision.types.GcsSource(uri=gcs_source_uri)
input_gcp = vision.types.InputConfig(gcs_source=gcs_source, mime_type=mime_type)
# from local
file_path = "./example.pdf"
with io.open(file_path, "rb") as f:
content = f.read()
input_local = {"mime_type": mime_type, "content": content}
# send the api request
pages = [1] # list of page#s, 5max for online / 2000max for offline/async
requests = [{"input_config": input_local, "features": features, "pages": pages}]
response = client.batch_annotate_files(requests)发布于 2021-01-20 01:07:15
您可以将PDF拆分为多个页面,将它们分别发送到在线OCR API,然后按顺序合并结果。或者,您也可以依赖于可以为您完成此操作的OCR服务,如https://base64.ai/demo/document-processing/ocr
https://stackoverflow.com/questions/51996399
复制相似问题