Introduction
Microsoft Cognitive services is set of cloud-based intelligence APIs for building richer and smarter application development. Cognitive API will use for Search meta data from Photos and video and emotions, sentiment analysis and authenticating speakers via voice verification.
The Computer Vision API will help developers to identify the objects with access to advanced algorithms for processing images and returning image meta data information. In this article, you will learn about Computer Vision API and how to implement Compute Vision API into Bot application.
You can follow below steps for implement object detection in Bot Application
Computer Vision API Key Creation
Computer Vision API returns information about visual content found in an image. You can follow the below steps for creating Vision API key.
- Navigate to https://azure.microsoft.com/en-us/try/cognitive-services/
- Click on “Get API Key “or Login with Azure login.
- Login with Microsoft Account and Get API key
- Copy API key and store securely, we will use this API key into our application
Step 2 Create New Bot Application
Let's create a new bot application using Visual Studio 2017. Open Visual Studio > Select File > Create New Project (Ctrl + Shift +N) > Select Bot application.
The Bot application template gets created with all the components and all required NuGet references installed in the solutions.
In this solution, we are going edit Messagecontroller and add Service class.
Install Microsoft.ProjectOxford.Vision Nuget Package
The Microsoft project oxford vision nuget package will help with access to cognitive service so Install “Microsoft.ProjectOxford.Vision” Library from the solution
Create Vision Service
Create a new helper class to the project called VisionService that wraps around the functionality from the VisionServiceClient from Cognitive Services and only returns what we currently need.
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using System.Web;
- using Microsoft.ProjectOxford.Vision;
- using Microsoft.ProjectOxford.Vision.Contract;
-
- namespace BotObjectDetection.Service
- {
- public class VisionService : ICaptionService
- {
-
-
-
- private static readonly string ApiKey = "<API Key>";
-
-
-
-
- private static readonly VisualFeature[] VisualFeatures = { VisualFeature.Description };
-
- public async Task<string> GetCaptionAsync(string url)
- {
- var client = new VisionServiceClient(ApiKey);
- var result = await client.AnalyzeImageAsync(url, VisualFeatures);
- return ProcessAnalysisResult(result);
- }
- public async Task<string> GetCaptionAsync(Stream stream)
- {
- var client = new VisionServiceClient(ApiKey);
- var result = await client.AnalyzeImageAsync(stream, VisualFeatures);
- return ProcessAnalysisResult(result);
- }
-
-
-
-
-
-
- private static string ProcessAnalysisResult(AnalysisResult result)
- {
- string message = result?.Description?.Captions.FirstOrDefault()?.Text;
-
- return string.IsNullOrEmpty(message) ?
- "Couldn't find a caption for this one" :
- "I think it's " + message;
- }
- }
- }
In the above helper class, replace vision API key and call the Analyze image client method for identify image meta data
Messages Controller
MessagesController is created by default and it is the main entry point of the application. it will call our helper service class which will handle the interaction with the Microsoft APIs. You can update “Post” method like below
Run Bot Application
The emulator is a desktop application that lets us test and debug our bot on localhost. Now, you can click on "Run the application" in Visual studio and execute in the browser
Test Application on Bot Emulator
You can follow the below steps to test your bot application.
- Open Bot Emulator.
- Copy the above localhost url and paste it in emulator e.g. - http://localHost:3979
- You can append the /api/messages in the above url; e.g. - http://localHost:3979/api/messages.
- You won't need to specify Microsoft App ID and Microsoft App Password for localhost testing, so click on "Connect".
Related Article
I have explained about Bot framework Installation, deployment and implementation in the below articles:
- Getting Started with Chatbot Using Azure Bot Service
- Getting Started with Bots Using Visual Studio 2017
- Deploying A Bot to Azure Using Visual Studio 2017
- How to Create ChatBot In Xamarin
- Getting Started with Dialog Using Microsoft Bot Framework
- Getting Started with Prompt Dialog Using Microsoft Bot Framework
- Getting Started With Conversational Forms And FormFlow Using Microsoft Bot Framework
- Getting Started With Customizing A FormFlow Using Microsoft Bot Framework
- Sending Bot Reply Message With Attachment Using Bot Framework
- Getting Started With Hero Card Design Using Microsoft Bot Framework
- Getting Started With Thumbnail Card Design Using Microsoft Bot Framework
- Getting Started With Adaptive Card Design Using Microsoft Bot Framework
- Getting Started with Receipt Card Design Using Microsoft Bot Framework
- Building Bot Application With Azure AD Login Authentication Using AuthBot
- Building Chat Bots With Bing Search Results Using Bot Framework
Summary
In this article, you learned how to create an Intelligent Image Object Detection Bot using Microsoft Cognitive Computer Vision API. If you have any questions/feedback/ issues, please write in the comment box.