/Google-Vision-Detective

A wrapper for Google Vision API

Primary LanguagePython

Google Vision Detective

A wrapper for Google Vision API. It eases all the preparations that needs to be done prior to sending a request to Google Vision API, which is image downloading (if it is taken from certain URL) and base64 encoding, API parameters preparation, credentials file invocation, etc.

It can be used as python package and CLI (command line interface). CLI is rather for demonstration purposes.

Instalation

Install using pip:

pip install git+git://github.com/arrrlo/Google-Vision-Detective@master

Get Google Vision API credentials

In order to use Google Vision API you must get credentials in form of json file. To do that go to Google Cloud Vision Homepage, enable Cloud Vision API in your Google API Manager and issue a credentials json file.

Detailed help: Google Cloud Vision API: Getting Started

Use it as python package

import os

from google_vision_detective import GoogleVisionDetective, Request
from google_vision_detective.features import Face, Label

credentials = os.path.join('__path_to_your_credentials_json_file__')
input_dir = os.path.join('__path_to_dir_where_image_from_url_is_saved__')

detective = GoogleVisionDetective(credentials=credentials, input_dir=input_dir)

images = [
  '__image1_url__',
  '__image2_url__',
  .
  .
  .
]

for image in images:
  with Request(detective, image) as request:
    request.feature(Label(max_results=10))
    request.feature(Face(max_results=10))

responses = detective.obj.detect()

Use it as CLI (command line interface)

Fetch a photo from URL and detect what is on that image:

gvdetective -c path/to/your/credentials/file.json -i ~/input_dir/ labels -i http://www.domain.com/image.jpg

Fetch a photo from URL, detect faces and save new image with faces in sqares:

gvdetective -c path/to/your/credentials/file.json -i ~/img/input faces -i http://www.domain.com/image.jpg -o ~/img/output

Type gvdetective --help

Usage: gvdetective [OPTIONS] COMMAND [ARGS]...

  Detective is a wrapper for Google Vision API

Options:
  -c, --credentials TEXT  Path to your credentials json file
  -i, --input_dir TEXT    Path to directory where input images will be stored
  --help                  Show this message and exit.

Commands:
  faces        Detect faces within the image
  labels       Execute Image Content Analysis on the entire...
  landmark     Detect geographic landmarks within the image
  logo         Detect company logos within the image
  properties   Compute a set of properties about the image...
  safe_search  Determine image safe search properties on the...
  text         Perform Optical Character Recognition (OCR)...

Path to credentials file in environment variables

Google Cloud Vision API python package needs to have GOOGLE_APPLICATION_CREDENTIALS environment variable set in order to work.

You can set it using this three different ways:

In Linux:

$ export GOOGLE_APPLICATION_CREDENTIALS=__path_to_your_credentials_jon_file__

In code:

credentials = os.path.join('__path_to_your_credentials_json_file__')
detective = GoogleVisionDetective(credentials=credentials)

In code:

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = os.path.join('__path_to_your_credentials_json_file__')
detective = GoogleVisionDetective()

In CLI:

gvdetective -c path/to/your/credentials/file.json ...

Once the environment variable is set, it doesn't needs to be set again, meaning you can use this code without explicitly inserting credentials file path into GoogleVisionDetective class.