Repository navigation
Expand file tree
/
Copy pathVisionClient.php
More file actions
283 lines (266 loc) · 10.1 KB
/
Copy pathVisionClient.php
File metadata and controls
283 lines (266 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/**
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Cloud\Vision;
use Google\Auth\FetchAuthTokenInterface;
use Google\Cloud\Core\ClientTrait;
use Google\Cloud\Core\ValidateTrait;
use Google\Cloud\Storage\StorageObject;
use Google\Cloud\Vision\Connection\Rest;
use InvalidArgumentException;
use Psr\Cache\CacheItemPoolInterface;
/**
* Google Cloud Vision allows you to understand the content of an image,
* classify images into categories, detect text, objects, faces and more. Find
* more information at the
* [Google Cloud Vision docs](https://cloud.google.com/vision/docs/).
*
* Example:
* ```
* use Google\Cloud\Vision\VisionClient;
*
* $vision = new VisionClient();
* ```
*/
class VisionClient
{
use ClientTrait;
use ValidateTrait;
const VERSION = '0.11.2';
const FULL_CONTROL_SCOPE = 'https://www.googleapis.com/auth/cloud-platform';
/**
* @var Connection\ConnectionInterface
*/
protected $connection;
/**
* Create a Vision client.
*
* Note that when creating a VisionClient instance, setting
* `$config.projectId` is not supported. To switch between projects, you
* must provide credentials with access to the project.
*
* @param array $config [optional] {
* Configuration Options.
*
* @type CacheItemPoolInterface $authCache A cache for storing access
* tokens. **Defaults to** a simple in memory implementation.
* @type array $authCacheOptions Cache configuration options.
* @type callable $authHttpHandler A handler used to deliver Psr7
* requests specifically for authentication.
* @type FetchAuthTokenInterface $credentialsFetcher A credentials
* fetcher instance.
* @type callable $httpHandler A handler used to deliver Psr7 requests.
* Only valid for requests sent over REST.
* @type array $keyFile The contents of the service account credentials
* .json file retrieved from the Google Developer's Console.
* Ex: `json_decode(file_get_contents($path), true)`.
* @type string $keyFilePath The full path to your service account
* credentials .json file retrieved from the Google Developers
* Console.
* @type float $requestTimeout Seconds to wait before timing out the
* request. **Defaults to** `0` with REST and `60` with gRPC.
* @type int $retries Number of retries for a failed request.
* **Defaults to** `3`.
* @type array $scopes Scopes to be used for the request.
* }
* @throws Google\Cloud\Core\Exception\GoogleException
*/
public function __construct(array $config = [])
{
if (!isset($config['scopes'])) {
$config['scopes'] = [self::FULL_CONTROL_SCOPE];
}
$this->connection = new Rest($this->configureAuthentication($config));
}
/**
* Create an instance of {@see Google\Cloud\Vision\Image} with required features and options.
*
* This method should be used to configure a single image, or when a set of
* images requires different settings for each member of the set. If you
* have a set of images which all will use the same settings,
* {@see Google\Cloud\Vision\VisionClient::images()} may be quicker and
* simpler to use.
*
* This method will not perform any service requests, and is meant to be
* used to configure a request prior to calling
* {@see Google\Cloud\Vision\VisionClient::annotate()}.
*
* For more information, including best practices and examples detailing
* other usage such as `$imageContext`, see {@see Google\Cloud\Vision\Image::__construct()}.
*
* Example:
* ```
* $imageResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
*
* $image = $vision->image($imageResource, [
* 'FACE_DETECTION'
* ]);
* ```
*
* ```
* // Setting maxResults for a feature
*
* $imageResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
*
* $image = $vision->image($imageResource, [
* 'FACE_DETECTION'
* ], [
* 'maxResults' => [
* 'FACE_DETECTION' => 1
* ]
* ]);
* ```
*
* @param resource|string|StorageObject $image An image to configure with
* the given settings. This parameter will accept a resource, a
* string of bytes, the URI of an image in a publicly-accessible
* web location, or an instance of {@see Google\Cloud\Storage\StorageObject}.
* @param array $features A list of cloud vision
* [features](https://cloud.google.com/vision/reference/rest/v1/images/annotate#type)
* to apply to the image.
* @param array $options See {@see Google\Cloud\Vision\Image::__construct()} for
* configuration details.
* @return Image
* @throws InvalidArgumentException
*/
public function image($image, array $features, array $options = [])
{
return new Image($image, $features, $options);
}
/**
* Create an array of type {@see Google\Cloud\Vision\Image} with required features and options set for
* each member of the set.
*
* This method is useful for quickly configuring every member of a set of
* images with the same features and options. Should you need to provide
* different features or options for one or more members of the set,
* {@see Google\Cloud\Vision\VisionClient::image()} is a better choice.
*
* This method will not perform any service requests, and is meant to be
* used to configure a request prior to calling
* {@see Google\Cloud\Vision\VisionClient::annotateBatch()}.
*
* For more information, including best practices and examples detailing
* other usage such as `$imageContext`, see {@see Google\Cloud\Vision\Image::__construct()}.
*
* Example:
* ```
* // In the example below, both images will have the same settings applied.
* // They will both run face detection and return up to 10 results.
*
* $familyPhotoResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
* $weddingPhotoResource = fopen(__DIR__ . '/assets/wedding-photo.jpg', 'r');
*
* $images = $vision->images([$familyPhotoResource, $weddingPhotoResource], [
* 'FACE_DETECTION'
* ], [
* 'maxResults' => [
* 'FACE_DETECTION' => 10
* ]
* ]);
* ```
*
* @param resource[]|string[]|StorageObject[] $images An array of images
* to configure with the given settings. Each member of the set can
* be a resource, a string of bytes, the URI of an image in a
* publicly-accessible web location, or an instance of
* {@see Google\Cloud\Storage\StorageObject}.
* @param array $features A list of cloud vision features to apply to each image.
* @param array $options See {@see Google\Cloud\Vision\Image::__construct()} for
* configuration details.
* @return Image[]
* @throws InvalidArgumentException
*/
public function images(array $images, array $features, array $options = [])
{
$result = [];
foreach ($images as $image) {
$result[] = $this->image($image, $features, $options);
}
return $result;
}
/**
* Annotate a single image.
*
* Example:
* ```
* $familyPhotoResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
*
* $image = $vision->image($familyPhotoResource, [
* 'FACE_DETECTION'
* ]);
*
* $result = $vision->annotate($image);
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate Annotate API documentation
*
* @param Image $image The image to annotate
* @param array $options Configuration options
* @return Annotation
*/
public function annotate(Image $image, array $options = [])
{
$res = $this->annotateBatch([$image], $options);
return $res[0];
}
/**
* Annotate a set of images.
*
* Example:
* ```
* $images = [];
*
* $familyPhotoResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
* $eiffelTowerResource = fopen(__DIR__ . '/assets/eiffel-tower.jpg', 'r');
*
* $images[] = $vision->image($familyPhotoResource, [
* 'FACE_DETECTION'
* ]);
*
* $images[] = $vision->image($eiffelTowerResource, [
* 'LANDMARK_DETECTION'
* ]);
*
* $result = $vision->annotateBatch($images);
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate Annotate API documentation
*
* @param Image[] $images An array consisting of instances of
* {@see Google\Cloud\Vision\Image}.
* @param array $options Configuration Options
* @return Annotation[]
*/
public function annotateBatch(array $images, array $options = [])
{
$this->validateBatch($images, Image::class);
$requests = [];
foreach ($images as $image) {
$requests[] = $image->requestObject();
}
$res = $this->connection->annotate([
'requests' => $requests
] + $options);
$annotations = [];
if (isset($res['responses'])) {
foreach ($res['responses'] as $response) {
$annotations[] = new Annotation($response);
}
}
return $annotations;
}
}