{"id":278862,"date":"2016-04-22T15:45:04","date_gmt":"2016-04-22T11:45:04","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=278862"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=278862","title":{"rendered":"\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u043c \u043f\u043e\u043b \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Microsoft Project Oxford \u0438 C#"},"content":{"rendered":"<p>       \u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f <a href=\"https:\/\/habrahabr.ru\/post\/282125\/\">\u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b<\/a>, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Microsoft Project Oxford \u0438 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u043e\u0442\u0430 \u043d\u0430 Telegram \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u043b \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442 \u043f\u043e \u0444\u043e\u0442\u043e \u043d\u0430 PHP. <\/p>\n<p>  \u041d\u043e \u0441\u0435\u0433\u043e\u0434\u043d\u044f, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 WPF \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Microsoft Project Oxford Client SDK \u0438 C# \u043d\u0430 \u043f\u043e\u0434\u043e\u0431\u0438\u0438 <a href=\"http:\/\/How-Old.net\">How-Old.net<\/a><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/4d7\/60b\/f53\/4d760bf53d90434ca36df8aa42df3412.PNG\"\/><br \/>  <a name=\"habracut\"><\/a><br \/>  \u041f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c:<\/p>\n<p>  1) \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c Visual Studio 2015 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442 WPF<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/e06\/aa7\/472\/e06aa74724104ce4b6900b10f2dbff40.PNG\"\/><\/p>\n<p>  2) \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u0448\u0430\u0433\u043e\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 Manage NuGet Packages \u2014 <b>Newtonsoft.Json<\/b> <\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/bd6\/630\/6e5\/bd66306e58194f9aa58f3a9d6bbda8ba.PNG\"\/><\/p>\n<p>  3) \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c MainWindow.xaml \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c Image \u0438 Button<\/p>\n<pre><code class=\"javascript\">&lt;Image x:Name=&quot;FacePhoto&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;240&quot; Margin=&quot;72,25,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;381&quot;\/&gt;  &lt;Button x:Name=&quot;button&quot; Content=&quot;\u041d\u0430\u0436\u043c\u0438 \u043c\u0435\u043d\u044f&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;28&quot; Margin=&quot;72,281,0,0&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;381&quot; Click=&quot;button_Click&quot;\/&gt; <\/code><\/pre>\n<p>  4) \u041f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043a \u043a\u043e\u0434\u0443, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043e\u0434 \u043d\u0430 \u043a\u043d\u043e\u043f\u043a\u0443, \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0444\u043e\u0442\u043e\u0433\u0440\u0430\u0444\u0438\u0438   <\/p>\n<pre><code class=\"cs\"> private async void button_Click(object sender, RoutedEventArgs e)         {                       var openDlg = new Microsoft.Win32.OpenFileDialog();              openDlg.Filter = &quot;JPEG Image(*.jpg)|*.jpg&quot;;             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;         } <\/code><\/pre>\n<p>  5) \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u0442\u0430\u043a \u0436\u0435 \u0447\u0435\u0440\u0435\u0437 Manage NuGet Packages \u2014 <b>Microsoft.ProjectOxford.Face<\/b><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/db9\/305\/6fc\/db93056fc0294c3d9dec521624d716ab.PNG\"\/><\/p>\n<p>  6) \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u044b<\/p>\n<pre><code class=\"cs\">using Microsoft.ProjectOxford.Face; using Microsoft.ProjectOxford.Face.Contract; <\/code><\/pre>\n<p>  \u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u0432\u0441\u0435 \u043f\u0440\u0438\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0434\u0435\u043b\u0430\u043d\u044b, \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u043a \u0441\u0430\u043c\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 =)<\/p>\n<p>  7) \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043f\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043b\u044e\u0447 API <\/p>\n<pre><code class=\"cs\">private readonly IFaceServiceClient faceServiceClient = new FaceServiceClient(&quot;key&quot;); <\/code><\/pre>\n<p>  8) \u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0444\u043e\u0442\u043e.<\/p>\n<pre><code class=\"cs\">private async Task&lt;FaceRectangle[]&gt; UploadAndDetectFaces(string imageFilePath)         {             List&lt;FaceAttributeType&gt; FaceAG = new List&lt;FaceAttributeType&gt;();             a.Add(FaceAttributeType.Age);             a.Add(FaceAttributeType.Gender);\/\/ \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0445\u043e\u0442\u0438\u043c \u0443\u0437\u043d\u0430\u0442\u044c              try             {                 using (Stream imageFileStream = File.OpenRead(imageFilePath))                 {                     var faces = await faceServiceClient.DetectAsync(imageFileStream, true, false, FaceAG); \/\/ \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u043c \u0444\u043e\u0442\u043e \u043d\u0430 \u0430\u043d\u0430\u043b\u0438\u0437                     var faceRects = faces.Select(face =&gt; face.FaceRectangle); \/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u043b\u0438\u0446                     var faceA = faces.Select(face =&gt; face.FaceAttributes);\/\/ \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u044b - \u043f\u043e\u043b \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442                     faceAtr = faceA.ToArray();                     return faceRects.ToArray();                 }             }             catch (Exception)             {                 return new FaceRectangle[0];             }         } <\/code><\/pre>\n<p>  \u0412\u043e\u0442 \u0438 \u0432\u0441\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0444\u043e\u0440\u043c\u0438\u0442\u044c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u043e\u0431\u0432\u0435\u0434\u0435\u043c \u043b\u0438\u0446\u0430 \u043d\u0430 \u0444\u043e\u0442\u043e \u0438 \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u043f\u043e\u043b \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442.<\/p>\n<p>  \u0414\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b \u043a\u043e\u043d\u0441\u043e\u043b\u044c, \u043a\u0443\u0434\u0430 \u0431\u0443\u0434\u0443 \u0432\u044b\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u043e\u0437\u0440\u0430\u0441\u0442 \u0438 \u043f\u043e\u043b.<\/p>\n<p>  \u0418\u0434\u0435\u043c \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u043a\u043d\u043e\u043f\u043a\u0438 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u0443\u0434\u0430 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"cs\">            Title = &quot;\u0410\u043d\u0430\u043b\u0438\u0437...&quot;;             FaceRectangle[] faceRects = await UploadAndDetectFaces(filePath);             Title = String.Format(&quot;\u041d\u0430\u0439\u0434\u0435\u043d\u043e. {0} \u043b\u0438\u0446&quot;, faceRects.Length);                           if (faceRects.Length &gt; 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;             }              foreach (var fecea in faceAtr)             {                 Console.WriteLine(&quot;\u0412\u043e\u0437\u0440\u0430\u0441\u0442: {0}&quot;, fecea.Age);                 Console.WriteLine(&quot;\u041f\u043e\u043b: {0}&quot;, fecea.Gender);                 Console.WriteLine();              } <\/code><\/pre>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u0443 \u043d\u0430\u0441 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/bf7\/599\/4b6\/bf75994b6e7d4d8198fb91a2fdc54911.PNG\"\/><\/p>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u0435, \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0433\u043e\u0442\u043e\u0432, \u0432\u0441\u0435\u043c \u0443\u0434\u0430\u0447\u0438!               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habrahabr.ru\/post\/282231\/\"> https:\/\/habrahabr.ru\/post\/282231\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       \u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f <a href=\"https:\/\/habrahabr.ru\/post\/282125\/\">\u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u043b<\/a>, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 Microsoft Project Oxford \u0438 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u043e\u0442\u0430 \u043d\u0430 Telegram \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u043b \u0438 \u0432\u043e\u0437\u0440\u0430\u0441\u0442 \u043f\u043e \u0444\u043e\u0442\u043e \u043d\u0430 PHP. <\/p>\n<p>  \u041d\u043e \u0441\u0435\u0433\u043e\u0434\u043d\u044f, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 WPF \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Microsoft Project Oxford Client SDK \u0438 C# \u043d\u0430 \u043f\u043e\u0434\u043e\u0431\u0438\u0438 <a href=\"http:\/\/How-Old.net\">How-Old.net<\/a><\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/4d7\/60b\/f53\/4d760bf53d90434ca36df8aa42df3412.PNG\"\/>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-278862","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/278862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=278862"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/278862\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=278862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=278862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=278862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}