-1

Is there anyway I can analyse URL's using Google Cloud Vision. I know how to analyse images that I store locally, but I can't seem to analyse jpg's that exist on the internet:

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")

Is there anyway I can analyse a URL and get an answer as to whether there are any logo's in them?

Kara
  • 6,115
  • 16
  • 50
  • 57
semiflex
  • 1,176
  • 3
  • 25
  • 44

2 Answers2

1

I figured out how to do it. Re-wrote my code and added used urllib to open the image and then I passed it through base64 and the google cloud vision logo recognition 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
semiflex
  • 1,176
  • 3
  • 25
  • 44
0

The Google Cloud Vision API allows you to either specify the image content in base64 or a link to a file on Google Cloud storage. See:

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

This means that you will have to download each image url in your code (using Python's urllib2 library maybe) and encode it in base64, then add it to service_request.

trans1st0r
  • 2,023
  • 2
  • 17
  • 23