Cloud Vision APIã®LABEL_DETECTION
ã§ã¯ï¼ç»åãã¼ã¿ãPOSTéä¿¡ããã ãã§ï¼ãã®ç»åã®ä¸ã«ä½ãåã£ã¦ããããç¬æã«è¿ãã¦ãããï¼
https://cloud.google.com/vision/?hl=ja
æé表ã®èª¬æãæ¸ãã¦ãããï¼ä¸ã¶æããã1000ãªã¯ã¨ã¹ãã¾ã§ã¯ç¡æãªã®ã§ï¼ä¸äººã§å®é¨ç¨ã«ä½¿ãããããªãæ°ã«ããªãã¦ã大ä¸å¤«ã ã¨æãã®ã§ä½¿ã£ã¦ã¿ãï¼å®éã«è©¦ãã¦ã¿ãã¨åãããï¼ãã®ãããç確ãªçµæããã°ããè¿ã£ã¦ããï¼ããªãè奮ããï¼
ã¨ãããã¨ã§ï¼å®éã«APIãå¼ã³åºãã¦æåããã¾ã§ã®æé ãå®ç§ã«ã¾ã¨ãã¦ããï¼æ¬è¨äºå ã§ç´¹ä»ããã½ã¼ã¹ã³ã¼ãã¨ãã¯ã³ããããã°åãã¯ããªã®ã§ï¼æ¯éå®éã«å¥½ããªåçãä¸ãã¦å®è¡ãã¦ã¿ã¦ã»ããï¼
APIãã¼ãåå¾ãã
æåã®ã¹ãããã¯ï¼APIãã¼ãåå¾ããä½æ¥ï¼ãã¡ãã®è¨äº: ãã¨ã¦ãããããããã£ãï¼
éä¸ã§APIãã¼ã®ç¨®é¡ãé¸ã¶ã·ã¼ã³ãããï¼ä¸è¨è¨äºã§ã¯ããã©ã¦ã¶ ãã¼ããé¸æãã¦ãããï¼æ¬è¨äºã§ã¯ãã¼ã«ã«ãµã¼ãã¼ããAPIãå¼ã¶ã®ã§ããµã¼ãã¼ ãã¼ããé¸æãã¦ãã ããï¼ä»¥éã®èª¬æã§ã¯ããã§åå¾ãããã¼ãAPI_KEY
ã¨æ¸ãã¾ãï¼
ãã¼ã«ã«ãµã¼ãã®ããã°ã©ã ãæ¸ã
ãã¼ã«ã«ãµã¼ãã¼ã®ããã°ã©ã ãPython 2.7ã§æ¸ãã¾ãï¼å¾è¿°ãã¾ããï¼APIã«åçãä¸ããããã«èªä½ãã¼ã«tfPhotoPaletteã使ãã¾ããï¼ãã®ãã¼ã«ã§ã¯ï¼ç»åã®ä»»æé¨åãåãæãããï¼ãµã¤ãºã縮å°ãããã§ãã¾ãï¼
ä»åæ¸ããã¼ã«ã«ãµã¼ãã¼å´ã®ããã°ã©ã ã¯ï¼ç°¡åã«æ¸ãã¨ä»¥ä¸ã®ããã«ï¼
- tfPhotoPaletteããç»åãåãåã£ã¦ï¼
- åãåã£ãç»åãCloud Vision APIã«ã»ãããã¦ãªã¯ã¨ã¹ãéä¿¡ããï¼
- Cloud Vision APIããè¿å´ãããJSONãã¼ã¿ãtfPhotoPaletteã«è¿ã
ãããªå¦çãæ ãã¾ãï¼ãã®ä¸é£ã®æµããå®äºããã¨ï¼tfPhotoPaletteã«ã¯ã©ããªã³ã°ãããçµæã表示ããã¾ãï¼
ã¾ãã¯ï¼å¿ è¦ãªã¢ã¸ã¥ã¼ã«ã以ä¸ã®ããã«pipã¤ã³ã¹ãã¼ã«ãã¾ãï¼
$ pip install flask $ pip install requests
ç¶ãã¦ï¼ä»¥ä¸ã®Pythonããã°ã©ã ãé©å½ãªååï¼ä¾ãã°palette_server.py
ï¼ã§ä¿åãã¾ãï¼é¢æ°goog_cloud_vison
å
ã®ä¸è¡ç®ã®API_KEY
ã®é¨åã¯å
ã»ã©åå¾ããAPIãµã¼ãã¼ ãã¼ã§ç½®ãæãã¦ãã ããï¼*1
# coding: utf-8 from flask import Flask, request, jsonify import urllib import base64 import json import requests app = Flask(__name__) default_port = 52892 GOOGLE_CLOUD_VISION_API_URL = 'https://vision.googleapis.com/v1/images:annotate?key=' def goog_cloud_vison (image_content): api_url = GOOGLE_CLOUD_VISION_API_URL + API_KEY req_body = json.dumps({ 'requests': [{ 'image': { 'content': image_content }, 'features': [{ 'type': 'LABEL_DETECTION', 'maxResults': 10, }] }] }) res = requests.post(api_url, data=req_body) return res.json() @app.route('/', methods=['GET']) def hello (): return 'Hello! This is a palette_server.' @app.route('/api/classify', methods=['POST']) def classify (): # tfPhotoPaletteããåä¿¡ããJSONå½¢å¼ã®ãªã¯ã¨ã¹ã tfpp_json = request.json if ('jpg' in tfpp_json): image_base64_str = tfpp_json['jpg'].replace('data:image/jpeg;base64,', '') img_jpg = image_base64_str.decode('base64') image_content = base64.b64encode(img_jpg) # Label Detection: Google Cloud Vision ã§ç©ä½èªèãã res_json = goog_cloud_vison(image_content) # tfPhotoPaletteåãã®ä»å æ å ± res_json['description'] = 'Label Detection (Google Cloud Vision)' return jsonify(res_json) else: return jsonify(description='Bad request') app.run(port=default_port)
tfPhotoPaletteãå ¥æãã¦èµ·å
Chrome ã¦ã§ãã¹ãã¢ããã¤ã³ã¹ãã¼ã«ã§ãã¾ãï¼ ã¤ã³ã¹ãã¼ã«ãå®äºããã¨ï¼chrome://apps/ã®ãªã¹ãå ã«ã¢ããªã®ã¢ã¤ã³ã³ãç¾ããã®ã§ï¼ãããã¯ãªãã¯ããã¨èµ·åã§ãã¾ãï¼
ãã¼ã«ã«ãµã¼ããèµ·å
$ python palette_server.py
åçãä¸ãã¦ï¼Label Detection!
ã¦ã£ã³ãã¦å³ä¸ã®ãPlayããã¿ã³ãã¯ãªãã¯ããã¨å°çªãéãã¾ãï¼å·¦ä¸ã®ã»ã¬ã¯ãããã¯ã¹ããOriginal photoãããCropped photoãã«è¨å®ãã¦ï¼ãã®é£ã®ãClassifyããã¿ã³ãã¯ãªãã¯ãã¾ãï¼ããã§ãã¼ã«ã«ãµã¼ãã«ç»åãéä¿¡ããã¾ãï¼
ãã¼ã«ã«ãµã¼ãã¯åãåã£ãç»åãã¼ã¿ãã»ãããã¦ï¼Cloud Vision APIãªã¯ã¨ã¹ããéä¿¡ãã¾ãï¼1ç§æªæºå¾ ã¤ã¨ï¼ä»¥ä¸ã®ããã«ï¼å ç¨ã®å°çªã«ã©ãã«ä»ããããçµæã表示ããã¾ãï¼
ã¯ãªãã¯ãã¦æ¡å¤§
çµæãããè¦ã¦ã¿ãã¨ï¼vegetable
ãå
¥ã£ã¦ããsoup
ã£ã½ãcurry
ã¨ããç¶æ³ãå®ç§ã«èª¬æããã¦ãã¾ãï¼é©ããï¼
ä½è«
ã©ããªã³ã°çµæã®JSONã«å«ã¾ããmid
ã¨ããã®ã¯ï¼Freebaseã¨ãããã¬ãã¸ã°ã©ãä¸ã§ã®åºæIDãããï¼å®éã«Freebaseã®ã¦ã§ããµã¤ãã§æ¤ç´¢ãã¦ã¿ãã¨curry
ã/m/0hr1s_w
ã¨ãªã£ã¦ãããã¨ã確èªã§ããï¼*2
*1:ã³ã¼ãä¸å¼ã https://github.com/daiz713/cloud-vision ã«ãç½®ãã¦ããã¾ããï¼
*2:https://cloud.google.com/vision/reference/rest/v1/images/annotate