Used Watson API Visual Recognition from IBM Cloud in Python


I will show sample code to use Visual recognition (VR) which is one of the Watson APIs provided by IBM Cloud.

In this article, you can do the following two things
· I will present sample code
· Determine what is shown in the image
· Examine the face of the person from the image and judge age and sex

Environment

Python 3.5.4

Advance preparation

  1. Create a VR service with IBM Cloud and obtain the API key
  2. Install Watson-developer-cloud with pip install.
pip install watson-developer-cloud

Determine what is shown in the image

Let VR evaluate the image of Mr. Zinnie of the current IBM CEO

import json
from os.path import join, dirname
from os import environ
from watson_developer_cloud import VisualRecognitionV3

visual_recognition = VisualRecognitionV3(
    # The release date of the version of the API you want to use.
    '2016-05-20',
    # visual-recognition's API key
    api_key = '{api-key}')

images_url='https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg'

res = visual_recognition.classify(parameters=json.dumps({'url': images_url}))
print(json.dumps(res))

The result is here.
Watson has confidence about 0.6 and judges that it is a woman portrait, a woman.

{"custom_classes": 0,
 "images": [{"source_url": "https://www.ibm.com/ibm/ginni/i
mages/ginni_bio_780x981_v4_03162016.jpg",
 "resolved_url": "https://www.ibm.com/i
bm/ginni/images/ginni_bio_780x981_v4_03162016.jpg",
 "classifiers": [{"classes":
[{"score": 0.604,
  "class": "womans portrait photo",
  "type_hierarchy": "/person/female/woman/womans portrait photo"},
 {"score": 0.607, "class": "woman"},
 {"score": 0.608, "class": "female"},
 {"score": 0.725, "class": "person"},
 {"score": 0.552, "class": "adult person", "type_hierarchy": "/person/adult person"},
 {"score": 0.5, "class": "people", "type_hierarchy": "/person/people"},
 {"score": 0.726,"class": "light brown color"},
 {"score": 0.542, "class": "beige color"}],
 "classifier_id": "default", "name": "default"}]}],
 "images_processed": 1
}

Detects the face of a person from the image and judges age and sex

Determine the sex and age of the person in the photo.
It will also tell you the location of the face in the photo.

Code

import json
from os.path import join, dirname
from os import environ
from watson_developer_cloud import VisualRecognitionV3

visual_recognition = VisualRecognitionV3(
    # The release date of the version of the API you want to use.
    '2016-05-20',
    # visual-recognition's API key
    api_key = '{api-key}')

images_url='https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg'

res = visual_recognition.detect_faces(parameters=json.dumps({'url': images_url}))
print(json.dumps(res))

Result

face_location is the position of the face, and age is the estimated age.

{"images": [{"resolved_url": "https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg",
 "faces": [{"gender": {"gender": "FEMALE",
            "score": 0.989013},
            "face_location": {"width": 309, "top": 168, "left": 277, "height": 337},
            "age": {"max": 44, "min": 35, "score": 0.403753}}],
 "source_url":"https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg"}],
 "images_processed":1}

Conclusion

In this time, I used image classification prepared in VR in advance to perform image recognition.
According to the reference, it seems that we can make a custom classification, so next time I would like to try it.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?