UWP - Retrieving Image Features Using Cognitive Service Computer Vision API

Before reading this article, please go through the following article.

  1. Introduction To Universal Windows Platform (UWP) App Development Using Windows 10 And Visual Studio 2015
  2. Getting Started With Microsoft Azure Cognitive Services - Computer Vision API

Microsoft Cognitive Services (formerly Project Oxford) are a set of APIs, SDKs and services available to developers to make their applications more intelligent, engaging and discoverable. Microsoft Cognitive Services expand on Microsoft’s evolving portfolio of machine learning APIs and enables developers to easily add intelligent features. Microsoft Cognitive Services let you build apps with powerful algorithms using just a few lines of code. They work across devices and platforms, such as iOS, Android, and Windows, keep improving, and are easy to set up.

Computer Vision API, as described by Microsoft, extracts rich information from the images to categorize and process the visual data and protect your users from unwanted content. It supports, analyzing an image, generating thumbnail, tagging, describing , reading text in the images and recognizing.

Reading this article, you'll learn how to retrieve image features like Image Properties, Tags, Describing Image from the selected image using Cognitive Service Computer Vision API in Universal Windows Apps development with Azure, XAML and Visual C#.

The following important tools are required for developing UWP.

  1. Windows 10 (Recommended)
  2. Visual Studio 2015 Community Edition (It is a free software available online)
  3. Cognitive Service Computer Vision API Key

Now, we can discuss step by step app development.

Step 1

Open Visual Studio 2015 -> Start -> New Project-> Select Universal (under Visual C# -> Windows) -> Blank App -> Give suitable name for your App (UWPCogVisDesImg) -> OK.

Visual studio 2015

After choosing the target and minimum platform version that your app will support, the Project creates App.xaml and MainPage.xaml.

Visual studio 2015

Step 2

Open (double click) the file MainPage.xaml in the Solution Explorer and add the Microsoft.ProjectOxford.Vision reference in the project. Right click your project (UWPCogVisDesImg) and select "Manage NuGet Packages".

Visual studio 2015

Choose "Browse" and Search "Microsoft.ProjectOxford.Vision". Select the package and install it.

Visual studio 2015

Reference is added to your project.

Visual studio 2015

Step 3

Add a TextBlock control, change the name and text property for Title.

Visual studio 2015

Add a Button Control, set the Name for selecting an Image.

Visual studio 2015

Add an Image control, change the name and text property for displaying the selected image.

Visual studio 2015

Add a Button Control, set the name for getting the Image features.

Visual studio 2015

Add a TextBlock control, change the name, and clear the text property for displaying Description Result.

Visual studio 2015

Add a TextBlock control, change the name, and clear the text property for displaying Tags Result.



Add a TextBlock control, change the name and clear the text property for displaying Image Properties Result.

Visual studio 2015

Add a Click event method for both the button controls.

Visual studio 2015

Note

Automatically, the following code will be generated in XAML code View, while we are done in the design view.

  1. <Page x:Class="UWPCogVisDesImg.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:UWPCogVisDesImg" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">  
  2.     <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  3.         <TextBlock x:Name="tblTitle" HorizontalAlignment="Left" Margin="348,70,0,0" TextWrapping="Wrap" Text="UWP Cognitive Service Computer Vision Features" VerticalAlignment="Top" FontWeight="Bold" FontSize="20" />  
  4.         <Button x:Name="btnSel" Content="Select Your Image" HorizontalAlignment="Left" Margin="322,152,0,0" VerticalAlignment="Top" Click="btnSel_Click" />  
  5.         <Image x:Name="imgDescribe" HorizontalAlignment="Left" Height="356" Margin="84,218,0,0" VerticalAlignment="Top" Width="377" />  
  6.         <Button x:Name="btnCVFeature" Content="Get Features" HorizontalAlignment="Left" Margin="592,320,0,0" VerticalAlignment="Top" FontWeight="Bold" Click="btnCVFeature_Click" />  
  7.         <TextBlock x:Name="tblDescribe" HorizontalAlignment="Left" Margin="719,199,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="76" Width="366" FontSize="18" />  
  8.         <TextBlock x:Name="tblTags" HorizontalAlignment="Left" Margin="1132,165,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Height="456" Width="138" FontSize="18" />  
  9.         <TextBlock x:Name="tblImgtype" HorizontalAlignment="Left" Margin="847,389,0,0" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Height="147" Width="280" /> </Grid>  
  10. </Page>  
Step 4

Add the following namespaces in Mainpage.xaml.cs for Tagging Images.
  1. using Windows.Storage;  
  2. using Windows.UI.Xaml.Media.Imaging;  
  3. using Windows.Storage.Pickers;  
  4. using System.Threading.Tasks;  
  5. using Windows.UI.Popups;  
  6. using Windows.ApplicationModel;  
  7. using Microsoft.ProjectOxford.Vision.Contract;  
  8. using Microsoft.ProjectOxford.Vision;  
Step 5

Add the Cognitive Service Computer Vision API Client keys use Azure service and Generate it (For More Information, Please refer the article Getting Started With Microsoft Azure Cognitive Services - Computer Vision API.
  1. var visionClient = new VisionServiceClient("<Your Key>");  
  2. Add the following code with azure generated key  
  3. for getting Image Details,  
  4. StorageFile file;  
  5. public string Widt {  
  6.     get;  
  7.     set;  
  8. }  
  9. public string Heig {  
  10.     get;  
  11.     set;  
  12. }  
  13. public string Format {  
  14.     get;  
  15.     set;  
  16. }  
  17. public string ClipArtType {  
  18.     get;  
  19.     set;  
  20. }  
  21. public string LineDrawingType {  
  22.     get;  
  23.     set;  
  24. }  
  25. private async void btnSel_Click(object sender, RoutedEventArgs e) {  
  26.     file = await FilePicker.cur.GetImage();  
  27.     if (file != null) {  
  28.         var im = new BitmapImage();  
  29.         using(var straem = await file.OpenAsync(FileAccessMode.Read)) {  
  30.             im.SetSource(straem);  
  31.             imgDescribe.Source = im;  
  32.         }  
  33.     }  
  34. }  
  35. static async Task < AnalysisResult > getDescribe(Stream s) {  
  36.     var visionClient = new VisionServiceClient("<Your Key>");  
  37.     var features = new VisualFeature[] {  
  38.         VisualFeature.Categories, VisualFeature.Color, VisualFeature.Description, VisualFeature.ImageType, VisualFeature.Tags  
  39.     };  
  40.     AnalysisResult Result = await visionClient.AnalyzeImageAsync(s, features);  
  41.     return Result;  
  42. }  
  43. private async void btnCVFeature_Click(object sender, RoutedEventArgs e) {  
  44.     using(var stream = await file.OpenStreamForReadAsync()) {  
  45.         var resul = await getDescribe(stream);  
  46.         foreach(var caption in resul.Description.Captions) {  
  47.             tblDescribe.Text = "Description : \n" + "Caption : " + caption.Text + "\n Confidence : " + caption.Confidence + "\n";  
  48.         }  
  49.         string tags = "Tags : \n";  
  50.         foreach(var tag in resul.Description.Tags) {  
  51.             tags += tag + "\n";  
  52.         }  
  53.         tblTags.Text = tags;  
  54.         Widt = "Image Width : " + resul.Metadata.Width;  
  55.         Heig = "\n Image Height : " + resul.Metadata.Height;  
  56.         Format = "\n Image Format : " + resul.Metadata.Format;  
  57.         ClipArtType = "\n ClipArtType : " + resul.ImageType.ClipArtType;  
  58.         LineDrawingType = "\nLineDrawingType: " + resul.ImageType.LineDrawingType;  
  59.         string clipArtType;  
  60.         switch (resul.ImageType.ClipArtType) {  
  61.             case 0:  
  62.                 clipArtType = "\n ClipArtType : 0 Non-clipart";  
  63.                 break;  
  64.             case 1:  
  65.                 clipArtType = "\n ClipArtType : 1 ambiguous";  
  66.                 break;  
  67.             case 2:  
  68.                 clipArtType = "\n ClipArtType : 2 normal-clipart";  
  69.                 break;  
  70.             case 3:  
  71.                 clipArtType = "\n ClipArtType : 3 good-clipart";  
  72.                 break;  
  73.             default:  
  74.                 clipArtType = "\n ClipArtType : Unknown";  
  75.                 break;  
  76.         }  
  77.         tblImgtype.Text = "Image Properties : \n" + Widt + Heig + Format + clipArtType + LineDrawingType;  
  78.     }  
  79. }  
  80. }  
  81. class FilePicker {  
  82.     private FileOpenPicker pick;  
  83.     private static FilePicker inst;  
  84.     public static FilePicker cur {  
  85.         get {  
  86.             return inst;  
  87.         }  
  88.     }  
  89.     static FilePicker() {  
  90.         inst = new FilePicker();  
  91.     }  
  92.     private FilePicker() {  
  93.         pick = new FileOpenPicker();  
  94.         pick.FileTypeFilter.Add(".png");  
  95.         pick.FileTypeFilter.Add(".jpg");  
  96.         pick.FileTypeFilter.Add(".gif");  
  97.         pick.FileTypeFilter.Add(".bmp");  
  98.         pick.FileTypeFilter.Add(".jpeg");  
  99.     }  
  100.     public async Task < StorageFile > GetImage() {  
  101.         repeat: StorageFile s = await pick.PickSingleFileAsync();  
  102.         var prp = await s.GetBasicPropertiesAsync();  
  103.         if (prp.Size > 4000000) {  
  104.             MessageDialog dialog = new MessageDialog("File size should be greater than 4 MB");  
  105.             await dialog.ShowAsync();  
  106.             s = null;  
  107.             goto repeat;  
  108.         }  
  109.         return s;  
  110.     }  
  111.     public async Task < StorageFile > GetFile(string filename, string foldername) {  
  112.         StorageFolder appFolder = Package.Current.InstalledLocation;  
  113.         StorageFolder folder = await appFolder.GetFolderAsync(foldername);  
  114.         var file = await folder.GetFileAsync(filename);  
  115.         return file;  
  116.     }  
  117. }  
Visual studio 2015

Step 6

Deploy your app in Local Machine. The output of the UWPCogVisDesImg App is shown below.

Visual studio 2015

Another image with description.

Visual studio 2015

Summary

Now, you have successfully tested the selected image using Cognitive Service Vision API - Describe Image, Azure, XAML AND C# with UWP environment.

Up Next
    Ebook Download
    View all
    Learn
    View all