运行OCR Azure认知服务时出错-键错误

vh0rcniy  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(129)

我使用下面的代码对收据执行ocr。

f = open("C:\\Users\\xxx\\xxx\\Documents\\ocr\\xxx.json")
data = json.load(f)
f.close()
#print(data)

import os
import azure.ai.vision as sdk

service_options = sdk.VisionServiceOptions(os.environ["VISION_ENDPOINT"],
                                           os.environ["VISION_KEY"])

vision_source = sdk.VisionSource(
    url="https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png")

analysis_options = sdk.ImageAnalysisOptions()

analysis_options.features = (
    sdk.ImageAnalysisFeature.CAPTION |
    sdk.ImageAnalysisFeature.TEXT
)

analysis_options.language = "en"

analysis_options.gender_neutral_caption = True

image_analyzer = sdk.ImageAnalyzer(service_options, vision_source, analysis_options)

result = image_analyzer.analyze()

if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:

    if result.caption is not None:
        print(" Caption:")
        print("   '{}', Confidence {:.4f}".format(result.caption.content, result.caption.confidence))

    if result.text is not None:
        print(" Text:")
        for line in result.text.lines:
            points_string = "{" + ", ".join([str(int(point)) for point in line.bounding_polygon]) + "}"
            print("   Line: '{}', Bounding polygon {}".format(line.content, points_string))
            for word in line.words:
                points_string = "{" + ", ".join([str(int(point)) for point in word.bounding_polygon]) + "}"
                print("     Word: '{}', Bounding polygon {}, Confidence {:.4f}"
                      .format(word.content, points_string, word.confidence))

else:

    error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
    print(" Analysis failed.")
    print("   Error reason: {}".format(error_details.reason))
    print("   Error code: {}".format(error_details.error_code))
    print("   Error message: {}".format(error_details.message))

字符串
然而,在使用命令行运行上述代码时:python sample.py-k“my end_point”-e“my subscription key”返回以下错误。KeyError:'VISION_ENDPOINT'任何指针?

mwngjboj

mwngjboj1#

错误消息KeyError: 'VISION_ENDPOINT'表示找不到环境变量VISION_ENDPOINT
要解决这个问题,您可以首先为endpoint和key分配环境变量值。

import os
os.environ["VISION_ENDPOINT"] = "your_endpoint"
os.environ["VISION_KEY"] = "your_key"

字符串
然而,看起来你是用参数-k-e从命令行运行脚本的,因此你需要确保这些参数被正确解析并在脚本中使用。提供的代码片段没有显示如何处理这些参数。
下面是更新后的代码,其中包含端点和键的命令行参数:

import os
import argparse
import azure.ai.vision as sdk

# Parse command line arguments
parser = argparse.ArgumentParser(description='Perform OCR on receipts.')
parser.add_argument('-k', '--key', help='Your subscription key', required=True)
parser.add_argument('-e', '--endpoint', help='Your endpoint', required=True)
args = parser.parse_args()

service_options = sdk.VisionServiceOptions(args.endpoint, args.key)

vision_source = sdk.VisionSource(
    url="https://learn.microsoft.com/azure/ai-services/computer-vision/media/quickstarts/presentation.png")

analysis_options = sdk.ImageAnalysisOptions()

analysis_options.features = (
    sdk.ImageAnalysisFeature.CAPTION |
    sdk.ImageAnalysisFeature.TEXT
)

analysis_options.language = "en"

analysis_options.gender_neutral_caption = True

image_analyzer = sdk.ImageAnalyzer(service_options, vision_source, analysis_options)

result = image_analyzer.analyze()

if result.reason == sdk.ImageAnalysisResultReason.ANALYZED:

    if result.caption is not None:
        print(" Caption:")
        print("   '{}', Confidence {:.4f}".format(result.caption.content, result.caption.confidence))

    if result.text is not None:
        print(" Text:")
        for line in result.text.lines:
            points_string = "{" + ", ".join([str(int(point)) for point in line.bounding_polygon]) + "}"
            print("   Line: '{}', Bounding polygon {}".format(line.content, points_string))
            for word in line.words:
                points_string = "{" + ", ".join([str(int(point)) for point in word.bounding_polygon]) + "}"
                print("     Word: '{}', Bounding polygon {}, Confidence {:.4f}"
                      .format(word.content, points_string, word.confidence))

else:

    error_details = sdk.ImageAnalysisErrorDetails.from_result(result)
    print(" Analysis failed.")
    print("   Error reason: {}".format(error_details.reason))
    print("   Error code: {}".format(error_details.error_code))
    print("   Error message: {}".format(error_details.message))

相关问题