gpt4 book ai didi

c# - 从 Azure 计算机视觉 API 提取文本时出错

转载 作者:行者123 更新时间:2023-12-02 08:33:27 26 4
gpt4 key购买 nike

我正在测试 Azure 计算机视觉 API,以尝试从本地 .jpg 文件中提取手写文本。我正在遵循以下示例:https://learn.microsoft.com/en-us/azure/cognitive-services/computer-vision/quickstarts-sdk/csharp-sdk

不幸的是,当我运行代码时,抛出异常:System.AggregateException:“this.Endpoint”不能为空

Text

我当前的测试代码:

class Program
{
static string subscriptionKey = Environment.GetEnvironmentVariable("{SUBSCRIPTION-KEY}");
static string endpoint = Environment.GetEnvironmentVariable("https://{MY-ENDPOINT}.cognitiveservices.azure.com/");

private const string EXTRACT_TEXT_LOCAL_IMAGE = "vision3.jpg";

public static ComputerVisionClient Authenticate(string endpoint, string key)
{
ComputerVisionClient client =
new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{ Endpoint = endpoint };
return client;
}


static void Main()
{
Console.WriteLine("Azure Cognitive Services Computer Vision - .NET quickstart example");
Console.WriteLine();

ComputerVisionClient client = Authenticate(endpoint, subscriptionKey);

//ExtractTextUrl(client, EXTRACT_TEXT_URL_IMAGE).Wait();
ExtractTextLocal(client, EXTRACT_TEXT_LOCAL_IMAGE).Wait();


}
public static async Task ExtractTextLocal(ComputerVisionClient client, string localImage)
{
Console.WriteLine("----------------------------------------------------------");
Console.WriteLine("EXTRACT TEXT - LOCAL IMAGE");
Console.WriteLine();

// Helps calucalte starting index to retrieve operation ID
const int numberOfCharsInOperationId = 36;

Console.WriteLine($"Extracting text from local image {Path.GetFileName(localImage)}...");
Console.WriteLine();
using (Stream imageStream = File.OpenRead(localImage))
{
// Read the text from the local image
BatchReadFileInStreamHeaders localFileTextHeaders = await client.BatchReadFileInStreamAsync(imageStream);
// Get the operation location (operation ID)
string operationLocation = localFileTextHeaders.OperationLocation;

// Retrieve the URI where the recognized text will be stored from the Operation-Location header.
string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);

// Extract text, wait for it to complete.
int i = 0;
int maxRetries = 10;
ReadOperationResult results;
do
{
results = await client.GetReadOperationResultAsync(operationId);
Console.WriteLine("Server status: {0}, waiting {1} seconds...", results.Status, i);
await Task.Delay(1000);
if (maxRetries == 9)
{
Console.WriteLine("Server timed out.");
}
}
while ((results.Status == TextOperationStatusCodes.Running ||
results.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries);

// Display the found text.
Console.WriteLine();
var textRecognitionLocalFileResults = results.RecognitionResults;
foreach (TextRecognitionResult recResult in textRecognitionLocalFileResults)
{
foreach (Line line in recResult.Lines)
{
Console.WriteLine(line.Text);
}
}
Console.WriteLine();
}
}
}

编辑:

从尝试调试来看,尽管我立即初始化了它们,但 subscriptionKey 和端点变量似乎从一开始就为空。这是为什么?

Text

编辑2:

当我在 Main() 中对 subscriptionKey 和端点进行硬编码时:

ComputerVisionClient client = Authenticate("https://{END-POINT}.cognitiveservices.azure.com/", "{SUBSCRIPTION-KEY}");

效果很好。谁能告诉我为什么我的 2 个静态字符串变量不起作用?因为我不想对这些变量进行硬编码

最佳答案

不确定您希望这两行代码完成什么 -

static string subscriptionKey = Environment.GetEnvironmentVariable("{SUBSCRIPTION-KEY}");
static string endpoint = Environment.GetEnvironmentVariable("https://{MY-ENDPOINT}.cognitiveservices.azure.com/");

您要么选择环境变量,要么分配字符串文字,您试图...将两者结合起来?

也许可以从 appsettings.json 读取这些 secret 。

关于c# - 从 Azure 计算机视觉 API 提取文本时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58754402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com