Skip to content

Add support for multiple features in single call #4

@vladciulley

Description

@vladciulley

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions