導讀:這個由美麗的客服小姐姐提前申請好了企業試用的賬號,個人的話還得需要visa卡,
官網地址:azure.cn
登錄地址:portal.azure.cn
目錄
1.簡介
2.Azure 認知服務是什么?
? 3.認知服務的四種職能
4.人臉API
5.新建一個WPF應用實作以下功能:
安裝SDK
圖片選擇并顯示
呼叫SDK進行識別
總結
1.簡介
按照官方的要求,我們這次只體驗認知服務的部分功能即可,

然后進入登錄的主頁,找到下面這個認知服務進行創建,這個第一次進來可能不好找,我剛開始也是找了半天,好多技術檔案都是英文教程,看起來有點費勁,不過還有很多中文檔案引導,還是很容易入門,找不到的可以直接搜索認知服務,

主要有這幾個認知服務相關的功能

先找一個比較好玩的【人臉AI】

創建face(注意:名稱只能用字符數字及連接符)

創建虛擬網路

系統分配的托管標識

標記(默認)

創建資源組并驗證成功

點擊創建進行初始化部署,等待部署完成即可,

直接轉到資源組

這里可以對標記進行編輯(增加名稱及值即可:用于管理不同的資源組,標記是名稱/值對,可便于將相同的標記應用于多個資源和資源組,從而對資源進行分類,并查看合并的帳單,標記名稱不區分大小寫,但標記值區分大小寫,),也可以管理密鑰,密鑰一般有兩個(密鑰用于訪問認知服務 API,請勿共享你的密鑰,請安全地存盤這些密鑰(例如,使用 Azure Key Vault),另外,建議定期重新生成這些密鑰,執行 API 呼叫只需要一個密鑰,在重新生成第一個密鑰時,可以使用第二個密鑰繼續訪問服務,)

2.Azure 認知服務是什么?
認知服務使每位開發人員無需具備機器學習的專業知識就能接觸到 AI,只需要一個 API 呼叫,就可以將看、聽、說、搜索、理解和加速決策的能力嵌入到應用中,讓所有技能水平的開發人員都能輕松在其應用中添加 AI 功能,
3.認知服務的四種職能
決策

語言

語音

影像

4.人臉API
用于分析影像中的人臉的 AI 服務,功能包括人臉檢測功能(感知影像中的口罩、眼鏡或人臉位置等面部特征和屬性)和通過與專用存盤庫的匹配或照片 ID 識別人,
5.新建一個WPF應用實作以下功能:
- 選擇圖片后把原圖顯示出來
- 選中后馬上進行識別
- 識別成功后把臉部用紅框描述出來
- 當滑鼠移動到紅框內的時候顯示詳細臉部資訊
安裝SDK
使用nuget安裝對于的sdk包
Install-Package Microsoft.Azure.CognitiveServices.Vision.Face -Version 2.5.0-preview.2
編輯MainWindow.xml放置影像顯示區域、檔案選中、描述顯示區域
<Window x:Class="FaceWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:FaceWpf"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="800">
<Grid x:Name="BackPanel">
<Image x:Name="FacePhoto" Stretch="Uniform" Margin="0,0,0,50" MouseMove="FacePhoto_MouseMove" />
<DockPanel DockPanel.Dock="Bottom">
<Button x:Name="BrowseButton" Width="72" Height="80" VerticalAlignment="Bottom" HorizontalAlignment="Left"
Content="選擇圖片..."
Click="BrowseButton_Click" />
<StatusBar VerticalAlignment="Bottom">
<StatusBarItem>
<TextBlock Name="faceDescriptionStatusBar" Height="80" FontSize="20" Text="" Width="500" TextWrapping="Wrap"/>
</StatusBarItem>
</StatusBar>
</DockPanel>
</Grid>
</Window>
在編輯MainWindow類的建構式初始化FaceClient等資料
private IFaceClient _faceClient;
//檢測到的人臉
private IList<DetectedFace> _faceList;
//人臉描述資訊
private string[] _faceDescriptions;
private double _resizeFactor;
private const string _defaultStatusBarText =
"滑鼠移動到面部顯示描述資訊.";
public MainWindow()
{
InitializeComponent();
//faceid的訂閱key
string subscriptionKey = "";
// faceid的終結的配置
string faceEndpoint = "";
_faceClient = new FaceClient(
new ApiKeyServiceClientCredentials(subscriptionKey),
new System.Net.Http.DelegatingHandler[] { });
if (Uri.IsWellFormedUriString(faceEndpoint, UriKind.Absolute))
{
_faceClient.Endpoint = faceEndpoint;
}
else
{
MessageBox.Show(faceEndpoint,
"Invalid URI", MessageBoxButton.OK, MessageBoxImage.Error);
Environment.Exit(0);
}
}
圖片選擇并顯示
// 選擇圖片并上傳
private async void BrowseButton_Click(object sender, RoutedEventArgs e)
{
var openDlg = new Microsoft.Win32.OpenFileDialog();
openDlg.Filter = "JPEG Image(*.jpg)|*.jpg";
bool? result = openDlg.ShowDialog(this);
if (!(bool)result)
{
return;
}
// Display the image file.
string filePath = openDlg.FileName;
Uri fileUri = new Uri(filePath);
BitmapImage bitmapSource = new BitmapImage();
bitmapSource.BeginInit();
bitmapSource.CacheOption = BitmapCacheOption.None;
bitmapSource.UriSource = fileUri;
bitmapSource.EndInit();
FacePhoto.Source = bitmapSource;
// Detect any faces in the image.
Title = "識別中...";
_faceList = await UploadAndDetectFaces(filePath);
Title = String.Format(
"識別完成. {0}個人臉", _faceList.Count);
if (_faceList.Count > 0)
{
// Prepare to draw rectangles around the faces.
DrawingVisual visual = new DrawingVisual();
DrawingContext drawingContext = visual.RenderOpen();
drawingContext.DrawImage(bitmapSource,
new Rect(0, 0, bitmapSource.Width, bitmapSource.Height));
double dpi = bitmapSource.DpiX;
// Some images don't contain dpi info.
_resizeFactor = (dpi == 0) ? 1 : 96 / dpi;
_faceDescriptions = new String[_faceList.Count];
for (int i = 0; i < _faceList.Count; ++i)
{
DetectedFace face = _faceList[i];
//畫方框
drawingContext.DrawRectangle(
Brushes.Transparent,
new Pen(Brushes.Red, 2),
new Rect(
face.FaceRectangle.Left * _resizeFactor,
face.FaceRectangle.Top * _resizeFactor,
face.FaceRectangle.Width * _resizeFactor,
face.FaceRectangle.Height * _resizeFactor
)
);
_faceDescriptions[i] = FaceDescription(face);
}
drawingContext.Close();
RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
(int)(bitmapSource.PixelWidth * _resizeFactor),
(int)(bitmapSource.PixelHeight * _resizeFactor),
96,
96,
PixelFormats.Pbgra32);
faceWithRectBitmap.Render(visual);
FacePhoto.Source = faceWithRectBitmap;
faceDescriptionStatusBar.Text = _defaultStatusBarText;
}
}
呼叫SDK進行識別
// 上傳圖片使用faceclient識別
private async Task<IList<DetectedFace>> UploadAndDetectFaces(string imageFilePath)
{
IList<FaceAttributeType> faceAttributes =
new FaceAttributeType[]
{
FaceAttributeType.Gender, FaceAttributeType.Age,
FaceAttributeType.Smile, FaceAttributeType.Emotion,
FaceAttributeType.Glasses, FaceAttributeType.Hair
};
using (Stream imageFileStream = File.OpenRead(imageFilePath))
{
IList<DetectedFace> faceList =
await _faceClient.Face.DetectWithStreamAsync(
imageFileStream, true, false, faceAttributes);
return faceList;
}
}
對人臉識別后的結果資訊組裝成字串,當滑鼠移動到人臉上的時候顯示這些資訊,
private void FacePhoto_MouseMove(object sender, MouseEventArgs e)
{
if (_faceList == null)
return;
Point mouseXY = e.GetPosition(FacePhoto);
ImageSource imageSource = FacePhoto.Source;
BitmapSource bitmapSource = (BitmapSource)imageSource;
var scale = FacePhoto.ActualWidth / (bitmapSource.PixelWidth / _resizeFactor);
bool mouseOverFace = false;
for (int i = 0; i < _faceList.Count; ++i)
{
FaceRectangle fr = _faceList[i].FaceRectangle;
double left = fr.Left * scale;
double top = fr.Top * scale;
double width = fr.Width * scale;
double height = fr.Height * scale;
if (mouseXY.X >= left && mouseXY.X <= left + width &&
mouseXY.Y >= top && mouseXY.Y <= top + height)
{
faceDescriptionStatusBar.Text = _faceDescriptions[i];
mouseOverFace = true;
break;
}
}
if (!mouseOverFace) faceDescriptionStatusBar.Text = _defaultStatusBarText;
}
private string FaceDescription(DetectedFace face)
{
StringBuilder sb = new StringBuilder();
sb.Append("人臉: ");
// 性別年齡
sb.Append(face.FaceAttributes.Gender.Value == Gender.Female ? "女性" : "男性");
sb.Append(", ");
sb.Append(face.FaceAttributes.Age.ToString() + "歲");
sb.Append(", ");
sb.Append(String.Format("微笑 {0:F1}%, ", face.FaceAttributes.Smile * 100));
// 顯示超過0.1的表情
sb.Append("表情: ");
Emotion emotionScores = face.FaceAttributes.Emotion;
if (emotionScores.Anger >= 0.1f) sb.Append(
String.Format("生氣 {0:F1}%, ", emotionScores.Anger * 100));
if (emotionScores.Contempt >= 0.1f) sb.Append(
String.Format("蔑視 {0:F1}%, ", emotionScores.Contempt * 100));
if (emotionScores.Disgust >= 0.1f) sb.Append(
String.Format("厭惡 {0:F1}%, ", emotionScores.Disgust * 100));
if (emotionScores.Fear >= 0.1f) sb.Append(
String.Format("恐懼 {0:F1}%, ", emotionScores.Fear * 100));
if (emotionScores.Happiness >= 0.1f) sb.Append(
String.Format("高興 {0:F1}%, ", emotionScores.Happiness * 100));
if (emotionScores.Neutral >= 0.1f) sb.Append(
String.Format("自然 {0:F1}%, ", emotionScores.Neutral * 100));
if (emotionScores.Sadness >= 0.1f) sb.Append(
String.Format("悲傷 {0:F1}%, ", emotionScores.Sadness * 100));
if (emotionScores.Surprise >= 0.1f) sb.Append(
String.Format("驚喜 {0:F1}%, ", emotionScores.Surprise * 100));
sb.Append(face.FaceAttributes.Glasses);
sb.Append(", ");
sb.Append("頭發: ");
if (face.FaceAttributes.Hair.Bald >= 0.01f)
sb.Append(String.Format("禿頭 {0:F1}% ", face.FaceAttributes.Hair.Bald * 100));
IList<HairColor> hairColors = face.FaceAttributes.Hair.HairColor;
foreach (HairColor hairColor in hairColors)
{
if (hairColor.Confidence >= 0.1f)
{
sb.Append(hairColor.Color.ToString());
sb.Append(String.Format(" {0:F1}% ", hairColor.Confidence * 100));
}
}
return sb.ToString();
}
到此我們的應用打造完成了,我們使用孫叫獸的演講圖片驗證一下效果,和這個引數差不多,說明這個人臉識別的功能還是很強大的,

總結
使用更加通用的rest api來呼叫,這樣可以適配任何開發語言,這種應用可以用在上下班的大屏測體溫,檢測員工有沒有帶口罩,還有釘釘外勤打卡拍照等場景,十分的便捷,剛好開始接觸這個Azure還是有點懵比,好多東西都是英文檔案,搞得我還得用谷歌瀏覽器右鍵一下,其實可以做一個智能機器人,聊天室這種工具,能覆寫一下這種認知服務,比如聊天框文本轉語音,語音轉文本,轉表情包,圖片識別文本,中英文翻譯等功能,下次有機會再體驗吧,確實挺智能的,官方的一些案例也很不錯,大家可以參考一下!
Microsoft 開發者工具和技術入門, 瀏覽我們的示例,查看你可構建的內容,https://docs.microsoft.com/zh-cn/samples/browse/
https://docs.microsoft.com/zh-cn/samples/browse/
本期內容我們就分享到這里,我們下期見!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/344108.html
標籤:AI
