Azure 政府 – コンピューター ビジョン面、トランスレーター テキスト Api で認識サービス
Azure 政府の認知サービスの概要を表示するここをクリックしてです。
前提条件
- インストールし、構成Azure PowerShell
- 接続Azure 政府機関での PowerShell
パート 1: 認知サービス アカウントのプロビジョニング
認知サービス Api にアクセスするためには、まず各にアクセスする Api の認知サービス アカウントをプロビジョニングする必要があります。 Azure ポータルで、政府機関認知サービスがまだサポートされていないが、Api やサービスにアクセスする Azure PowerShell を使用することができます。
注意
アカウントを作成して、(下記参照)、キーの取得のプロセスを通過する必要があります各Api にアクセスするのです。
あることを確認してください、認知サービス リソース プロバイダーは、自分のアカウントに登録されているです。 こうこと次の Powershell コマンドを実行します。
Get-AzureRmResourceProvider
操作を行う場合が表示されない
Microsoft.CognitiveServices
、によってリソース プロバイダーを登録する必要がある**、次のコマンドを実行している**:Register-AzureRmResourceProvider -ProviderNamespace Microsoft.CognitiveServices
次の Powershell コマンドでは、関連するアカウント情報で「rg 名」、"名前-の-your-api"、および"resourcegroup 場所"を置き換えます。
アクセスする次の 3 つの Api のいずれかと、API の「型」タグを置き換えます。 * ComputerVision * Face * TextTranslation
New-AzureRmCognitiveServicesAccount -ResourceGroupName 'rg-name' -name 'name-of-your-api' -Type <type of API> -SkuName S0 -Location 'location-of-resourcegroup'
例:
New-AzureRmCognitiveServicesAccount -ResourceGroupName 'resourcegrouptest' -name 'myFaceAPI' -Type Face -SkuName S0 -Location 'usgovvirginia'
コマンドを実行した後の次のように表示されます。
コピーし、「エンドポイント」保存属性の場所に API の呼び出しを行うときに必要になります。
アカウント キーを取得します。
特定の API にアクセスするアカウント キーを取得する必要があります。
次の Powershell コマンドで、"youraccountname"タグを作成したアカウントを指定した名前に置き換えます。 'Rg 名' タグをリソース グループの名前に置き換えます。
Get-AzureRmCognitiveServicesAccountKey -Name <youraccountname> -ResourceGroupName 'rg-name'
例:
Get-AzureRmCognitiveServicesAccountKey -Name myFaceAPI -ResourceGroupName 'resourcegrouptest'
コピーし、最初の保存キーどこかにも必要になる API に対して呼び出しを行います。
Api に対して呼び出しを行う準備が整いました。
パート 2: API のクイック スタート
次のクイック スタートは Azure 政府機関認知サービスを通じて使用可能な Api を使用した作業を開始するのに役立ちます。
Computer Vision API
前提条件
コンピューター ビジョンの API を Microsoft Windows SDK の取得ここです。
Visual Studio がインストールされていることを確認します。
- Visual Studio 2017 バージョン 15.3 (Azure 開発ワークロードを含む)。
注意
インストールまたは Visual Studio 2017 15.3 のバージョンにアップグレードした後、Azure 関数の Visual Studio 2017 ツールを手動で更新する必要もあります。 ツールを更新するには、[ツール] メニューで [拡張機能と更新プログラム] > [更新プログラム] > [Visual Studio Marketplace] > [Azure Functions and Web Jobs Tools](Azure Functions と Web ジョブのツール) > [更新] の順に選択します。
バリエーション
- Azure 政府機関 Face API にアクセスするための URI です。
https://(resource-group-location).api.cognitive.microsoft.us/face/v1.0
- この URI と商用 Azure で使用される URI の主な違いは、の終了**.us**と uri の先頭の場所
イメージをコンピューター ビジョン API を使用して c# の分析します。
画像の分析方法イメージの内容に基づいたビジュアルの機能を抽出することができます。 イメージをアップロードまたはイメージ URL を指定を選択する返すには、必要な機能を含みます。
- イメージのコンテンツに関連するタグの詳細な一覧。
- 完全な文章のイメージのコンテンツの説明です。
- 座標、性別、およびイメージに含まれているすべての面の経過時間。
- イメージの種類 (クリップアートまたは線の描画)。
- 主要な色、アクセント カラー イメージは黒かどうか、または白黒です。
- これで定義されたカテゴリ分類です。
- イメージは、性的に露骨挑発的なコンテンツを含みますか。
イメージ (C#) 要求の例を分析します。
- Visual Studio で新しいコンソール ソリューションを作成します。
- Program.cs を次のコードに置き換えます。
- 変更、
uriBase
第 1 部から保存して保持する「エンドポイント」属性に、"/analyze"エンドポイントの後にします。 - 置換、
subscriptionKey
有効なサブスクリプション キーを持つ値です。 - プログラムを実行します。
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace VisionApp1
{
static class Program
{
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
const string subscriptionKey = "<subscription key>";
//Copy and paste the "Endpoint" attribute that you saved before into the uriBase string "/analyze" at the end.
//Example: https://virginia.api.cognitive.microsoft.us/vision/v1.0/analyze
const string uriBase = "<endpoint>/analyze";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Analyze an image:");
Console.Write("Enter the path to an image you wish to analyze: ");
string imageFilePath = Console.ReadLine();
// Execute the REST API call.
MakeAnalysisRequest(imageFilePath);
Console.WriteLine("\nPlease wait a moment for the results to appear. Then, press Enter to exit...\n");
Console.ReadLine();
}
/// <summary>
/// Gets the analysis of the specified image file by using the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file.</param>
static async void MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "visualFeatures=Categories,Description,Color&language=en";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettyPrint(contentString));
}
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
/// <summary>
/// Formats the given JSON string by adding line breaks and indents.
/// </summary>
/// <param name="json">The raw JSON string to format.</param>
/// <returns>The formatted JSON string.</returns>
static string JsonPrettyPrint(string json)
{
if (string.IsNullOrEmpty(json))
return string.Empty;
json = json.Replace(Environment.NewLine, "").Replace("\t", "");
StringBuilder sb = new StringBuilder();
bool quote = false;
bool ignore = false;
int offset = 0;
int indentLength = 3;
foreach (char ch in json)
{
switch (ch)
{
case '"':
if (!ignore) quote = !quote;
break;
case '\'':
if (quote) ignore = !ignore;
break;
}
if (quote)
sb.Append(ch);
else
{
switch (ch)
{
case '{':
case '[':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', ++offset * indentLength));
break;
case '}':
case ']':
sb.Append(Environment.NewLine);
sb.Append(new string(' ', --offset * indentLength));
sb.Append(ch);
break;
case ',':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', offset * indentLength));
break;
case ':':
sb.Append(ch);
sb.Append(' ');
break;
default:
if (ch != ' ') sb.Append(ch);
break;
}
}
}
return sb.ToString().Trim();
}
}
}
イメージ、応答を分析します。
JSON では、正常な応答が返されます。 正常な応答の例を次に示します。
{
"categories": [
{
"name": "people_baby",
"score": 0.52734375
},
{
"name": "people_young",
"score": 0.4375
}
],
"description": {
"tags": [
"person",
"indoor",
"clothing",
"woman",
"white",
"table",
"food",
"girl",
"smiling",
"posing",
"holding",
"black",
"sitting",
"young",
"plate",
"hair",
"wearing",
"cake",
"large",
"shirt",
"dress",
"eating",
"standing",
"blue"
],
"captions": [
{
"text": "a woman posing for a picture",
"confidence": 0.460196158842535
}
]
},
"requestId": "7c20cc50-f5eb-453b-abb5-98378917431c",
"metadata": {
"width": 721,
"height": 960,
"format": "Jpeg"
},
"color": {
"dominantColorForeground": "Black",
"dominantColorBackground": "White",
"dominantColors": [
"White"
],
"accentColor": "7C4F57",
"isBWImg": false
}
}
詳細についてを参照してくださいパブリック ドキュメントとパブリック API ドキュメントコンピューター ビジョン api です。
Face API
前提条件
Microsoft Face API の Windows SDK の取得ここ
Visual Studio がインストールされていることを確認します。
- Visual Studio 2017 バージョン 15.3 (Azure 開発ワークロードを含む)。
注意
インストールまたは Visual Studio 2017 15.3 のバージョンにアップグレードした後、Azure 関数の Visual Studio 2017 ツールを手動で更新する必要もあります。 ツールを更新するには、[ツール] メニューで [拡張機能と更新プログラム] > [更新プログラム] > [Visual Studio Marketplace] > [Azure Functions and Web Jobs Tools](Azure Functions と Web ジョブのツール) > [更新] の順に選択します。
バリエーション
- Azure 政府機関 Face API にアクセスするための URI です。
https://(resource-group-location).api.cognitive.microsoft.us/face/v1.0
- この URI と商用 Azure で使用される URI の主な違いは、の終了**.us**と uri の先頭の場所
Face API を使用して c# でのイメージに顔を検出します。
使用して、に直面しています-メソッドを検出画像の顔を検出し、フェイスを返す属性を含みます。
- フェイス ID: 一意の ID Face API のいくつかのシナリオで使用します。
- : 面の四角形、左、上、幅、および高さ直面しているイメージ内の場所を示すです。
- 目印: 27 ポイント フェイス目印フェイス コンポーネントの重要な位置を指すの配列。
- 年齢、性別を含む顔の属性は、強度、ヘッド ポーズ、および顔髪を笑いました。
顔の検出の要求の例 (C#)
サンプルは、Face API のクライアント ライブラリを使用して c# で記述します。
- Visual Studio で新しいコンソール ソリューションを作成します。
- Program.cs を次のコードに置き換えます。
- 置換、
subscriptionKey
上で取得したキーの値を持つ値です。 - 変更、
uriBase
上で取得した「エンドポイント」属性の値。 - プログラムを実行します。
- ハード ドライブにイメージへのパスを入力します。
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace FaceApp1
{
static class Program
{
// **********************************************
// *** Update or verify the following values. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
const string subscriptionKey = "<subscription key>";
//Copy and paste the "Endpoint" attribute that you saved before into the uriBase string "/detect" at the end.
//Example: https://virginia.api.cognitive.microsoft.us/face/v1.0/detect
const string uriBase ="<endpoint>/detect";
static void Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Detect faces:");
Console.Write("Enter the path to an image with faces that you wish to analzye: ");
string imageFilePath = Console.ReadLine();
// Execute the REST API call.
MakeAnalysisRequest(imageFilePath);
Console.WriteLine("\nPlease wait a moment for the results to appear. Then, press Enter to exit...\n");
Console.ReadLine();
}
/// <summary>
/// Gets the analysis of the specified image file by using the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file.</param>
static async void MakeAnalysisRequest(string imageFilePath)
{
HttpClient client = new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
// Request parameters. A third optional parameter is "details".
string requestParameters = "returnfaceId=true&returnfaceLandmarks=false&returnfaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion";
// Assemble the URI for the REST API Call.
string uri = uriBase + "?" + requestParameters;
HttpResponseMessage response;
// Request body. Posts a locally stored JPEG image.
byte[] byteData = GetImageAsByteArray(imageFilePath);
using (ByteArrayContent content = new ByteArrayContent(byteData))
{
// This example uses content type "application/octet-stream".
// The other content types you can use are "application/json" and "multipart/form-data".
content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
// Execute the REST API call.
response = await client.PostAsync(uri, content);
// Get the JSON response.
string contentString = await response.Content.ReadAsStringAsync();
// Display the JSON response.
Console.WriteLine("\nResponse:\n");
Console.WriteLine(JsonPrettyPrint(contentString));
}
}
/// <summary>
/// Returns the contents of the specified file as a byte array.
/// </summary>
/// <param name="imageFilePath">The image file to read.</param>
/// <returns>The byte array of the image data.</returns>
static byte[] GetImageAsByteArray(string imageFilePath)
{
FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read);
BinaryReader binaryReader = new BinaryReader(fileStream);
return binaryReader.ReadBytes((int)fileStream.Length);
}
/// <summary>
/// Formats the given JSON string by adding line breaks and indents.
/// </summary>
/// <param name="json">The raw JSON string to format.</param>
/// <returns>The formatted JSON string.</returns>
static string JsonPrettyPrint(string json)
{
if (string.IsNullOrEmpty(json))
return string.Empty;
json = json.Replace(Environment.NewLine, "").Replace("\t", "");
StringBuilder sb = new StringBuilder();
bool quote = false;
bool ignore = false;
int offset = 0;
int indentLength = 3;
foreach (char ch in json)
{
switch (ch)
{
case '"':
if (!ignore) quote = !quote;
break;
case '\'':
if (quote) ignore = !ignore;
break;
}
if (quote)
sb.Append(ch);
else
{
switch (ch)
{
case '{':
case '[':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', ++offset * indentLength));
break;
case '}':
case ']':
sb.Append(Environment.NewLine);
sb.Append(new string(' ', --offset * indentLength));
sb.Append(ch);
break;
case ',':
sb.Append(ch);
sb.Append(Environment.NewLine);
sb.Append(new string(' ', offset * indentLength));
break;
case ':':
sb.Append(ch);
sb.Append(' ');
break;
default:
if (ch != ' ') sb.Append(ch);
break;
}
}
}
return sb.ToString().Trim();
}
}
}
顔の検出の応答
JSON では、正常な応答が返されます。 正常な応答の例を次に示します。
Response:
[
{
"faceId": "0ed7f4db-1207-40d4-be2e-84694e42d682",
"faceRectangle": {
"top": 60,
"left": 83,
"width": 361,
"height": 361
},
"faceAttributes": {
"smile": 0.284,
"headPose": {
"pitch": 0.0,
"roll": -12.2,
"yaw": -16.7
},
"gender": "female",
"age": 16.5,
"facialHair": {
"moustache": 0.0,
"beard": 0.0,
"sideburns": 0.0
},
"glasses": "NoGlasses",
"emotion": {
"anger": 0.003,
"contempt": 0.001,
"disgust": 0.001,
"fear": 0.002,
"happiness": 0.284,
"neutral": 0.694,
"sadness": 0.012,
"surprise": 0.004
}
}
}
]
詳細についてを参照してくださいパブリック ドキュメント、およびパブリック API ドキュメントFace api です。
テキスト変換 API
前提条件
Visual Studio がインストールされていることを確認します。
- Visual Studio 2017 バージョン 15.3 (Azure 開発ワークロードを含む)。
注意
インストールまたは Visual Studio 2017 15.3 のバージョンにアップグレードした後、Azure 関数の Visual Studio 2017 ツールを手動で更新する必要もあります。 ツールを更新するには、[ツール] メニューで [拡張機能と更新プログラム] > [更新プログラム] > [Visual Studio Marketplace] > [Azure Functions and Web Jobs Tools](Azure Functions と Web ジョブのツール) > [更新] の順に選択します。
バリエーション
- Azure 政府機関、テキスト変換 API にアクセスするための URI です。
https://api.microsofttranslator.us/v2/http.svc
- 商用の URI から唯一の違いは、エンドポイントの**.us**
- テキスト変換 API にアクセス トークンを取得するための URI は、
Endpoint
によってこのクイック スタートの第 1 部で保存された属性/IssueToken
終了時
テキスト変換方法
このサンプルを使用して、テキストに変換 - Translate メソッド別の言語から、テキスト文字列に変換するには、言語を指定します。 複数ある言語コードでテキスト変換 API を使用できます。
テキスト変換 (C#) の要求の例
サンプルは、c# で書き込まれます。
- Visual Studio で新しいコンソール ソリューションを作成します。
- Program.cs を以下の対応するコードに置き換えます。
- 置換、
subscriptionKey
上で取得したキーの値を持つ値です。 - Token.cs クラスおよびコピーを作成し、次の対応するコードを貼り付けます。
- プログラムを実行します。
「テキスト」、「から」と"to"Program.cs 内の変数を置き換えることでさまざまな言語とテキストをテストすることもできます。
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Threading.Tasks;
namespace TextTranslationApp1
{
static class Program
{
// **********************************************
// *** Update or verify the following value. ***
// **********************************************
// Replace the subscriptionKey string value with your valid subscription key.
const string subscriptionKey = "<subscription key>";
// This is the base uri for accessing the Text Translation API. We will be testing the /translate method.
const string uriBase = "https://api.microsofttranslator.us/v2/http.svc/translate?";
static void Main()
{
MainAsync().Wait();
}
private static async Task MainAsync()
{
// Replace the "text" variable with whatever text you want to translate
// Replace the "from" variable with the language that the "text" string is written in
// Replace the "to" variable with the language that you want the text to be translated into
string text = "elephants are cute";
string from = "en";
string to = "es";
//Formulating the uri to make a GET call to the API
string uri = uriBase + "text=" + text + "&from=" + from + "&to=" + to;
//Declare an instance of the Token class
Token token = new Token();
//Retrieve the token
string accessToken = await token.GetToken(subscriptionKey);
// Make the GET call
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add("Authorization", accessToken);
//Getting the JSON response
using (WebResponse response = httpWebRequest.GetResponse())
using (Stream stream = response.GetResponseStream())
{
DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
string translation = (string)dcs.ReadObject(stream);
System.Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, from, to);
Console.WriteLine(translation);
Console.ReadLine();
}
}
}
}
今すぐしようとして、テキスト変換 API にアクセス トークンを取得するためには、このメソッドを格納する Token.cs クラスを作成します。 ソリューション エクスプ ローラーからプロジェクトを右クリックし、「新しいクラス」を追加します。 コピーし、Token.cs クラスに次のコードを貼り付けます。
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
namespace TextTranslationApp1
{
public class Token
{
/// URL of the token service
/// Replace this url with your endpoint (the location should be the location of your resource group) and add the "IssueToken" at the end
private static readonly Uri ServiceUrl = new Uri("https://virginia.api.cognitive.microsoft.us/sts/v1.0/IssueToken");
/// Name of header used to pass the subscription key to the token service
private const string OcpApimSubscriptionKeyHeader = "Ocp-Apim-Subscription-Key";
/// After obtaining a valid token, this class will cache it for this duration.
/// Use a duration of 5 minutes, which is less than the actual token lifetime of 10 minutes.
private static readonly TimeSpan TokenCacheDuration = new TimeSpan(0, 5, 0);
/// Cache the value of the last valid token obtained from the token service.
public string _storedTokenValue = string.Empty;
/// When the last valid token was obtained.
private DateTime _storedTokenTime = DateTime.MinValue;
/// Gets the HTTP status code for the most recent request to the token service.
public HttpStatusCode RequestStatusCode
{
get; private set;
}
// This method retrieves and caches an access token and generates a new one after 10 minutes, when the token expires.
public async Task<string> GetToken(string SubscriptionKey)
{
if (string.IsNullOrWhiteSpace(SubscriptionKey))
{
return string.Empty;
}
// Re-use the cached token if there is one.
if ((DateTime.Now - _storedTokenTime) < TokenCacheDuration)
{
return _storedTokenValue;
}
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = ServiceUrl;
request.Content = new StringContent(string.Empty);
request.Headers.TryAddWithoutValidation(OcpApimSubscriptionKeyHeader, SubscriptionKey);
client.Timeout = TimeSpan.FromMinutes(10);
var response = await client.SendAsync(request);
this.RequestStatusCode = response.StatusCode;
response.EnsureSuccessStatusCode();
var token = await response.Content.ReadAsStringAsync();
_storedTokenTime = DateTime.Now;
_storedTokenValue = "Bearer " + token;
return _storedTokenValue;
}
}
}
}
翻訳のテキスト応答
コンソールから、正常な応答の出力の例は、次に示します。
詳細についてを参照してくださいパブリック ドキュメントとパブリック API ドキュメントトランスレーター テキスト api です。
次の手順
- サブスクライブ、 Azure 政府機関向けブログ
- 使用して、スタック オーバーフローに関するヘルプを表示、"azure gov"タグ
- フィードバックのお願いまたは経由での新機能を要求、 Azure 政府フィードバック フォーラム