• 微软认知服务——人脸识别


    微软认知服务——人脸识别

    之前做微软编程之美的时候接触了下微软认知服务相应的api,但没有仔细研究。最近在做计算机视觉相关的内容,正好顺着文档写了一个demo。

    申请API

    人脸识别API:https://www.azure.cn/cognitive-services/zh-cn/face-api

    可以用你的微软账号申请免费试用,申请之后会得到相应的密钥。

    搭建demo

    我使用的平台是Visual Studio 2013,而不是文档中要求的Visual Studio 2015。使用的语言是C#,用WPF搭建一个简单的界面。

    在WPF下加入如下结构:

    <Grid x:Name="BackPanel">
    	<Image x:Name="FacePhoto" Stretch="Uniform" Margin="0,0,0,30"/>
    	<Button x:Name="BrowseButton" Margin="20,5" Height="20" VerticalAlignment="Bottom" Content="Browse..." Click="BrowseButton_Click"/>
    </Grid>
    

    在解决方案资源管理器中右键你的解决方案名称,打开管理NuGet程序包,搜索Netonsoft.json和Microsoft.ProjectOxford.Face,分别安装。

    在MainWindow.xaml.cs中:
    添加你的密钥:

     private readonly IFaceServiceClient faceServiceClient = new FaceServiceClient("你的密钥");
    

    编写对上传照片检测人脸的代码,如下:

    private async Task<FaceRectangle[]> UploadAndDetectFaces(string imageFilePath)
    {
    	try
        {
        	using (Stream imageFileStream = File.OpenRead(imageFilePath))
            {
            	var faces = await faceServiceClient.DetectAsync(imageFileStream);
                var faceRects = faces.Select(face => face.FaceRectangle);
                return faceRects.ToArray();
            }
        }
        catch (Exception)
        {
        	return new FaceRectangle[0];
        }
    }
    

    添加button的Click事件,并添加async关键字,如下:

    		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;
                }
    
                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;
    
                Title = "Detecting...";
                FaceRectangle[] faceRects = await UploadAndDetectFaces(filePath);
                Title = String.Format("Detection Finished. {0} face(s) detected", faceRects.Length);
    
                if (faceRects.Length > 0)
                {
                    DrawingVisual visual = new DrawingVisual();
                    DrawingContext drawingContext = visual.RenderOpen();
                    drawingContext.DrawImage(bitmapSource,
                        new Rect(0, 0, bitmapSource.Width, bitmapSource.Height));
                    double dpi = bitmapSource.DpiX;
                    double resizeFactor = 96 / dpi;
    
                    foreach (var faceRect in faceRects)
                    {
                        drawingContext.DrawRectangle(
                            Brushes.Transparent,
                            new Pen(Brushes.Red, 2),
                            new Rect(
                                faceRect.Left * resizeFactor,
                                faceRect.Top * resizeFactor,
                                faceRect.Width * resizeFactor,
                                faceRect.Height * resizeFactor
                                )
                        );
                    }
    
                    drawingContext.Close();
                    RenderTargetBitmap faceWithRectBitmap = new RenderTargetBitmap(
                        (int)(bitmapSource.PixelWidth * resizeFactor),
                        (int)(bitmapSource.PixelHeight * resizeFactor),
                        96,
                        96,
                        PixelFormats.Pbgra32);
    
                    faceWithRectBitmap.Render(visual);
                    FacePhoto.Source = faceWithRectBitmap;
                }
    
            }
    

    运行结果

    初始界面:

    打开照片:

    图片来源:网上搜索

    检测结果:

    可以看到有两个人脸并没有很好地识别出来,具体的参数是可以获取到的,需要进一步研究。

  • 相关阅读:
    springaopxml
    【Cocos2dhtml5】解析例子中的飞机游戏(一)
    springiocannotation
    模板方法设计模式(JDBCTeampleta
    springaopannotation
    从前,有座山,山里有座庙,庙里有苦逼IT
    day 41 Nginx进阶
    day 42 作业
    day43 LNMP单机环境安装
    day 42 nginx rewrite跳转
  • 原文地址:https://www.cnblogs.com/syncCN/p/5601312.html
Copyright © 2020-2023  润新知