【发布时间】: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