[PYTHON] Category estimation using docomo's image recognition API

Thing you want to do

Estimate the image category using the image recognition API published by docomo.

The image recognition API estimates the category based on the model learned by Deep Learning. For those who do not want to implement a deep learning image recognition program from scratch, but want to try image recognition.

** The code implemented this time is uploaded to here. ** **

Click here if you want to perform character recognition

-Extract characters from images using docomo's character recognition API

Advance preparation

--Register with docomo Developer support to get the API key.

Image file to recognize (rose.jpg)

rose.jpg

Development environment

--Python 2.7 series

program

For (API KEY) in the code, substitute the API key obtained from docomo developer support.

imageRecognition.py


# -*- coding: utf-8 -*-
# implemented by ichiroex
# other codes are also available on GitHub (https://github.com/ichiroex)

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import json
import time 
import urllib
import re
import sys
import argparse

#Throw image data and get the top 5 category candidates(Category recognition)
def getImageCategory(fname, modelName):

    register_openers()
    
    APIKEY = "(APIKEY)"
    url = 'https://api.apigw.smt.docomo.ne.jp/imageRecognition/v1/concept/classify/?APIKEY=' + APIKEY
    
    f = open(fname, 'r')

    datagen, headers = multipart_encode({"image": f, 'modelName': modelName})
    request = urllib2.Request(url,datagen, headers)
    response = urllib2.urlopen(request)
    
    res_dat = response.read()
   
    #return candidate list
    return json.loads(res_dat)['candidates']


if __name__ == '__main__':
    
    #argument(Option setting)
    parser = argparse.ArgumentParser()
    parser.add_argument('--image'    , dest='image', type=str, default='rose.jpg', help='name of input image')
    parser.add_argument('--model'    , dest='model', type=str, default='scene', help='modelName = {scene, fashion_pattern, fashion_type, fashion_style, fashion_color, food, flower, kinoko}')

    args = parser.parse_args()
    
    #Image file name,modelName setting
    fname = args.image
    model_name = args.model
    
    #Get category candidates(Use category recognition)
    candidate_list = getImageCategory(fname, model_name)
    
    #View category tags and scores
    for can in candidate_list:
        print can['tag'], can['score']

Execution result

You can specify an image file with the --image option. The default is rose.jpg.

You can also use the --model option to specify the model to use when estimating. For example, if you specify "--model scene", you can use a model that estimates scenes such as "wedding", "aquarium", "flower", and "animal". (By default, it is scene.)

Below is the result of estimating the scene category of rose.jpg. The score showing the category and certainty is displayed.

$ python imageRecognition.py --image rose.jpg
Flower 0.999078631401
Outdoor 0.000487390527269
Golf 0.000105955921754
Soccer 0.000101881953015
Painting / Craft 3.79223347409e-05

You can infer the flower category by adding "--model flower" as an option, as shown below.

$ python imageRecognition.py --image rose.jpg --model flower
Rose 0.992434620857
Mini rose 0.00756544619799
Sasanqua 3.99395894135e-09
Kalanchoe 2.60835908428e-09
Geranium 1.24757426612e-09

Afterword

I was able to easily estimate the image categories using the model learned by Deep Learning. As long as you register as a developer, you can easily use it, which is convenient for those who want to create some kind of application.

In addition to image recognition, various APIs are open to the public, so I would like to utilize them.

Recommended Posts

Category estimation using docomo's image recognition API
Extract characters from images using docomo's character recognition API
Age recognition using Pepper's API
Image recognition with API from zero knowledge using AutoML Vision
Facial expression recognition using Pepper's API
Image recognition of fruits using VGG16
Image recognition
Python: Basics of image recognition using CNN
Python: Application of image recognition using CNN
Image recognition model using deep learning in 2016
Image recognition using CNN Horses and deer
Image collection using Google Custom Search API
How to code a drone using image recognition
Get image URL using Flickr API in Python
Speech file recognition by Google Speech API v2 using Python
Image recognition with keras
Pepper Tutorial (7): Image Recognition
Image segmentation using U-net
Stat estimation using pyirt
CNN 1 Image Recognition Basics
Tree disease determination by image recognition using CNTK and SVM
I tried using Microsoft's Cognitive Services facial expression recognition API
Collect large numbers of images using Bing's image search API
Image Optimize on the server side using TinyPNG's Web API
Similar face image detection using face recognition and PCA and K-means clustering