【问题标题】:AWS start document analysis using textract not workingAWS 使用 texttract 开始文档分析不起作用
【发布时间】:2021-06-19 16:29:22
【问题描述】:

我正在为我的学校做一个项目,我应该使用 textract 对表单进行文档分析,并将该输出运行到 A2I,其中算法将确定表单是否被批准、拒绝或需要审查。将文档上传到 S3 后,应触发此 textract lambda 函数。但是,当我遵循本文档时,我会遇到语法错误; https://docs.aws.amazon.com/textract/latest/dg/API_StartDocumentAnalysis.html

我的代码是:

import urllib.parse
import boto3

print('Loading function')

##Clients
s3 = boto3.client('s3')
textract = boto3.client('textract')

def analyzedata(bucketName,documentKey):
    print("Loading")
    AnalyzedData= textract.StartDocumentAnalysis("DocumentLocation": { 
      "S3Object": { 
         "Bucket": "bucketName",
         "Name": "documentKey",
      })
    detectedText = ''

    # Print detected text
    for item in AnalyzedData['Blocks']:
        if item['BlockType'] == 'LINE':
            detectedText += item['Text'] + '\n'
            
    return detectedText
      
def writeTextractToS3File(textractData, bucketName, createdS3Document):
    print('Loading writeTextractToS3File')
    generateFilePath = os.path.splitext(createdS3Document)[0] + '.csv'
    s3.put_object(Body=textractData, Bucket=bucketName, Key=generateFilePath)
    print('Generated ' + generateFilePath)





def lambda_handler(event, context):
    #print("Received event: " + json.dumps(event, indent=2))

    # Get the object from the event and show its content type
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.parse.unquote_plus(event['Records'][0]['s3']['object']['key'], encoding='utf-8')
    try:
        detectedText = analyzedata(bucket, key)
        writeTextractToS3File(detectedText, bucket, key)
        
        return 'Processing Done!'
        
        
        
    except Exception as e:
        print(e)
        print('Error getting object {} from bucket {}. Make sure they exist and your bucket is in the same region as this function.'.format(key, bucket))
        raise e

代码尚未完成,但我已经收到语法错误:

  "errorMessage": "Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 13)",
  "errorType": "Runtime.UserCodeSyntaxError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\" Line 13\n        AnalyzedData= textract.Start_Document_Analysis(\"DocumentLocation\": { \n"
  ]
}

【问题讨论】:

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


    【解决方案1】:

    根据boto3 docs,你的语法应该更像:

    AnalyzedData= textract.start_document_analysis(DocumentLocation={ 
      "S3Object": { 
         "Bucket": "bucketName",
         "Name": "documentKey",
      })
    

    另请注意,FeatureTypes 参数是按要求列出的。

    【讨论】:

    • 天哪,这解决了它。我可以问一下,如果语法是 start_document_analysis,为什么 aws 文档没有说明这一点?他们只是说 StartDocumentAnalysis.. 对像我这样的初学者真的有误导
    • 我不知道。我猜 API 文档是针对 Web API 的,而 boto3 文档是专门针对 Python 客户端的?
    • 嗯,有道理。如果你有时间,你能帮我看看stackoverflow.com/questions/62900687/…吗?它是这个问题的延续,我这次使用 get_document_analysis 来获得结果。再次感谢您;)
    【解决方案2】:

    你应该尝试 pip install awscli

    pip install awscli

    或者 pip3 如果效果更好

    然后导入并尝试运行代码。

    【讨论】:

      【解决方案3】:

      我认为您为此缺少一个起始大括号字符。

      AnalyzedData= textract.StartDocumentAnalysis("DocumentLocation": { # missing { in this line
        "S3Object": { 
           "Bucket": "bucketName",
           "Name": "documentKey",
        })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-20
        • 1970-01-01
        • 2014-08-04
        • 1970-01-01
        • 1970-01-01
        • 2014-11-09
        相关资源
        最近更新 更多