【发布时间】:2021-05-05 10:36:02
【问题描述】:
我已从官方 microsoft 认知 github 存储库下载此代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace Microsoft.Azure.CognitiveServices.Samples.ComputerVision.BatchReadFile
{
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
class Program
{
static void Main(string[] args)
{
// Add your Computer Vision subscription key and endpoint to your environment variables
string subscriptionKey = "my key0001"; // Environment.GetEnvironmentVariable("my key0001");
string endpoint = "https://controllo.cognitiveservices.azure.com/"; // Environment.GetEnvironmentVariable("https://controllo.cognitiveservices.azure.com/");
try
{
BatchReadFileSample.RunAsync(endpoint, subscriptionKey).Wait(5000);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.WriteLine("\nPress ENTER to exit.");
Console.ReadLine();
}
}
public class BatchReadFileSample
{
public static async Task RunAsync(string endpoint, string key)
{
ComputerVisionClient computerVision = new ComputerVisionClient(new ApiKeyServiceClientCredentials(key))
{
Endpoint = endpoint
};
const int numberOfCharsInOperationId = 36;
// string localImagePath = @"Images\handwritten_text.jpg"; // See this repo's readme.md for info on how to get these images. Alternatively, you can just set the path to any appropriate image on your machine.
string localImagePath = @"C:\Users\marco.panza\OneDrive - Accenture\Desktop\Sorgenti\OCR C#\info.png";
// string remoteImageUrl = "https://github.com/Azure-Samples/cognitive-services-sample-data-files/raw/master/ComputerVision/Images/printed_text.jpg";
Console.WriteLine("Text being batch read ...");
await BatchReadFileFromStreamAsync(computerVision, localImagePath, numberOfCharsInOperationId);
// await BatchReadFileFromUrlAsync(computerVision, remoteImageUrl, numberOfCharsInOperationId);
}
// Read text from a remote image
private static async Task BatchReadFileFromUrlAsync(ComputerVisionClient computerVision, string imageUrl, int numberOfCharsInOperationId)
{
if (!Uri.IsWellFormedUriString(imageUrl, UriKind.Absolute))
{
Console.WriteLine("\nInvalid remote image url:\n{0} \n", imageUrl);
return;
}
// Start the async process to read the text
BatchReadFileHeaders textHeaders = await computerVision.BatchReadFileAsync(imageUrl);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
// Recognize text from a local image
private static async Task BatchReadFileFromStreamAsync(ComputerVisionClient computerVision, string imagePath, int numberOfCharsInOperationId)
{
if (!File.Exists(imagePath))
{
Console.WriteLine("\nUnable to open or read local image path:\n{0} \n", imagePath);
return;
}
using (Stream imageStream = File.OpenRead(imagePath))
{
// Start the async process to recognize the text
BatchReadFileInStreamHeaders textHeaders = await computerVision.BatchReadFileInStreamAsync(imageStream);
await GetTextAsync(computerVision, textHeaders.OperationLocation, numberOfCharsInOperationId);
}
}
// Retrieve the recognized text
private static async Task GetTextAsync(ComputerVisionClient computerVision, string operationLocation, int numberOfCharsInOperationId)
{
// Retrieve the URI where the recognized text will be
// stored from the Operation-Location header
string operationId = operationLocation.Substring(operationLocation.Length - numberOfCharsInOperationId);
ReadOperationResult result = await computerVision.GetReadOperationResultAsync(operationId);
// Wait for the operation to complete
int i = 0;
int maxRetries = 10;
while ((result.Status == TextOperationStatusCodes.Running ||
result.Status == TextOperationStatusCodes.NotStarted) && i++ < maxRetries)
{
Console.WriteLine("Server status: {0}, waiting {1} seconds...", result.Status, i);
await Task.Delay(1000);
result = await computerVision.GetReadOperationResultAsync(operationId);
}
// Display the results
Console.WriteLine();
var recResults = result.RecognitionResults;
foreach (TextRecognitionResult recResult in recResults)
{
foreach (Line line in recResult.Lines)
{
Console.WriteLine(line.Text);
}
}
Console.WriteLine();
}
}
}
但我收到此错误:
发生了一个或多个错误。 (操作返回无效状态码“未授权”)
密钥和终点是正确的(出于安全原因,我发布了一个伪密钥)。
我第一次尝试使用此代码:
string subscriptionKey = Environment.GetEnvironmentVariable("my key0001");
string endpoint = Environment.GetEnvironmentVariable("https://controllo.cognitiveservices.azure.com/");
但是这些字符串返回 null 并且在我尝试直接分配值之后:
string subscriptionKey = "my key0001");
string endpoint = "https://controllo.cognitiveservices.azure.com/);
我得到这个错误:
"One or more errors occurred. (Operation returned an invalid status code 'Unauthorized')"
有人可以帮帮我吗?
【问题讨论】:
-
您是否从微软网站获得了密钥?现有密钥可能无效。你下载的代码是多少年的?代码使用的是 HTTP 还是 HTTPS(安全)的 URL?大多数服务器现在都使用 HTTPS,如果使用 HTTP,将会失败。
-
我从微软网站获得了真正的密钥。和这里的来源github.com/Azure-Samples/cognitive-services-dotnet-sdk-samples/…
-
我需要在本地驱动器上对 PDF 进行 OCR 处理
-
查看示例和代码使用 HTTPS 和 Net 4.6.2。 HTTPS 使用 TLS 进行身份验证。去年 6 月,微软进行了一次安全推送,在服务器上禁用了 TLS 1.0 和 1.1,现在需要 TLS 1.2 或 1.3。支持所有加密模式的旧版本 Net 也存在问题,尤其是在使用 TLS 1.3 的情况下。旧版本的网络可能不支持加密模式。首先,我会确保您使用最新版本的 AZURE。那么最好使用 Net 4.7.2 或更高版本,它提供了使用 Net for TLS 或 Windows Operating System for TLS 的选项,并在配置文件中选择操作系统。
-
我使用了vs2019,它自动将“ComputerVision”更新为5.0,这是最后一次更新。你知道 extst 是否有更好的“Microsoft.Azure.CognitiveServices.Vision.ComputerVision”api 源代码?