Image recognition using third party web service (recognize.im)
In this example I am going to show you how to consume with C# a third party image recognition web service (
REST). First we need to create a new (30 days free) account in our web service provider:
www.recognize.im. Remember to save your account settings to make the connection:
client_id, clapi_key and
api_key. Then upload some images to your new account and save these images in small size to be used by our new application. I used some star wars images, you will see in the code below. Once all of these steps are finished we can start with our app.
Create a new solution in your VS with two projects inside. A console application will be the interface between the user and the second project, a library which saves all the logic for the web service.
Here below you can see the interface application stored in our console project. It gives some image options to our user. Then the user select one by writing the number reference and we make a call to our library.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using OneFlowApp;
namespace OneFlowApp
{
class Program
{
//Data members
private static string client_id;
private static string clapi_key;
private static string api_key;
private static string url;
private static Dictionary<int, string> images;
private static void Main(string[] args)
{
Program p = new Program();
p.run();
}
private Program()
{
client_id = ""; //API User ID
clapi_key = ""; //CLAPI user key
api_key = ""; //API user key
url = @"http://recognize.im/v2/recognize/single/";
images = new Dictionary<int, string> {
{1, "anakinT.jpg"}, {2, "kenobiT.jpg"}, {3, "lukeT.jpg"},
{4, "vaderT.jpg"}, {5, "winduT.jpg"}, {6, "yodaT.jpg"}};
}
private void run()
{
Console.WriteLine("\n|-| Star Wars characters recognition system |-|\n");
Console.WriteLine("\rPlease select a character (1-6) from the list below and press enter:\n");
Console.WriteLine("1.Anakin\n2.Kenobi\n3.Luke\n4.Vader\n5.Windu\n6.Yoda");
string opt = Console.ReadLine();
short selection;
if (Int16.TryParse(opt, out selection)) {
if (selection > 0 && selection < 7) {
string imagePath = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\Resources\\" + images[selection].ToString();
RestService rest = new RestService(api_key, url);
Dictionary<string, object> response = rest.sendData(imagePath);
if (response.ContainsKey("status") && response["status"].ToString() == "0") {
ArrayList responseArray = (ArrayList)response["objects"];
Dictionary<string, object> responseImage = (Dictionary<string, object>)responseArray[0];
Console.WriteLine("Match found! ID: " + responseImage["id"].ToString() + ", Name: " + responseImage["name"].ToString());
}
else if (response["status"].ToString() == "1") {
Console.WriteLine("Error: " + response["message"].ToString());
}
else if (response["status"].ToString() == "2") Console.WriteLine("No match found.");
else Console.WriteLine("No match found.");
}
else Console.WriteLine("Not valid option.");
Console.ReadLine();
}
else {
Console.WriteLine("Error: wrong selection");
Console.ReadLine();
}
}
}
}
In this part of the code we read the answer from the recognize server to know if it has been able to recognize the image sent. We get different kind of errors and only number “0” means that everything worked fine. In this case we can show the image reference name in the console view.