2016-04-19 27 views
-1

Google Cloud Visionを使用してURLを分析できますか。私はローカルに保存した画像を解析する方法を知っているが、私は、インターネット上に存在するのjpgの分析に見えることはできません:私はURLを解析し、そこにあるかどうかについて答えを得ることができGoogle Cloud Vision - Pythonを使用したURLの分析

import argparse 
import base64 
import httplib2 
from googleapiclient.discovery import build 
import collections 
import time 
import datetime 
import pyodbc 

time_start = datetime.datetime.now() 

def main(photo_file): 
    '''Run a label request on a single image''' 

    API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1' 
    http = httplib2.Http() 

    service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE, developerKey=INSERT API KEY HERE) 

    with open(photo_file, 'rb') as image: 
     image_content = base64.b64encode(image.read()) 
     service_request = service.images().annotate(
      body={ 
       'requests': [{ 
        'image': { 
         'content': image_content 
        }, 
        'features': [{ 
         'type': 'LOGO_DETECTION', 
         'maxResults': 10, 
        }] 
       }] 
      }) 
     response = service_request.execute() 

     try: 
      logo_description = response['responses'][0]['logoAnnotations'][0]['description'] 
      logo_description_score = response['responses'][0]['logoAnnotations'][0]['score'] 
      print logo_description 
      print logo_description_score 
     except KeyError: 
      print "logo nonexistent" 
      pass 

     print time_start 

if __name__ == '__main__': 
    main("C:\Users\KVadher\Desktop\image_file1.jpg") 

とにかくありそれらのロゴはどれですか?

答えて

0

Google Cloud Vision APIを使用すると、Base64で画像コンテンツを指定したり、Google Cloudストレージ上のファイルにリンクを設定したりできます。参照:

https://cloud.google.com/vision/docs/requests-and-responses#json_request_format

これは、あなたが(多分Pythonのurllib2のライブラリを使用して)あなたのコード内の各画像のURLをダウンロードして、BASE64でそれをエンコードし、その後、service_requestに追加しなければならないことを意味します。

+0

を私はすべての画像をダウンロードするにはどうすればよいですか? – semiflex

+1

urllib2ライブラリで試してください... urlopen()メソッドを使用してください。 – trans1st0r

1

私はそれを行う方法を理解しました。再書いた私のコードをして画像を開くためにurllibはを使用して、私はBASE64とGoogleのクラウドビジョンのロゴ認識APIを介して、それを通過した追加しました:

import argparse 
import base64 
import httplib2 
from googleapiclient.discovery import build 
import collections 
import time 
import datetime 
import pyodbc 
import urllib 
import urllib2 
time_start = datetime.datetime.now() 


#API AND DEVELOPER KEY DETAILS 
API_DISCOVERY_FILE = 'https://vision.googleapis.com/$discovery/rest?version=v1' 
http = httplib2.Http() 
service = build('vision', 'v1', http, discoveryServiceUrl=API_DISCOVERY_FILE, developerKey=INSERT DEVELOPER KEY HERE) 

url = "http://www.lcbo.com/content/dam/lcbo/products/218040.jpg/jcr:content/renditions/cq5dam.web.1280.1280.jpeg" 
opener = urllib.urlopen(url) 

#with open(photo_file) as image: 
image_content = base64.b64encode(opener.read()) 
service_request = service.images().annotate(
    body={ 
     'requests': [{ 
      'image': { 
       'content': image_content 
      }, 
      'features': [{ 
       'type': 'LOGO_DETECTION', 
       'maxResults': 10, 
      }] 
     }] 
    }) 
response = service_request.execute() 

try: 
    logo_description = response['responses'][0]['logoAnnotations'][0]['description'] 
    logo_description_score = response['responses'][0]['logoAnnotations'][0]['score'] 
    print logo_description 
    print logo_description_score 
except KeyError: 
    print "logo nonexistent" 
    pass 

    print time_start 
関連する問題