使用前的准備
-
獲取SDK:
新建項目,在引用項上右擊點擊管理Nuget包程序包,在瀏覽里面搜索並下載下列程序包,這些引用分別對應着微軟牛津計划的認知服務。(情感識別、人臉識別、計算機視覺等)。
注:使用Nuget包需要下載安裝nuget插件(https://www.nuget.org/) -
獲取認知服務訂閱密鑰:
點擊下列鏈接來Create你的認知服務密鑰:
(https://azure.microsoft.com/en-us/try/cognitive-services/)
注:其實光生成密鑰也沒用,你還需要在Azure中開認知通服務(https://portal.azure.com)Azure的月租,只能呵呵。
但是有個好消息,就是新注冊的用戶可以享受免費一個月Azure的體驗.
開始使用(以人臉識別為例)
服務調用
<pre><code>
private string key_face = "867218d1154f4d9eb91198fe6092ebf3";
FaceAttributeType[] faceAttributes=new FaceAttributeType[] {
FaceAttributeType.Age,
FaceAttributeType.Gender,
FaceAttributeType.Smile,
FaceAttributeType.FacialHair,
FaceAttributeType.HeadPose,
FaceAttributeType.Glasses
};
public async Task<Face[]> GetFaces(StorageFile photo)
{
var stream = await photo.OpenAsync(FileAccessMode.Read);
var stream_send = stream.CloneStream();
var image = new BitmapImage();
image.SetSource(stream);
FaceServiceClient faceClient = new FaceServiceClient(key_face);
var face_task = faceClient.DetectAsync(stream_send.AsStream(), true, true, faceAttributes);
return await face_task;
}
</code></pre>
獲取圖片
<pre><code>
public async Task<StorageFile> GetImageFromAlbum()
{
FileOpenPicker fop = new FileOpenPicker();
fop.ViewMode = PickerViewMode.Thumbnail;
fop.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fop.FileTypeFilter.Add(".jpg");
fop.FileTypeFilter.Add(".png");
fop.FileTypeFilter.Add(".bmp");
StorageFile photo = await fop.PickSingleFileAsync();
return photo;
}
</code></pre>
使用服務
<pre><code>
StorageFile file = await GetImageFromAlbum();
var faces = await cognitiveService.GetFaces(file);
</code></pre>
附: