Teach your Computer Visual Recognition with IBM Watson and Python - Step by Step Tutorial

in machinelearning •  8 years ago 

I'm going to show you how to perform image recognition: detect and classify objects, recognize text, and detect faces. We'll use the Visual Recognition API of IBM Watson by calling it from Python.

According to IBM's Website:

"IBM Watson is a technology platform that uses natural language processing and machine learning to reveal insights from large amounts of unstructured data."

Watson is basically a complex algorithm that can help make sense of large data.

If you wanted to do machine learning, visual recognition, natural language processing, and other similar sophisticated processes just a few years, you'd require installing numerous applications and modules, use hundreds if not thousands of lines of code, and train classifiers for prolonged machine hours. Today, you can get away with the minimum setup, in just a few minutes.

Of course, you'd want to dive deep into coding and teaching yourself such algebraic algorithms if you want to become really versed into this stuff. But, here, I'm going to give you a flavor of it. And you should decide if you want to dive deeper...

I'm assuming you have Python 3.x installed on your system. It doesn't matter if you're using Windows, Linux or Mac. I'm also assuming you have very basic knowledge of the Python Programming Language. But, if you follow through with the tutorial, you don't need to know anything.

1. Sign-Up for Free on Bluemix

Bluemix is a platform that allows developer to access IBM products, including Watson.

"Bluemix is the home of 130+ unique services, including offerings like IBM Watson and Weather.com, and millions of running applications, containers, servers, and more."

IBM offers a 30-day free trial of the platform where you can use most of its services for free. Thereafter, you can still access the platform and use a certain quota of the services for free. Once you reach the quota, your credit card will be billed accordingly.

If you only want to play around, you'll most likely be able to keep using Bluemix for free - until, of course, they change their terms of service.

To signup for your free account go here

2. Install watson_developer_cloud module in Python

Go to command line in Windows or to the Console in Linux or Mac and do:

pip install watson_developer_cloud

If you don't have 'pip' (but you should if you're running a recent version of Python), see here

I'm using Windows here

3. Setup Visual Recognition API in Bluemix

1. Go to your Bluemix Console and click on Watson

2. Click on the + button (right side of the screen - blue color)

3. Scroll down to Visual Recognition, click on it.

4. Fill in 'Service Name' and 'Credential name', select free plan, scroll down and click 'Create'.

5. You will be redirected to your new Visual Recognition API page. Go to 'Service Credentials' and copy your 'api_key'

4. Call Watson from Python and Run Image Recognition

1. Open your python command line

from watson_developer_cloud import VisualRecognitionV3 as vr
instance = vr(api_key='paste your api _key here', version='2016-05-20')

2. Now, we can simply select an image from the web or from offline and run visual recognition on it. For this tutorial, I'm using this giraffe image:

img = instance.classify(images_url='urltoyoung-giraffe.jpg')

3. To access the results, I'm gonna give you the shortcut:

img['images'][0]['classifiers'][0]['classes']

4. The response that you get for this command is:

You can see there is a 92% chance there is a giraffe in this image.

5. With a little bit of python coding, we can prettify the response:

for things in img['images'][0]['classifiers'][0]['classes']:
... print('\n There is a ' + str(things['score']*100) + ' percent chance the image contains: '+ things['class'])

6. Hit enter twice and you get this:

Which I think is much easier to read.

Additionally

Similar to 'instance.classify(...', you can use:

  • 'instance.detect_faces(...'
  • 'instance.recognize_text(...'
  • and so on.

In your Python Interpreter you can simply run:

help(instance)

And see the exact commands for face detection and text recognition.

I'm really sure you can now imagine the unlimited applications this technology has! Enjoy coding!

Photo adapted from here

#machinelearning #computervision #ibmwatson #imagerecognition


Cristi Vlad, Self-Experimenter and Author

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

I upvote U

Good one! Are we colleagues? I work for ibm uk ;)

well, I dont officially work for IBM but I use the platform. what are you working on?

I'm a ux design consultant for ibm interactive experience(working with different clients). I just took one year off (leave of absence) to explore the world

nice! enjoy your year off! do you think IBM is going to offer free access to Watson in the future?

Awesome! Thank you for that walkthrough..

you're welcome @blakemiles84