Sử dụng Textract cho OCR cục bộ

Sep 24 2020

Tôi muốn trích xuất văn bản từ hình ảnh bằng Python. (Tessaract lib không hoạt động với tôi vì nó yêu cầu cài đặt).

Tôi đã tìm thấy boto3 lib và Textract, nhưng tôi gặp sự cố khi làm việc với nó. Tôi vẫn còn mới với điều này. Bạn có thể cho tôi biết tôi cần làm gì để chạy tập lệnh của mình một cách chính xác không.

Đây là mã của tôi:

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)

Khi tôi chạy mã này, tôi nhận được:

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.

Tôi cũng đã thử điều này:

# 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')

Nhưng tôi gặp lỗi này:

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.

Tôi không có việc này, vì vậy mọi sự giúp đỡ sẽ tốt. Làm cách nào để đọc dạng văn bản hình ảnh hoặc tệp pdf của tôi?

Tôi cũng đã thêm khối mã này, nhưng lỗi vẫn còn Unable to locate credentials.

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

Trả lời

2 VipinKumar Oct 08 2020 at 10:22

Có vấn đề khi chuyển thông tin đăng nhập tới boto3. Bạn phải vượt qua thông tin đăng nhập trong khi tạo ứng dụng 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')

Xin lưu ý, không nên mã hóa các Khóa AWS trong mã. Vui lòng tham khảo sau tài liệu này

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