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.
-Extract characters from images using docomo's character recognition API
--Register with docomo Developer support to get the API key.
--Python 2.7 series
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']
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
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