谷歌云视觉API-OCR

Man*_*a P 1 c# ocr google-cloud-vision

我想使用谷歌云视觉 API 的图像文本检测(OCR)。但我不知道如何获取订阅密钥以及如何在 C# 中进行身份验证和拨打电话。有人可以告诉我执行此操作的步骤吗?顺便说一句,我很新。

dse*_*sto 7

I think the question is a bit messed up, so let me take a step back and try to cover the most important things regarding authentication when using the Cloud Vision API.

First of all, the documentation offers a really clear explanation on how to authenticate to the Cloud Vision API, using API keys or Service Accounts. Bear in mind that, as documented in the best practices for authentication in the Google Cloud Platform:

For almost all cases, whether you are developing locally or in a production application, you should use service accounts, rather than user accounts or API keys.

Being this clarified, it is obviously up to you whether to use API keys (I understand this is what you refer to when you mention "subscription keys") or Service Accounts. Here are the main differences that may be relevant for you regarding these two authentication methods:

  • Service Accounts: they are more secure, the recommended approach to use, and integrate well and easily with the APIs Client Libraries, which make your life much easier when it comes to interacting with GCP APIs. I strongly recommend you to use Service Accounts and idiomatic Client Libraries.
  • API keys: you should follow a set of best practices for securely using them, and they cannot (or at least I am not aware of that) integrate with the Client Libraries; therefore you will need to make calls to the REST API directly, which is more complex than using idiomatic Client Libraries.

I hope I have been able to highlight the differences between the alternative authentication methods available. So let's move on to the next topic, authenticating requests:

如果您使用 API 密钥,则只需将 API 密钥附加到您要调用的REST API 方法即可,如下所示:

https://vision.googleapis.com/v1/images:annotate?key=YOUR_API_KEY
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您必须找到一种方法在 C# 中发出 HTTP 请求、解析 JSON 响应等。

如果您正在使用服务帐户(我希望我在答案的第一部分说服您这样做),您将需要按照文档中详细说明的以下步骤进行操作:

  1. 下载您的服务帐户的 JSON 密钥。
  2. 参考它在GOOGLE_APPLICATION_CREDENTIALS
  3. 按照这个简单示例中的说明使用 C# 客户端库,或者使用完整的文档参考来获取有关如何使用此库的所有详细信息(客户端库文档ImageAnnotatorClient文档)。