Repository navigation
Expand file tree
/
Copy pathAnnotation.php
More file actions
399 lines (356 loc) · 9.15 KB
/
Copy pathAnnotation.php
File metadata and controls
399 lines (356 loc) · 9.15 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?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\Cloud\Vision\Annotation\CropHint;
use Google\Cloud\Vision\Annotation\Document;
use Google\Cloud\Vision\Annotation\Entity;
use Google\Cloud\Vision\Annotation\Face;
use Google\Cloud\Vision\Annotation\ImageProperties;
use Google\Cloud\Vision\Annotation\SafeSearch;
use Google\Cloud\Vision\Annotation\Web;
/**
* Represents a [Google Cloud Vision](https://cloud.google.com/vision) image
* annotation result.
*
* Example:
* ```
* use Google\Cloud\Vision\VisionClient;
*
* $vision = new VisionClient();
*
* $imageResource = fopen(__DIR__ . '/assets/family-photo.jpg', 'r');
* $image = $vision->image($imageResource, [
* 'FACE_DETECTION'
* ]);
*
* $annotation = $vision->annotate($image);
* ```
*/
class Annotation
{
/**
* @var array
*/
private $info;
/**
* @var Face[]|null
*/
private $faces;
/**
* @var Entity[]|null
*/
private $landmarks;
/**
* @var Entity[]|null
*/
private $logos;
/**
* @var Entity[]|null
*/
private $labels;
/**
* @var Entity[]|null
*/
private $text;
/**
* @var Document|null
*/
private $fullText;
/**
* @var SafeSearch|null
*/
private $safeSearch;
/**
* @var ImageProperties|null
*/
private $imageProperties;
/**
* @var CropHint[]|null
*/
private $cropHints;
/**
* @var Web|null
*/
private $web;
/**
* @var array|null
*/
private $error;
/**
* Create an annotation result.
*
* This class represents a single image annotation response from Cloud Vision. If multiple images were tested at
* once, the result will be an array of Annotation instances.
*
* This class is not intended to be instantiated outside the Google Cloud Platform Client Library.
*
* @param array $info The annotation result
*/
public function __construct($info)
{
$this->info = $info;
if (isset($info['faceAnnotations'])) {
$this->faces = [];
foreach ($info['faceAnnotations'] as $face) {
$this->faces[] = new Face($face);
}
}
if (isset($info['landmarkAnnotations'])) {
$this->landmarks = [];
foreach ($info['landmarkAnnotations'] as $landmark) {
$this->landmarks[] = new Entity($landmark);
}
}
if (isset($info['logoAnnotations'])) {
$this->logos = [];
foreach ($info['logoAnnotations'] as $logo) {
$this->logos[] = new Entity($logo);
}
}
if (isset($info['labelAnnotations'])) {
$this->labels = [];
foreach ($info['labelAnnotations'] as $label) {
$this->labels[] = new Entity($label);
}
}
if (isset($info['textAnnotations'])) {
$this->text = [];
foreach ($info['textAnnotations'] as $text) {
$this->text[] = new Entity($text);
}
}
if (isset($info['fullTextAnnotation'])) {
$this->fullText = new Document($info['fullTextAnnotation']);
}
if (isset($info['safeSearchAnnotation'])) {
$this->safeSearch = new SafeSearch($info['safeSearchAnnotation']);
}
if (isset($info['imagePropertiesAnnotation'])) {
$this->imageProperties = new ImageProperties($info['imagePropertiesAnnotation']);
}
if (isset($info['cropHintsAnnotation']) && is_array($info['cropHintsAnnotation']['cropHints'])) {
$this->cropHints = [];
foreach ($info['cropHintsAnnotation']['cropHints'] as $hint) {
$this->cropHints[] = new CropHint($hint);
}
}
if (isset($info['webDetection'])) {
$this->web = new Web($info['webDetection']);
}
if (isset($info['error'])) {
$this->error = $info['error'];
}
}
/**
* Return raw annotation response array
*
* Example:
* ```
* $info = $annotation->info();
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/vision/reference/rest/v1/images/annotate#annotateimageresponse AnnotateImageResponse
* @codingStandardsIgnoreEnd
*
* @return array|null
*/
public function info()
{
return $this->info;
}
/**
* Return an array of faces
*
* Example:
* ```
* $faces = $annotation->faces();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#FaceAnnotation FaceAnnotation
*
* @return Face[]|null
*/
public function faces()
{
return $this->faces;
}
/**
* Return an array of landmarks
*
* Example:
* ```
* $landmarks = $annotation->landmarks();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#EntityAnnotation EntityAnnotation
*
* @return Entity[]|null
*/
public function landmarks()
{
return $this->landmarks;
}
/**
* Return an array of logos
*
* Example:
* ```
* $logos = $annotation->logos();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#EntityAnnotation EntityAnnotation
*
* @return Entity[]|null
*/
public function logos()
{
return $this->logos;
}
/**
* Return an array of labels
*
* Example:
* ```
* $labels = $annotation->labels();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#EntityAnnotation EntityAnnotation
*
* @return Entity[]|null
*/
public function labels()
{
return $this->labels;
}
/**
* Return an array containing all text found in the image
*
* Example:
* ```
* $text = $annotation->text();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#EntityAnnotation EntityAnnotation
*
* @return Entity[]|null
*/
public function text()
{
return $this->text;
}
/**
* Return the full text annotation.
*
* Example:
* ```
* $fullText = $annotation->fullText();
* ```
*
* @see https://cloud.google.com/vision/reference/rest/v1/images/annotate#fulltextannotation FullTextAnnotation
*
* @return Document|null
*/
public function fullText()
{
return $this->fullText;
}
/**
* Get the result of a safe search detection
*
* Example:
* ```
* $safeSearch = $annotation->safeSearch();
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#SafeSearchAnnotation SafeSearchAnnotation
* @codingStandardsIgnoreEnd
*
* @return SafeSearch|null
*/
public function safeSearch()
{
return $this->safeSearch;
}
/**
* Fetch image properties
*
* Example:
* ```
* $properties = $annotation->imageProperties();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#ImageProperties ImageProperties
*
* @return ImageProperties|null
*/
public function imageProperties()
{
return $this->imageProperties;
}
/**
* Fetch Crop Hints
*
* Example:
* ```
* $hints = $annotation->cropHints();
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#CropHintsAnnotation CropHintsAnnotation
* @codingStandardsIgnoreEnd
*
* @return CropHint[]|null
*/
public function cropHints()
{
return $this->cropHints;
}
/**
* Fetch the Web Annotatation.
*
* Example:
* ```
* $web = $annotation->web();
* ```
*
* @see https://cloud.google.com/vision/docs/reference/rest/v1/images/annotate#WebDetection WebDetection
*
* @return Web|null
*/
public function web()
{
return $this->web;
}
/**
* Get error information, if present
*
* Example:
* ```
* $error = $annotation->error();
* ```
*
* @see https://cloud.google.com/vision/reference/rest/v1/images/annotate#status Status Format
*
* @return array|null
*/
public function error()
{
return $this->error;
}
}