Published April 22, 2017 by with 5 comments

Cognitive Services Computer Vision In Xamarin Forms


Before jumping into the cognitive service, we first have an idea of what the cognitive services do in the real life. So let us take a real life example of a Blind Person. Saqib Sheikh is blind from both eyes and he lost his vision at the age of seven. However, thanks to Microsoft Cognitive services he is still feels the emotions as well as picture of everything by using the Microsoft Cognitive Services. So today, we are talking about the World best Cognitive Services from the Microsoft end and latter we will implement this in Xamarin Forms so every single person can enjoy the Cognitive Services.
So let’s get started, The very first thing you need to know is Restful Api and then you simply switch to https://www.microsoft.com/cognitive-services where we have couple of restful API’s . So in short, we will consume the rest API’s in Xamarin Forms by using the Vision Nugget Package. So let’s switch to Visual Studio 2015 or Xamarin Studio and create a new project, add a few xaml lines to your xaml file i.e.
<StackLayout Padding="50">
    <Button x:Name="BtnImage" Text="Pick Image" Clicked="BtnImage_OnClicked" />
    <Image x:Name="Img" WidthRequest="200" HeightRequest="200"/>
    <Label x:Name="LblResult" FontSize="32"  />
  </StackLayout>
Then import a following nugget package Microsoft.ProjectOxford.Vision
After this add one more package for Camera that is Xam.Plugin.Media.
Now just initialize the Camera and add a few lines of code so you could access the camera of mobile and desktop devices.

var media = Plugin.Media.CrossMedia.Current;
await media.Initialize();
var file = await media.TakePhotoAsync(new StoreCameraMediaOptions { SaveToAlbum = false });
Img.Source = ImageSource.FromStream(() => file.GetStream());
Then it’s time to call a vision client just like HttpClient.
Therefore, what I am going to do is I will simply add few lines of code here for accessing the Cognitive Services calling and feedback response and then populate the actual result into the Label.

  var visionclient = new VisionServiceClient(visionKey);
  var result = await visionclient.DescribeAsync(file.GetStream());
  LblResult.Text = result.Description.Captions.First().Text;

Then it's time to call the Dispose function to avoid memory leakage and memory flaws in your applications by using the following line i.e. 
file.Dispose();
Now all you need is just a device or an internet connection for accessing the JSON response and calling to the Microsoft cognitive services API.
Let us see how it looks like when I take a picture from camera and what comes via JSON response.
                                                


5 comments:

  1. Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best. Hire xamarin developer india, xamarin development company

    ReplyDelete
  2. I think one of your advertisings triggered my browser to resize, you might want to put that on your blacklist. 4k computer monitor

    ReplyDelete
  3. The article is very informative. Thanks for sharing such helpful blog post. Keep posting.

    Translation Company | Translation Company in India

    ReplyDelete
  4. Your site is truly cool and this is an extraordinary moving article. århus it support

    ReplyDelete
  5. By investing in SEO Services in Jaipur , you can gain a competitive edge over your rivals. With the growing number of businesses going online, it's crucial to have a solid SEO strategy in place to ensure your website ranks higher than your competitors. SEO experts in Jaipur understand the local market dynamics and can devise effective strategies to outrank your competitors in search engine results.

    ReplyDelete