【问题标题】:Using Textract for OCR locally在本地使用 Textract 进行 OCR
【发布时间】:2021-06-19 16:28:27
【问题描述】:

我想使用 Python 从图像中提取文本。 (Tessaract 库对我不起作用,因为它需要安装)。

我找到了 boto3 lib 和 Textract,但我无法使用它。我还是新手。你能告诉我我需要做什么才能正确运行我的脚本吗?

这是我的代码:

import cv2
import boto3
import textract


#img = cv2.imread('slika2.jpg') #this is jpg file
with open('slika2.pdf', 'rb') as document:
    img = bytearray(document.read())

textract = boto3.client('textract',region_name='us-west-2')

response = textract.detect_document_text(Document={'Bytes': img}). #gives me error
print(response)

当我运行这段代码时,我得到:

botocore.exceptions.ClientError: An error occurred (InvalidSignatureException) when calling the DetectDocumentText operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

我也试过这个:

# Document
documentName = "slika2.jpg"

# Read document content
with open(documentName, 'rb') as document:
    imageBytes = bytearray(document.read())

# Amazon Textract client
textract = boto3.client('textract',region_name='us-west-2')

# Call Amazon Textract
response = textract.detect_document_text(Document={'Bytes': imageBytes}) #ERROR

#print(response)

# Print detected text
for item in response["Blocks"]:
    if item["BlockType"] == "LINE":
        print ('\033[94m' +  item["Text"] + '\033[0m')

但我收到此错误:

botocore.exceptions.ClientError: An error occurred (InvalidSignatureException) when calling the DetectDocumentText operation: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

我是菜鸟,所以任何帮助都会很好。如何从我的图像或 pdf 文件中读取文本?

我也加了这块代码,但是报错还是Unable to locate credentials

session = boto3.Session(
    aws_access_key_id='xxxxxxxxxxxx',
    aws_secret_access_key='yyyyyyyyyyyyyyyyyyyyy'
)

【问题讨论】:

  • stackoverflow.com/questions/33297172/… 看到这可以帮助你。如我所见,您尚未设置 AWS 配置文件。
  • @aviboy2006 您能告诉我在设置 AWS 配置文件时应该在代码中添加什么吗?
  • 如果你设置了个人资料然后检查我的第一个答案。
  • @aviboy2006 抱歉,但这对我没有帮助。我还在学习aws和texttract。我希望能够从 pdf 或图像中读取文本。我有上面写的代码,所以如果可以的话,告诉我我需要做什么,我应该在我的代码中添加什么,我应该删除什么等等。

标签: python amazon-web-services amazon-textract


【解决方案1】:

将凭据传递给 boto3 时出现问题。您必须在创建 boto3 客户端时传递凭据。

import boto3

# boto3 client
client = boto3.client(
    'textract', 
    region_name='us-west-2', 
    aws_access_key_id='xxxxxxx', 
    aws_secret_access_key='xxxxxxx'
)

# Read image
with open('slika2.png', 'rb') as document:
    img = bytearray(document.read())

# Call Amazon Textract
response = client.detect_document_text(
    Document={'Bytes': img}
)

# Print detected text
for item in response["Blocks"]:
    if item["BlockType"] == "LINE":
        print ('\033[94m' +  item["Text"] + '\033[0m')

请注意,不建议在代码中硬编码 AWS 密钥。请参考以下文档

https://boto3.amazonaws.com/v1/documentation/api/1.9.42/guide/configuration.html

【讨论】:

  • 我没有测试过 pdf,如果有任何问题,请尝试告诉我。 :)
  • 它给出的错误,我不知道我是否可以做到没有s3 bucket
  • 是的,你是对的。对于 PDF,您已经使用 S3 使用异步方法。解决方法可以是将 pdf 转换为图像,然后使用 textract。如果您需要示例,请告诉我。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 2018-12-31
  • 2020-07-13
  • 2016-07-10
相关资源
最近更新 更多