Giter Site home page Giter Site logo

googlevisionapibundle's Introduction

GoogleVisionApiBundle

Build Status Code Climate Latest Stable Version codecov

GoogleVisionApiBundle is a Symfony2/3 Bundle for use the API Google Vision simply (https://cloud.google.com/vision/)

Installation

Via Composer

$ composer require headoo/google-vision-api-bundle

or in composer.json file

"headoo/google-vision-api-bundle": "dev-master"

Register the bundle in app/AppKernel.php:

public function registerBundles()
{
    return array(
        // ...
        new Headoo\GoogleVisionApiBundle\HeadooGoogleVisionApiBundle(),
        // ...
    );
}

Configuration

Configure the google api key in your config.yml :

headoo_google_vision_api:
    api_key: '%apikey%'

Usage

It works like a service. On a Controller you can call like this:

	$google_vision = $this->container->get('headoo_google_vision_api.helper');

You can use all detections functionality with 1 endpoint:

	$google_vision->vision($image, $type);

Your $image must be base64 encoded, or via an URL , or an absolute path.

Available $type are :

  • TYPE_UNSPECIFIED
  • FACE_DETECTION
  • LANDMARK_DETECTION
  • LOGO_DETECTION
  • LABEL_DETECTION
  • TEXT_DETECTION
  • SAFE_SEARCH_DETECTION
  • IMAGE_PROPERTIES
  • WEB_DETECTION

Return

You will receive an array with http_code, raw_response and a parsed_response.

  • http_code : Google Http Code Response (often 200 for OK, and 400 for error)
  • raw_response : Google Vision Api Response Raw, without manipulation
  • parsed_response : Parsed response with objects, easier to use for a PHP user.

Security

If you discover a security vulnerability , please email instead of using the issue tracker. All security vulnerabilities will be promptly addressed.

Standalone Test

How to test

  1. clone repo : $ sudo git clone https://github.com/Headoo/GoogleVisionApiBundle.git
  2. go into directory : $ cd GoogleVisionApiBundle/
  3. install composer as explained here : https://getcomposer.org/download/
  4. launch composer update : $ ./composer.phar update
  5. modify headoo_google_vision_api.api_key in Tests/App/config.yml
  6. launch test : $ ./vendor/phpunit/phpunit/phpunit

License

This Bundle is open-sourced software licensed under the MIT license

googlevisionapibundle's People

Contributors

gozutok avatar kcassam avatar miste avatar misteio avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

googlevisionapibundle's Issues

Add support for multiple features in single call

Hello,

I would suggest adding support for launching calls with multiple detection types, since some of them are free if used in conjunction with another. For example, Safe Search Detection is free with Label Detection or Crop Hints, which is free if used with Image Properties.

Check this link: https://cloud.google.com/vision/pricing#prices to better understand my point.

In order to achieve this I've changed the GoogleVisionApiHelper::_request() like this:

private function _request($base64Image, $types){
        
        if (empty($types)) {
            throw new \InvalidArgumentException('Type missing from request.');
        }
        
        $url    = $this->_url . $this->_api_key;
        $json   ='{
			  	"requests": [
					{
					  "image": {
					    "content":"' .$base64Image. '"
					  },
					  "features": [';
        
        $lastType = end($types);
        
        foreach ($types as $type) {
            
            $json .= '
                            {
                                "type": "' .$type. '",
                                "maxResults": 200
                            }';
            if ($type !== $lastType) {
                $json .= ',';
            }
        }
					 
        $json .= '    ]
					}
				]
			}';

        $data                       = $this->_makeCall($url,$json);
        $jsonResponse               = json_decode($data['raw_response']);
        
        if($data['http_code'] !== 200){
            $data['status']          = $jsonResponse->error->status;
            $data['message']         = $jsonResponse->error->message;
            $data['error']           = $jsonResponse->error;
            $data['parsed_response'] = [];
        }else{
            
            foreach ($types as $type) {
                $_type                  = strtolower($type);
                $_type                  = str_replace('_', ' ', $_type);
                $_type                  = ucwords($_type);
                $_type                  = str_replace(' ', '', $_type);
                $parseFunction          = '_parse' . $_type;
                $data['parsed_response'][$type] = $this->$parseFunction($jsonResponse->responses[0]);
            }

            $data['raw_response'] = $jsonResponse->responses;
        }

        return $data;
    }

Hope it helps!

Separate this project in 2 projects

Separate this project in 2 projects :

  • One with the standalone code, works with php without Symfony (the helper mainly)
  • One with all the bundle code

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.