📜 ⬆️ ⬇️

Cat Recognition Service

The problem of recognition of cats in the images can not be neglected. Alternatively, to solve it, you can create and train your own classifier, which will require tens of thousands of fluffy photos and several months of work on preparing a data set and, in fact, the training itself. The only pity is that the finished classifier, trained specifically for cats, could not be found in the open spaces of the network.

And in general, is it possible to create a service that confidently recognizes cats, given the inherent desire to accept the most unexpected posture? Let's try.



To solve the problem, an IBM Bluemix PaaS platform was chosen, which brings together about a hundred different services for all occasions. There is a separate group Watson, containing various tools for solving problems of cognitive computing - very similar to what we need.
')
Among the services Watson found something called AlchemyAPI, which, in turn, looks like a universal set of cognitive alchemist, presented in the form of unified REST API. And there is an API for Image Tagging. The API is very simple. Simply transfer the image to it either as a file or as a URL, and it returns JSON with a set of tags and relevance coefficients. That is, all you need to do is to write a small service that will receive a file with a picture, send it to AlchemyAPI and tell us what happened as a result.

The development was on python, they took Tornado as an HTTP server. In principle, you can take any other, just I'm used to the Tornado and everything turns out simply and quickly on it.

Now we need to create the conditions for the development of our application. We will do this in such a way that you can write and debug code on your computer, and then transfer what happened to the Bluemix cloud without making any additional changes.



Let's start with creating a working environment for Python under our Bluemix account. If you do not have an account yet, register - this is fast, free of charge and without SMS. Go to the Dashboard section and click Create App , then select the type of Web application and get a list of available media for our service (we need Python). Add a name to the service (I called it catreco) and in about half a minute you will see that the environment has been created, and there is Hello World there, which can be viewed at http://catreco.mybluemix.net .



Now it's time to do business. Bluemix offers two ways to synchronize the work environment between the local computer and the cloud: the CF command line interface and the GIT familiar to most of us, which we will choose. In the menu on the left, click on Overview and get to the page of our application. Click the Add GIT button and we have a GIT URL pointing to the newly created repository containing Hello World.

Hello World itself is of little interest, however, the repository contains several useful files needed to deploy our application in the cloud.



Clone this repository on our computer:

git clone < > 

We will need the following files (the rest can be deleted):

 manifest.yml, Procfile, requirements.txt. 

Now it's up to our service recognizer. I put his code here . Download it to your computer and copy the contents to the catreco folder. In the code itself there is nothing worthy of special attention - only a few dozen lines, so I will not comment on it in detail. Although there are a few nuances.

First, you need to connect the AlchemyAPI service, for which we return to Bluemix in the Dashboard section and click the Use Services or APIs button , and then select the AlchemyAPI service. On the configuration page of the service it is indicated that you need to create an account at AlchemyAPI itself - follow the link, create an account and get the key by mail. This key needs to be copied to the APIKEY variable in the webserver.py file. At this configuration service can be considered complete.

Secondly, when you start the HTTP server in your working environment, Bluemix uses a special port that is different from the 80th port and then, using internal routing mechanisms, allows you to access your application on port 80. It is necessary to provide this in the server code webserver.py:

 PORT = int(os.getenv('VCAP_APP_PORT', 8080)) … application.listen(PORT) 

That is, if we are in the Bluemix environment, the server will be started on the port contained in the variable 'VCAP_APP_PORT', and from the local machine on port 8080 or one that will be convenient. In most cases, this is the only specific setting you need to consider when porting your code to Bluemix.

Third, we need to prepare the environment on Bluemix for the launch of our application. At the moment there is only python 2.7.8 and standard libraries, and we still need to install Tornado and the requests module, tell Bluemix which file should be running. For this, the files that we took from Hello World are responsible.

The file manifest.yml contains the configuration of our work environment. Here we will not change anything:

 applications: - disk_quota: 1024M host: catreco name: catreco path: . domain: mybluemix.net instances: 1 memory: 128M 

The Procfile file contains information on which command to launch our application. Our executable file is called webserver.py, so we write:

 web: python webserver.py 

The requirements.txt file contains a list of modules that must be installed in a production environment. After seeing this list, Bluemix will launch a pip and install everything you need:

 tornado>=4.0 requests>=2.7 

At this training is over. We configured the external AlchemyAPI service, took into account the features of the Bluemix environment when starting the HTTP server and set the necessary parameters for deploying our application in the cloud.



You can try to start the application on a local machine, for which we write python webserver.py on the command line and type the coveted localhost: 8080 in the browser



That is what lies on my lap, is a cat with a probability of 91%. I tried other angles, took pictures from the network - it works! So it's up to you - go back to the clouds. In the folder where we first cloned Hello World, and then placed the code of our project, we already have everything we need - then simply update the local repository:

 git add . git commit –m "first deployment" 

And send it back to Bluemix:

 git push 




Bluemix will do all the necessary operations to build a new application (instead of Hello World will now be our code) and run it. As soon as the status of your app becomes Your app is running , you can follow the link http://catreco.mybluemix.net/ and make sure that our service works in the cloud, you can try it from any device. Beautiful plugin for uploading files taken from here .

Examples of work:

Cat - 94%


Cat - 48%, pet - 63%


Cat - 89%, rabbit - 80%


Cat - 99%, kitten - 50%


Cat - 71%, dog - 59%


The previous picture looks like a tiger, with this: tiger - 99%, cat - 75%



Dog - 99%, puppy - 68%


Dog - 99%


Cat - 99%, dog - 57%


Cat: 99%


Cat - 65%, eagle - 43%


If you make a mistake somewhere and something went wrong - don't panic. Above the status of your application on the Bluemix panel there is a magic Edit Code button, clicking which takes you to the DevOps service, where there is a code editor, repository control and build builds, as well as access to all logs. The service is large and its description will take longer than our entire project - if required, please consult the documentation .

Instead of output:
  1. We learned to recognize cats using a simple web application and service provided by the Bluemix platform;
  2. We now know how to prepare an application for deployment in the Bluemix cloud and how, in fact, to deploy it using one git command;
  3. We know where to look if something went wrong;
  4. As you already understood, business can not be limited only to cats.



Not bad for a rainy morning, I think? Uncover folders with cats, try . Seals, shake on the mustache, as it is now better to mask.

UPD:
Services from users (if the limit is suddenly over):
le_den catreco.eu-gb.mybluemix.net
inwardik rcat.mybluemix.net
Peter P. (no account, dropped in VK) 2catreco.mybluemix.net

Thank you for not being left out :) The recognition problem for cats is really very important.

Source: https://habr.com/ru/post/262911/


All Articles